/
stableswap-stx-ststx-v-1-1.clar
1124 lines (951 loc) · 53.2 KB
/
stableswap-stx-ststx-v-1-1.clar
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
;; Bitflow Stableswap Core Contract
;; This contract handles the core logic of the Stableswap protocol.
;; The trading pair is STX<>stSTX where stSTX is the liquid stacking token by StackingDAO.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Cons, Vars, & Maps ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-trait sip-010-trait .sip-010-trait-ft-standard.sip-010-trait)
(use-trait lp-trait .lp-trait.lp-trait)
;;;;;;;;;;;;;;;
;; Constants ;;
;;;;;;;;;;;;;;;
;; This contract address
(define-constant this-contract (as-contract contract-caller))
;; Deployment height
(define-constant deployment-height block-height)
;; Cycle length in blocks (1 day = 144 blocks)
(define-constant cycle-length u144)
;; Index loop for using Newton-Raphson method to converge square root that goes up to u384
(define-constant index-list (list u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50 u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75 u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100 u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125 u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150 u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175 u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200 u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225 u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250 u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275 u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300 u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325 u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350 u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375 u376 u377 u378 u379 u380 u381 u382 u383 u384))
;; Number of tokens per pair
(define-constant number-of-tokens u2)
;; Contract deployer
(define-constant contract-deployer contract-caller)
;;;;;;;;;;;;
;; Errors ;;
;;;;;;;;;;;;
;;;;;;;;;;;;;;;
;; Variables ;;
;;;;;;;;;;;;;;;
;; Contract for Stableswap Staking and Rewards
(define-data-var staking-and-rewards-contract principal contract-caller)
;; Define a variable to track whether the staking-and-rewards contract has been set
(define-data-var staking-and-rewards-contract-is-set bool false)
;; Contract for the Stacking DAO to receive fees
(define-data-var stacking-dao-contract principal 'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG)
;; Contract for the Bitflow protocol to receive fees
(define-data-var bitflow-contract principal 'SP1G6QWV6X9EVXH7QMMFVHNC3VHWJC28MHR6P8TX2)
;; Admin Governance List
(define-data-var admins (list 5 principal) (list contract-caller))
;; Swap Fees (5 total bps initialized, 3 bps to LPs, 1 bps to Stacking DAO protocol, 1bps to Bitflow protocol)
(define-data-var buy-fees {lps: uint, stacking-dao: uint, bitflow: uint} {lps: u3, stacking-dao: u1, bitflow: u1})
;; Swap Fees (5 total bps initialized, 3 bps to LPs, 1 bps to Stacking DAO protocol, 1bps to Bitflow protocol)
(define-data-var sell-fees {lps: uint, stacking-dao: uint, bitflow: uint} {lps: u3, stacking-dao: u1, bitflow: u1})
;; Admin Swap Fees - Set to Zero
(define-data-var admin-swap-fees {lps: uint, stacking-dao: uint, bitflow: uint} {lps: u0, stacking-dao: u0, bitflow: u0})
;; Liquidity Fees (3 bps initialized, all to protocol)
(define-data-var liquidity-fees uint u3)
;; Helper var to remove admin
(define-data-var helper-principal principal contract-caller)
;; Convergence Threshold
(define-data-var convergence-threshold uint u2)
;;;;;;;;;;
;; Maps ;;
;;;;;;;;;;
(define-map PairsDataMap {y-token: principal, lp-token: principal} {
approval: bool,
total-shares: uint,
x-decimals: uint,
y-decimals: uint,
balance-x: uint,
balance-y: uint,
d: uint,
amplification-coefficient: uint,
})
(define-map CycleDataMap {y-token: principal, lp-token: principal, cycle-num: uint} {
cycle-fee-balance-x: uint
})
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Read-Only Functions ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Get pair data
(define-read-only (get-pair-data (y-token <sip-010-trait>) (lp-token <sip-010-trait>))
(map-get? PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)})
)
;; Get cycle data
(define-read-only (get-cycle-data (y-token principal) (lp-token principal) (cycle-num uint))
(map-get? CycleDataMap {y-token: y-token, lp-token: lp-token, cycle-num: cycle-num})
)
;; Get current cycle
(define-read-only (get-current-cycle)
(/ (- block-height deployment-height) cycle-length)
)
;; Get cycle from height
(define-read-only (get-cycle-from-height (height uint))
(/ (- height deployment-height) cycle-length)
)
;; Get starting height from cycle
(define-read-only (get-starting-height-from-cycle (cycle uint))
(+ deployment-height (* cycle cycle-length))
)
;; Get deployment height
(define-read-only (get-deployment-height)
deployment-height
)
;; Get up to last 120 cycle rewards -> nice to have
;; (define-read-only (get-cycle-rewards) body)
;; Get DX
(define-read-only (get-dx (y-token <sip-010-trait>) (lp-token <lp-trait>) (y-amount uint))
(let
(
(pair-data (unwrap! (map-get? PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)}) (err "err-no-pair-data")))
(current-balance-x (get balance-x pair-data))
(current-balance-y (get balance-y pair-data))
(x-decimals (get x-decimals pair-data))
(y-decimals (get y-decimals pair-data))
(swap-fee-lps (get lps (var-get sell-fees)))
(swap-fee-stacking-dao (get stacking-dao (var-get sell-fees)))
(swap-fee-bitflow (get bitflow (var-get sell-fees)))
(total-swap-fee (+ swap-fee-lps swap-fee-stacking-dao swap-fee-bitflow))
;; Scale up balances to perform AMM calculations with get-x
(scaled-up-balances (get-scaled-up-token-amounts current-balance-x current-balance-y x-decimals y-decimals))
(current-balance-x-scaled (get scaled-x scaled-up-balances))
(current-balance-y-scaled (get scaled-y scaled-up-balances))
(scaled-up-swap-amount (get-scaled-up-token-amounts u0 y-amount x-decimals y-decimals))
(y-amount-scaled (get scaled-y scaled-up-swap-amount))
(updated-y-balance-scaled (+ current-balance-y-scaled y-amount-scaled))
(new-x-scaled (get-x updated-y-balance-scaled current-balance-x-scaled y-amount-scaled (* (get amplification-coefficient pair-data) number-of-tokens)))
(new-x (get scaled-x (get-scaled-down-token-amounts new-x-scaled u0 x-decimals y-decimals)))
(new-y (+ current-balance-y y-amount))
;; Apply fees on the delta in x
(dx-without-fees (- current-balance-x new-x))
(x-amount-fee-lps (/ (* dx-without-fees swap-fee-lps) u10000))
(x-amount-fee-stacking-dao (/ (* dx-without-fees swap-fee-stacking-dao) u10000))
(x-amount-fee-bitflow (/ (* dx-without-fees swap-fee-bitflow) u10000))
(dx (- dx-without-fees (+ x-amount-fee-lps x-amount-fee-stacking-dao x-amount-fee-bitflow)))
)
(ok dx)
)
)
;; Get X
(define-read-only (get-x (y-bal uint) (x-bal uint) (y-amount uint) (ann uint))
(let
(
(y-bal-new (+ y-bal y-amount))
(current-D (get-D x-bal y-bal ann))
(c0 current-D)
(c1 (/ (* c0 current-D) (* number-of-tokens y-bal-new)))
(c2 (/ (* c1 current-D) (* ann number-of-tokens)))
(b (+ y-bal-new (/ current-D ann)))
)
(get converged (fold x-for-loop index-list {x: current-D, c: c2, b: b, D: current-D, converged: u0}))
)
)
;; Get X Helper
(define-private (x-for-loop (n uint) (x-info {x: uint, c: uint, b: uint, D: uint, converged: uint}))
(let
(
(current-x (get x x-info))
(current-c (get c x-info))
(current-b (get b x-info))
(current-D (get D x-info))
(current-converged (get converged x-info))
(x-numerator (+ (* current-x current-x) current-c))
(x-denominator (- (+ (* u2 current-x) current-b) current-D))
(new-x (/ x-numerator x-denominator))
(threshold (var-get convergence-threshold))
)
(if (is-eq current-converged u0)
(if (> new-x current-x)
(if (<= (- new-x current-x) threshold)
{x: new-x, c: current-c, b: current-b, D: current-D, converged: new-x}
{x: new-x, c: current-c, b: current-b, D: current-D, converged: u0}
)
(if (<= (- current-x new-x) threshold)
{x: new-x, c: current-c, b: current-b, D: current-D, converged: new-x}
{x: new-x, c: current-c, b: current-b, D: current-D, converged: u0}
)
)
x-info
)
)
)
;; Get DY
(define-read-only (get-dy (y-token <sip-010-trait>) (lp-token <lp-trait>) (x-amount uint))
(let
(
(pair-data (unwrap! (map-get? PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)}) (err "err-no-pair-data")))
(current-balance-x (get balance-x pair-data))
(current-balance-y (get balance-y pair-data))
(x-decimals (get x-decimals pair-data))
(y-decimals (get y-decimals pair-data))
(swap-fee-lps (get lps (var-get buy-fees)))
(swap-fee-stacking-dao (get stacking-dao (var-get buy-fees)))
(swap-fee-bitflow (get bitflow (var-get buy-fees)))
(total-swap-fee (+ swap-fee-lps swap-fee-stacking-dao swap-fee-bitflow))
;; Scale up balances to perform AMM calculations with get-y
(scaled-up-balances (get-scaled-up-token-amounts current-balance-x current-balance-y x-decimals y-decimals))
(current-balance-x-scaled (get scaled-x scaled-up-balances))
(current-balance-y-scaled (get scaled-y scaled-up-balances))
(scaled-up-swap-amount (get-scaled-up-token-amounts x-amount u0 x-decimals y-decimals))
(x-amount-scaled (get scaled-x scaled-up-swap-amount))
(x-amount-fees-lps-scaled (/ (* x-amount-scaled swap-fee-lps) u10000))
(x-amount-fees-stacking-dao-scaled (/ (* x-amount-scaled swap-fee-stacking-dao) u10000))
(x-amount-fees-bitflow-scaled (/ (* x-amount-scaled swap-fee-bitflow) u10000))
(x-amount-total-fees-scaled (/ (* x-amount total-swap-fee) u10000))
(updated-x-amount-scaled (- x-amount-scaled x-amount-total-fees-scaled))
(updated-x-balance-scaled (+ current-balance-x-scaled updated-x-amount-scaled))
(new-y-scaled (get-y updated-x-balance-scaled current-balance-y-scaled updated-x-amount-scaled (* (get amplification-coefficient pair-data) number-of-tokens)))
;; Scale down to precise amounts for y and dy, as well as x-amount-fee-lps, and x-amount-fee-stacking-dao
(new-y (get scaled-y (get-scaled-down-token-amounts u0 new-y-scaled x-decimals y-decimals)))
(dy (- current-balance-y new-y))
)
(ok dy)
)
)
;; Get Y
(define-read-only (get-y (x-bal uint) (y-bal uint) (x-amount uint) (ann uint))
(let
(
(x-bal-new (+ x-bal x-amount))
(current-D (get-D x-bal y-bal ann))
(c0 current-D)
(c1 (/ (* c0 current-D) (* number-of-tokens x-bal-new)))
(c2 (/ (* c1 current-D) (* ann number-of-tokens)))
(b (+ x-bal-new (/ current-D ann)))
)
(get converged (fold y-for-loop index-list {y: current-D, c: c2, b: b, D: current-D, converged: u0}))
)
)
;; Get Y Helper
(define-private (y-for-loop (n uint) (y-info {y: uint, c: uint, b: uint, D: uint, converged: uint}))
(let
(
(current-y (get y y-info))
(current-c (get c y-info))
(current-b (get b y-info))
(current-D (get D y-info))
(current-converged (get converged y-info))
(y-numerator (+ (* current-y current-y) current-c))
(y-denominator (- (+ (* u2 current-y) current-b) current-D))
(new-y (/ y-numerator y-denominator))
(threshold (var-get convergence-threshold))
)
(if (is-eq current-converged u0)
(if (> new-y current-y)
(if (<= (- new-y current-y) threshold)
{y: new-y, c: current-c, b: current-b, D: current-D, converged: new-y}
{y: new-y, c: current-c, b: current-b, D: current-D, converged: u0}
)
(if (<= (- current-y new-y) threshold)
{y: new-y, c: current-c, b: current-b, D: current-D, converged: new-y}
{y: new-y, c: current-c, b: current-b, D: current-D, converged: u0}
)
)
y-info
)
)
)
;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;
;;; Swap Functions ;;;
;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;
;; Swap X -> Y
;; @desc: Swaps X token for Y token
;; @params: y-token: principal, lp-token: principal, x-amount: uint, min-y-amount: uint
(define-public (swap-x-for-y (y-token <sip-010-trait>) (lp-token <lp-trait>) (x-amount uint) (min-y-amount uint))
(let
(
(swapper contract-caller)
(pair-data (unwrap! (map-get? PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)}) (err "err-no-pair-data")))
(current-approval (get approval pair-data))
(current-balance-x (get balance-x pair-data))
(current-balance-y (get balance-y pair-data))
(x-decimals (get x-decimals pair-data))
(y-decimals (get y-decimals pair-data))
;; Admins pay no fees on swaps
(swap-fee-lps (if (is-some (index-of (var-get admins) contract-caller ))
(get lps (var-get buy-fees))
(get lps (var-get admin-swap-fees))
))
(swap-fee-stacking-dao (if (is-some (index-of (var-get admins) contract-caller ))
(get stacking-dao (var-get buy-fees))
(get stacking-dao (var-get admin-swap-fees))
))
(swap-fee-bitflow (if (is-some (index-of (var-get admins) contract-caller ))
(get bitflow (var-get buy-fees))
(get bitflow (var-get admin-swap-fees))
))
(total-swap-fee (+ swap-fee-lps swap-fee-stacking-dao swap-fee-bitflow))
;; Scale up balances and the swap amount to perform AMM calculations with get-y
(scaled-up-balances (get-scaled-up-token-amounts current-balance-x current-balance-y x-decimals y-decimals))
(current-balance-x-scaled (get scaled-x scaled-up-balances))
(current-balance-y-scaled (get scaled-y scaled-up-balances))
(scaled-up-swap-amount (get-scaled-up-token-amounts x-amount u0 x-decimals y-decimals))
(x-amount-scaled (get scaled-x scaled-up-swap-amount))
(x-amount-fees-lps-scaled (/ (* x-amount-scaled swap-fee-lps) u10000))
(x-amount-fees-stacking-dao-scaled (/ (* x-amount-scaled swap-fee-stacking-dao) u10000))
(x-amount-fees-bitflow-scaled (/ (* x-amount-scaled swap-fee-bitflow) u10000))
(updated-x-amount-scaled (- x-amount-scaled (+ x-amount-fees-lps-scaled x-amount-fees-stacking-dao-scaled x-amount-fees-bitflow-scaled)))
(updated-x-balance-scaled (+ current-balance-x-scaled updated-x-amount-scaled))
(new-y-scaled (get-y updated-x-balance-scaled current-balance-y-scaled updated-x-amount-scaled (* (get amplification-coefficient pair-data) number-of-tokens)))
;; Scale down to precise amounts for y and dy, as well as updated-x-amount and x-fees
(new-y (get scaled-y (get-scaled-down-token-amounts u0 new-y-scaled x-decimals y-decimals)))
(dy (- current-balance-y new-y))
(x-amount-fee-lps (get scaled-x (get-scaled-down-token-amounts x-amount-fees-lps-scaled u0 x-decimals y-decimals)))
(x-amount-fee-stacking-dao (get scaled-x (get-scaled-down-token-amounts x-amount-fees-stacking-dao-scaled u0 x-decimals y-decimals)))
(x-amount-fee-bitflow (get scaled-x (get-scaled-down-token-amounts x-amount-fees-bitflow-scaled u0 x-decimals y-decimals)))
(updated-x-amount (- x-amount (+ x-amount-fee-lps x-amount-fee-stacking-dao x-amount-fee-bitflow)))
(updated-x-balance (+ current-balance-x updated-x-amount))
)
;; Assert that pair is approved
(asserts! current-approval (err "err-pair-not-approved"))
;; Assert that x-amount is less than x10 of current-balance-x
(asserts! (< x-amount (* u10 current-balance-x)) (err "err-x-amount-too-high"))
;; Assert that dy is greater than min-y-amount
(asserts! (> dy min-y-amount) (err "err-min-y-amount"))
;; Transfer updated-x-amount tokens from contract-caller to this contract
(if (> updated-x-amount u0)
(unwrap! (stx-transfer? updated-x-amount swapper (as-contract contract-caller)) (err "err-transferring-token-x"))
false
)
;; Transfer x-amount-fee-lps tokens from contract-caller to staking-and-rewards-contract
(if (> x-amount-fee-lps u0)
(unwrap! (stx-transfer? x-amount-fee-lps swapper (var-get staking-and-rewards-contract)) (err "err-transferring-token-x-fee-lps"))
false
)
;; Transfer x-amount-fee-stacking-dao tokens from contract-caller to stacking-dao-address
(if (> x-amount-fee-stacking-dao u0)
(unwrap! (stx-transfer? x-amount-fee-stacking-dao swapper (var-get stacking-dao-contract)) (err "err-transferring-token-x-fee-stacking-dao"))
false
)
;; Transfer x-amount-fee-biflow tokens from contract-caller to bitflow-address
(if (> x-amount-fee-bitflow u0)
(unwrap! (stx-transfer? x-amount-fee-bitflow swapper (var-get bitflow-contract)) (err "err-transferring-token-x-fee-bitflow"))
false
)
;; Transfer dy tokens from this contract to contract-caller
(if (> dy u0)
(unwrap! (as-contract (contract-call? y-token transfer dy contract-caller swapper none)) (err "err-transferring-token-y"))
false
)
;; Update all appropriate maps
;; Update PairsDataMap
(map-set PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)} (merge
pair-data
{
balance-x: updated-x-balance,
balance-y: new-y,
d: (get-D updated-x-balance-scaled new-y-scaled (* (get amplification-coefficient pair-data) number-of-tokens))
}
))
;; Match if map-get? returns some for CycleDataMap
(match (map-get? CycleDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token), cycle-num: (get-current-cycle)})
cycle-data
;; Update CycleDataMap
(map-set CycleDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token), cycle-num: (get-current-cycle)} (merge
cycle-data
{
cycle-fee-balance-x: (+ (get cycle-fee-balance-x cycle-data) x-amount-fee-lps)
}
))
;; Create new CycleDataMap
(map-set CycleDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token), cycle-num: (get-current-cycle)} {
cycle-fee-balance-x: x-amount-fee-lps,
})
)
;; Return the number of y tokens returned from the swap
(ok dy)
)
)
;; Swap Y -> X
;; @desc: Swaps Y token for X token
;; @params: y-token: principal, lp-token: principal, x-amount: uint, min-x-amount: uint
(define-public (swap-y-for-x (y-token <sip-010-trait>) (lp-token <lp-trait>) (y-amount uint) (min-x-amount uint))
(let
(
(swapper contract-caller)
(pair-data (unwrap! (map-get? PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)}) (err "err-no-pair-data")))
(current-approval (get approval pair-data))
(current-balance-x (get balance-x pair-data))
(current-balance-y (get balance-y pair-data))
(x-decimals (get x-decimals pair-data))
(y-decimals (get y-decimals pair-data))
;; Admins pay no fees on swaps
(swap-fee-lps (if (is-some (index-of (var-get admins) contract-caller ))
(get lps (var-get sell-fees))
(get lps (var-get admin-swap-fees))
))
(swap-fee-stacking-dao (if (is-some (index-of (var-get admins) contract-caller ))
(get stacking-dao (var-get sell-fees))
(get stacking-dao (var-get admin-swap-fees))
))
(swap-fee-bitflow (if (is-some (index-of (var-get admins) contract-caller ))
(get bitflow (var-get sell-fees))
(get bitflow (var-get admin-swap-fees))
))
(total-swap-fee (+ swap-fee-lps swap-fee-stacking-dao))
;; Scale up balances and the swap amount to perform AMM calculations with get-x
(scaled-up-balances (get-scaled-up-token-amounts current-balance-x current-balance-y x-decimals y-decimals))
(current-balance-x-scaled (get scaled-x scaled-up-balances))
(current-balance-y-scaled (get scaled-y scaled-up-balances))
(scaled-up-swap-amount (get-scaled-up-token-amounts u0 y-amount x-decimals y-decimals))
(y-amount-scaled (get scaled-y scaled-up-swap-amount))
(updated-y-balance-scaled (+ current-balance-y-scaled y-amount-scaled))
(new-x-scaled (get-x updated-y-balance-scaled current-balance-x-scaled y-amount-scaled (* (get amplification-coefficient pair-data) number-of-tokens)))
(new-x (get scaled-x (get-scaled-down-token-amounts new-x-scaled u0 x-decimals y-decimals)))
(new-y (+ current-balance-y y-amount))
;; Apply fees on the delta in x
(dx-without-fees (- current-balance-x new-x))
(x-amount-fee-lps (/ (* dx-without-fees swap-fee-lps) u10000))
(x-amount-fee-stacking-dao (/ (* dx-without-fees swap-fee-stacking-dao) u10000))
(x-amount-fee-bitflow (/ (* dx-without-fees swap-fee-bitflow) u10000))
(dx (- dx-without-fees (+ x-amount-fee-lps x-amount-fee-stacking-dao x-amount-fee-bitflow)))
)
;; Assert that pair is approved
(asserts! current-approval (err "err-pair-not-approved"))
;; Assert that y-amount is less than x10 of current-balance-y
(asserts! (< y-amount (* u10 current-balance-y)) (err "err-y-amount-too-high"))
;; Assert that dx is greater than min-x-amount
(asserts! (> dx min-x-amount) (err "err-min-x-amount"))
;; Transfer y-amount tokens from contract-caller to this contract
(if (> y-amount u0)
(unwrap! (contract-call? y-token transfer y-amount swapper (as-contract contract-caller) none) (err "err-transferring-token-y"))
false
)
;; Transfer x-amount-fee-lps tokens from this-contract to staking-and-rewards-contract
(if (> x-amount-fee-lps u0)
(unwrap! (as-contract (stx-transfer? x-amount-fee-lps contract-caller (var-get staking-and-rewards-contract))) (err "err-transferring-token-x-swap-fee"))
false
)
;; Transfer x-amount-fee-stacking-dao tokens from this-contract to stacking-dao-address
(if (> x-amount-fee-stacking-dao u0)
(unwrap! (as-contract (stx-transfer? x-amount-fee-stacking-dao contract-caller (var-get stacking-dao-contract))) (err "err-transferring-token-x-stacking-dao-fee"))
false
)
;; Transfer x-amount-fee-bitflow tokens from this-contract to bitflow-address
(if (> x-amount-fee-bitflow u0)
(unwrap! (as-contract (stx-transfer? x-amount-fee-bitflow contract-caller (var-get bitflow-contract))) (err "err-transferring-token-x-bitflow-fee"))
false
)
;; Transfer dx tokens from this contract to contract-caller
(if (> dx u0)
(unwrap! (as-contract (stx-transfer? dx contract-caller swapper)) (err "err-transferring-token-x"))
false
)
;; Update all appropriate maps
;; Update PairsDataMap
(map-set PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)} (merge
pair-data
{
balance-x: new-x,
balance-y: new-y,
d: (get-D new-x-scaled updated-y-balance-scaled (* (get amplification-coefficient pair-data) number-of-tokens))
}
))
;; Match if map-get? returns some for CycleDataMap
(match (map-get? CycleDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token), cycle-num: (get-current-cycle)})
cycle-data
;; Update CycleDataMap
(map-set CycleDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token), cycle-num: (get-current-cycle)} (merge
cycle-data
{
cycle-fee-balance-x: (+ (get cycle-fee-balance-x cycle-data) x-amount-fee-lps)
}
))
;; Create new CycleDataMap
(map-set CycleDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token), cycle-num: (get-current-cycle)} {
cycle-fee-balance-x: x-amount-fee-lps,
})
)
;; Return the number of x tokens returned from the swap
(ok dx)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Liquidity Functions ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Add Liquidity
;; @desc: Adds liquidity to a pair, mints the appropriate amount of LP tokens
;; @params: y-token: principal, lp-token: principal, x-amount-added: uint, y-amount-added: uint
(define-public (add-liquidity (y-token <sip-010-trait>) (lp-token <lp-trait>) (x-amount-added uint) (y-amount-added uint) (min-lp-amount uint) )
(let
(
;; Grabbing all data from PairsDataMap
(liquidity-provider contract-caller)
(current-pair (unwrap! (map-get? PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)}) (err "err-no-pair-data")))
(current-approval (get approval current-pair))
(x-decimals (get x-decimals current-pair))
(y-decimals (get y-decimals current-pair))
(current-balance-x (get balance-x current-pair))
(new-balance-x (+ current-balance-x x-amount-added))
(current-balance-y (get balance-y current-pair))
(new-balance-y (+ current-balance-y y-amount-added))
(current-total-shares (get total-shares current-pair))
(current-amplification-coefficient (get amplification-coefficient current-pair))
;; Scale up for AMM calculations depending on decimal places assigned to tokens
(amounts-added-scaled (get-scaled-up-token-amounts x-amount-added y-amount-added x-decimals y-decimals))
(x-amount-added-scaled (get scaled-x amounts-added-scaled))
(y-amount-added-scaled (get scaled-y amounts-added-scaled))
(current-balances-scaled (get-scaled-up-token-amounts current-balance-x current-balance-y x-decimals y-decimals))
(current-balance-x-scaled (get scaled-x current-balances-scaled))
(current-balance-y-scaled (get scaled-y current-balances-scaled))
(new-balances-scaled (get-scaled-up-token-amounts new-balance-x new-balance-y x-decimals y-decimals))
(new-balance-x-scaled (get scaled-x new-balances-scaled))
(new-balance-y-scaled (get scaled-y new-balances-scaled))
;; Calculating the ideal balance
(d0 (get-D current-balance-x-scaled current-balance-y-scaled current-amplification-coefficient))
(d1 (get-D new-balance-x-scaled new-balance-y-scaled current-amplification-coefficient))
(ideal-balance-x-scaled (/ (* d1 current-balance-x-scaled) d0))
(ideal-balance-y-scaled (/ (* d1 current-balance-y-scaled) d0))
(x-difference (if (> ideal-balance-x-scaled new-balance-x-scaled) (- ideal-balance-x-scaled new-balance-x-scaled) (- new-balance-x-scaled ideal-balance-x-scaled)))
(y-difference (if (> ideal-balance-y-scaled new-balance-y-scaled) (- ideal-balance-y-scaled new-balance-y-scaled) (- new-balance-y-scaled ideal-balance-y-scaled)))
;; Fees applied if adding imbalanced liquidity
(ideal-x-fee-scaled (/ (* x-difference (var-get liquidity-fees)) u10000))
(ideal-y-fee-scaled (/ (* y-difference (var-get liquidity-fees)) u10000))
(x-fee-scaled (if (> x-amount-added-scaled ideal-x-fee-scaled) ideal-x-fee-scaled x-amount-added-scaled))
(y-fee-scaled (if (> y-amount-added-scaled ideal-y-fee-scaled) ideal-y-fee-scaled y-amount-added-scaled))
(x-amount-added-updated-scaled (- x-amount-added-scaled x-fee-scaled))
(y-amount-added-updated-scaled (- y-amount-added-scaled y-fee-scaled))
(new-balance-x-post-fee-scaled (+ current-balance-x-scaled x-amount-added-updated-scaled))
(new-balance-y-post-fee-scaled (+ current-balance-y-scaled y-amount-added-updated-scaled))
(d2 (get-D new-balance-x-post-fee-scaled new-balance-y-post-fee-scaled current-amplification-coefficient))
;; Scale down for precise token balance updates and transfers
(precise-fees (get-scaled-down-token-amounts x-fee-scaled y-fee-scaled x-decimals y-decimals))
(x-fee (get scaled-x precise-fees))
(y-fee (get scaled-y precise-fees))
(amounts-added-scaled-down (get-scaled-down-token-amounts x-amount-added-updated-scaled y-amount-added-updated-scaled x-decimals y-decimals))
(x-amount-added-updated (get scaled-x amounts-added-scaled-down))
(y-amount-added-updated (get scaled-y amounts-added-scaled-down))
(balances-post-fee-scaled-down (get-scaled-down-token-amounts new-balance-x-post-fee-scaled new-balance-y-post-fee-scaled x-decimals y-decimals))
(new-balance-x-post-fee (get scaled-x balances-post-fee-scaled-down))
(new-balance-y-post-fee (get scaled-y balances-post-fee-scaled-down))
)
;; Assert that pair is approved
(asserts! current-approval (err "err-pair-not-approved"))
;; Assert that either x-amount-added or y-amount-added is greater than 0
(asserts! (or (> x-amount-added u0) (> y-amount-added u0)) (err "err-x-or-y-amount-added-zero"))
;; Assert that d2 is greater than d0
(asserts! (> d2 d0) (err "err-d2-less-than-d0"))
;; Assert that derived mint amount is greater than min-lp-amount
(asserts! (> (/ (* current-total-shares (- d2 d0)) d0) min-lp-amount) (err "err-derived-amount-less-than-lp"))
;; ;; Transfer x-amount-added tokens from contract-caller to this contract
(if (> x-amount-added-updated u0)
(unwrap! (stx-transfer? x-amount-added-updated liquidity-provider (as-contract contract-caller)) (err "err-transferring-token-x-escrow"))
false
)
;; Transfer y-amount-added tokens from contract-caller to this contract
(if (> y-amount-added-updated u0)
(unwrap! (contract-call? y-token transfer y-amount-added-updated liquidity-provider (as-contract contract-caller) none) (err "err-transferring-token-y"))
false
)
;; Transfer x-fees tokens from contract-caller to bitflow-address
(if (> x-fee u0)
(unwrap! (stx-transfer? x-fee liquidity-provider (var-get bitflow-contract)) (err "err-transferring-token-x-protocol"))
false
)
;; Transfer y-fees tokens from contract-caller to bitflow-address
(if (> y-fee u0)
(unwrap! (contract-call? y-token transfer y-fee liquidity-provider (var-get bitflow-contract) none) (err "err-transferring-token-y-protocol"))
false
)
;; Mint LP tokens to contract-caller
(unwrap! (as-contract (contract-call? lp-token mint liquidity-provider (/ (* current-total-shares (- d2 d0)) d0))) (err "err-minting-lp-tokens"))
;; Update all appropriate maps
;; Update PairsDataMap
(map-set PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)}
(merge
current-pair
{
balance-x: new-balance-x-post-fee,
balance-y: new-balance-y-post-fee,
total-shares: (+ current-total-shares (/ (* current-total-shares (- d2 d0)) d0)),
d: d2
}
))
;; Return the number of LP tokens minted
(ok (/ (* current-total-shares (- d2 d0)) d0))
)
)
;; Withdraw Liquidity
;; @desc: Withdraws liquidity from both pairs & burns the appropriate amount of LP tokens
;; @params: y-token: principal, lp-token: principal, lp-amount: uint, min-x-amount: uint, min-y-amount: uint
(define-public (withdraw-liquidity (y-token <sip-010-trait>) (lp-token <lp-trait>) (lp-amount uint) (min-x-amount uint) (min-y-amount uint))
(let
(
;; Grabbing all data from PairsDataMap
(current-pair (unwrap! (map-get? PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)}) (err "err-no-pair-data")))
(current-approval (get approval current-pair))
(x-decimals (get x-decimals current-pair))
(y-decimals (get y-decimals current-pair))
(current-balance-x (get balance-x current-pair))
(current-balance-y (get balance-y current-pair))
(current-total-shares (get total-shares current-pair))
(current-amplification-coefficient (get amplification-coefficient current-pair))
(withdrawal-balance-x (/ (* current-balance-x lp-amount) current-total-shares))
(withdrawal-balance-y (/ (* current-balance-y lp-amount) current-total-shares))
(new-balance-x (- current-balance-x withdrawal-balance-x))
(new-balance-y (- current-balance-y withdrawal-balance-y))
(liquidity-remover contract-caller)
;; get-D using the new-balance-x and new-balance-y
(new-balances-scaled (get-scaled-up-token-amounts new-balance-x new-balance-y x-decimals y-decimals))
(new-balance-x-scaled (get scaled-x new-balances-scaled))
(new-balance-y-scaled (get scaled-y new-balances-scaled))
(new-d (get-D new-balance-x-scaled new-balance-y-scaled current-amplification-coefficient))
)
;; Assert that withdrawal-balance-x is greater than min-x-amount
(asserts! (> withdrawal-balance-x min-x-amount) (err "err-withdrawal-balance-x-less-than-min-x-amount"))
;; Assert that withdrawal-balance-y is greater than min-y-amount
(asserts! (> withdrawal-balance-y min-y-amount) (err "err-withdrawal-balance-y-less-than-min-y-amount"))
;; Burn LP tokens from contract-caller
(unwrap! (contract-call? lp-token burn liquidity-remover lp-amount) (err "err-burning-lp-tokens"))
;; Transfer withdrawal-balance-x tokens from this contract to liquidity-taker
(unwrap! (as-contract (stx-transfer? withdrawal-balance-x contract-caller liquidity-remover)) (err "err-transferring-token-x"))
;; Transfer withdrawal-balance-y tokens from this contract to liquidity-taker
(unwrap! (as-contract (contract-call? y-token transfer withdrawal-balance-y contract-caller liquidity-remover none)) (err "err-transferring-token-y"))
;; Update all appropriate maps
;; Update PairsDataMap
(map-set PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)} (merge
current-pair
{
balance-x: new-balance-x,
balance-y: new-balance-y,
total-shares: (- current-total-shares lp-amount),
d: new-d
}
))
;; Return the number X tokens and Y tokens received after withdrawing from pool
(ok {withdrawal-x-balance: withdrawal-balance-x, withdrawal-y-balance: withdrawal-balance-y})
)
)
;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;
;;; AMM Functions ;;;
;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;
;; D for loop
;; Get D
(define-read-only (get-D (x-bal uint) (y-bal uint) (ann uint))
(get converged (fold D-for-loop index-list {D: (+ x-bal y-bal), x-bal: x-bal, y-bal: y-bal, ann: ann, converged: u0}))
)
;; Get D Helper
(define-private (D-for-loop (n uint) (D-info {D: uint, x-bal: uint, y-bal: uint, ann: uint, converged: uint}))
(let
(
;; Grabbing everything from D-info
(current-D-partial (get D D-info))
(current-D (get D D-info))
(current-x-bal (get x-bal D-info))
(current-y-bal (get y-bal D-info))
(current-S (+ current-x-bal current-y-bal))
(current-ann (get ann D-info))
(current-converged (get converged D-info))
;; Start logic for calculating new D
;; Calculate new partial D with respect to x
(new-D-partial-x (/ (* current-D current-D-partial) (* u2 current-x-bal)))
;; Calculate new partial D with respect to now x & y
(new-D-partial (/ (* current-D new-D-partial-x ) (* u2 current-y-bal)))
(new-numerator (* (+ (* current-ann current-S) (* number-of-tokens new-D-partial)) current-D))
(new-denominator (+ (* (- current-ann u1) current-D) (* (+ number-of-tokens u1 ) new-D-partial)))
(new-D (/ new-numerator new-denominator))
(threshold (var-get convergence-threshold))
)
;; Check if converged value / new D was already found
(if (is-eq current-converged u0)
(if (> new-D current-D)
(if (<= (- new-D current-D) threshold)
{D: new-D, x-bal: current-x-bal, y-bal: current-y-bal, ann: current-ann, converged: new-D}
{D: new-D, x-bal: current-x-bal, y-bal: current-y-bal, ann: current-ann, converged: u0}
)
(if (<= (- current-D new-D) threshold)
{D: new-D, x-bal: current-x-bal, y-bal: current-y-bal, ann: current-ann, converged: new-D}
{D: new-D, x-bal: current-x-bal, y-bal: current-y-bal, ann: current-ann, converged: u0}
)
)
D-info
)
)
)
;; Scale up the token amounts to the same level of precision before performing AMM calculations
;; @params: x-amount-unscaled: uint, y-amount-unscaled:uint, x-num-decimals: uint, y-num-decimals: uint
(define-private (get-scaled-up-token-amounts (x-amount-unscaled uint) (y-amount-unscaled uint) (x-num-decimals uint) (y-num-decimals uint))
(let
(
(scaled-x
;; if same number of decimals, set to x-amount-unscaled
(if (is-eq x-num-decimals y-num-decimals)
x-amount-unscaled
;; if x has more decimals, set to x-amount-unscaled; otherwise scale up by the difference in decimals
(if (> x-num-decimals y-num-decimals) x-amount-unscaled (* x-amount-unscaled (pow u10 (- y-num-decimals x-num-decimals))))
)
)
(scaled-y
;; if same number of decimals, set to y-amount-unscaled
(if (is-eq x-num-decimals y-num-decimals)
y-amount-unscaled
;; if y has more decimals, set to y-amount-unscaled; otherwise scale up by the difference in decimals
(if (> y-num-decimals x-num-decimals) y-amount-unscaled (* y-amount-unscaled (pow u10 (- x-num-decimals y-num-decimals))))
)
)
)
{scaled-x: scaled-x, scaled-y: scaled-y}
)
)
;; Scale down the token amounts to their respective levels of precision before performing any transfers
;; @params: x-amount-scaled: uint, y-amount-scaled:uint, x-num-decimals: uint, y-num-decimals: uint
(define-private (get-scaled-down-token-amounts (x-amount-scaled uint) (y-amount-scaled uint) (x-num-decimals uint) (y-num-decimals uint))
(let
(
(scaled-x
;; if same number of decimals, set to x-amount-scaled
(if (is-eq x-num-decimals y-num-decimals)
x-amount-scaled
;; if x has more decimals, set to x-amount-scaled; otherwise scale down by the difference in decimals
(if (> x-num-decimals y-num-decimals) x-amount-scaled (/ x-amount-scaled (pow u10 (- y-num-decimals x-num-decimals))))
)
)
(scaled-y
;; if same number of decimals, set to y-amount-scaled
(if (is-eq x-num-decimals y-num-decimals)
y-amount-scaled
;; if y has more decimals, set to y-amount-scaled; otherwise scale down by the difference in decimals
(if (> y-num-decimals x-num-decimals) y-amount-scaled (/ y-amount-scaled (pow u10 (- x-num-decimals y-num-decimals))))
)
)
)
{scaled-x: scaled-x, scaled-y: scaled-y}
)
)
;; @desc - Helper function for removing a admin
(define-private (is-not-removeable (admin principal))
(not (is-eq admin (var-get helper-principal)))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Goverance Functions ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Create Pair
;; @desc: Creates a new pair for trading
;; @params: y-token: principal, lp-token: principal, amplification-coefficient: uint, pair-name: string, x-balance: uint, y-balance: uint
(define-public (create-pair (y-token <sip-010-trait>) (lp-token <lp-trait>) (amplification-coefficient uint) (pair-name (string-ascii 32)) (initial-x-bal uint) (initial-y-bal uint))
(let
(
(lp-owner contract-caller)
(x-decimals u6) ;; STX token has precision of 6 decimals
(y-decimals (unwrap! (contract-call? y-token get-decimals) (err "err-getting-y-decimals")))
(scaled-up-balances (get-scaled-up-token-amounts initial-x-bal initial-y-bal x-decimals y-decimals))
(initial-x-bal-scaled (get scaled-x scaled-up-balances))
(initial-y-bal-scaled (get scaled-y scaled-up-balances))
)
;; Assert that contract-caller is an admin using is-some & index-of with the admins var
(asserts! (is-some (index-of (var-get admins) contract-caller )) (err "err-not-admin"))
;; Assert using and that the pair does not already exist using is-none & map-get?
(asserts! (is-none (map-get? PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)})) (err "err-pair-xy-or-yx-exists"))
;; Assert that both initial balances are greater than 0
(asserts! (or (> initial-x-bal u0) (> initial-y-bal u0)) (err "err-initial-bal-zero"))
;; Assert that x & y tokens are the same
(asserts! (is-eq initial-x-bal-scaled initial-y-bal-scaled) (err "err-initial-bal-odd"))
;; Mint LP tokens to contract-caller
(unwrap! (as-contract (contract-call? lp-token mint lp-owner (+ initial-x-bal-scaled initial-y-bal-scaled))) (err "err-minting-lp-tokens"))
;; Transfer token x liquidity to this contract
(unwrap! (stx-transfer? initial-x-bal contract-caller (as-contract contract-caller)) (err "err-transferring-token-x"))
;; Transfer token y liquidity to this contract
(unwrap! (contract-call? y-token transfer initial-y-bal contract-caller (as-contract contract-caller) none) (err "err-transferring-token-y"))
;; Update all appropriate maps
(ok (map-set PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)} {
approval: true,
total-shares: (+ initial-x-bal-scaled initial-y-bal-scaled),
x-decimals: x-decimals,
y-decimals: y-decimals,
balance-x: initial-x-bal,
balance-y: initial-y-bal,
d: (+ initial-x-bal-scaled initial-y-bal-scaled),
amplification-coefficient: amplification-coefficient,
}))
)
)
;; Setting Pair Approval
;; @desc: Sets the approval of a pair
;; @params: y-token: principal, lp-token: principal, approval: bool
(define-public (set-pair-approval (y-token <sip-010-trait>) (lp-token <lp-trait>) (approval bool))
(let
(
(current-pair (unwrap! (map-get? PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)}) (err "err-no-pair-data")))
)
;; Assert that contract-caller is an admin using is-some & index-of with the admins var
(asserts! (is-some (index-of (var-get admins) contract-caller)) (err "err-not-admin"))
;; Update all appropriate maps
(ok (map-set PairsDataMap {y-token: (contract-of y-token), lp-token: (contract-of lp-token)} (merge
current-pair
{
approval: approval
}
)))
)
)
;; Add Admin
;; @desc: Adds an admin to the admins var list
;; @params: admin: principal
(define-public (add-admin (admin principal))
(let
(
(current-admins (var-get admins))
;;(new-admins (unwrap! (as-max-len? (append current-admins admin) u5) ("err-add-admin-overflow")))
)
;; Assert that contract-caller is an admin using is-some & index-of with the admins var
(asserts! (is-some (index-of current-admins contract-caller)) (err "err-not-admin"))
;; Assert that admin is not already an admin using is-none & index-of with the admins var
(asserts! (is-none (index-of current-admins admin)) (err "err-already-admin"))
;; Update all appropriate maps
(ok (var-set admins (unwrap! (as-max-len? (append current-admins admin) u5) (err "err-admin-overflow"))))
)
)
;; Remove admin
(define-public (remove-admin (admin principal))
(let
(
(current-admin-list (var-get admins))
(caller-principal-position-in-list (index-of current-admin-list contract-caller))
(removeable-principal-position-in-list (index-of current-admin-list admin))
)
;; asserts contract-caller is an existing whitelist address
(asserts! (is-some caller-principal-position-in-list) (err "err-not-auth"))
;; asserts param principal (removeable whitelist) already exist
(asserts! (is-some removeable-principal-position-in-list) (err "err-not-whitelisted"))
;; asserts the initial contract deployer cannot be removed from admin list to protect against rogue admins
(asserts! (not (is-eq admin contract-deployer)) (err "err-cannot-remove-contract-deployer"))
;; temporary var set to help remove param principal
(var-set helper-principal admin)
;; filter existing whitelist address
(ok
(var-set admins (filter is-not-removeable current-admin-list))
)
)
)
;; Change Swap Fee
(define-public (change-buy-fee (new-lps-fee uint) (new-protocol-fee uint) (new-bitflow-fee uint))
(let
(
(current-admins (var-get admins))
)
;; Assert that contract-caller is an admin using is-some & index-of with the admins var
(asserts! (is-some (index-of current-admins contract-caller)) (err "err-not-admin"))
(ok (var-set buy-fees {lps: new-lps-fee, stacking-dao: new-protocol-fee, bitflow: new-bitflow-fee}))
)
)
;; Change Swap Fee
(define-public (change-sell-fee (new-lps-fee uint) (new-protocol-fee uint) (new-bitflow-fee uint))
(let
(
(current-admins (var-get admins))