-
Notifications
You must be signed in to change notification settings - Fork 0
/
quipuswap_maker_ceiling.py
1176 lines (945 loc) · 39.1 KB
/
quipuswap_maker_ceiling.py
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
import smartpy as sp
Addresses = sp.io.import_script_from_url("file:test-helpers/addresses.py")
Constants = sp.io.import_script_from_url("file:common/constants.py")
Errors = sp.io.import_script_from_url("file:common/errors.py")
################################################################
# State Machine
################################################################
IDLE = 0
WAITING_FOR_TOKEN_BALANCE = 1
################################################################
# Contract
################################################################
class MakerContract(sp.Contract):
def __init__(
self,
governorContractAddress = Addresses.GOVERNOR_ADDRESS,
pauseGuardianContractAddress = Addresses.PAUSE_GUARDIAN_ADDRESS,
receiverContractAddress = Addresses.RECEIVER_ADDRESS, # Address to send the output to
vwapContractAddress = Addresses.HARBINGER_VWAP_ADDRESS,
spotContractAddress = Addresses.HARBINGER_SPOT_ADDRESS,
quipuswapContractAddress = Addresses.QUIPUSWAP_ADDRESS,
tokenAddress = Addresses.TOKEN_ADDRESS,
paused = False,
maxDataDelaySec = sp.nat(60 * 5), # 5 minutes
minTradeDelaySec = sp.nat(0), # Time to wait in seconds between allowing swaps (use 0 to allow batch transactions)
spreadAmount = sp.nat(0), # How far below the oracle price the exchange price must be in percent before allowing a swap
volatilityTolerance = sp.nat(5), # 5%
tradeAmount = sp.nat(10),
tokenBalance = sp.nat(0), # this should be 0 when deployed
lastTradeTime = sp.timestamp(1),
state = IDLE,
):
self.init(
governorContractAddress = governorContractAddress,
pauseGuardianContractAddress = pauseGuardianContractAddress,
receiverContractAddress = receiverContractAddress,
vwapContractAddress = vwapContractAddress,
spotContractAddress = spotContractAddress,
quipuswapContractAddress = quipuswapContractAddress,
tokenAddress = tokenAddress,
paused = paused,
maxDataDelaySec = maxDataDelaySec,
minTradeDelaySec = minTradeDelaySec,
spreadAmount = spreadAmount,
volatilityTolerance = volatilityTolerance,
tradeAmount = tradeAmount,
tokenBalance = tokenBalance,
lastTradeTime = lastTradeTime,
state = state
)
################################################################
# Quipuswap API
################################################################
@sp.entry_point(check_no_incoming_transfer=True)
def tokenToTezPayment(self):
# Verify the contract isn't paused.
sp.verify(self.data.paused == False, Errors.PAUSED)
# Make sure enough time has passed
timeDeltaSeconds = sp.as_nat(sp.now - self.data.lastTradeTime)
sp.verify(timeDeltaSeconds >= self.data.minTradeDelaySec, Errors.TRADE_TIME)
# Read vwap from Harbinger Normalizer
harbingerVwap = sp.local('harbingerVwap', sp.view(
"getPrice",
self.data.vwapContractAddress,
Constants.ASSET_CODE,
sp.TPair(sp.TTimestamp, sp.TNat)
).open_some(message = Errors.VWAP_VIEW_ERROR))
# Read spot price from Harbinger Spot
harbingerSpot = sp.local('harbingerSpot', sp.view(
"getPrice",
self.data.spotContractAddress,
Constants.ASSET_CODE,
sp.TPair(
sp.TTimestamp, # Start
sp.TPair(
sp.TTimestamp, # End
sp.TPair(
sp.TNat, # Open
sp.TPair(
sp.TNat, # High
sp.TPair(
sp.TNat, # Low
sp.TPair(
sp.TNat, # Close
sp.TNat # Volume
)
)
)
)
)
)
).open_some(message = Errors.SPOT_VIEW_ERROR))
# Extract spot price
spotPrice = sp.local('spotPrice', (sp.fst(sp.snd(sp.snd(sp.snd(sp.snd(sp.snd(harbingerSpot.value))))))))
# Assert that the Harbinger spot data is newer than max data delay
dataAge = sp.as_nat(sp.now - sp.fst(sp.snd(harbingerSpot.value)))
sp.verify(dataAge <= self.data.maxDataDelaySec, Errors.STALE_DATA)
# Assert that latest Harbinger Normalizer update is newer than max data delay
vwapAge = sp.as_nat(sp.now - sp.fst(harbingerVwap.value))
sp.verify(vwapAge <= self.data.maxDataDelaySec, Errors.STALE_DATA)
# Upsample price numbers using token precision constant
harbingerVwapPrice = (sp.snd(harbingerVwap.value) * Constants.PRECISION) // 1_000_000
harbingerSpotPrice = sp.local('harbingerSpotPrice', (spotPrice.value * Constants.PRECISION) // 1_000_000)
# Check for volatility difference between VWAP and spot
volatilityDifference = (abs(harbingerVwapPrice - harbingerSpotPrice.value) * 100 // harbingerSpotPrice.value) # because tolerance is a percent
sp.verify(self.data.volatilityTolerance > volatilityDifference, Errors.VOLATILITY)
# Upsample
tokensToTrade = sp.local('tokensToTrade', (self.data.tradeAmount * Constants.PRECISION))
# Calculate the expected XTZ with no slippage.
# Expected out with no slippage = (number of tokens to trade // mutez Spot price) / 1e6
neutralOut = (tokensToTrade.value // spotPrice.value) // 1_000_000
# Apply spread multiplier
# Expected out multiplied by spread = (neutral out from above) * (1 + spread amount)
percent = sp.nat(100) + self.data.spreadAmount
requiredOut = (neutralOut * percent) // 100 # Note that percent is specified in scale = 100
# Approve Quipuswap contract to spend on token contract
approveHandle = sp.contract(
sp.TPair(sp.TAddress, sp.TNat),
self.data.tokenAddress,
"approve"
).open_some(message = Errors.APPROVAL)
approveArg = sp.pair(self.data.quipuswapContractAddress, tokensToTrade.value)
sp.transfer(approveArg, sp.mutez(0), approveHandle)
# Invoke a quipuswap trade
tradeHandle = sp.contract(
sp.TPair(sp.TPair(sp.TNat, sp.TNat), sp.TAddress),
self.data.quipuswapContractAddress,
"tokenToTezPayment"
).open_some(message = Errors.DEX_CONTRACT_ERROR)
tradeArg = sp.pair(sp.pair(tokensToTrade.value, requiredOut), self.data.receiverContractAddress)
sp.transfer(tradeArg, sp.mutez(0), tradeHandle)
# Write last trade timestamp to storage
self.data.lastTradeTime = sp.now
# Revoke Quipuswap contract approval on token contract
approveHandle = sp.contract(
sp.TPair(sp.TAddress, sp.TNat),
self.data.tokenAddress,
"approve"
).open_some(message = Errors.APPROVAL)
approveArg = sp.pair(self.data.quipuswapContractAddress, 0)
sp.transfer(approveArg, sp.mutez(0), approveHandle)
################################################################
# Balance functions
################################################################
# Return FA 1.2 balance to receiverContractAddress
@sp.entry_point(check_no_incoming_transfer=True)
def returnBalance(self):
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
# Verify state is correct.
sp.verify(self.data.state == IDLE, message = Errors.BAD_STATE)
# Call token contract to update balance.
param = (sp.self_address, sp.self_entry_point(entry_point = 'redeemCallback'))
contractHandle = sp.contract(
sp.TPair(sp.TAddress, sp.TContract(sp.TNat)),
self.data.tokenAddress,
"getBalance",
).open_some()
sp.transfer(param, sp.mutez(0), contractHandle)
# Save state to state machine
self.data.state = WAITING_FOR_TOKEN_BALANCE
# Private callback for updating Balance.
@sp.entry_point(check_no_incoming_transfer=True)
def redeemCallback(self, updatedBalance):
sp.set_type(updatedBalance, sp.TNat)
# Validate sender
sp.verify(sp.sender == self.data.tokenAddress, Errors.BAD_SENDER)
# Verify state is correct.
sp.verify(self.data.state == WAITING_FOR_TOKEN_BALANCE, message = Errors.BAD_STATE)
self.data.tokenBalance = updatedBalance
# Send balance to Receiver
sendParam = (
sp.self_address,
self.data.receiverContractAddress,
self.data.tokenBalance
)
sendHandle = sp.contract(
sp.TTuple(sp.TAddress, sp.TAddress, sp.TNat),
self.data.tokenAddress,
"transfer"
).open_some()
sp.transfer(sendParam, sp.mutez(0), sendHandle)
# Reset state
self.data.state = IDLE
################################################################
# Pause Guardian
################################################################
# Pause the system
@sp.entry_point(check_no_incoming_transfer=True)
def pause(self):
sp.verify(sp.sender == self.data.pauseGuardianContractAddress, message = Errors.NOT_PAUSE_GUARDIAN)
self.data.paused = True
################################################################
# Governance
################################################################
# Update the max data delay (stale data).
@sp.entry_point(check_no_incoming_transfer=True)
def setMaxDataDelaySec(self, newMaxDataDelaySec):
sp.set_type(newMaxDataDelaySec, sp.TNat)
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.maxDataDelaySec = newMaxDataDelaySec
# Update the delay between swaps.
@sp.entry_point(check_no_incoming_transfer=True)
def setMinTradeDelaySec(self, newMinTradeDelaySec):
sp.set_type(newMinTradeDelaySec, sp.TNat)
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.minTradeDelaySec = newMinTradeDelaySec
# Set the trade amount (in normalized tokens).
@sp.entry_point(check_no_incoming_transfer=True)
def setTradeAmount(self, newTradeAmount):
sp.set_type(newTradeAmount, sp.TNat)
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.tradeAmount = newTradeAmount
# Set spread amount (in percent)
@sp.entry_point(check_no_incoming_transfer=True)
def setSpreadAmount(self, newSpreadAmount):
sp.set_type(newSpreadAmount, sp.TNat)
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.spreadAmount = newSpreadAmount
# Set volatility tolerance (in percent)
@sp.entry_point(check_no_incoming_transfer=True)
def setVolatilityTolerance(self, newVolatilityTolerance):
sp.set_type(newVolatilityTolerance, sp.TNat)
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.volatilityTolerance = newVolatilityTolerance
# Unpause the system.
@sp.entry_point(check_no_incoming_transfer=True)
def unpause(self):
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.paused = False
# Update the Harbinger normalizer contract.
@sp.entry_point(check_no_incoming_transfer=True)
def setVwapContract(self, newVwapContractAddress):
sp.set_type(newVwapContractAddress, sp.TAddress)
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.vwapContractAddress = newVwapContractAddress
# Update the Harbinger spot contract.
@sp.entry_point(check_no_incoming_transfer=True)
def setSpotContract(self, newSpotContractAddress):
sp.set_type(newSpotContractAddress, sp.TAddress)
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.spotContractAddress = newSpotContractAddress
# Update the pause guardian contract.
@sp.entry_point(check_no_incoming_transfer=True)
def setPauseGuardianContract(self, newPauseGuardianContractAddress):
sp.set_type(newPauseGuardianContractAddress, sp.TAddress)
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.pauseGuardianContractAddress = newPauseGuardianContractAddress
# Update the Quipuswap AMM contract.
@sp.entry_point(check_no_incoming_transfer=True)
def setQuipuswapContract(self, newQuipuswapContractAddress):
sp.set_type(newQuipuswapContractAddress, sp.TAddress)
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.quipuswapContractAddress = newQuipuswapContractAddress
# Update the governor contract.
@sp.entry_point(check_no_incoming_transfer=True)
def setGovernorContract(self, newGovernorContractAddress):
sp.set_type(newGovernorContractAddress, sp.TAddress)
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.governorContractAddress = newGovernorContractAddress
# Update the Receiver contract.
@sp.entry_point(check_no_incoming_transfer=True)
def setReceiverContract(self, newReceiverContractAddress):
sp.set_type(newReceiverContractAddress, sp.TAddress)
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.receiverContractAddress = newReceiverContractAddress
# Only run tests if this file is main.
if __name__ == "__main__":
################################################################
################################################################
# Tests
################################################################
################################################################
FakeHarbingerVwap = sp.io.import_script_from_url("file:test-helpers/fake-harbinger-normalizer.py")
FakeHarbingerSpot = sp.import_script_from_url("file:test-helpers/fake-harbinger-spot.py")
FakeQuipuswap = sp.io.import_script_from_url("file:test-helpers/fake-quipuswap.py")
################################################################
# tokenToTezPayment
################################################################
@sp.add_test(name="tokenToTezPayment - correctly calculates amount out with 10 percent spread")
def test():
scenario = sp.test_scenario()
# GIVEN a moment in time.
currentTime = sp.timestamp(1000)
# AND fake harbinger normalizer contract
harbingerVwap = FakeHarbingerVwap.FakeHarbingerContract(
harbingerValue = sp.nat(1_000_000), # $1.00
harbingerUpdateTime = currentTime,
harbingerAsset = Constants.ASSET_CODE
)
scenario += harbingerVwap
# AND fake harbinger spot contract
harbingerSpot = FakeHarbingerSpot.FakeHarbingerContract(
harbingerValue = sp.nat(1_000_000), # $1.00
harbingerUpdateTime = currentTime,
harbingerAsset = Constants.ASSET_CODE,
harbingerVolume = sp.nat(1000)
)
scenario += harbingerSpot
# AND a fake quipuswap contract
quipuswap = FakeQuipuswap.FakeQuipuswapContract()
scenario += quipuswap
# AND a Market Making Ceiling contract with 10% spread requirement
maxDataDelaySec = 60
minTradeDelaySec = 1
lastTrade = sp.timestamp(1)
proxy = MakerContract(
vwapContractAddress = harbingerVwap.address,
spotContractAddress = harbingerSpot.address,
quipuswapContractAddress = quipuswap.address,
maxDataDelaySec = maxDataDelaySec,
minTradeDelaySec = minTradeDelaySec,
lastTradeTime = lastTrade,
spreadAmount = sp.nat(10),
)
scenario += proxy
# WHEN a trade is initiated
param = (sp.nat(10), sp.nat(1), Addresses.RECEIVER_ADDRESS)
amount = sp.nat(10)
scenario += proxy.tokenToTezPayment().run(
now = currentTime
)
# THEN a trade was attempted requiring a 10% spread between harbinger price and quipuswap price
# Expected Amount = (tokens sent / harbinger price) * (1 + (spread amount // 100))
# = (10 / $1.00) * (1 + .1)
# = 10 * 1.1
# = 11
scenario.verify(quipuswap.data.amountOut == 11 * 1_000_000)
scenario.verify(quipuswap.data.destination == Addresses.RECEIVER_ADDRESS)
@sp.add_test(name="tokenToTezPayment - correctly calculates amount out with no spread")
def test():
scenario = sp.test_scenario()
# GIVEN a moment in time.
currentTime = sp.timestamp(1000)
# AND fake harbinger normalizer contract
harbingerVwap = FakeHarbingerVwap.FakeHarbingerContract(
harbingerValue = sp.nat(1_000_000), # $1.00
harbingerUpdateTime = currentTime,
harbingerAsset = Constants.ASSET_CODE
)
scenario += harbingerVwap
# AND fake harbinger spot contract
harbingerSpot = FakeHarbingerSpot.FakeHarbingerContract(
harbingerValue = sp.nat(1_000_000), # $1.00
harbingerUpdateTime = currentTime,
harbingerAsset = Constants.ASSET_CODE,
harbingerVolume = sp.nat(1000)
)
scenario += harbingerSpot
# AND a fake quipuswap contract
quipuswap = FakeQuipuswap.FakeQuipuswapContract()
scenario += quipuswap
# AND a Market Making Ceiling contract with 0% spread requirement
spreadAmount = 0
maxDataDelaySec = 60
minTradeDelaySec = 1
lastTrade = sp.timestamp(1)
proxy = MakerContract(
vwapContractAddress = harbingerVwap.address,
spotContractAddress = harbingerSpot.address,
quipuswapContractAddress = quipuswap.address,
maxDataDelaySec = maxDataDelaySec,
minTradeDelaySec = minTradeDelaySec,
lastTradeTime = lastTrade,
spreadAmount = spreadAmount,
)
scenario += proxy
# WHEN a trade is initiated
scenario += proxy.tokenToTezPayment().run(
now = currentTime
)
# THEN a trade was attempted requiring a 0 spread between harbinger price and quipuswap price
# Expected Amount = (tokens sent / harbinger price) * (1 + (spread amount / 100))
# = (10 / $1.00) * (1 + 0)
# = 10 * 1
# = 10
scenario.verify(quipuswap.data.amountOut == 10 * 1_000_000)
scenario.verify(quipuswap.data.destination == Addresses.RECEIVER_ADDRESS)
@sp.add_test(name="tokenToTezPayment - fails if oracle is outdated")
def test():
scenario = sp.test_scenario()
# GIVEN a moment in time.
currentTime = sp.timestamp(1000)
# AND fake harbinger normalizer
harbingerVwap = FakeHarbingerVwap.FakeHarbingerContract(
harbingerValue = sp.nat(3_500_000),
harbingerAsset = Constants.ASSET_CODE
)
scenario += harbingerVwap
# AND fake harbinger spot contract that is out of date
lastUpdateTime = sp.timestamp(500)
harbingerSpot = FakeHarbingerSpot.FakeHarbingerContract(
harbingerValue = sp.nat(3_500_000), # $3.50
harbingerUpdateTime = lastUpdateTime,
harbingerAsset = Constants.ASSET_CODE
)
scenario += harbingerSpot
# AND a Quipuswap Proxy contract with max data delay less than (currentTime - lastUpdateTime)
maxDataDelaySec = sp.nat(499) # currentTime - lastUpdateTime - 1
proxy = MakerContract(
spotContractAddress = harbingerSpot.address,
vwapContractAddress = harbingerVwap.address,
maxDataDelaySec = maxDataDelaySec
)
scenario += proxy
# WHEN a trade is initiated
# THEN the call fails
scenario += proxy.tokenToTezPayment().run(
sender = Addresses.NULL_ADDRESS,
now = currentTime,
valid = False,
exception = Errors.STALE_DATA
)
@sp.add_test(name="tokenToTezPayment - fails if contract is paused")
def test():
# GIVEN a Quipuswap Proxy contract that is paused
scenario = sp.test_scenario()
proxy = MakerContract(
paused = True
)
scenario += proxy
# WHEN a trade is initiated
# THEN the call fails
scenario += proxy.tokenToTezPayment().run(
valid = False,
exception = Errors.PAUSED
)
@sp.add_test(name="tokenToTezPayment - fails if trade is sooner than minimum delay")
def test():
# GIVEN a Quipuswap Proxy contract with minimum trade delay of 60 seconds
scenario = sp.test_scenario()
delay = 60
proxy = MakerContract(
minTradeDelaySec = delay
)
scenario += proxy
# WHEN a trade is initiated at a time sooner than the minimum trade delay
# THEN the call fails
stamp = sp.timestamp(delay - 1)
scenario += proxy.tokenToTezPayment().run(
now = stamp,
valid = False,
exception = Errors.TRADE_TIME
)
@sp.add_test(name="tokenToTezPayment - fails if volatility between VWAP and Spot is too great")
def test():
scenario = sp.test_scenario()
# GIVEN a moment in time.
currentTime = sp.timestamp(1000)
# AND fake harbinger normalizer contract with a price of $1.00
harbingerVwap = FakeHarbingerVwap.FakeHarbingerContract(
harbingerValue = sp.nat(1_000_000), # $1.00
harbingerUpdateTime = currentTime,
harbingerAsset = Constants.ASSET_CODE
)
scenario += harbingerVwap
# AND fake harbinger spot contract with a price of $1.10
harbingerSpot = FakeHarbingerSpot.FakeHarbingerContract(
harbingerValue = sp.nat(1_100_000), # $1.10
harbingerUpdateTime = currentTime,
harbingerAsset = Constants.ASSET_CODE,
harbingerVolume = sp.nat(1000)
)
scenario += harbingerSpot
# AND Quipuswap Proxy contract with volatilityTolerance of 5%
proxy = MakerContract(
vwapContractAddress = harbingerVwap.address,
spotContractAddress = harbingerSpot.address,
volatilityTolerance = 5,
)
scenario += proxy
# WHEN a trade is initiated
# THEN the call fails
amount = sp.nat(10)
scenario += proxy.tokenToTezPayment().run(
now = currentTime,
valid = False,
exception = Errors.VOLATILITY
)
################################################################
# returnBalance
################################################################
@sp.add_test(name="returnBalance - succeeds when called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
# AND a market making contract
proxy = MakerContract()
scenario += proxy
# WHEN returnBalance is called by the governor THEN the call succeeds
scenario += proxy.returnBalance().run(
sender = Addresses.GOVERNOR_ADDRESS,
valid = True
)
@sp.add_test(name="returnBalance - fails when not called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
# AND a maker contract
proxy = MakerContract()
scenario += proxy
# WHEN returnBalance is called THEN the call fails
notGovernor = Addresses.NULL_ADDRESS
scenario += proxy.returnBalance().run(
sender = notGovernor,
valid = False
)
################################################################
# pause
################################################################
@sp.add_test(name="pause - succeeds when called by pause guardian")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
paused = False
)
scenario += proxy
# WHEN pause is called
scenario += proxy.pause().run(
sender = Addresses.PAUSE_GUARDIAN_ADDRESS,
)
# THEN the contract is paused
scenario.verify(proxy.data.paused == True)
@sp.add_test(name="pause - fails when not called by pause guardian")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
paused = False
)
scenario += proxy
# WHEN pause is called is called by someone who isn't the pause guardian THEN the call fails
scenario += proxy.pause().run(
sender = Addresses.NULL_ADDRESS,
valid = False,
exception = Errors.NOT_PAUSE_GUARDIAN
)
################################################################
# setMaxDataDelaySec
################################################################
@sp.add_test(name="setMaxDataDelaySec - succeeds when called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
maxDataDelaySec = sp.nat(10)
)
scenario += proxy
# WHEN setMaxDataDelaySec is called
newValue = sp.nat(20)
scenario += proxy.setMaxDataDelaySec(newValue).run(
sender = Addresses.GOVERNOR_ADDRESS,
)
# THEN the storage is updated
scenario.verify(proxy.data.maxDataDelaySec == newValue)
@sp.add_test(name="setMaxDataDelaySec - fails when not called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
maxDataDelaySec = sp.nat(10)
)
scenario += proxy
# WHEN setMaxDataDelaySec is called is called by someone who isn't the governor THEN the call fails
newValue = sp.nat(20)
scenario += proxy.setMaxDataDelaySec(newValue).run(
sender = Addresses.NULL_ADDRESS,
valid = False,
exception = Errors.NOT_GOVERNOR
)
################################################################
# setMinTradeDelaySec
################################################################
@sp.add_test(name="setMinTradeDelaySec - succeeds when called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
minTradeDelaySec = sp.nat(10)
)
scenario += proxy
# WHEN setMinTradeDelaySec is called
newValue = sp.nat(20)
scenario += proxy.setMinTradeDelaySec(newValue).run(
sender = Addresses.GOVERNOR_ADDRESS,
)
# THEN the storage is updated
scenario.verify(proxy.data.minTradeDelaySec == newValue)
@sp.add_test(name="setMinTradeDelaySec - fails when not called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
maxDataDelaySec = sp.nat(10)
)
scenario += proxy
# WHEN setMinTradeDelaySec is called is called by someone who isn't the governor THEN the call fails
newValue = sp.nat(20)
scenario += proxy.setMinTradeDelaySec(newValue).run(
sender = Addresses.NULL_ADDRESS,
valid = False,
exception = Errors.NOT_GOVERNOR
)
################################################################
# setSpreadAmount
################################################################
@sp.add_test(name="setSpreadAmount - succeeds when called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
spreadAmount = sp.nat(10)
)
scenario += proxy
# WHEN setSpreadAmount is called
newValue = sp.nat(20)
scenario += proxy.setSpreadAmount(newValue).run(
sender = Addresses.GOVERNOR_ADDRESS,
)
# THEN the storage is updated
scenario.verify(proxy.data.spreadAmount == newValue)
@sp.add_test(name="setSpreadAmount - fails when not called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
spreadAmount = sp.nat(10)
)
scenario += proxy
# WHEN setSpreadAmount is called is called by someone who isn't the governor THEN the call fails
newValue = sp.nat(20)
scenario += proxy.setSpreadAmount(newValue).run(
sender = Addresses.NULL_ADDRESS,
valid = False,
exception = Errors.NOT_GOVERNOR
)
################################################################
# setVolatilityTolerance
################################################################
@sp.add_test(name="setVolatilityTolerance - succeeds when called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
volatilityTolerance = sp.nat(10)
)
scenario += proxy
# WHEN setVolatilityTolerance is called
newValue = sp.nat(20)
scenario += proxy.setVolatilityTolerance(newValue).run(
sender = Addresses.GOVERNOR_ADDRESS,
)
# THEN the storage is updated
scenario.verify(proxy.data.volatilityTolerance == newValue)
@sp.add_test(name="setVolatilityTolerance - fails when not called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
volatilityTolerance = sp.nat(10)
)
scenario += proxy
# WHEN setVolatilityTolerance is called is called by someone who isn't the governor THEN the call fails
newValue = sp.nat(20)
scenario += proxy.setVolatilityTolerance(newValue).run(
sender = Addresses.NULL_ADDRESS,
valid = False,
exception = Errors.NOT_GOVERNOR
)
################################################################
# setTradeAmount
################################################################
@sp.add_test(name="setTradeAmount - succeeds when called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
tradeAmount = sp.nat(10)
)
scenario += proxy
# WHEN setTradeAmount is called
newValue = sp.nat(20)
scenario += proxy.setTradeAmount(newValue).run(
sender = Addresses.GOVERNOR_ADDRESS,
)
# THEN the storage is updated
scenario.verify(proxy.data.tradeAmount == newValue)
@sp.add_test(name="setTradeAmount - fails when not called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
tradeAmount = sp.nat(10)
)
scenario += proxy
# WHEN setTradeAmount is called is called by someone who isn't the governor THEN the call fails
newValue = sp.nat(20)
scenario += proxy.setTradeAmount(newValue).run(
sender = Addresses.NULL_ADDRESS,
valid = False,
exception = Errors.NOT_GOVERNOR
)
################################################################
# unpause
################################################################
@sp.add_test(name="unpause - succeeds when called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
paused = True
)
scenario += proxy
# WHEN unpause is called
scenario += proxy.unpause().run(
sender = Addresses.GOVERNOR_ADDRESS,
)
# THEN the contract is unpaused
scenario.verify(proxy.data.paused == False)
@sp.add_test(name="unpause - fails when not called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
proxy = MakerContract(
paused = True
)
scenario += proxy
# WHEN unpause is called is called by someone who isn't the governor THEN the call fails
scenario += proxy.unpause().run(
sender = Addresses.NULL_ADDRESS,
valid = False,
exception = Errors.NOT_GOVERNOR
)
################################################################
# setSpotContract
################################################################
@sp.add_test(name="setSpotContract - succeeds when called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
spotContractAddress = Addresses.HARBINGER_SPOT_ADDRESS
proxy = MakerContract(
spotContractAddress = spotContractAddress
)
scenario += proxy
# WHEN setSpotContract is called with a new contract
rotatedAddress = Addresses.ROTATED_ADDRESS
scenario += proxy.setSpotContract(rotatedAddress).run(
sender = Addresses.GOVERNOR_ADDRESS,
)
# THEN the contract is updated.
scenario.verify(proxy.data.spotContractAddress == rotatedAddress)
@sp.add_test(name="setSpotContract - fails when not called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
spotContractAddress = Addresses.HARBINGER_SPOT_ADDRESS
proxy = MakerContract(
spotContractAddress = spotContractAddress
)
scenario += proxy
# WHEN setSpotContract is called by someone who isn't the governor THEN the call fails
rotatedAddress = Addresses.ROTATED_ADDRESS
scenario += proxy.setSpotContract(rotatedAddress).run(
sender = Addresses.NULL_ADDRESS,
valid = False,
exception = Errors.NOT_GOVERNOR
)
################################################################
# setVwapContract
################################################################
@sp.add_test(name="setVwapContract - succeeds when called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
vwapContractAddress = Addresses.HARBINGER_VWAP_ADDRESS
proxy = MakerContract(
vwapContractAddress = vwapContractAddress
)
scenario += proxy
# WHEN setVwapContract is called with a new contract
rotatedAddress = Addresses.ROTATED_ADDRESS
scenario += proxy.setVwapContract(rotatedAddress).run(
sender = Addresses.GOVERNOR_ADDRESS,
)
# THEN the contract is updated.
scenario.verify(proxy.data.vwapContractAddress == rotatedAddress)
@sp.add_test(name="setVwapContract - fails when not called by governor")
def test():
# GIVEN a Quipuswap Proxy contract
scenario = sp.test_scenario()
vwapContractAddress = Addresses.HARBINGER_VWAP_ADDRESS
proxy = MakerContract(
vwapContractAddress = vwapContractAddress
)
scenario += proxy
# WHEN setVwapContract is called by someone who isn't the governor THEN the call fails
rotatedAddress = Addresses.ROTATED_ADDRESS
scenario += proxy.setVwapContract(rotatedAddress).run(
sender = Addresses.NULL_ADDRESS,
valid = False,
exception = Errors.NOT_GOVERNOR