-
Notifications
You must be signed in to change notification settings - Fork 34
/
BasicTokenAdapters.sol
312 lines (294 loc) · 12.2 KB
/
BasicTokenAdapters.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
/// BasicTokenAdapters.sol
// 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.6.7;
abstract contract CollateralLike {
function decimals() virtual public view returns (uint);
function transfer(address,uint) virtual public returns (bool);
function transferFrom(address,address,uint) virtual public returns (bool);
}
abstract contract DSTokenLike {
function mint(address,uint) virtual external;
function burn(address,uint) virtual external;
}
abstract contract SAFEEngineLike {
function modifyCollateralBalance(bytes32,address,int) virtual external;
function transferInternalCoins(address,address,uint) virtual external;
}
/*
Here we provide *adapters* to connect the SAFEEngine to arbitrary external
token implementations, creating a bounded context for the SAFEEngine. The
adapters here are provided as working examples:
- `BasicCollateralJoin`: For well behaved ERC20 tokens, with simple transfer semantics.
- `ETHJoin`: For native Ether.
- `CoinJoin`: For connecting internal coin balances to an external
`Coin` implementation.
In practice, adapter implementations will be varied and specific to
individual collateral types, accounting for different transfer
semantics and token standards.
Adapters need to implement two basic methods:
- `join`: enter collateral into the system
- `exit`: remove collateral from the system
*/
contract BasicCollateralJoin {
// --- Auth ---
mapping (address => uint) public authorizedAccounts;
/**
* @notice Add auth to an account
* @param account Account to add auth to
*/
function addAuthorization(address account) external isAuthorized {
authorizedAccounts[account] = 1;
emit AddAuthorization(account);
}
/**
* @notice Remove auth from an account
* @param account Account to remove auth from
*/
function removeAuthorization(address account) external isAuthorized {
authorizedAccounts[account] = 0;
emit RemoveAuthorization(account);
}
/**
* @notice Checks whether msg.sender can call an authed function
**/
modifier isAuthorized {
require(authorizedAccounts[msg.sender] == 1, "BasicCollateralJoin/account-not-authorized");
_;
}
// SAFE database
SAFEEngineLike public safeEngine;
// Collateral type name
bytes32 public collateralType;
// Actual collateral token contract
CollateralLike public collateral;
// How many decimals the collateral token has
uint public decimals;
// Whether this adapter contract is enabled or not
uint public contractEnabled;
// --- Events ---
event AddAuthorization(address account);
event RemoveAuthorization(address account);
event DisableContract();
event Join(address sender, address account, uint wad);
event Exit(address sender, address account, uint wad);
constructor(address safeEngine_, bytes32 collateralType_, address collateral_) public {
authorizedAccounts[msg.sender] = 1;
contractEnabled = 1;
safeEngine = SAFEEngineLike(safeEngine_);
collateralType = collateralType_;
collateral = CollateralLike(collateral_);
decimals = collateral.decimals();
emit AddAuthorization(msg.sender);
}
/**
* @notice Disable this contract
*/
function disableContract() external isAuthorized {
contractEnabled = 0;
emit DisableContract();
}
/**
* @notice Join collateral in the system
* @dev This function locks collateral in the adapter and creates a 'representation' of
* the locked collateral inside the system. This adapter assumes that the collateral
* has 18 decimals
* @param account Account from which we transferFrom collateral and add it in the system
* @param wad Amount of collateral to transfer in the system (represented as a number with 18 decimals)
**/
function join(address account, uint wad) external {
require(contractEnabled == 1, "BasicCollateralJoin/contract-not-enabled");
require(int(wad) >= 0, "BasicCollateralJoin/overflow");
safeEngine.modifyCollateralBalance(collateralType, account, int(wad));
require(collateral.transferFrom(msg.sender, address(this), wad), "BasicCollateralJoin/failed-transfer");
emit Join(msg.sender, account, wad);
}
/**
* @notice Exit collateral from the system
* @dev This function destroys the collateral representation from inside the system
* and exits the collateral from this adapte. The adapter assumes that the collateral
* has 18 decimals
* @param account Account to which we transfer the collateral
* @param wad Amount of collateral to transfer to 'account' (represented as a number with 18 decimals)
**/
function exit(address account, uint wad) external {
require(wad <= 2 ** 255, "BasicCollateralJoin/overflow");
safeEngine.modifyCollateralBalance(collateralType, msg.sender, -int(wad));
require(collateral.transfer(account, wad), "BasicCollateralJoin/failed-transfer");
emit Exit(msg.sender, account, wad);
}
}
contract ETHJoin {
// --- Auth ---
mapping (address => uint) public authorizedAccounts;
/**
* @notice Add auth to an account
* @param account Account to add auth to
*/
function addAuthorization(address account) external isAuthorized {
authorizedAccounts[account] = 1;
emit AddAuthorization(account);
}
/**
* @notice Remove auth from an account
* @param account Account to remove auth from
*/
function removeAuthorization(address account) external isAuthorized {
authorizedAccounts[account] = 0;
emit RemoveAuthorization(account);
}
/**
* @notice Checks whether msg.sender can call an authed function
**/
modifier isAuthorized {
require(authorizedAccounts[msg.sender] == 1, "ETHJoin/account-not-authorized");
_;
}
// SAFE database
SAFEEngineLike public safeEngine;
// Collateral type name
bytes32 public collateralType;
// Whether this contract is enabled or not
uint public contractEnabled;
// Number of decimals ETH has
uint public decimals;
// --- Events ---
event AddAuthorization(address account);
event RemoveAuthorization(address account);
event DisableContract();
event Join(address sender, address account, uint wad);
event Exit(address sender, address account, uint wad);
constructor(address safeEngine_, bytes32 collateralType_) public {
authorizedAccounts[msg.sender] = 1;
contractEnabled = 1;
safeEngine = SAFEEngineLike(safeEngine_);
collateralType = collateralType_;
decimals = 18;
emit AddAuthorization(msg.sender);
}
/**
* @notice Disable this contract
*/
function disableContract() external isAuthorized {
contractEnabled = 0;
emit DisableContract();
}
/**
* @notice Join ETH in the system
* @param account Account that will receive the ETH representation inside the system
**/
function join(address account) external payable {
require(contractEnabled == 1, "ETHJoin/contract-not-enabled");
require(int(msg.value) >= 0, "ETHJoin/overflow");
safeEngine.modifyCollateralBalance(collateralType, account, int(msg.value));
emit Join(msg.sender, account, msg.value);
}
/**
* @notice Exit ETH from the system
* @param account Account that will receive the ETH representation inside the system
**/
function exit(address payable account, uint wad) external {
require(int(wad) >= 0, "ETHJoin/overflow");
safeEngine.modifyCollateralBalance(collateralType, msg.sender, -int(wad));
emit Exit(msg.sender, account, wad);
account.transfer(wad);
}
}
contract CoinJoin {
// --- Auth ---
mapping (address => uint) public authorizedAccounts;
/**
* @notice Add auth to an account
* @param account Account to add auth to
*/
function addAuthorization(address account) external isAuthorized {
authorizedAccounts[account] = 1;
emit AddAuthorization(account);
}
/**
* @notice Remove auth from an account
* @param account Account to remove auth from
*/
function removeAuthorization(address account) external isAuthorized {
authorizedAccounts[account] = 0;
emit RemoveAuthorization(account);
}
/**
* @notice Checks whether msg.sender can call an authed function
**/
modifier isAuthorized {
require(authorizedAccounts[msg.sender] == 1, "CoinJoin/account-not-authorized");
_;
}
// SAFE database
SAFEEngineLike public safeEngine;
// Coin created by the system; this is the external, ERC-20 representation, not the internal 'coinBalance'
DSTokenLike public systemCoin;
// Whether this contract is enabled or not
uint public contractEnabled;
// Number of decimals the system coin has
uint public decimals;
// --- Events ---
event AddAuthorization(address account);
event RemoveAuthorization(address account);
event DisableContract();
event Join(address sender, address account, uint wad);
event Exit(address sender, address account, uint wad);
constructor(address safeEngine_, address systemCoin_) public {
authorizedAccounts[msg.sender] = 1;
contractEnabled = 1;
safeEngine = SAFEEngineLike(safeEngine_);
systemCoin = DSTokenLike(systemCoin_);
decimals = 18;
emit AddAuthorization(msg.sender);
}
/**
* @notice Disable this contract
*/
function disableContract() external isAuthorized {
contractEnabled = 0;
emit DisableContract();
}
uint constant RAY = 10 ** 27;
function multiply(uint x, uint y) internal pure returns (uint z) {
require(y == 0 || (z = x * y) / y == x);
}
/**
* @notice Join system coins in the system
* @dev Exited coins have 18 decimals but inside the system they have 45 (rad) decimals.
When we join, the amount (wad) is multiplied by 10**27 (ray)
* @param account Account that will receive the joined coins
* @param wad Amount of external coins to join (18 decimal number)
**/
function join(address account, uint wad) external {
safeEngine.transferInternalCoins(address(this), account, multiply(RAY, wad));
systemCoin.burn(msg.sender, wad);
emit Join(msg.sender, account, wad);
}
/**
* @notice Exit system coins from the system and inside 'Coin.sol'
* @dev Inside the system, coins have 45 (rad) decimals but outside they have 18 decimals (wad).
When we exit, we specify a wad amount of coins and then the contract automatically multiplies
wad by 10**27 to move the correct 45 decimal coin amount to this adapter
* @param account Account that will receive the exited coins
* @param wad Amount of internal coins to join (18 decimal number that will be multiplied by ray)
**/
function exit(address account, uint wad) external {
require(contractEnabled == 1, "CoinJoin/contract-not-enabled");
safeEngine.transferInternalCoins(msg.sender, address(this), multiply(RAY, wad));
systemCoin.mint(account, wad);
emit Exit(msg.sender, account, wad);
}
}