-
Notifications
You must be signed in to change notification settings - Fork 37
/
join.sol
429 lines (356 loc) · 14.6 KB
/
join.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/// join.sol -- Non-standard token adapters
// Copyright (C) 2018 Rain <rainbreak@riseup.net>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity >=0.5.12;
import "dss/lib.sol";
interface VatLike {
function slip(bytes32,address,int) external;
}
// GemJoin2
// For a token that does not return a bool on transfer or transferFrom (like OMG)
// This is one way of doing it. Check the balances before and after calling a transfer
interface GemLike2 {
function decimals() external view returns (uint);
function transfer(address,uint) external;
function transferFrom(address,address,uint) external;
function balanceOf(address) external view returns (uint);
function allowance(address,address) external view returns (uint);
}
contract GemJoin2 is LibNote {
// --- Auth ---
mapping (address => uint) public wards;
function rely(address usr) external note auth { wards[usr] = 1; }
function deny(address usr) external note auth { wards[usr] = 0; }
modifier auth { require(wards[msg.sender] == 1); _; }
VatLike public vat;
bytes32 public ilk;
GemLike2 public gem;
uint public dec;
uint public live; // Access Flag
constructor(address vat_, bytes32 ilk_, address gem_) public {
wards[msg.sender] = 1;
live = 1;
vat = VatLike(vat_);
ilk = ilk_;
gem = GemLike2(gem_);
dec = gem.decimals();
}
function cage() external note auth {
live = 0;
}
function mul(uint x, uint y) internal pure returns (uint z) {
require(y == 0 || (z = x * y) / y == x, "GemJoin2/overflow");
}
function join(address urn, uint wad) public note {
require(live == 1, "GemJoin2/not-live");
require(wad <= 2 ** 255, "GemJoin2/overflow");
vat.slip(ilk, urn, int(wad));
uint256 prevBalance = gem.balanceOf(msg.sender);
require(prevBalance >= wad, "GemJoin2/no-funds");
require(gem.allowance(msg.sender, address(this)) >= wad, "GemJoin2/no-allowance");
(bool ok,) = address(gem).call(
abi.encodeWithSignature("transferFrom(address,address,uint256)", msg.sender, address(this), wad)
);
require(ok, "GemJoin2/failed-transfer");
require(prevBalance - wad == gem.balanceOf(msg.sender), "GemJoin2/failed-transfer");
}
function exit(address guy, uint wad) public note {
require(wad <= 2 ** 255, "GemJoin2/overflow");
vat.slip(ilk, msg.sender, -int(wad));
uint256 prevBalance = gem.balanceOf(address(this));
require(prevBalance >= wad, "GemJoin2/no-funds");
(bool ok,) = address(gem).call(
abi.encodeWithSignature("transfer(address,uint256)", guy, wad)
);
require(ok, "GemJoin2/failed-transfer");
require(prevBalance - wad == gem.balanceOf(address(this)), "GemJoin2/failed-transfer");
}
}
// GemJoin3
// For a token that has a lower precision than 18 and doesn't have decimals field in place (like DGD)
interface GemLike3 {
function transfer(address,uint) external returns (bool);
function transferFrom(address,address,uint) external returns (bool);
}
contract GemJoin3 is LibNote {
// --- Auth ---
mapping (address => uint) public wards;
function rely(address usr) external note auth { wards[usr] = 1; }
function deny(address usr) external note auth { wards[usr] = 0; }
modifier auth { require(wards[msg.sender] == 1); _; }
VatLike public vat;
bytes32 public ilk;
GemLike3 public gem;
uint public dec;
uint public live; // Access Flag
constructor(address vat_, bytes32 ilk_, address gem_, uint decimals) public {
require(decimals < 18, "GemJoin3/decimals-18-or-higher");
wards[msg.sender] = 1;
live = 1;
vat = VatLike(vat_);
ilk = ilk_;
gem = GemLike3(gem_);
dec = decimals;
}
function cage() external note auth {
live = 0;
}
function mul(uint x, uint y) internal pure returns (uint z) {
require(y == 0 || (z = x * y) / y == x, "GemJoin3/overflow");
}
function join(address urn, uint wad) public note {
require(live == 1, "GemJoin3/not-live");
uint wad18 = mul(wad, 10 ** (18 - dec));
require(wad18 <= 2 ** 255, "GemJoin3/overflow");
vat.slip(ilk, urn, int(wad18));
require(gem.transferFrom(msg.sender, address(this), wad), "GemJoin3/failed-transfer");
}
function exit(address guy, uint wad) public note {
uint wad18 = mul(wad, 10 ** (18 - dec));
require(wad18 <= 2 ** 255, "GemJoin3/overflow");
vat.slip(ilk, msg.sender, -int(wad18));
require(gem.transfer(guy, wad), "GemJoin3/failed-transfer");
}
}
/// GemJoin4
// Copyright (C) 2019 Lorenzo Manacorda <lorenzo@mailbox.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// For tokens that do not implement transferFrom (like GNT), meaning the usual adapter
// approach won't work: the adapter cannot call transferFrom and therefore
// has no way of knowing when users deposit gems into it.
// To work around this, we introduce the concept of a bag, which is a trusted
// (it's created by the adapter), personalized component (one for each user).
// Users first have to create their bag with `GemJoin4.make`, then transfer
// gem to it, and then call `GemJoin4.join`, which transfer the gems from the
// bag to the adapter.
interface GemLike4 {
function decimals() external view returns (uint);
function balanceOf(address) external returns (uint256);
function transfer(address, uint256) external returns (bool);
}
contract GemBag {
address public ada;
address public lad;
GemLike4 public gem;
constructor(address lad_, address gem_) public {
ada = msg.sender;
lad = lad_;
gem = GemLike4(gem_);
}
function exit(address usr, uint256 wad) external {
require(msg.sender == ada || msg.sender == lad, "GemBag/invalid-caller");
require(gem.transfer(usr, wad), "GemBag/failed-transfer");
}
}
contract GemJoin4 is LibNote {
// --- Auth ---
mapping (address => uint) public wards;
function rely(address usr) external note auth { wards[usr] = 1; }
function deny(address usr) external note auth { wards[usr] = 0; }
modifier auth { require(wards[msg.sender] == 1); _; }
VatLike public vat;
bytes32 public ilk;
GemLike4 public gem;
uint public dec;
uint public live; // Access Flag
mapping(address => address) public bags;
constructor(address vat_, bytes32 ilk_, address gem_) public {
wards[msg.sender] = 1;
live = 1;
vat = VatLike(vat_);
ilk = ilk_;
gem = GemLike4(gem_);
dec = gem.decimals();
}
function cage() external note auth {
live = 0;
}
// -- admin --
function make() external returns (address bag) {
bag = make(msg.sender);
}
function make(address usr) public note returns (address bag) {
require(bags[usr] == address(0), "GemJoin4/bag-already-exists");
bag = address(new GemBag(address(usr), address(gem)));
bags[usr] = bag;
}
// -- gems --
function join(address urn, uint256 wad) external note {
require(live == 1, "GemJoin4/not-live");
require(int256(wad) >= 0, "GemJoin4/negative-amount");
GemBag(bags[msg.sender]).exit(address(this), wad);
vat.slip(ilk, urn, int256(wad));
}
function exit(address usr, uint256 wad) external note {
require(int256(wad) >= 0, "GemJoin4/negative-amount");
vat.slip(ilk, msg.sender, -int256(wad));
require(gem.transfer(usr, wad), "GemJoin4/failed-transfer");
}
}
// GemJoin5
// For a token that has a lower precision than 18 and it has decimals (like USDC)
interface GemLike5 {
function decimals() external view returns (uint8);
function transfer(address,uint) external returns (bool);
function transferFrom(address,address,uint) external returns (bool);
}
contract GemJoin5 is LibNote {
// --- Auth ---
mapping (address => uint) public wards;
function rely(address usr) external note auth { wards[usr] = 1; }
function deny(address usr) external note auth { wards[usr] = 0; }
modifier auth { require(wards[msg.sender] == 1); _; }
VatLike public vat;
bytes32 public ilk;
GemLike5 public gem;
uint public dec;
uint public live; // Access Flag
constructor(address vat_, bytes32 ilk_, address gem_) public {
gem = GemLike5(gem_);
dec = gem.decimals();
require(dec < 18, "GemJoin5/decimals-18-or-higher");
wards[msg.sender] = 1;
live = 1;
vat = VatLike(vat_);
ilk = ilk_;
}
function cage() external note auth {
live = 0;
}
function mul(uint x, uint y) internal pure returns (uint z) {
require(y == 0 || (z = x * y) / y == x, "GemJoin5/overflow");
}
function join(address urn, uint wad) public note {
require(live == 1, "GemJoin5/not-live");
uint wad18 = mul(wad, 10 ** (18 - dec));
require(int(wad18) >= 0, "GemJoin5/overflow");
vat.slip(ilk, urn, int(wad18));
require(gem.transferFrom(msg.sender, address(this), wad), "GemJoin5/failed-transfer");
}
function exit(address guy, uint wad) public note {
uint wad18 = mul(wad, 10 ** (18 - dec));
require(int(wad18) >= 0, "GemJoin5/overflow");
vat.slip(ilk, msg.sender, -int(wad18));
require(gem.transfer(guy, wad), "GemJoin5/failed-transfer");
}
}
// GemJoin6
// For a token with a proxy and implementation contract (like tUSD)
// If the implementation behind the proxy is changed, this prevents joins
// and exits until the implementation is reviewed and approved by governance.
interface GemLike6 {
function decimals() external view returns (uint);
function balanceOf(address) external returns (uint256);
function transfer(address, uint256) external returns (bool);
function transferFrom(address,address,uint) external returns (bool);
function implementation() external view returns (address);
}
contract GemJoin6 is LibNote {
// --- Auth ---
mapping (address => uint256) public wards;
function rely(address usr) external note auth { wards[usr] = 1; }
function deny(address usr) external note auth { wards[usr] = 0; }
modifier auth {
require(wards[msg.sender] == 1, "GemJoin6/not-authorized");
_;
}
VatLike public vat;
bytes32 public ilk;
GemLike6 public gem;
uint public dec;
uint public live; // Access Flag
mapping (address => uint256) public implementations;
constructor(address vat_, bytes32 ilk_, address gem_) public {
wards[msg.sender] = 1;
live = 1;
vat = VatLike(vat_);
ilk = ilk_;
gem = GemLike6(gem_);
setImplementation(gem.implementation(), 1);
dec = gem.decimals();
}
function cage() external note auth {
live = 0;
}
function setImplementation(address implementation, uint256 permitted) public auth note {
implementations[implementation] = permitted; // 1 live, 0 disable
}
function join(address usr, uint wad) external note {
require(live == 1, "GemJoin6/not-live");
require(int(wad) >= 0, "GemJoin6/overflow");
require(implementations[gem.implementation()] == 1, "GemJoin6/implementation-invalid");
vat.slip(ilk, usr, int(wad));
require(gem.transferFrom(msg.sender, address(this), wad), "GemJoin6/failed-transfer");
}
function exit(address usr, uint wad) external note {
require(wad <= 2 ** 255, "GemJoin6/overflow");
require(implementations[gem.implementation()] == 1, "GemJoin6/implementation-invalid");
vat.slip(ilk, msg.sender, -int(wad));
require(gem.transfer(usr, wad), "GemJoin6/failed-transfer");
}
}
// AuthGemJoin
// For a token that needs restriction on the sources which are able to execute the join function (like SAI through Migration contract)
interface GemLike {
function decimals() external view returns (uint);
function transfer(address,uint) external returns (bool);
function transferFrom(address,address,uint) external returns (bool);
}
contract AuthGemJoin is LibNote {
VatLike public vat;
bytes32 public ilk;
GemLike public gem;
uint public dec;
uint public live; // Access Flag
// --- Auth ---
mapping (address => uint) public wards;
function rely(address usr) public note auth { wards[usr] = 1; }
function deny(address usr) public note auth { wards[usr] = 0; }
modifier auth { require(wards[msg.sender] == 1, "AuthGemJoin/non-authed"); _; }
constructor(address vat_, bytes32 ilk_, address gem_) public {
wards[msg.sender] = 1;
live = 1;
vat = VatLike(vat_);
ilk = ilk_;
gem = GemLike(gem_);
dec = gem.decimals();
}
function cage() external note auth {
live = 0;
}
function join(address usr, uint wad) public auth note {
require(live == 1, "AuthGemJoin/not-live");
require(int(wad) >= 0, "AuthGemJoin/overflow");
vat.slip(ilk, usr, int(wad));
require(gem.transferFrom(msg.sender, address(this), wad), "AuthGemJoin/failed-transfer");
}
function exit(address usr, uint wad) public note {
require(wad <= 2 ** 255, "AuthGemJoin/overflow");
vat.slip(ilk, msg.sender, -int(wad));
require(gem.transfer(usr, wad), "AuthGemJoin/failed-transfer");
}
}