-
Notifications
You must be signed in to change notification settings - Fork 0
/
ton_dca_wallet.tact
308 lines (255 loc) · 9.73 KB
/
ton_dca_wallet.tact
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
import "@stdlib/ownable";
import "@stdlib/stoppable";
import "./types.tact";
import "./traits.tact";
message InitUsdtJettonAddress {
queryId: Int as uint64;
usdt_jetton_address: Address;
}
struct JettonTransferForwardPayload {
is_right: Bool;
value: Slice as remaining;
}
message(0x0f8a7ea5) JettonTransfer {
query_id: Int as uint64;
amount: Int as coins;
destination: Address;
response_destination: Address;
custom_payload: Cell?;
forward_ton_amount: Int as coins;
forward_payload: JettonTransferForwardPayload;
}
message(0x7362d09c) JettonNotify {
queryId: Int as uint64;
amount: Int as coins;
sender: Address;
forward_payload: Slice as remaining;
}
message(0xd53276db) TokenExcesses {
queryId: Int as uint64;
}
message WithdrawJetton {
queryId: Int as uint64;
amount: Int as coins;
}
message WithdrawTon {
queryId: Int as uint64;
}
struct StonfiSwapForwardPayloadValue {
token_wallet: Address;
min_out: Int as coins;
to_address: Address;
referral_address: Address?;
}
struct StonfiSwapForwardPayload {
sum_type: String;
op_code: Int as uint32;
value: StonfiSwapForwardPayloadValue;
}
const JETTON_TRANSFER_OP_CODE: Int = 0x0f8a7ea5;
const STONFI_SWAP_OK_REF_OP_CODE: Int = 0x2283c2a0;
const STONFI_SWAP_OP_CODE: Int = 0x25938561;
const STONFI_SWAP_FORWARD_TON_AMOUNT: Int = ton("0.265");
const STONFI_SWAP_TON_AMOUNT: Int = ton("0.35");
//const ONE_COIN: Int = ton("1");
//@todo заменить
const ONE_COIN: Int = 1;
contract TonDcaWallet with Ownable, Stoppable, TonReceivable {
const MIN_TON_BALANCE: Int = ton("0.05");
//@todo продумать как правильно держать эти адреса для тестнета и майннета
const STONFI_ROUTER_ADDRESS: Address = address("kQBsGx9ArADUrREB34W-ghgsCgBShvfUr4Jvlu-0KGc33a1n");
const STONFI_PROXY_TON_WALLET: Address = address("kQCdC2b1GG1saybYxCwRfEqr4WlOexsQIcYcfMYObk_477vs"); // ProxyTON Wallet
owner: Address;
master: Address;
stopped: Bool;
dca_amount: Int as uint32 = 0;
dca_interval_in_seconds: Int as uint32 = 0;
dca_next_buy_time: Int = 0;
usdt_jetton_address: Address?;
usdt_jetton_balance: Int as coins = 0;
buys_count: Int as uint32 = 0;
avg_buy_price: Int as coins = 0;
init(master: Address, owner: Address) {
self.master = master;
self.owner = owner;
self.stopped = false;
}
receive(msg: InternalInitWalletSettings) {
self.requireMaster();
self.validateAmount(msg.amount);
self.validateInterval(msg.interval);
self.owner = msg.owner;
self.dca_amount = msg.amount;
self.dca_interval_in_seconds = msg.interval;
self.dca_next_buy_time = msg.next_buy_time;
let msgValue: Int = context().value;
let tonBalanceBeforeMsg: Int = myBalance() - msgValue;
let storageFee: Int = self.MIN_TON_BALANCE + min(tonBalanceBeforeMsg, self.MIN_TON_BALANCE);
msgValue = msgValue - storageFee;
self.notifyOwner(msgValue, "DCA Strategy created".asComment());
}
receive(msg: InitUsdtJettonAddress) {
self.requireOwner();
self.usdt_jetton_address = msg.usdt_jetton_address;
}
receive(msg: UpdateWalletSettings) {
self.requireOwner();
self.validateAmount(msg.amount);
self.validateInterval(msg.interval);
self.dca_amount = msg.amount;
self.dca_interval_in_seconds = msg.interval;
self.dca_next_buy_time = msg.next_buy_time;
self.reply("Settings updated".asComment());
}
receive("Resume") {
self.requireOwner();
self.requireStopped();
self.stopped = false;
self.dca_next_buy_time = now() + self.dca_interval_in_seconds;
self.reply("Resumed".asComment());
}
receive(msg: JettonNotify) {
if(sender() == self.STONFI_PROXY_TON_WALLET && msg.sender == self.STONFI_ROUTER_ADDRESS){
let swapOpCode: Int = msg.forward_payload.loadUint(32);
if(swapOpCode == STONFI_SWAP_OK_REF_OP_CODE) {
self.buys_count = self.buys_count + 1;
let currentBuyPrice: Int = self.dca_amount / msg.amount;
if(self.avg_buy_price == 0) {
self.avg_buy_price = currentBuyPrice;
} else {
self.avg_buy_price = (currentBuyPrice + self.avg_buy_price) / 2;
}
if(self.usdt_jetton_balance < self.dca_amount) {
self.notifyOwner(ton("0.01"), "DCA Strategy was stopped. USD is over.".asComment());
}
}
return;
}
if(self.usdt_jetton_address == null) {
return;
}
if(sender() == self.usdt_jetton_address!! && msg.sender == self.owner) {
self.usdt_jetton_balance = self.usdt_jetton_balance + msg.amount;
self.notifyOwner(ton("0.01"), "Jetton balance has been topped up".asComment());
}
}
receive(msg: WithdrawJetton) {
self.requireOwner();
let jettonBalanceAfterWithdraw: Int = self.usdt_jetton_balance - msg.amount;
require(jettonBalanceAfterWithdraw >= 0, "Invalid requested amount");
self.usdt_jetton_balance = jettonBalanceAfterWithdraw;
if(self.usdt_jetton_balance < self.dca_amount) {
self.notifyOwner(ton("0.01"), "DCA Strategy was stopped. USD is over.".asComment());
}
send(SendParameters{
to: self.usdt_jetton_address!!,
value: ton("0.1"),
mode: SendIgnoreErrors,
body: JettonTransfer {
query_id: msg.queryId,
amount: msg.amount,
destination: sender(),
response_destination: myAddress(),
custom_payload: null,
forward_ton_amount: 1,
forward_payload: JettonTransferForwardPayload{
is_right: false,
value: emptySlice()
}
}.toCell()
});
}
receive(msg: WithdrawTon) {
self.requireOwner();
let msgValue: Int = myBalance() - self.MIN_TON_BALANCE;
require(msgValue > self.MIN_TON_BALANCE, "Infuccient funds");
self.stopped = true;
self.notifyOwner(msgValue, "Withdraw TON and Stop DCA.".asComment());
}
receive(msg: TokenExcesses) {
//для получения кэшбека после withdraw
}
external("ProcessPay") {
self.requireNotStopped();
require(self.dca_next_buy_time <= now(), "Too early for buy");
require(self.usdt_jetton_balance >= self.dca_amount, "Insufficient funds for DCA buy");
acceptMessage();
self.dca_next_buy_time = self.dca_next_buy_time + self.dca_interval_in_seconds;
self.usdt_jetton_balance = self.usdt_jetton_balance - self.dca_amount;
self.requestStonfiSwap(self.dca_amount);
}
bounced(src: bounced<JettonTransfer>) {
self.stopped = true;
self.dca_next_buy_time = self.dca_next_buy_time - self.dca_interval_in_seconds;
self.usdt_jetton_balance = self.usdt_jetton_balance + self.dca_amount;
send(SendParameters{
to: self.master,
value: ton("0.01"),
bounce: false,
mode: SendIgnoreErrors,
body: "Something went wrong with DCA buy process".asComment()
});
}
get fun seqno(): Int {
return self.seqno;
}
get fun get_settings(): WalletSettings {
return WalletSettings{
owner: self.owner,
master: self.master,
amount: self.dca_amount,
interval: self.dca_interval_in_seconds,
stopped: self.stopped,
next_buy_time: self.dca_next_buy_time,
usdt_balance: self.usdt_jetton_balance,
usdt_jetton_address: self.usdt_jetton_address,
buys_count: self.buys_count,
avg_buy_price: self.avg_buy_price
};
}
fun requestStonfiSwap(amount: Int) {
let swapPayload: Cell = beginCell()
.storeUint(STONFI_SWAP_OP_CODE, 32)
.storeAddress(self.STONFI_PROXY_TON_WALLET)
.storeCoins(1)
.storeAddress(self.owner)
.storeUint(1, 1)//.storeUint(0, 1) - referral_address: null
.storeAddress(myAddress())
.endCell();
let body: Cell = beginCell()
.storeUint(JETTON_TRANSFER_OP_CODE, 32)//op_code
.storeUint(999, 64)//query_id
.storeCoins(amount)//jetton_amount
.storeAddress(self.STONFI_ROUTER_ADDRESS)//send_to
.storeAddress(self.owner)//excess_to //.storeUint(0, 2)//пустой адрес - excess_to
.storeUint(0, 1)//custom_payload: null
.storeCoins(STONFI_SWAP_FORWARD_TON_AMOUNT)//forward_ton_amount
.storeUint(1, 1)//has_forward_payload: true
.storeRef(swapPayload)//forward_payload
.endCell();
send(SendParameters{
to: self.usdt_jetton_address!!,
value: STONFI_SWAP_TON_AMOUNT,
mode: SendIgnoreErrors,
body: body
});
}
fun notifyOwner(value: Int, msg: Cell) {
send(SendParameters{
to: self.owner,
value: value,
bounce: false,
mode: SendIgnoreErrors,
body: msg
});
}
fun requireMaster() {
require(sender() == self.master, "Master Only");
}
fun validateAmount(amount: Int) {
require(amount > ONE_COIN, "Amount can't be less than 1 USD");
}
fun validateInterval(interval: Int) {
require(interval > 43200, "Interval can't be less than 12 hours");
}
}