-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVault.sol
202 lines (161 loc) · 6.49 KB
/
Vault.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./IDRC20.sol";
contract Vault {
bytes32 private constant DOMAIN_NAME = keccak256("Vault");
bytes32 public constant DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
bytes32 public constant WITHDRAW_TYPEHASH = keccak256(abi.encodePacked("Withdraw(address token,address to,uint256 amount,string txid)"));
bytes32 public immutable DOMAIN_SEPARATOR;
bool private entered;
address public admin;
address public pendingAdmin;
uint256 public fee;
uint256 public totalFees;
address[] public signers;
mapping (address => bool) public authorized;
mapping (address => uint256) public indexes;
mapping (bytes32 => bool) public used;
mapping (address => uint256) public allowances;
event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin);
event NewAdmin(address oldAdmin, address newAdmin);
event FeeChanged(uint256 indexed oldFee, uint256 indexed newFee);
event Withdraw(address indexed token, address indexed to, uint256 indexed amount, string txid);
event Deposit(address indexed token, address indexed from, uint256 indexed amount, uint256 fee, string receiver);
event Approval(address indexed sender, address indexed token, uint256 amount);
event SignerAdded(address indexed sender, address indexed account);
event SignerRemoved(address indexed sender, address indexed account);
modifier nonReentrant() {
require(!entered, "reentrant");
entered = true;
_;
entered = false;
}
modifier onlyAdmin() {
require(msg.sender == admin, "unauthorized");
_;
}
constructor(address[] memory _signers) {
for (uint256 i = 0; i < _signers.length; i++) {
address _addr = _signers[i];
require(!authorized[_addr], "duplicate existence");
signers.push(_addr);
authorized[_addr] = true;
indexes[_addr] = i;
}
admin = msg.sender;
emit NewAdmin(address(0), msg.sender);
fee = 0.01 ether;
uint256 chainId;
assembly {
chainId := chainid()
}
DOMAIN_SEPARATOR = keccak256(abi.encode(DOMAIN_TYPEHASH, DOMAIN_NAME, keccak256(bytes('1')), chainId, address(this)));
}
function deposit(
address token,
uint256 amount,
string calldata receiver
) external payable nonReentrant {
require(bytes(receiver).length > 0, "invalid receiver");
if (token == address(0)) {
require(msg.value >= amount + fee, "invalid value");
} else {
require(msg.value >= fee, "invalid value");
IDRC20(token).transferFrom(msg.sender, address(this), amount);
}
totalFees += fee;
emit Deposit(token, msg.sender, amount, fee, receiver);
}
function withdraw(
address token,
address to,
uint256 amount,
string calldata txid,
uint8[] calldata v,
bytes32[] calldata r,
bytes32[] calldata s
) external nonReentrant {
require(allowances[token] >= amount, "insufficient allowance");
require(
signers.length > 0 &&
signers.length == v.length &&
signers.length == r.length &&
signers.length == s.length,
"invalid signatures"
);
bytes32 digest = buildWithdrawSeparator(token, to, amount, txid);
require(!used[digest], "reuse");
used[digest] = true;
address[] memory signatures = new address[](signers.length);
for (uint256 i = 0; i < signers.length; i++) {
address signer = ecrecover(digest, v[i], r[i], s[i]);
require(authorized[signer], "invalid signer");
for (uint256 j = 0; j < i; j++) {
require(signatures[j] != signer, "duplicated");
}
signatures[i] = signer;
}
if (token == address(0)) {
require((address(this)).balance >= amount, "insufficient balance");
payable(to).transfer(amount);
} else {
require(IDRC20(token).balanceOf(address(this)) >= amount, "insufficient balance");
IDRC20(token).transfer(to, amount);
}
allowances[token] -= amount;
emit Withdraw(token, to, amount, txid);
}
function withdrawFees(address to) external onlyAdmin {
require(address(this).balance >= totalFees, "insufficient balance");
payable(to).transfer(totalFees);
totalFees = 0;
}
function approve(address token, uint256 amount) external onlyAdmin {
allowances[token] = amount;
emit Approval(msg.sender, token, amount);
}
function setPendingAdmin(address newPendingAdmin) external onlyAdmin {
emit NewPendingAdmin(pendingAdmin, newPendingAdmin);
pendingAdmin = newPendingAdmin;
}
function acceptAdmin() external {
require(msg.sender == pendingAdmin, "pendingAdmin only");
emit NewAdmin(admin, pendingAdmin);
emit NewPendingAdmin(pendingAdmin, address(0));
admin = pendingAdmin;
pendingAdmin = address(0);
}
function setFee(uint256 _fee) external onlyAdmin {
emit FeeChanged(fee, _fee);
fee = _fee;
}
function addSigner(address account) external onlyAdmin {
require(!authorized[account], "already exists");
indexes[account] = signers.length;
authorized[account] = true;
signers.push(account);
emit SignerAdded(msg.sender, account);
}
function removeSigner(address account) external onlyAdmin {
require(signers.length > 1, "illogical");
require(authorized[account], "non-existent");
uint256 index = indexes[account];
uint256 lastIndex = signers.length - 1;
if (index != lastIndex) {
address lastAddr = signers[lastIndex];
signers[index] = lastAddr;
indexes[lastAddr] = index;
}
delete authorized[account];
delete indexes[account];
signers.pop();
emit SignerRemoved(msg.sender, account);
}
function buildWithdrawSeparator(address token, address to, uint256 amount, string calldata txid) view public returns (bytes32) {
return keccak256(abi.encodePacked(
'\x19\x01',
DOMAIN_SEPARATOR,
keccak256(abi.encode(WITHDRAW_TYPEHASH, token, to, amount, keccak256(bytes(txid))))
));
}
}