-
Notifications
You must be signed in to change notification settings - Fork 24
/
l0k_router.cairo
519 lines (417 loc) · 15.5 KB
/
l0k_router.cairo
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
from starkware.cairo.common.bool import TRUE, FALSE
from starkware.cairo.common.math_cmp import is_le_felt, is_not_zero
from starkware.cairo.common.bitwise import bitwise_or, bitwise_and
from starkware.cairo.common.uint256 import Uint256, uint256_le, uint256_lt
from starkware.cairo.common.math import assert_nn, assert_not_equal, assert_not_zero
from starkware.starknet.common.syscalls import (
deploy,
get_caller_address,
get_contract_address,
get_block_timestamp,
)
from starkware.cairo.common.hash import hash2
from openzeppelin.token.erc20.IERC20 import IERC20
from openzeppelin.security.safemath.library import SafeUint256
from interfaces.Il0kFactory import Il0kFactory
from interfaces.Il0kPair import Il0kPair
from libraries.l0k_library import l0kLibrary
#
# Storage
#
@storage_var
func _factory() -> (factory : felt):
end
@storage_var
func _pairClass() -> (pairClass : felt):
end
#
# Constructor
#
@constructor
func constructor{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
factory : felt, pairClass : felt
):
_factory.write(factory)
_pairClass.write(pairClass)
return ()
end
#
# Getters
#
@view
func factory{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> (
factory : felt
):
let (value) = _factory.read()
return (factory=value)
end
@view
func quote{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
amountA : Uint256, reserveA : felt, reserveB : felt
) -> (amountB : Uint256):
let (amountB : Uint256) = l0kLibrary.quote(amountA, reserveA, reserveB)
return (amountB=amountB)
end
@view
func getAmountOut{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
amountIn : Uint256, reserveIn : felt, reserveOut : felt
) -> (amountOut : Uint256):
let (amountOut : Uint256) = l0kLibrary.getAmountOut(amountIn, reserveIn, reserveOut)
return (amountOut=amountOut)
end
@view
func getAmountIn{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
amountOut : Uint256, reserveIn : felt, reserveOut : felt
) -> (amountIn : Uint256):
let (amountIn : Uint256) = l0kLibrary.getAmountIn(amountOut, reserveIn, reserveOut)
return (amountIn=amountIn)
end
@view
func getAmountsOut{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
amountIn : Uint256, path_len : felt, path : felt*
) -> (amounts_len : felt, amounts : Uint256*):
let (factory) = _factory.read()
let (pairClass) = _pairClass.read()
let (amounts_len, amounts : Uint256*) = l0kLibrary.getAmountsOut(
factory, pairClass, amountIn, path_len, path
)
return (amounts_len=amounts_len, amounts=amounts)
end
@view
func getAmountsIn{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
amountOut : Uint256, path_len : felt, path : felt*
) -> (amounts_len : felt, amounts : Uint256*):
let (factory) = _factory.read()
let (pairClass) = _pairClass.read()
let (amounts_len, amounts : Uint256*) = l0kLibrary.getAmountsIn(
factory, pairClass, amountOut, path_len, path
)
return (amounts_len=amounts_len, amounts=amounts)
end
#
# Externals
#
@external
func addLiquidity{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr, bitwise_ptr : BitwiseBuiltin*
}(
tokenA : felt,
tokenB : felt,
amountADesired : Uint256,
amountBDesired : Uint256,
amountAMin : Uint256,
amountBMin : Uint256,
to : felt,
deadline : felt,
) -> (amountA : Uint256, amountB : Uint256, liquidity : Uint256):
alloc_locals
_ensure(deadline)
let (factory) = _factory.read()
let (pairClass) = _pairClass.read()
let (caller) = get_caller_address()
let (amountA : Uint256, amountB : Uint256) = _addLiquidity(
tokenA, tokenB, amountADesired, amountBDesired, amountAMin, amountBMin
)
let (pair) = l0kLibrary.pairFor(factory, pairClass, tokenA, tokenB)
# TODO. Not implemented safeTransferFrom
IERC20.transferFrom(contract_address=tokenA, sender=caller, recipient=pair, amount=amountA)
IERC20.transferFrom(contract_address=tokenB, sender=caller, recipient=pair, amount=amountB)
let (liquidity : Uint256) = Il0kPair.mint(contract_address=pair, to=to)
return (amountA=amountA, amountB=amountB, liquidity=liquidity)
end
@external
func removeLiquidity{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
tokenA : felt,
tokenB : felt,
liquidity : Uint256,
amountAMin : Uint256,
amountBMin : Uint256,
to : felt,
deadline : felt,
) -> (amountA : Uint256, amountB : Uint256):
alloc_locals
_ensure(deadline)
let (amountA : Uint256, amountB : Uint256) = _removeLiquidity(tokenA, tokenB, liquidity, to)
# Insufficient_a_amount
with_attr error_message("10kSwapRouter: IAA"):
let (lt0) = uint256_lt(amountA, amountAMin)
assert lt0 = FALSE
end
# Insufficient_b_amount
with_attr error_message("10kSwapRouter: IBA"):
let (lt0) = uint256_lt(amountB, amountBMin)
assert lt0 = FALSE
end
return (amountA=amountA, amountB=amountB)
end
@external
func swapExactTokensForTokens{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
amountIn : Uint256,
amountOutMin : Uint256,
path_len : felt,
path : felt*,
to : felt,
deadline : felt,
) -> (amounts_len : felt, amounts : Uint256*):
alloc_locals
_ensure(deadline)
let (factory) = _factory.read()
let (pairClass) = _pairClass.read()
let (caller) = get_caller_address()
let (amounts_len, amounts : Uint256*) = l0kLibrary.getAmountsOut(
factory, pairClass, amountIn, path_len, path
)
# Insufficient output amount
with_attr error_message("10kSwapRouter: IOA"):
# amounts[amounts_len - 1] >= amountOutMin
let (le) = uint256_le(amountOutMin, amounts[amounts_len - 1])
assert le = TRUE
end
let (pair) = l0kLibrary.pairFor(factory, pairClass, path[0], path[1])
# TODO. Not implemented safeTransferFrom
IERC20.transferFrom(contract_address=path[0], sender=caller, recipient=pair, amount=amounts[0])
_swap(amounts_len, amounts, path_len, path, to)
return (amounts_len=amounts_len, amounts=amounts)
end
@external
func swapTokensForExactTokens{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
amountOut : Uint256,
amountInMax : Uint256,
path_len : felt,
path : felt*,
to : felt,
deadline : felt,
) -> (amounts_len : felt, amounts : Uint256*):
alloc_locals
_ensure(deadline)
let (factory) = _factory.read()
let (pairClass) = _pairClass.read()
let (caller) = get_caller_address()
let (amounts_len, amounts : Uint256*) = l0kLibrary.getAmountsIn(
factory, pairClass, amountOut, path_len, path
)
# Excessive input amount
with_attr error_message("10kSwapRouter: EIA"):
# amounts[0] <= amountInMax
let (le) = uint256_le(amounts[0], amountInMax)
assert le = TRUE
end
let (pair) = l0kLibrary.pairFor(factory, pairClass, path[0], path[1])
# TODO. Not implemented safeTransferFrom
IERC20.transferFrom(contract_address=path[0], sender=caller, recipient=pair, amount=amounts[0])
_swap(amounts_len, amounts, path_len, path, to)
return (amounts_len=amounts_len, amounts=amounts)
end
@external
func swapExactTokensForTokensSupportingFeeOnTransferTokens{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr
}(
amountIn : Uint256,
amountOutMin : Uint256,
path_len : felt,
path : felt*,
to : felt,
deadline : felt,
):
alloc_locals
_ensure(deadline)
let (factory) = _factory.read()
let (pairClass) = _pairClass.read()
let (caller) = get_caller_address()
let (pair) = l0kLibrary.pairFor(factory, pairClass, path[0], path[1])
# TODO. Not implemented safeTransferFrom
IERC20.transferFrom(contract_address=path[0], sender=caller, recipient=pair, amount=amountIn)
let (balanceBefore : Uint256) = IERC20.balanceOf(
contract_address=path[path_len - 1], account=to
)
_swapSupportingFeeOnTransferTokens(path_len, path, to)
# Insufficient output amount
with_attr error_message("10kSwapRouter: IOA"):
let (balanceAfter : Uint256) = IERC20.balanceOf(
contract_address=path[path_len - 1], account=to
)
let (balanceDiff) = SafeUint256.sub_le(balanceAfter, balanceBefore)
let (r_le) = uint256_le(amountOutMin, balanceDiff)
assert r_le = TRUE
end
return ()
end
#
# Internal
#
func _ensure{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(deadline : felt):
alloc_locals
# Expired
with_attr error_message("10kSwapRouter: E"):
let (block_timestamp) = get_block_timestamp()
let (le) = is_le_felt(block_timestamp, deadline)
assert le = TRUE
end
return ()
end
func _addLiquidity{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr, bitwise_ptr : BitwiseBuiltin*
}(
tokenA : felt,
tokenB : felt,
amountADesired : Uint256,
amountBDesired : Uint256,
amountAMin : Uint256,
amountBMin : Uint256,
) -> (amountA : Uint256, amountB : Uint256):
alloc_locals
let (factory) = _factory.read()
let (pairClass) = _pairClass.read()
let (pair) = Il0kFactory.getPair(contract_address=factory, tokenA=tokenA, tokenB=tokenB)
if pair == 0:
Il0kFactory.createPair(contract_address=factory, tokenA=tokenA, tokenB=tokenB)
end
let (reserveA, reserveB) = l0kLibrary.getReserves(factory, pairClass, tokenA, tokenB)
let (rA_neq0) = is_not_zero(reserveA)
let (rB_neq0) = is_not_zero(reserveB)
let (b0) = bitwise_and(1 - rA_neq0, 1 - rB_neq0)
if b0 == TRUE:
return (amountA=amountADesired, amountB=amountBDesired)
else:
let (amountBOptimal) = l0kLibrary.quote(amountADesired, reserveA, reserveB)
let (le1) = uint256_le(amountBOptimal, amountBDesired)
if le1 == TRUE:
# Insufficient_b_amount
with_attr error_message("10kSwapRouter: IBA"):
let (lt2) = uint256_lt(amountBOptimal, amountBMin)
assert lt2 = FALSE
end
return (amountA=amountADesired, amountB=amountBOptimal)
else:
let (amountAOptimal) = l0kLibrary.quote(amountBDesired, reserveB, reserveA)
let (le2) = uint256_le(amountAOptimal, amountADesired)
assert le2 = TRUE
# Insufficient_a_amount
with_attr error_message("10kSwapRouter: IAA"):
let (lt2) = uint256_lt(amountAOptimal, amountAMin)
assert lt2 = FALSE
end
return (amountA=amountAOptimal, amountB=amountBDesired)
end
end
end
func _removeLiquidity{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
tokenA : felt, tokenB : felt, liquidity : Uint256, to : felt
) -> (amountA : Uint256, amountB : Uint256):
alloc_locals
let (factory) = _factory.read()
let (pairClass) = _pairClass.read()
let (caller) = get_caller_address()
let (pair) = l0kLibrary.pairFor(factory, pairClass, tokenA, tokenB)
# send liquidity to pair
Il0kPair.transferFrom(contract_address=pair, sender=caller, recipient=pair, amount=liquidity)
let (amount0 : Uint256, amount1 : Uint256) = Il0kPair.burn(contract_address=pair, to=to)
let (token0, _) = l0kLibrary.sortTokens(tokenA, tokenB)
if tokenA == token0:
return (amountA=amount0, amountB=amount1)
else:
return (amountA=amount1, amountB=amount0)
end
end
# requires the initial amount to have already been sent to the first pair
func _swap{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
amounts_len : felt, amounts : Uint256*, path_len : felt, path : felt*, _to : felt
):
alloc_locals
let (factory) = _factory.read()
let (pairClass) = _pairClass.read()
# *** Start loop ***
# Equal to: for (uint i; i < path_len - 1; i++)
tempvar next = 1
tempvar i = 0
loop:
tempvar input = path[i]
tempvar output = path[i + 1]
let (token0, _) = l0kLibrary.sortTokens(input, output)
tempvar amountOut = amounts[i + 1]
let (amount0Out : Uint256, amount1Out : Uint256) = _swap_amountOuts(input, token0, amountOut)
let (to) = _swap_to(factory, pairClass, i, path_len, path, output, _to)
let (pair) = l0kLibrary.pairFor(factory, pairClass, input, output)
Il0kPair.swap(contract_address=pair, amount0Out=amount0Out, amount1Out=amount1Out, to=to)
tempvar i = i + 1
tempvar next = path_len - 1 - i
jmp loop if next != 0
# *** End loop ***
return ()
end
func _swap_amountOuts{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
input : felt, token0 : felt, amountOut : Uint256
) -> (amount0Out : Uint256, amount1Out : Uint256):
if input == token0:
return (amount0Out=Uint256(0, 0), amount1Out=amountOut)
else:
return (amount0Out=amountOut, amount1Out=Uint256(0, 0))
end
end
func _swap_to{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
factory : felt,
pairClass : felt,
i : felt,
path_len : felt,
path : felt*,
output : felt,
_to : felt,
) -> (to : felt):
alloc_locals
let (r_le) = is_le_felt(path_len - 2, i)
# If i < path.length - 2
if r_le == FALSE:
let (pair) = l0kLibrary.pairFor(factory, pairClass, output, path[i + 2])
return (to=pair)
else:
return (to=_to)
end
end
# **** SWAP (supporting fee-on-transfer tokens) ****
# requires the initial amount to have already been sent to the first pair
# There is a vulnerability. danger level: low. https://www.btcfans.com/article/63438
func _swapSupportingFeeOnTransferTokens{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr
}(path_len : felt, path : felt*, _to : felt):
alloc_locals
let (factory) = _factory.read()
let (pairClass) = _pairClass.read()
# *** Start loop ***
# Equal to: for (uint i; i < path.length - 1; i++)
tempvar next = 1
tempvar i = 0
loop:
tempvar input = path[i]
tempvar output = path[i + 1]
let (token0, _) = l0kLibrary.sortTokens(input, output)
let (pair) = l0kLibrary.pairFor(factory, pairClass, input, output)
let (reserve0, reserve1, _) = Il0kPair.getReserves(contract_address=pair)
let (reserveInput, reserveOutput) = _swapSupportingFeeOnTransferTokens_reserves(
input, token0, reserve0, reserve1
)
let (balanceInput) = IERC20.balanceOf(contract_address=input, account=pair)
let (amountInput) = SafeUint256.sub_le(balanceInput, Uint256(reserveInput, 0))
let (amountOutput) = l0kLibrary.getAmountOut(amountInput, reserveInput, reserveOutput)
let (amount0Out : Uint256, amount1Out : Uint256) = _swap_amountOuts(input, token0, amountOutput)
let (to) = _swap_to(factory, pairClass, i, path_len, path, output, _to)
let (pair) = l0kLibrary.pairFor(factory, pairClass, input, output)
Il0kPair.swap(contract_address=pair, amount0Out=amount0Out, amount1Out=amount1Out, to=to)
tempvar i = i + 1
tempvar next = path_len - 1 - i
jmp loop if next != 0
# *** End loop ***
return ()
end
func _swapSupportingFeeOnTransferTokens_reserves{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr
}(input : felt, token0 : felt, reserve0 : felt, reserve1 : felt) -> (
reserveInput : felt, reserveOutput : felt
):
if input == token0:
return (reserveInput=reserve0, reserveOutput=reserve1)
else:
return (reserveInput=reserve1, reserveOutput=reserve0)
end
end