-
Notifications
You must be signed in to change notification settings - Fork 21
/
join-4.sol
118 lines (94 loc) · 3.79 KB
/
join-4.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
// SPDX-License-Identifier: AGPL-3.0-or-later
/// join-4.sol -- Non-standard token adapters
// Copyright (C) 2018 Rain <rainbreak@riseup.net>
// Copyright (C) 2018-2020 Maker Ecosystem Growth Holdings, INC.
//
// 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, int256) external;
}
interface GemLike {
function decimals() external view returns (uint256);
function balanceOf(address) external returns (uint256);
function transfer(address, uint256) external returns (bool);
}
// 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.
contract GemBag {
address public ada;
address public lad;
GemLike public gem;
constructor(address lad_, address gem_) public {
ada = msg.sender;
lad = lad_;
gem = GemLike(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 => 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); _; }
VatLike public vat;
bytes32 public ilk;
GemLike public gem;
uint256 public dec;
uint256 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 = GemLike(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");
}
}