This repository has been archived by the owner on Jan 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDca.vy
457 lines (357 loc) · 13.5 KB
/
Dca.vy
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# @version ^0.3.7
###################################################################
#
# @title Unstoppable Spot DEX - DCA (Dollar-Cost-Average)
# @license GNU AGPLv3
# @author unstoppable.ooo
#
# @custom:security-contact [email protected]
#
# @notice
# This contract is part of the Unstoppable Spot DEX.
# It provides the infrastructure for placing permissionless,
# self-custodial DCA orders for Uni v3 pairs.
#
# This is an early ALPHA release, use at your own risk!
#
###################################################################
from vyper.interfaces import ERC20
from vyper.interfaces import ERC20Detailed
# struct ExactInputParams {
# bytes path;
# address recipient;
# uint256 deadline;
# uint256 amountIn;
# uint256 amountOutMinimum;
# }
struct ExactInputParams:
path: Bytes[66]
recipient: address
deadline: uint256
amountIn: uint256
amountOutMinimum: uint256
# function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);
interface UniswapV3SwapRouter:
def exactInput(_params: ExactInputParams) -> uint256: payable
interface Univ3Twap:
def getTwap(_path: DynArray[address, 3], _fees: DynArray[uint24, 2], _twapLength: uint32) -> uint256: view
PRECISISON: constant(uint256) = 10**18
UNISWAP_ROUTER: constant(address) = 0xE592427A0AEce92De3Edee1F18E0157C05861564
TWAP: constant(address) = 0xFa64f316e627aD8360de2476aF0dD9250018CFc5
FEE_BASE: constant(uint256) = 1000000 # 100 percent
fee: public(uint256) # = 1000 # 0.1%
MAX_SLIPPAGE: constant(uint256) = 10000 # 1 percent
# owner
owner: public(address)
suggested_owner: public(address)
# DCA Orders
struct DcaOrder:
uid: bytes32
account: address
token_in: address
token_out: address
amount_in_per_execution: uint256
seconds_between_executions: uint256
max_number_of_executions: uint8
max_slippage: uint256
twap_length: uint32
number_of_executions: uint8
last_execution: uint256
# user address -> DcaOrder UID
dca_order_uids: public(HashMap[address, DynArray[bytes32, 1024]])
# UID -> DcaOrder
dca_orders: public(HashMap[bytes32, DcaOrder])
position_nonce: uint256
is_paused: public(bool)
is_accepting_new_orders: public(bool)
@external
def __init__():
self.owner = msg.sender
self.fee = 1000 # 0.1%
event DcaOrderPosted:
uid: bytes32
account: indexed(address)
token_in: indexed(address)
token_out: indexed(address)
amount_in_per_execution: uint256
seconds_between_executions: uint256
max_number_of_executions: uint8
twap_length: uint32
@external
def post_dca_order(
_token_in: address,
_token_out: address,
_amount_in_per_execution: uint256,
_seconds_between_executions: uint256,
_max_number_of_executions: uint8,
_max_slippage: uint256,
_twap_length: uint32
):
assert not self.is_paused, "paused"
assert self.is_accepting_new_orders, "not accepting new orders"
# check msg.sender approved contract to spend amount token_in
total: uint256 = convert(_max_number_of_executions, uint256) * _amount_in_per_execution
allowance: uint256 = ERC20(_token_in).allowance(msg.sender, self)
assert allowance >= total, "insufficient allowance"
max_slippage: uint256 = _max_slippage
if max_slippage == 0:
max_slippage = MAX_SLIPPAGE
order: DcaOrder = DcaOrder({
uid: empty(bytes32),
account: msg.sender,
token_in: _token_in,
token_out: _token_out,
amount_in_per_execution: _amount_in_per_execution,
seconds_between_executions: _seconds_between_executions,
max_number_of_executions: _max_number_of_executions,
max_slippage: _max_slippage,
twap_length: _twap_length,
number_of_executions: 0,
last_execution: 0
})
uid: bytes32 = self._generate_uid()
order.uid = uid
self.dca_orders[uid] = order
self.dca_order_uids[msg.sender].append(uid)
log DcaOrderPosted(uid, msg.sender, _token_in, _token_out, _amount_in_per_execution, _seconds_between_executions, _max_number_of_executions, _twap_length)
event DcaOrderExecuted:
uid: bytes32
account: indexed(address)
execution_number: uint8
amount_in: uint256
amount_out: uint256
event DcaOrderFailed:
uid: bytes32
account: indexed(address)
reason: String[32]
event DcaCompleted:
uid: bytes32
account: indexed(address)
@external
@nonreentrant('lock')
def execute_dca_order(_uid: bytes32, _uni_hop_path: DynArray[address, 3], _uni_pool_fees: DynArray[uint24, 2], _share_profit: bool):
assert not self.is_paused, "paused"
order: DcaOrder = self.dca_orders[_uid]
# validate
assert order.number_of_executions < order.max_number_of_executions, "max executions completed"
assert order.last_execution + order.seconds_between_executions < block.timestamp, "too soon"
# ensure path is valid
assert len(_uni_hop_path) in [2, 3], "[path] invlid path"
assert len(_uni_pool_fees) == len(_uni_hop_path)-1, "[path] invalid fees"
assert _uni_hop_path[0] == order.token_in, "[path] invalid token_in"
assert _uni_hop_path[len(_uni_hop_path)-1] == order.token_out, "[path] invalid token_out"
# effects
order.last_execution = block.timestamp
order.number_of_executions += 1
self.dca_orders[_uid] = order
# ensure user has enough token_in
account_balance: uint256 = ERC20(order.token_in).balanceOf(order.account)
if account_balance < order.amount_in_per_execution:
log DcaOrderFailed(_uid, order.account, "insufficient balance")
self._cancel_dca_order(_uid, "insufficient balance")
return
# ensure self has enough allowance to spend amount token_in
account_allowance: uint256 = ERC20(order.token_in).allowance(order.account, self)
if account_allowance < order.amount_in_per_execution:
log DcaOrderFailed(_uid, order.account, "insufficient allowance")
self._cancel_dca_order(_uid, "insufficient allowance")
return
# transfer token_in from user to self
self._safe_transfer_from(order.token_in, order.account, self, order.amount_in_per_execution)
# approve UNISWAP_ROUTER to spend amount token_in
ERC20(order.token_in).approve(UNISWAP_ROUTER, order.amount_in_per_execution)
# Vyper way to accommodate abi.encode_packed
path: Bytes[66] = empty(Bytes[66])
if(len(_uni_hop_path) == 2):
path = concat(convert(_uni_hop_path[0], bytes20), convert(_uni_pool_fees[0], bytes3), convert(_uni_hop_path[1], bytes20))
elif(len(_uni_hop_path) == 3):
path = concat(convert(_uni_hop_path[0], bytes20), convert(_uni_pool_fees[0], bytes3), convert(_uni_hop_path[1], bytes20), convert(_uni_pool_fees[1], bytes3), convert(_uni_hop_path[2], bytes20))
min_amount_out: uint256 = self._calc_min_amount_out(order.amount_in_per_execution, _uni_hop_path, _uni_pool_fees, order.twap_length, order.max_slippage)
uni_params: ExactInputParams = ExactInputParams({
path: path,
recipient: self,
deadline: block.timestamp,
amountIn: order.amount_in_per_execution,
amountOutMinimum: min_amount_out
})
amount_out: uint256 = UniswapV3SwapRouter(UNISWAP_ROUTER).exactInput(uni_params)
# transfer amount_out - fee to user
amount_minus_fee: uint256 = amount_out * (FEE_BASE - self.fee) / FEE_BASE
self._safe_transfer(order.token_out, order.account, amount_minus_fee)
# allows searchers to execute for 50% of profits
if _share_profit:
profit: uint256 = amount_out - amount_minus_fee
self._safe_transfer(order.token_out, msg.sender, profit/2)
log DcaOrderExecuted(_uid, order.account, order.number_of_executions, order.amount_in_per_execution, amount_minus_fee)
if order.number_of_executions == order.max_number_of_executions:
self._cleanup_order(_uid)
log DcaCompleted(_uid, order.account)
@view
@external
def calc_min_amount_out(
_amount_in: uint256,
_path: DynArray[address, 3],
_fees: DynArray[uint24, 2],
_twap_length: uint32,
_max_slippage: uint256
) -> uint256:
return self._calc_min_amount_out(_amount_in, _path, _fees, _twap_length, _max_slippage)
@view
@internal
def _calc_min_amount_out(
_amount_in: uint256,
_path: DynArray[address, 3],
_fees: DynArray[uint24, 2],
_twap_length: uint32,
_max_slippage: uint256
) -> uint256:
uni_fees_total: uint256 = 0
for fee in _fees:
uni_fees_total += convert(fee, uint256)
token_in_decimals: uint256 = convert(ERC20Detailed(_path[0]).decimals(), uint256)
twap_value: uint256 = Univ3Twap(TWAP).getTwap(_path, _fees, _twap_length)
min_amount_out: uint256 = _amount_in * PRECISISON
min_amount_out = min_amount_out * twap_value
min_amount_out = (min_amount_out * (FEE_BASE - uni_fees_total - _max_slippage)) / FEE_BASE
min_amount_out = min_amount_out / 10**token_in_decimals
min_amount_out = min_amount_out / PRECISISON
return min_amount_out
event DcaOrderCanceled:
uid: bytes32
reason: String[32]
@external
def cancel_dca_order(_uid: bytes32):
order: DcaOrder = self.dca_orders[_uid]
assert order.account == msg.sender, "unauthorized"
self._cancel_dca_order(_uid, "user canceled")
@internal
def _cancel_dca_order(_uid: bytes32, _reason: String[32]):
self._cleanup_order(_uid)
log DcaOrderCanceled(_uid, _reason)
event OrderCleanedUp:
uid: bytes32
account: indexed(address)
@internal
def _cleanup_order(_uid: bytes32):
order: DcaOrder = self.dca_orders[_uid]
self.dca_orders[_uid] = empty(DcaOrder)
uids: DynArray[bytes32, 1024] = self.dca_order_uids[order.account]
for i in range(1024):
if uids[i] == _uid:
uids[i] = uids[len(uids) - 1]
uids.pop()
break
if i == len(uids)-1:
raise
self.dca_order_uids[order.account] = uids
log OrderCleanedUp(_uid, order.account)
@view
@external
def get_all_open_positions(_account: address) -> DynArray[DcaOrder, 1024]:
uids: DynArray[bytes32, 1024] = self.dca_order_uids[_account]
orders: DynArray[DcaOrder, 1024] = empty(DynArray[DcaOrder, 1024])
for uid in uids:
orders.append(self.dca_orders[uid])
return orders
@external
def withdraw_fees(_token: address):
amount: uint256 = ERC20(_token).balanceOf(self)
assert amount > 0, "zero balance"
ERC20(_token).transfer(self.owner, amount)
@internal
def _generate_uid() -> bytes32:
uid: bytes32 = keccak256(_abi_encode(chain.id, self.position_nonce, block.timestamp))
self.position_nonce += 1
return uid
@internal
def _safe_transfer(_token: address, _to: address, _amount: uint256) -> bool:
res: Bytes[32] = raw_call(
_token,
concat(
method_id("transfer(address,uint256)"),
convert(_to, bytes32),
convert(_amount, bytes32),
),
max_outsize=32,
)
if len(res) > 0:
assert convert(res, bool), "transfer failed"
return True
@internal
def _safe_transfer_from(
_token: address, _from: address, _to: address, _amount: uint256
):
res: Bytes[32] = raw_call(
_token,
concat(
method_id("transferFrom(address,address,uint256)"),
convert(_from, bytes32),
convert(_to, bytes32),
convert(_amount, bytes32),
),
max_outsize=32,
)
if len(res) > 0:
assert convert(res, bool), "transfer failed"
#############################
#
# ADMIN
#
#############################
event Paused:
is_paused: bool
@external
def pause(_is_paused: bool):
assert msg.sender == self.owner, "unauthorized"
assert _is_paused != self.is_paused, "already in state"
self.is_paused = _is_paused
log Paused(_is_paused)
event AcceptingNewOrders:
is_accepting_new_orders: bool
@external
def set_is_accepting_new_orders(_is_accepting_new_orders: bool):
assert msg.sender == self.owner, "unauthorized"
assert _is_accepting_new_orders != self.is_accepting_new_orders, "already in state"
self.is_accepting_new_orders = _is_accepting_new_orders
log AcceptingNewOrders(_is_accepting_new_orders)
event NewOwnerSuggested:
new_owner: indexed(address)
suggested_by: indexed(address)
@external
def suggest_owner(_new_owner: address):
"""
@notice
Step 1 of the 2 step process to transfer ownership.
Current owner suggests a new owner.
Requires the new owner to accept ownership in step 2.
@param _new_owner
The address of the new owner.
"""
assert msg.sender == self.owner, "unauthorized"
assert _new_owner != empty(address), "cannot set owner to zero address"
self.suggested_owner = _new_owner
log NewOwnerSuggested(_new_owner, msg.sender)
event OwnershipTransferred:
new_owner: indexed(address)
promoted_by: indexed(address)
@external
def accept_ownership():
"""
@notice
Step 2 of the 2 step process to transfer ownership.
The suggested owner accepts the transfer and becomes the
new owner.
"""
assert msg.sender == self.suggested_owner, "unauthorized"
prev_owner: address = self.owner
self.owner = self.suggested_owner
log OwnershipTransferred(self.owner, prev_owner)
event FeeUpdated:
new_fee: uint256
@external
def set_fee(_fee: uint256):
assert msg.sender == self.owner, "unauthorized"
assert _fee < FEE_BASE, "invalid fee"
assert _fee != self.fee, "new fee cannot be same as old fee"
self.fee = _fee
log FeeUpdated(_fee)