forked from BibliothecaDAO/realms-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExchange_ERC20_1155.cairo
1067 lines (882 loc) · 32.2 KB
/
Exchange_ERC20_1155.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.alloc import alloc
from starkware.starknet.common.syscalls import (
get_caller_address,
get_contract_address,
get_block_timestamp,
)
from starkware.cairo.common.math import assert_nn, assert_le, assert_not_zero
from starkware.cairo.common.math_cmp import is_not_zero
from starkware.cairo.common.uint256 import (
Uint256,
uint256_add,
uint256_sub,
uint256_mul,
uint256_unsigned_div_rem,
uint256_le,
uint256_lt,
uint256_eq,
)
from openzeppelin.token.erc20.interfaces.IERC20 import IERC20
from contracts.settling_game.interfaces.IERC1155 import IERC1155
from openzeppelin.token.erc1155.library import (
ERC1155_initializer,
ERC1155_supportsInterface,
ERC1155_uri,
ERC1155_balanceOf,
ERC1155_balanceOfBatch,
ERC1155_isApprovedForAll,
ERC1155_setApprovalForAll,
ERC1155_safeTransferFrom,
ERC1155_safeBatchTransferFrom,
ERC1155_mint,
ERC1155_burn,
IERC1155_RECEIVER_ID,
ON_ERC1155_RECEIVED_SELECTOR,
)
from openzeppelin.upgrades.library import (
Proxy_initializer,
Proxy_only_admin,
Proxy_set_implementation,
)
from contracts.exchange.library import AMM
@event
func LiquidityAdded(
caller : felt, currency_amount : Uint256, token_id : Uint256, token_amount : Uint256
):
end
@event
func LiquidityRemoved(
caller : felt, currency_amount : Uint256, token_id : Uint256, token_amount : Uint256
):
end
@event
func TokensPurchased(
caller : felt, currency_sold : Uint256, token_id : Uint256, tokens_bought : Uint256
):
end
@event
func CurrencyPurchased(
caller : felt, currency_bought : Uint256, token_id : Uint256, tokens_sold : Uint256
):
end
# Contract Address of ERC20 address for this swap contract
@storage_var
func currency_address() -> (address : felt):
end
# Contract Address of ERC1155 address for this swap contract
@storage_var
func token_address() -> (address : felt):
end
# Current reserves of currency
@storage_var
func currency_reserves(token_id : Uint256) -> (reserves : Uint256):
end
# Total issued LP totals
@storage_var
func lp_reserves(token_id : Uint256) -> (total : Uint256):
end
# Liquidity pool rewards in thousandths (e.g. 15 = 1.5% fee)
@storage_var
func lp_fee_thousands() -> (lp_fee_thousands : Uint256):
end
# Note: We use ERC1155_balanceOf(contract_address) to record token reserves
# Global royalty fee in thousandths (e.g. 15 = 1.5% fee)
@storage_var
func royalty_fee_thousands() -> (royalty_fee_thousands : Uint256):
end
# Global royalty fee addr
@storage_var
func royalty_fee_address() -> (royalty_fee_address : felt):
end
###############
# CONSTRUCTOR #
###############
@external
func initializer{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
currency_address_ : felt,
token_address_ : felt,
lp_fee_thousands_ : Uint256,
royalty_fee_thousands_ : Uint256,
royalty_fee_address_ : felt,
proxy_admin : felt,
):
currency_address.write(currency_address_)
token_address.write(token_address_)
lp_fee_thousands.write(lp_fee_thousands_)
set_royalty_info(royalty_fee_thousands_, royalty_fee_address_)
Proxy_initializer(proxy_admin)
return ()
end
@external
func upgrade{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
new_implementation : felt
):
Proxy_only_admin()
Proxy_set_implementation(new_implementation)
return ()
end
######
# LP #
######
# input arguments are 3 arrays of:
# 1) amount of currency supplied to LP
# 2) ERC1155 token IDs
# 3) amount of tokens supplied
@external
func initial_liquidity{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
currency_amounts_len : felt,
currency_amounts : Uint256*,
token_ids_len : felt,
token_ids : Uint256*,
token_amounts_len : felt,
token_amounts : Uint256*,
):
alloc_locals
# Recursive break
if currency_amounts_len == 0:
return ()
end
assert currency_amounts_len = token_ids_len
assert currency_amounts_len = token_amounts_len
let (caller) = get_caller_address()
let (contract) = get_contract_address()
let (token_address_) = token_address.read()
let (currency_address_) = currency_address.read()
# Only valid for first liquidity add to LP
let (currency_reserves_ : Uint256) = currency_reserves.read([token_ids])
with_attr error_message("Only valid for initial liquidity add"):
assert currency_reserves_ = Uint256(0, 0)
end
# Transfer currency and token to exchange
IERC20.transferFrom(currency_address_, caller, contract, [currency_amounts])
tempvar syscall_ptr : felt* = syscall_ptr
IERC1155.safeTransferFrom(token_address_, caller, contract, [token_ids], [token_amounts])
# Assert otherwise rounding error could end up being significant on second deposit
let (ok) = uint256_le(Uint256(1000, 0), [currency_amounts])
with_attr error_message("Must supply larger currency for initial deposit"):
assert_not_zero(ok)
end
# Update currency reserve size for token id before transfer
currency_reserves.write([token_ids], [currency_amounts])
# Initial liquidity is currency amount deposited
lp_reserves.write([token_ids], [currency_amounts])
# Mint LP tokens
ERC1155_mint(caller, [token_ids], [currency_amounts])
# Emit event
LiquidityAdded.emit(caller, [currency_amounts], [token_ids], [token_amounts])
# Recurse
return initial_liquidity(
currency_amounts_len - 1,
currency_amounts + Uint256.SIZE,
token_ids_len - 1,
token_ids + Uint256.SIZE,
token_amounts_len - 1,
token_amounts + Uint256.SIZE,
)
end
##########
# ADD LP #
##########
# input arguments are:
# 1) array of maximum amount of currency supplied to LP
# 2) array of ERC1155 token IDs
# 3) array of fixed amount of tokens supplied
# 4) the maximum time which this transaction can be accepted
@external
func add_liquidity{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
max_currency_amounts_len : felt,
max_currency_amounts : Uint256*,
token_ids_len : felt,
token_ids : Uint256*,
token_amounts_len : felt,
token_amounts : Uint256*,
deadline : felt,
):
alloc_locals
assert max_currency_amounts_len = token_ids_len
assert max_currency_amounts_len = token_amounts_len
# Check deadline within bounds
let (block_timestamp) = get_block_timestamp()
with_attr error_message("Deadline exceeded"):
assert_le(block_timestamp, deadline)
end
return add_liquidity_loop(
max_currency_amounts_len,
max_currency_amounts,
token_ids_len,
token_ids,
token_amounts_len,
token_amounts,
)
end
# input arguments are:
# 1) array of maximum amount of currency supplied to LP
# 2) array of ERC1155 token IDs
# 3) array of fixed amount of token supplied
func add_liquidity_loop{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
max_currency_amounts_len : felt,
max_currency_amounts : Uint256*,
token_ids_len : felt,
token_ids : Uint256*,
token_amounts_len : felt,
token_amounts : Uint256*,
):
alloc_locals
# Recursive break
if max_currency_amounts_len == 0:
return ()
end
let (caller) = get_caller_address()
let (contract) = get_contract_address()
let (token_address_) = token_address.read()
let (currency_address_) = currency_address.read()
# Read current reserve levels
let (lp_reserves_ : Uint256) = lp_reserves.read([token_ids])
let (token_reserves : Uint256) = IERC1155.balanceOf(token_address_, contract, [token_ids])
let (currency_reserves_ : Uint256) = currency_reserves.read([token_ids])
# Ensure this method is only called for subsequent liquidity adds
let (above_zero) = uint256_lt(Uint256(0, 0), currency_reserves_)
with_attr error_message("This method is only for subsequent liquidity additions"):
assert_not_zero(above_zero)
end
# Required price calc
# X/Y = dx/dy
# dx = X*dy/Y
let (numerator, mul_overflow) = uint256_mul(currency_reserves_, [token_amounts])
with_attr error_message("Values too large"):
assert mul_overflow = Uint256(0, 0)
end
let (currency_amount, _) = uint256_unsigned_div_rem(numerator, token_reserves)
# Ignore remainder as this favours existing LP holders
# Check within bounds of the maximum allowed currency spend
let (ok) = uint256_le(currency_amount, [max_currency_amounts])
with_attr error_message("Price exceeds max currency amount"):
assert_not_zero(ok)
end
# Transfer tokens to exchange contract
IERC20.transferFrom(currency_address_, caller, contract, currency_amount)
tempvar syscall_ptr : felt* = syscall_ptr
IERC1155.safeTransferFrom(token_address_, caller, contract, [token_ids], [token_amounts])
# Update the new currency and LP reserves
let (new_reserves, add_overflow) = uint256_add(currency_reserves_, currency_amount)
with_attr error_message("Currency value overflow"):
assert (add_overflow) = 0
end
currency_reserves.write([token_ids], new_reserves)
let (new_supplies, add_overflow) = uint256_add(lp_reserves_, currency_amount)
with_attr error_message("LP value overflow"):
assert (add_overflow) = 0
end
lp_reserves.write([token_ids], new_supplies)
# Mint LP tokens
ERC1155_mint(caller, [token_ids], currency_amount)
# Emit event
LiquidityAdded.emit(caller, currency_amount, [token_ids], [token_amounts])
return add_liquidity_loop(
max_currency_amounts_len - 1,
max_currency_amounts + Uint256.SIZE,
token_ids_len - 1,
token_ids + Uint256.SIZE,
token_amounts_len - 1,
token_amounts + Uint256.SIZE,
)
end
#############
# REMOVE LP #
#############
# input arguments are
# 1) array of minimum amount of currency received from LP
# 2) array of ERC1155 token IDs
# 3) array of minimum amount of tokens received from LP
# 4) array of exact amount of LP tokens to spend
# 5) maximum time which this transaction can be accepted
@external
func remove_liquidity{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
min_currency_amounts_len : felt,
min_currency_amounts : Uint256*,
token_ids_len : felt,
token_ids : Uint256*,
min_token_amounts_len : felt,
min_token_amounts : Uint256*,
lp_amounts_len : felt,
lp_amounts : Uint256*,
deadline : felt,
):
alloc_locals
assert min_currency_amounts_len = token_ids_len
assert min_currency_amounts_len = min_token_amounts_len
assert min_currency_amounts_len = lp_amounts_len
# Check deadline within bounds
let (block_timestamp) = get_block_timestamp()
with_attr error_message("Deadline exceeded"):
assert_le(block_timestamp, deadline)
end
return remove_liquidity_loop(
min_currency_amounts_len,
min_currency_amounts,
token_ids_len,
token_ids,
min_token_amounts_len,
min_token_amounts,
lp_amounts_len,
lp_amounts,
)
end
func remove_liquidity_loop{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
min_currency_amounts_len : felt,
min_currency_amounts : Uint256*,
token_ids_len : felt,
token_ids : Uint256*,
min_token_amounts_len : felt,
min_token_amounts : Uint256*,
lp_amounts_len : felt,
lp_amounts : Uint256*,
):
alloc_locals
# Recursive break
if min_currency_amounts_len == 0:
return ()
end
let (caller) = get_caller_address()
let (contract) = get_contract_address()
let (token_address_) = token_address.read()
let (currency_address_) = currency_address.read()
# Read current reserve levels
let (lp_reserves_ : Uint256) = lp_reserves.read([token_ids])
let (currency_reserves_ : Uint256) = currency_reserves.read([token_ids])
let (token_reserves : Uint256) = IERC1155.balanceOf(token_address_, contract, [token_ids])
let (new_supplies) = uint256_sub(lp_reserves_, [lp_amounts])
# It should not be possible to go below zero as LP reflects supply
let (above_zero) = uint256_le(Uint256(0, 0), new_supplies)
with_attr error_message("LP total exceeded"):
assert_not_zero(above_zero)
end
# Calculate percentage of reserves this LP amount is worth
let (numerator, mul_overflow) = uint256_mul(currency_reserves_, [lp_amounts])
with_attr error_message("Values too large"):
assert mul_overflow = Uint256(0, 0)
end
let (currency_owed, _) = uint256_unsigned_div_rem(numerator, lp_reserves_)
let (numerator, mul_overflow) = uint256_mul(token_reserves, [lp_amounts])
with_attr error_message("Values too large"):
assert mul_overflow = Uint256(0, 0)
end
# Ignore remainder as it favours LP holders
let (tokens_owed, _) = uint256_unsigned_div_rem(numerator, lp_reserves_)
# Check actual values to receive are above minimum values requested
let (above_min) = uint256_le([min_currency_amounts], currency_owed)
with_attr error_message("Minimum currency amount exceeded"):
assert_not_zero(above_min)
end
let (above_min) = uint256_le([min_token_amounts], tokens_owed)
with_attr error_message("Minimum token amount exceeded"):
assert_not_zero(above_min)
end
# New totals
let (new_currency) = uint256_sub(currency_reserves_, currency_owed)
# Update storage
lp_reserves.write([token_ids], new_supplies)
currency_reserves.write([token_ids], new_currency)
# Take LP tokens
ERC1155_burn(caller, [token_ids], [lp_amounts])
# Send currency and tokens
IERC20.transfer(currency_address_, caller, currency_owed)
tempvar syscall_ptr : felt* = syscall_ptr
IERC1155.safeTransferFrom(token_address_, contract, caller, [token_ids], tokens_owed)
# Emit event
LiquidityRemoved.emit(caller, currency_owed, [token_ids], tokens_owed)
return remove_liquidity_loop(
min_currency_amounts_len - 1,
min_currency_amounts + Uint256.SIZE,
token_ids_len - 1,
token_ids + Uint256.SIZE,
min_token_amounts_len - 1,
min_token_amounts + Uint256.SIZE,
lp_amounts_len - 1,
lp_amounts + Uint256.SIZE,
)
end
##############
# BUY TOKENS #
##############
# input arguments are:
# 1) maximum amount of currency to sell
# 2) array of ERC1155 token IDs
# 3) array of exact amount of tokens to buy
# 4) maximum time which this transaction can be accepted
@external
func buy_tokens{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
max_currency_amount : Uint256,
token_ids_len : felt,
token_ids : Uint256*,
token_amounts_len : felt,
token_amounts : Uint256*,
deadline : felt,
) -> (sold : Uint256):
alloc_locals
assert token_ids_len = token_amounts_len
# Check deadline within bounds
let (block_timestamp) = get_block_timestamp()
with_attr error_message("Deadline exceeded"):
assert_le(block_timestamp, deadline)
end
# Loop
let (currency_amount) = buy_tokens_loop(
token_ids_len, token_ids, token_amounts_len, token_amounts
)
let (above_max_curr) = uint256_le(currency_amount, max_currency_amount)
with_attr error_message("Maximum currency amount exceeded"):
assert_not_zero(above_max_curr)
end
return (currency_amount)
end
func buy_tokens_loop{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
token_ids_len : felt, token_ids : Uint256*, token_amounts_len : felt, token_amounts : Uint256*
) -> (sold : Uint256):
alloc_locals
# Recursive break
if token_ids_len == 0:
return (Uint256(0, 0))
end
let (caller) = get_caller_address()
let (contract) = get_contract_address()
let (token_address_) = token_address.read()
let (currency_address_) = currency_address.read()
let (royalty_fee_thousands_) = royalty_fee_thousands.read()
let (royalty_fee_address_) = royalty_fee_address.read()
# Read current reserve levels
let (currency_reserves_ : Uint256) = currency_reserves.read([token_ids])
let (token_reserves : Uint256) = IERC1155.balanceOf(token_address_, contract, [token_ids])
let (lp_fee_thousands_) = lp_fee_thousands.read()
# Calculate prices
let (currency_amount_sans_royal) = AMM.get_buy_price(
[token_amounts], currency_reserves_, token_reserves, lp_fee_thousands_
)
# Add royalty fee
let (royalty_fee) = get_royalty_for_price(currency_amount_sans_royal)
let (currency_amount, _) = uint256_add(currency_amount_sans_royal, royalty_fee) # Overflow will never happen here
# Update reserves
let (new_reserves, add_overflow) = uint256_add(currency_reserves_, currency_amount_sans_royal)
with_attr error_message("Currency value overflow"):
assert add_overflow = 0
end
currency_reserves.write([token_ids], new_reserves)
# Transfer currency and purchased tokens
IERC20.transferFrom(currency_address_, caller, contract, currency_amount)
tempvar syscall_ptr : felt* = syscall_ptr
IERC1155.safeTransferFrom(token_address_, contract, caller, [token_ids], [token_amounts])
IERC20.transfer(currency_address_, royalty_fee_address_, royalty_fee) # Royalty
# Emit event
TokensPurchased.emit(caller, currency_amount, [token_ids], [token_amounts])
# Recurse
let (currency_total) = buy_tokens_loop(
token_ids_len - 1,
token_ids + Uint256.SIZE,
token_amounts_len - 1,
token_amounts + Uint256.SIZE,
)
let (currency_sold, add_overflow) = uint256_add(currency_total, currency_amount)
with_attr error_message("Total currency overflow"):
assert add_overflow = 0
end
return (currency_sold)
end
###############
# SELL TOKENS #
###############
# input arguments are
# 1) maximum amount of currency to buy
# 2) array of ERC1155 token IDs
# 3) array of exact amount of token to sell
# 4) maximum time which this transaction can be accepted
# FIXME IERC1155 doesn't have a call back when using ERC1155.safeTransfer.
# User will need to `setApprovalForAll` to this contract, which poses a security risk for repeat transactions.
@external
func sell_tokens{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
min_currency_amount : Uint256,
token_ids_len : felt,
token_ids : Uint256*,
token_amounts_len : felt,
token_amounts : Uint256*,
deadline : felt,
) -> (sold : Uint256):
alloc_locals
assert token_ids_len = token_amounts_len
# Check deadline within bounds
let (block_timestamp) = get_block_timestamp()
with_attr error_message("Deadline exceeded"):
assert_le(block_timestamp, deadline)
end
# Loop
let (currency_amount) = sell_tokens_loop(
token_ids_len, token_ids, token_amounts_len, token_amounts
)
# Check min_currency_amount
let (above_min) = uint256_le(min_currency_amount, currency_amount)
with_attr error_message("Below minimum currency amount"):
assert_not_zero(above_min)
end
return (currency_amount)
end
func sell_tokens_loop{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
token_ids_len : felt, token_ids : Uint256*, token_amounts_len : felt, token_amounts : Uint256*
) -> (sold : Uint256):
alloc_locals
# Recursive break
if token_ids_len == 0:
return (Uint256(0, 0))
end
let (caller) = get_caller_address()
let (contract) = get_contract_address()
let (token_address_) = token_address.read()
let (currency_address_) = currency_address.read()
let (royalty_fee_thousands_) = royalty_fee_thousands.read()
let (royalty_fee_address_) = royalty_fee_address.read()
# Read current reserve levels
let (currency_reserves_ : Uint256) = currency_reserves.read([token_ids])
let (token_reserves : Uint256) = IERC1155.balanceOf(token_address_, contract, [token_ids])
# Take the token amount
IERC1155.safeTransferFrom(token_address_, caller, contract, [token_ids], [token_amounts])
let (lp_fee_thousands_) = lp_fee_thousands.read()
# Calculate prices
let (currency_amount_sans_royal) = AMM.get_sell_price(
[token_amounts], currency_reserves_, token_reserves, lp_fee_thousands_
)
# Add royalty fee
let (royalty_fee) = get_royalty_for_price(currency_amount_sans_royal)
let (currency_amount) = uint256_sub(currency_amount_sans_royal, royalty_fee)
# Transfer currency
IERC20.transfer(currency_address_, caller, currency_amount)
tempvar syscall_ptr : felt* = syscall_ptr
IERC20.transfer(currency_address_, royalty_fee_address_, royalty_fee) # Royalty
# Update reserves
let (new_reserves) = uint256_sub(currency_reserves_, currency_amount_sans_royal)
currency_reserves.write([token_ids], new_reserves)
# Emit event
CurrencyPurchased.emit(caller, currency_amount, [token_ids], [token_amounts])
# Recurse
let (currency_total) = sell_tokens_loop(
token_ids_len - 1,
token_ids + Uint256.SIZE,
token_amounts_len - 1,
token_amounts + Uint256.SIZE,
)
let (currency_owed, add_overflow) = uint256_add(currency_total, currency_amount)
with_attr error_message("Total currency overflow"):
assert add_overflow = 0
end
return (currency_owed)
end
#################
# PRICING CALCS #
#################
# input argument:
# 1) price without royalty amount
@view
func get_royalty_for_price{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
price_sans_royalty : Uint256
) -> (price : Uint256):
alloc_locals
let (royalty_fee_thousands_) = royalty_fee_thousands.read()
# Add royalty fee
let (currency_mul_royal_thou, mul_overflow) = uint256_mul(
price_sans_royalty, royalty_fee_thousands_
)
with_attr error_message("Royalty too large"):
assert mul_overflow = Uint256(0, 0)
end
let (royalty_fee, _) = uint256_unsigned_div_rem(currency_mul_royal_thou, Uint256(1000, 0)) # Ignore remainder
return (royalty_fee)
end
# input arguments are:
# 1) exact amount of token to buy
# 2) currency reserve amount
# 3) token reserve amount
@view
func get_buy_price_with_royalty{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
token_amount : Uint256, currency_reserves : Uint256, token_reserves : Uint256
) -> (price : Uint256):
alloc_locals
let (lp_fee_thousands_) = lp_fee_thousands.read()
# Calculate prices
let (price_sans_royalty) = AMM.get_buy_price(
token_amount, currency_reserves, token_reserves, lp_fee_thousands_
)
let (royalty_fee) = get_royalty_for_price(price_sans_royalty)
let (price, _) = uint256_add(price_sans_royalty, royalty_fee) # Will never overflow
return (price)
end
# input arguments are:
# 1) exact amount of token to sell
# 2) currency reserve amount
# 3) token reserve amount
@view
func get_sell_price_with_royalty{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
token_amount : Uint256, currency_reserves : Uint256, token_reserves : Uint256
) -> (price : Uint256):
alloc_locals
let (lp_fee_thousands_) = lp_fee_thousands.read()
# Calculate prices
let (price_sans_royalty) = AMM.get_sell_price(
token_amount, currency_reserves, token_reserves, lp_fee_thousands_
)
let (royalty_fee) = get_royalty_for_price(price_sans_royalty)
let (price) = uint256_sub(price_sans_royalty, royalty_fee)
return (price)
end
#############
# RECEIVERS #
#############
@external
func onERC1155Received{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
operator : felt, _from : felt, id : Uint256, value : Uint256
) -> (selector : felt):
return (ON_ERC1155_RECEIVED_SELECTOR)
end
###########
# Getters #
###########
@view
func get_currency_address{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> (
currency_address : felt
):
return currency_address.read()
end
@view
func get_token_address{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> (
token_address : felt
):
return token_address.read()
end
@view
func get_currency_reserves{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
token_id : Uint256
) -> (currency_reserves : Uint256):
return currency_reserves.read(token_id)
end
@view
func get_lp_fee_thousands{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> (
lp_fee_thousands : Uint256
):
return lp_fee_thousands.read()
end
@view
func get_all_sell_price{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
token_ids_len : felt, token_ids : Uint256*, token_amounts_len : felt, token_amounts : Uint256*
) -> (sell_value_len : felt, sell_value : Uint256*):
alloc_locals
# Loop
let (local prices : Uint256*) = alloc()
let (sell_prices : Uint256*) = get_all_sell_price_loop(
token_ids_len, token_ids, token_amounts_len, token_amounts, token_amounts_len, prices
)
return (token_amounts_len, prices)
end
func get_all_sell_price_loop{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
token_ids_len : felt,
token_ids : Uint256*,
token_amounts_len : felt,
token_amounts : Uint256*,
prices_len : felt,
prices : Uint256*,
) -> (total_token_value : Uint256*):
alloc_locals
# Recursive break
if token_ids_len == 0:
return (token_amounts)
end
let (contract) = get_contract_address()
let (token_address_) = token_address.read()
let (currency_address_) = currency_address.read()
let (royalty_fee_thousands_) = royalty_fee_thousands.read()
let (royalty_fee_address_) = royalty_fee_address.read()
# Read current reserve levels
let (currency_reserves_ : Uint256) = currency_reserves.read([token_ids])
let (token_reserves : Uint256) = IERC1155.balanceOf(token_address_, contract, [token_ids])
# FOR TESTS
# let currency_reserves_ = Uint256(10000, 0)
# let token_reserves = Uint256(1000, 0)
# Calculate prices
let (currency_amount) = get_sell_price_with_royalty(
[token_amounts], currency_reserves_, token_reserves
)
prices.high = currency_amount.high
prices.low = currency_amount.low
return get_all_sell_price_loop(
token_ids_len - 1,
token_ids + Uint256.SIZE,
token_amounts_len - 1,
token_amounts + Uint256.SIZE,
prices_len - 1,
prices + Uint256.SIZE,
)
end
@view
func get_all_buy_price{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
token_ids_len : felt, token_ids : Uint256*, token_amounts_len : felt, token_amounts : Uint256*
) -> (prices_len : felt, prices : Uint256*):
alloc_locals
# Loop
let (local prices : Uint256*) = alloc()
let (sell_prices : Uint256*) = get_all_buy_price_loop(
token_ids_len, token_ids, token_amounts_len, token_amounts, token_amounts_len, prices
)
return (token_amounts_len, prices)
end
func get_all_buy_price_loop{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
token_ids_len : felt,
token_ids : Uint256*,
token_amounts_len : felt,
token_amounts : Uint256*,
prices_len : felt,
prices : Uint256*,
) -> (total_token_value : Uint256*):
alloc_locals
# Recursive break
if token_ids_len == 0:
return (token_amounts)
end
let (contract) = get_contract_address()
let (token_address_) = token_address.read()
let (currency_address_) = currency_address.read()
let (royalty_fee_thousands_) = royalty_fee_thousands.read()
let (royalty_fee_address_) = royalty_fee_address.read()
# # Read current reserve levels
let (currency_reserves_ : Uint256) = currency_reserves.read([token_ids])
let (token_reserves : Uint256) = IERC1155.balanceOf(token_address_, contract, [token_ids])
# FOR TESTS
# let currency_reserves_ = Uint256(10000, 0)
# let token_reserves = Uint256(1000, 0)
# Calculate prices
let (currency_amount) = get_buy_price_with_royalty(
[token_amounts], currency_reserves_, token_reserves
)
prices.high = currency_amount.high
prices.low = currency_amount.low
return get_all_buy_price_loop(
token_ids_len - 1,
token_ids + Uint256.SIZE,
token_amounts_len - 1,
token_amounts + Uint256.SIZE,
prices_len - 1,
prices + Uint256.SIZE,
)
end
@view
func get_all_rates{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
token_ids_len : felt, token_ids : Uint256*, token_amounts_len : felt, token_amounts : Uint256*
) -> (prices_len : felt, prices : Uint256*):
alloc_locals
# Loop
let (local prices : Uint256*) = alloc()
let (sell_prices : Uint256*) = get_all_rates_loop(
token_ids_len, token_ids, token_amounts_len, token_amounts, token_amounts_len, prices
)
return (token_amounts_len, prices)
end
func get_all_rates_loop{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
token_ids_len : felt,
token_ids : Uint256*,
token_amounts_len : felt,
token_amounts : Uint256*,
prices_len : felt,
prices : Uint256*,
) -> (total_token_value : Uint256*):
alloc_locals
# Recursive break
if token_ids_len == 0:
return (token_amounts)
end
let (contract) = get_contract_address()
let (token_address_) = token_address.read()
let (currency_address_) = currency_address.read()
let (royalty_fee_thousands_) = royalty_fee_thousands.read()
let (royalty_fee_address_) = royalty_fee_address.read()
# # Read current reserve levels
let (currency_reserves_ : Uint256) = currency_reserves.read([token_ids])
let (token_reserves : Uint256) = IERC1155.balanceOf(token_address_, contract, [token_ids])
# FOR TESTS
# let currency_reserves_ = Uint256(10000, 0)
# let token_reserves = Uint256(1000, 0)
# Calculate prices | NO FEES
let (currency_amount) = AMM.get_sell_price(
[token_amounts], currency_reserves_, token_reserves, Uint256(0, 0)
)
prices.high = currency_amount.high
prices.low = currency_amount.low
return get_all_buy_price_loop(
token_ids_len - 1,
token_ids + Uint256.SIZE,
token_amounts_len - 1,
token_amounts + Uint256.SIZE,
prices_len - 1,
prices + Uint256.SIZE,
)
end
#########################
# ERC1155 for LP tokens #
#########################
@view
func supportsInterface(interface_id : felt) -> (is_supported : felt):
if interface_id == IERC1155_RECEIVER_ID:
return (1)