-
Notifications
You must be signed in to change notification settings - Fork 41
/
TokenWalletBase.sol
281 lines (241 loc) · 8.85 KB
/
TokenWalletBase.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
pragma ton-solidity >= 0.57.0;
pragma AbiHeader expire;
pragma AbiHeader pubkey;
import "../interfaces/ITokenWallet.sol";
import "../interfaces/ITokenRoot.sol";
import "../interfaces/IAcceptTokensTransferCallback.sol";
import "../interfaces/IAcceptTokensMintCallback.sol";
import "../interfaces/IBounceTokensTransferCallback.sol";
import "../interfaces/IBounceTokensBurnCallback.sol";
import "../libraries/TokenErrors.sol";
import "../libraries/TokenMsgFlag.sol";
abstract contract TokenWalletBase is ITokenWallet {
address static root_;
address static owner_;
uint128 balance_;
modifier onlyRoot() {
require(root_ == msg.sender, TokenErrors.NOT_ROOT);
_;
}
modifier onlyOwner() {
require(owner_ == msg.sender, TokenErrors.NOT_OWNER);
_;
}
function balance() override external view responsible returns (uint128) {
return { value: 0, flag: TokenMsgFlag.REMAINING_GAS, bounce: false } balance_;
}
function owner() override external view responsible returns (address) {
return { value: 0, flag: TokenMsgFlag.REMAINING_GAS, bounce: false } owner_;
}
function root() override external view responsible returns (address) {
return { value: 0, flag: TokenMsgFlag.REMAINING_GAS, bounce: false } root_;
}
function walletCode() override external view responsible returns (TvmCell) {
return { value: 0, flag: TokenMsgFlag.REMAINING_GAS, bounce: false } tvm.code();
}
function transfer(
uint128 amount,
address recipient,
uint128 deployWalletValue,
address remainingGasTo,
bool notify,
TvmCell payload
)
override
external
onlyOwner
{
require(amount > 0, TokenErrors.WRONG_AMOUNT);
require(amount <= balance_, TokenErrors.NOT_ENOUGH_BALANCE);
require(recipient.value != 0 && recipient != owner_, TokenErrors.WRONG_RECIPIENT);
tvm.rawReserve(_reserve(), 0);
TvmCell stateInit = _buildWalletInitData(recipient);
address recipientWallet;
if (deployWalletValue > 0) {
recipientWallet = _deployWallet(stateInit, deployWalletValue, remainingGasTo);
} else {
recipientWallet = address(tvm.hash(stateInit));
}
balance_ -= amount;
ITokenWallet(recipientWallet).acceptTransfer{ value: 0, flag: TokenMsgFlag.ALL_NOT_RESERVED, bounce: true }(
amount,
owner_,
remainingGasTo,
notify,
payload
);
}
/*
@notice Transfer tokens using another TokenWallet address, that wallet must be deployed previously
@dev Can be called only by token wallet owner
@param amount How much tokens to transfer
@param recipientWallet Recipient TokenWallet address
@param remainingGasTo Remaining gas receiver
@param notify Notify receiver on incoming transfer
@param payload Notification payload
*/
function transferToWallet(
uint128 amount,
address recipientTokenWallet,
address remainingGasTo,
bool notify,
TvmCell payload
)
override
external
onlyOwner
{
require(amount > 0, TokenErrors.WRONG_AMOUNT);
require(amount <= balance_, TokenErrors.NOT_ENOUGH_BALANCE);
require(recipientTokenWallet.value != 0 && recipientTokenWallet != address(this), TokenErrors.WRONG_RECIPIENT);
tvm.rawReserve(_reserve(), 0);
balance_ -= amount;
ITokenWallet(recipientTokenWallet).acceptTransfer{ value: 0, flag: TokenMsgFlag.ALL_NOT_RESERVED, bounce: true }(
amount,
owner_,
remainingGasTo,
notify,
payload
);
}
/*
@notice Callback for transfer operation
@dev Can be called only by another valid TokenWallet contract with same root
@param amount How much tokens to receive
@param sender Sender address
@param remainingGasTo Remaining gas receiver
@param notify Notify receiver on incoming transfer
@param payload Notification payload
*/
function acceptTransfer(
uint128 amount,
address sender,
address remainingGasTo,
bool notify,
TvmCell payload
)
override
external
functionID(0x67A0B95F)
{
require(msg.sender == address(tvm.hash(_buildWalletInitData(sender))), TokenErrors.SENDER_IS_NOT_VALID_WALLET);
tvm.rawReserve(_reserve(), 2);
balance_ += amount;
if (notify) {
IAcceptTokensTransferCallback(owner_).onAcceptTokensTransfer{
value: 0,
flag: TokenMsgFlag.ALL_NOT_RESERVED + TokenMsgFlag.IGNORE_ERRORS,
bounce: false
}(
root_,
amount,
sender,
msg.sender,
remainingGasTo,
payload
);
} else {
remainingGasTo.transfer({ value: 0, flag: TokenMsgFlag.ALL_NOT_RESERVED + TokenMsgFlag.IGNORE_ERRORS, bounce: false });
}
}
/*
@notice Accept minted tokens from root
@dev Can be called only by root token
@param amount How much tokens to accept
@param data Additional data
*/
function acceptMint(uint128 amount, address remainingGasTo, bool notify, TvmCell payload)
override
external
functionID(0x4384F298)
onlyRoot
{
tvm.rawReserve(_reserve(), 2);
balance_ += amount;
if (notify) {
IAcceptTokensMintCallback(owner_).onAcceptTokensMint{
value: 0,
bounce: false,
flag: TokenMsgFlag.ALL_NOT_RESERVED + TokenMsgFlag.IGNORE_ERRORS
}(
root_,
amount,
remainingGasTo,
payload
);
} else if (remainingGasTo.value != 0 && remainingGasTo != address(this)) {
remainingGasTo.transfer({ value: 0, flag: TokenMsgFlag.ALL_NOT_RESERVED + TokenMsgFlag.IGNORE_ERRORS, bounce: false });
}
}
/*
@notice On-bounce handler
@dev Catch bounce if acceptTransfer or tokensBurned fails
@dev If transfer fails - increase back tokens balance and notify owner
@dev If tokens burn root token callback fails - increase back tokens balance and notify owner
*/
onBounce(TvmSlice body) external {
tvm.rawReserve(_reserve(), 2);
uint32 functionId = body.decode(uint32);
if (functionId == tvm.functionId(ITokenWallet.acceptTransfer)) {
uint128 amount = body.decode(uint128);
balance_ += amount;
IBounceTokensTransferCallback(owner_).onBounceTokensTransfer{
value: 0,
flag: TokenMsgFlag.ALL_NOT_RESERVED + TokenMsgFlag.IGNORE_ERRORS,
bounce: false
}(
root_,
amount,
msg.sender
);
} else if (functionId == tvm.functionId(ITokenRoot.acceptBurn)) {
uint128 amount = body.decode(uint128);
balance_ += amount;
IBounceTokensBurnCallback(owner_).onBounceTokensBurn{
value: 0,
flag: TokenMsgFlag.ALL_NOT_RESERVED + TokenMsgFlag.IGNORE_ERRORS,
bounce: false
}(
root_,
amount
);
}
}
function _burn(
uint128 amount,
address remainingGasTo,
address callbackTo,
TvmCell payload
) internal {
require(amount > 0, TokenErrors.WRONG_AMOUNT);
require(amount <= balance_, TokenErrors.NOT_ENOUGH_BALANCE);
tvm.rawReserve(_reserve(), 0);
balance_ -= amount;
ITokenRoot(root_).acceptBurn{ value: 0, flag: TokenMsgFlag.ALL_NOT_RESERVED, bounce: true }(
amount,
owner_,
remainingGasTo,
callbackTo,
payload
);
}
/*
@notice Withdraw all surplus balance in EVERs
@dev Can by called only by owner address
@param to Withdraw receiver
*/
function sendSurplusGas(address to) external view onlyOwner {
tvm.rawReserve(_targetBalance(), 0);
to.transfer({
value: 0,
flag: TokenMsgFlag.ALL_NOT_RESERVED + TokenMsgFlag.IGNORE_ERRORS,
bounce: false
});
}
function _reserve() internal pure returns (uint128) {
return math.max(address(this).balance - msg.value, _targetBalance());
}
function _targetBalance() virtual internal pure returns (uint128);
function _buildWalletInitData(address walletOwner) virtual internal view returns (TvmCell);
function _deployWallet(TvmCell initData, uint128 deployWalletValue, address remainingGasTo) virtual internal view returns (address);
}