Skip to content

Commit 12b195d

Browse files
author
minion
committed
with πŸ’•
1 parent 3de3ace commit 12b195d

File tree

1,702 files changed

+2061689
-133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,702 files changed

+2061689
-133
lines changed

β€Žcontracts/goerli/09/09efd5671647501022602a24d88f7df050b23e90_OneNFT.sol

+1,128
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
// SPDX-License-Identifier: AGPL-3.0-or-later
2+
// Copyright (C) 2021 Dai Foundation
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU Affero General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Affero General Public License
14+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
16+
pragma solidity ^0.7.6;
17+
18+
import "./L1DAIBridge.sol";
19+
20+
contract L1GovernanceRelay {
21+
// --- Auth ---
22+
mapping(address => uint256) public wards;
23+
function rely(address usr) external auth {
24+
wards[usr] = 1;
25+
emit Rely(usr);
26+
}
27+
function deny(address usr) external auth {
28+
wards[usr] = 0;
29+
emit Deny(usr);
30+
}
31+
modifier auth() {
32+
require(wards[msg.sender] == 1, "L1GovernanceRelay/not-authorized");
33+
_;
34+
}
35+
36+
event Rely(address indexed usr);
37+
event Deny(address indexed usr);
38+
39+
uint256 constant RELAY_SELECTOR = 300224956480472355485152391090755024345070441743081995053718200325371913697;
40+
41+
address public immutable starkNet;
42+
uint256 public immutable l2GovernanceRelay;
43+
44+
constructor(address _starkNet, uint256 _l2GovernanceRelay) {
45+
wards[msg.sender] = 1;
46+
emit Rely(msg.sender);
47+
48+
starkNet = _starkNet;
49+
l2GovernanceRelay = _l2GovernanceRelay;
50+
}
51+
52+
// Forward a call to be repeated on L2
53+
function relay(uint256 spell) external auth {
54+
uint256[] memory payload = new uint256[](1);
55+
payload[0] = spell;
56+
StarkNetLike(starkNet).sendMessageToL2(l2GovernanceRelay, RELAY_SELECTOR, payload);
57+
}
58+
}
59+
60+
// SPDX-License-Identifier: AGPL-3.0-or-later
61+
// Copyright (C) 2021 Dai Foundation
62+
// This program is free software: you can redistribute it and/or modify
63+
// it under the terms of the GNU Affero General Public License as published by
64+
// the Free Software Foundation, either version 3 of the License, or
65+
// (at your option) any later version.
66+
//
67+
// This program is distributed in the hope that it will be useful,
68+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
69+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
70+
// GNU Affero General Public License for more details.
71+
//
72+
// You should have received a copy of the GNU Affero General Public License
73+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
74+
75+
pragma solidity ^0.7.6;
76+
77+
interface TokenLike {
78+
function transferFrom(
79+
address from,
80+
address to,
81+
uint256 value
82+
) external returns (bool success);
83+
84+
function balanceOf(address account) external view returns (uint256);
85+
}
86+
87+
interface StarkNetLike {
88+
function sendMessageToL2(
89+
uint256 to,
90+
uint256 selector,
91+
uint256[] calldata payload
92+
) external;
93+
94+
function consumeMessageFromL2(
95+
uint256 from,
96+
uint256[] calldata payload
97+
) external;
98+
}
99+
100+
contract L1DAIBridge {
101+
// --- Auth ---
102+
mapping(address => uint256) public wards;
103+
104+
function rely(address usr) external auth {
105+
wards[usr] = 1;
106+
emit Rely(usr);
107+
}
108+
109+
function deny(address usr) external auth {
110+
wards[usr] = 0;
111+
emit Deny(usr);
112+
}
113+
114+
modifier auth() {
115+
require(wards[msg.sender] == 1, "L1DAIBridge/not-authorized");
116+
_;
117+
}
118+
119+
event Rely(address indexed usr);
120+
event Deny(address indexed usr);
121+
122+
123+
uint256 public isOpen = 1;
124+
125+
modifier whenOpen() {
126+
require(isOpen == 1, "L1DAIBridge/closed");
127+
_;
128+
}
129+
130+
function close() external auth {
131+
isOpen = 0;
132+
emit Closed();
133+
}
134+
135+
event Closed();
136+
137+
address public immutable starkNet;
138+
address public immutable dai;
139+
uint256 public immutable l2Dai;
140+
address public immutable escrow;
141+
uint256 public immutable l2DaiBridge;
142+
143+
uint256 public ceiling = 0;
144+
145+
uint256 constant HANDLE_WITHDRAW = 0;
146+
147+
// src/starkware/cairo/lang/cairo_constants.py
148+
// 2 ** 251 + 17 * 2 ** 192 + 1;
149+
uint256 constant SN_PRIME =
150+
3618502788666131213697322783095070105623107215331596699973092056135872020481;
151+
152+
// from starkware.starknet.compiler.compile import get_selector_from_name
153+
// print(get_selector_from_name('handle_deposit'))
154+
uint256 constant DEPOSIT =
155+
1285101517810983806491589552491143496277809242732141897358598292095611420389;
156+
157+
// print(get_selector_from_name('handle_force_withdrawal'))
158+
uint256 constant FORCE_WITHDRAW =
159+
1137729855293860737061629600728503767337326808607526258057644140918272132445;
160+
161+
event LogCeiling(uint256 ceiling);
162+
event LogDeposit(address indexed l1Sender, uint256 amount, uint256 l2Recipient);
163+
event LogWithdrawal(address indexed l1Recipient, uint256 amount);
164+
event LogForceWithdrawal(
165+
address indexed l1Recipient,
166+
uint256 amount,
167+
uint256 indexed l2Sender
168+
);
169+
170+
constructor(
171+
address _starkNet,
172+
address _dai,
173+
uint256 _l2Dai,
174+
address _escrow,
175+
uint256 _l2DaiBridge
176+
) {
177+
wards[msg.sender] = 1;
178+
emit Rely(msg.sender);
179+
180+
starkNet = _starkNet;
181+
dai = _dai;
182+
l2Dai = _l2Dai;
183+
escrow = _escrow;
184+
l2DaiBridge = _l2DaiBridge;
185+
}
186+
187+
function setCeiling(uint256 _ceiling) external auth whenOpen {
188+
ceiling = _ceiling;
189+
emit LogCeiling(_ceiling);
190+
}
191+
192+
// slither-disable-next-line similar-names
193+
function deposit(
194+
uint256 amount,
195+
uint256 l2Recipient
196+
) external whenOpen {
197+
emit LogDeposit(msg.sender, amount, l2Recipient);
198+
199+
require(l2Recipient != 0 && l2Recipient != l2Dai && l2Recipient < SN_PRIME, "L1DAIBridge/invalid-address");
200+
201+
TokenLike(dai).transferFrom(msg.sender, escrow, amount);
202+
203+
require(
204+
TokenLike(dai).balanceOf(escrow) <= ceiling,
205+
"L1DAIBridge/above-ceiling"
206+
);
207+
208+
uint256[] memory payload = new uint256[](3);
209+
payload[0] = l2Recipient;
210+
(payload[1], payload[2]) = toSplitUint(amount);
211+
212+
StarkNetLike(starkNet).sendMessageToL2(l2DaiBridge, DEPOSIT, payload);
213+
}
214+
215+
function toSplitUint(uint256 value) internal pure returns (uint256, uint256) {
216+
uint256 low = value & ((1 << 128) - 1);
217+
uint256 high = value >> 128;
218+
return (low, high);
219+
}
220+
221+
// slither-disable-next-line similar-names
222+
function withdraw(uint256 amount, address l1Recipient) external {
223+
emit LogWithdrawal(l1Recipient, amount);
224+
225+
uint256[] memory payload = new uint256[](4);
226+
payload[0] = HANDLE_WITHDRAW;
227+
payload[1] = uint256(uint160(msg.sender));
228+
(payload[2], payload[3]) = toSplitUint(amount);
229+
230+
StarkNetLike(starkNet).consumeMessageFromL2(l2DaiBridge, payload);
231+
TokenLike(dai).transferFrom(escrow, l1Recipient, amount);
232+
}
233+
234+
function forceWithdrawal(uint256 amount, uint256 l2Sender) external whenOpen {
235+
emit LogForceWithdrawal(msg.sender, amount, l2Sender);
236+
237+
uint256[] memory payload = new uint256[](4);
238+
payload[0] = l2Sender;
239+
payload[1] = uint256(uint160(msg.sender));
240+
(payload[2], payload[3]) = toSplitUint(amount);
241+
242+
StarkNetLike(starkNet).sendMessageToL2(l2DaiBridge, FORCE_WITHDRAW, payload);
243+
}
244+
}

0 commit comments

Comments
Β (0)