-
Notifications
You must be signed in to change notification settings - Fork 21
/
join.sol
159 lines (133 loc) · 5.46 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
// SPDX-License-Identifier: AGPL-3.0-or-later
/// join.sol -- Basic token adapters
// Copyright (C) 2018 Rain <[email protected]>
//
// 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 "./lib.sol";
interface GemLike {
function decimals() external view returns (uint);
function transfer(address,uint) external returns (bool);
function transferFrom(address,address,uint) external returns (bool);
}
interface DSTokenLike {
function mint(address,uint) external;
function burn(address,uint) external;
}
interface VatLike {
function slip(bytes32,address,int) external;
function move(address,address,uint) external;
}
/*
Here we provide *adapters* to connect the Vat to arbitrary external
token implementations, creating a bounded context for the Vat. The
adapters here are provided as working examples:
- `GemJoin`: For well behaved ERC20 tokens, with simple transfer
semantics.
- `ETHJoin`: For native Ether.
- `DaiJoin`: For connecting internal Dai balances to an external
`DSToken` 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
*/
//ERC20 Token的适配器模块
//Join模块通过实现Token的抽象外部接口来连接Vat,为vat创建上下文
//Join由GemJoin和DaiJoin两个合约组成
//GemJoin允许标准的ERC20 Token存入系统以供使用
//DaiJoin允许用户提供dai到标准的ERC20 Token中。
//每个Join都专门的创建,以允许特定的Token被加入到到Vat中,因此每个Join合约都有略微不同的逻辑来说明系统中不同的Token.
contract GemJoin 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, "GemJoin/not-authorized");
_;
}
//vat合约的部署地址
VatLike public vat; // CDP Engine
//GemJoin创建的Ilk的id
bytes32 public ilk; // Collateral Type
//ERC20 Token合约的地址
GemLike public gem;
//ERC20 Token的精度
uint public dec;
uint public live; // Active Flag
//Join模块有4个公共函数,分别是构造函数, join, exit, and cage
//join、exit为用户提供了特定Token加入到Vat的机制,每个Token略有不同,但是总的来说是对vat里面transfer和call函数的调用
//exit函数允许用户移除在vat中的token
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;
}
//将抵押物gem转移wad数量到当前合约下,在vat中记载数量
function join(address usr, uint wad) external note {
require(live == 1, "GemJoin/not-live");
require(int(wad) >= 0, "GemJoin/overflow");
vat.slip(ilk, usr, int(wad));
require(gem.transferFrom(msg.sender, address(this), wad), "GemJoin/failed-transfer");
}
function exit(address usr, uint wad) external note {
require(wad <= 2 ** 255, "GemJoin/overflow");
vat.slip(ilk, msg.sender, -int(wad));
require(gem.transfer(usr, wad), "GemJoin/failed-transfer");
}
}
contract DaiJoin 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, "DaiJoin/not-authorized");
_;
}
VatLike public vat; // CDP Engine
DSTokenLike public dai; // Stablecoin Token
uint public live; // Active Flag
constructor(address vat_, address dai_) public {
wards[msg.sender] = 1;
live = 1;
vat = VatLike(vat_);
dai = DSTokenLike(dai_);
}
function cage() external note auth {
live = 0;
}
uint constant ONE = 10 ** 27;
function mul(uint x, uint y) internal pure returns (uint z) {
require(y == 0 || (z = x * y) / y == x);
}
function join(address usr, uint wad) external note {
vat.move(address(this), usr, mul(ONE, wad));
dai.burn(msg.sender, wad);
}
function exit(address usr, uint wad) external note {
require(live == 1, "DaiJoin/not-live");
vat.move(msg.sender, address(this), mul(ONE, wad));
dai.mint(usr, wad);
}
}