-
Notifications
You must be signed in to change notification settings - Fork 2
/
voter.py
1469 lines (1219 loc) · 50.9 KB
/
voter.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
Errors = sp.io.import_script_from_url("file:utils/errors.py")
FA12 = sp.io.import_script_from_url("file:ply_fa12.py").FA12
TokenUtils = sp.io.import_script_from_url("file:utils/token.py")
VE = sp.io.import_script_from_url("file:helpers/dummy/ve.py").VE
Constants = sp.io.import_script_from_url("file:utils/constants.py")
Ply = sp.io.import_script_from_url("file:helpers/dummy/ply.py").Ply
Addresses = sp.io.import_script_from_url("file:helpers/addresses.py")
FeeDist = sp.io.import_script_from_url("file:helpers/dummy/fee_dist.py").FeeDist
GaugeBribe = sp.io.import_script_from_url("file:helpers/dummy/gauge_bribe.py").GaugeBribe
############
# Constants
############
DAY = Constants.DAY
WEEK = Constants.WEEK
YEAR = Constants.YEAR
MAX_TIME = Constants.MAX_TIME
DECIMALS = Constants.DECIMALS
PRECISION = Constants.PRECISION
YEARLY_DROP = Constants.YEARLY_DROP
INITIAL_DROP = Constants.INITIAL_DROP
EMISSION_FACTOR = Constants.EMISSION_FACTOR
INITIAL_EMISSION = Constants.INITIAL_EMISSION
DROP_GRANULARITY = Constants.DROP_GRANULARITY
VOTE_SHARE_MULTIPLIER = Constants.VOTE_SHARE_MULTIPLIER
########
# Types
########
class Types:
# Bigmap key and value types
TOKEN_AMM_VOTES_KEY = sp.TRecord(
token_id=sp.TNat,
amm=sp.TAddress,
epoch=sp.TNat,
).layout(("token_id", ("amm", "epoch")))
TOTAL_AMM_VOTES_KEY = sp.TRecord(
amm=sp.TAddress,
epoch=sp.TNat,
).layout(("amm", "epoch"))
TOTAL_TOKEN_VOTES_KEY = sp.TRecord(
token_id=sp.TNat,
epoch=sp.TNat,
).layout(("token_id", "epoch"))
AMM_TO_GAUGE_BRIBE = sp.TRecord(
gauge=sp.TAddress,
bribe=sp.TAddress,
).layout(("gauge", "bribe"))
# Param types
ADD_AMM_PARAMS = sp.TRecord(
amm=sp.TAddress,
gauge=sp.TAddress,
bribe=sp.TAddress,
).layout(("amm", ("gauge", "bribe")))
VOTE_PARAMS = sp.TRecord(
token_id=sp.TNat,
vote_items=sp.TList(
sp.TRecord(
amm=sp.TAddress,
votes=sp.TNat,
)
),
).layout(("token_id", "vote_items"))
CLAIM_BRIBE_PARAMS = sp.TRecord(
token_id=sp.TNat,
amm=sp.TAddress,
epoch=sp.TNat,
bribe_id=sp.TNat,
).layout(("token_id", ("amm", ("epoch", "bribe_id"))))
CLAIM_FEE_PARAMS = sp.TRecord(
token_id=sp.TNat,
amm=sp.TAddress,
epochs=sp.TList(sp.TNat),
).layout(("token_id", ("amm", "epochs")))
# Enumeration for voting power readers
CURRENT = sp.nat(0)
WHOLE_WEEK = sp.nat(1)
###########
# Contract
###########
class Voter(sp.Contract):
def __init__(
self,
core_factory=Addresses.DUMMY,
fee_distributor=Addresses.DUMMY,
ply_address=Addresses.TOKEN,
ve_address=Addresses.CONTRACT,
epoch=sp.nat(0),
epoch_end=sp.big_map(
l={},
tkey=sp.TNat,
tvalue=sp.TTimestamp,
),
emission=sp.record(
base=INITIAL_EMISSION,
real=sp.nat(0),
genesis=sp.nat(0),
),
amm_to_gauge_bribe=sp.big_map(
l={},
tkey=sp.TAddress,
tvalue=Types.AMM_TO_GAUGE_BRIBE,
),
total_amm_votes=sp.big_map(
l={},
tkey=Types.TOTAL_AMM_VOTES_KEY,
tvalue=sp.TNat,
),
token_amm_votes=sp.big_map(
l={},
tkey=Types.TOKEN_AMM_VOTES_KEY,
tvalue=sp.TNat,
),
total_token_votes=sp.big_map(
l={},
tkey=Types.TOTAL_TOKEN_VOTES_KEY,
tvalue=sp.TNat,
),
total_epoch_votes=sp.big_map(
l={},
tkey=sp.TNat,
tvalue=sp.TNat,
),
):
self.init(
core_factory=core_factory,
fee_distributor=fee_distributor,
ply_address=ply_address,
ve_address=ve_address,
epoch=epoch,
epoch_end=epoch_end,
emission=emission,
amm_to_gauge_bribe=amm_to_gauge_bribe,
total_amm_votes=total_amm_votes,
token_amm_votes=token_amm_votes,
total_token_votes=total_token_votes,
total_epoch_votes=total_epoch_votes,
)
self.init_type(
sp.TRecord(
core_factory=sp.TAddress,
fee_distributor=sp.TAddress,
ply_address=sp.TAddress,
ve_address=sp.TAddress,
epoch=sp.TNat,
epoch_end=sp.TBigMap(sp.TNat, sp.TTimestamp),
emission=sp.TRecord(base=sp.TNat, real=sp.TNat, genesis=sp.TNat),
amm_to_gauge_bribe=sp.TBigMap(sp.TAddress, Types.AMM_TO_GAUGE_BRIBE),
total_amm_votes=sp.TBigMap(Types.TOTAL_AMM_VOTES_KEY, sp.TNat),
token_amm_votes=sp.TBigMap(Types.TOKEN_AMM_VOTES_KEY, sp.TNat),
total_token_votes=sp.TBigMap(Types.TOTAL_TOKEN_VOTES_KEY, sp.TNat),
total_epoch_votes=sp.TBigMap(sp.TNat, sp.TNat),
)
)
@sp.entry_point
def next_epoch(self):
# Reject tez
sp.verify(sp.amount == sp.tez(0), Errors.ENTRYPOINT_DOES_NOT_ACCEPT_TEZ)
with sp.if_(self.data.epoch == 0):
# Calculate timestamp rounded to nearest whole week, based on Unix epoch - 12 AM UTC, Thursday
now_ = sp.as_nat(sp.now - sp.timestamp(0))
ts_ = sp.compute(((now_ + WEEK) // WEEK) * WEEK)
# Set genesis for emission
self.data.emission.genesis = ts_
self.data.epoch += 1
self.data.epoch_end[self.data.epoch] = sp.timestamp(0).add_seconds(sp.to_int(ts_))
with sp.else_():
# Verify that previous epoch is over
sp.verify(sp.now > self.data.epoch_end[self.data.epoch], Errors.PREVIOUS_EPOCH_YET_TO_END)
now_ = sp.as_nat(sp.now - sp.timestamp(0))
rounded_now = sp.compute((now_ // WEEK) * WEEK)
# Fetch total supply of PLY
ply_total_supply = sp.compute(
sp.view(
"get_total_supply",
self.data.ply_address,
sp.unit,
sp.TNat,
).open_some(Errors.INVALID_VIEW)
)
# Fetch PLY supply locked up in vote escrow
ply_locked_supply = sp.compute(
sp.view(
"get_locked_supply",
self.data.ve_address,
sp.unit,
sp.TNat,
).open_some(Errors.INVALID_VIEW)
)
# Store as local variable to keep on stack
current_emission = sp.compute(self.data.emission)
# Calculate decrease offset based on locked supply ratio
emission_offset = ((current_emission.base * ply_locked_supply) // ply_total_supply) // EMISSION_FACTOR
# Calculate real emission for the epoch that just ended
real_emission = sp.as_nat(current_emission.base - emission_offset)
self.data.emission.real = real_emission
# Calculate growth due to the emission
growth = (real_emission * PRECISION) // ply_total_supply
lockers_inflation = sp.compute((growth * ply_locked_supply) // (PRECISION * EMISSION_FACTOR))
# Mint required number of PLY tokens (lockers inflation) for VoteEscrow
c = sp.contract(
sp.TRecord(address=sp.TAddress, value=sp.TNat),
self.data.ply_address,
"mint",
).open_some()
sp.transfer(
sp.record(address=self.data.ve_address, value=lockers_inflation),
sp.tez(0),
c,
)
# Inflate lockers proportionally
c = sp.contract(
sp.TRecord(epoch=sp.TNat, value=sp.TNat).layout(("epoch", "value")),
self.data.ve_address,
"add_inflation",
).open_some()
sp.transfer(
sp.record(epoch=self.data.epoch, value=lockers_inflation),
sp.tez(0),
c,
)
# Adjust base emission value based on emission drop
with sp.if_((rounded_now - current_emission.genesis) == (4 * WEEK)):
self.data.emission.base = (current_emission.base * INITIAL_DROP) // (100 * DROP_GRANULARITY)
with sp.if_(((rounded_now - current_emission.genesis) % YEAR) == 0):
self.data.emission.base = (current_emission.base * YEARLY_DROP) // (100 * DROP_GRANULARITY)
# Update weekly epoch
self.data.epoch += 1
self.data.epoch_end[self.data.epoch] = self.data.epoch_end[sp.as_nat(self.data.epoch - 1)].add_seconds(WEEK)
# NOTE: This is called only once during origination sequence
@sp.entry_point
def set_factory_and_fee_dist(self, params):
sp.set_type(params, sp.TRecord(factory=sp.TAddress, fee_dist=sp.TAddress))
with sp.if_(self.data.core_factory == Addresses.DUMMY):
self.data.core_factory = params.factory
self.data.fee_distributor = params.fee_dist
# NOTE: This is tested in CoreFactory
@sp.entry_point
def add_amm(self, params):
sp.set_type(params, Types.ADD_AMM_PARAMS)
# Verify that the sender is the core factory
sp.verify(sp.sender == self.data.core_factory, Errors.NOT_AUTHORISED)
# Add to storage
self.data.amm_to_gauge_bribe[params.amm] = sp.record(
gauge=params.gauge,
bribe=params.bribe,
)
# NOTE: This is tested in CoreFactory
@sp.entry_point
def remove_amm(self, amm):
sp.set_type(amm, sp.TAddress)
# Verify that the sender is the core factory
sp.verify(sp.sender == self.data.core_factory, Errors.NOT_AUTHORISED)
# Delete AMM from storage
del self.data.amm_to_gauge_bribe[amm]
@sp.entry_point
def vote(self, params):
sp.set_type(params, Types.VOTE_PARAMS)
# Reject tez
sp.verify(sp.amount == sp.tez(0), Errors.ENTRYPOINT_DOES_NOT_ACCEPT_TEZ)
# Verify that current epoch is not yet over
sp.verify(sp.now <= self.data.epoch_end[self.data.epoch], Errors.EPOCH_ENDED)
# nat version of block timestamp
now_ = sp.as_nat(sp.now - sp.timestamp(0))
# Store as local variable to keep on stack
epoch_ = sp.compute(self.data.epoch)
# Get available voting power for the lock / token-id (rounded to previous whole week)
max_token_voting_power = sp.view(
"get_token_voting_power",
self.data.ve_address,
sp.record(token_id=params.token_id, ts=now_, time=Types.WHOLE_WEEK),
sp.TNat,
).open_some(Errors.INVALID_VIEW)
# Verify that the sender owns the specified token / lock
is_owner = sp.view(
"is_owner",
self.data.ve_address,
sp.record(address=sp.sender, token_id=params.token_id),
sp.TBool,
).open_some(Errors.INVALID_VIEW)
sp.verify(is_owner, Errors.SENDER_DOES_NOT_OWN_LOCK)
# Calculate available voting power for token i.e max power - used up power
used_power = self.data.total_token_votes.get(
sp.record(token_id=params.token_id, epoch=self.data.epoch),
sp.nat(0),
)
power_available = sp.local("power_used", sp.as_nat(max_token_voting_power - used_power))
with sp.for_("vote_item", params.vote_items) as vote_item:
# Verify that the amm being voted on exists in voter i.e whitelisted
sp.verify(self.data.amm_to_gauge_bribe.contains(vote_item.amm), Errors.AMM_INVALID_OR_NOT_WHITELISTED)
# Verify that vote is non-zero
sp.verify(vote_item.votes != 0, Errors.ZERO_VOTE_NOT_ALLOWED)
# Re-votes in the same epoch gets added up
key_ = sp.record(token_id=params.token_id, epoch=epoch_, amm=vote_item.amm)
votes_ = self.data.token_amm_votes.get(key_, 0)
self.data.token_amm_votes[key_] = votes_ + vote_item.votes
# Update total epoch votes for amm
key_ = sp.record(amm=vote_item.amm, epoch=epoch_)
votes_ = self.data.total_amm_votes.get(key_, 0)
self.data.total_amm_votes[key_] = votes_ + vote_item.votes
# Update total epoch votes for token
key_ = sp.record(token_id=params.token_id, epoch=epoch_)
votes_ = self.data.total_token_votes.get(key_, 0)
self.data.total_token_votes[key_] = votes_ + vote_item.votes
# Update total epoch votes as a whole
votes_ = self.data.total_epoch_votes.get(epoch_, 0)
self.data.total_epoch_votes[epoch_] = votes_ + vote_item.votes
# Update power used & verify that available voting power of token has not been overshot
power_available.value = sp.as_nat(
power_available.value - vote_item.votes, Errors.NOT_ENOUGH_VOTING_POWER_AVAILABLE
)
@sp.entry_point
def claim_bribe(self, params):
sp.set_type(params, Types.CLAIM_BRIBE_PARAMS)
# Reject tez
sp.verify(sp.amount == sp.tez(0), Errors.ENTRYPOINT_DOES_NOT_ACCEPT_TEZ)
# Sanity checks
sp.verify(self.data.epoch > params.epoch, Errors.INVALID_EPOCH)
sp.verify(self.data.amm_to_gauge_bribe.contains(params.amm), Errors.AMM_INVALID_OR_NOT_WHITELISTED)
# Verify that the sender owns the specified token / lock
is_owner = sp.view(
"is_owner",
self.data.ve_address,
sp.record(address=sp.sender, token_id=params.token_id),
sp.TBool,
).open_some(Errors.INVALID_VIEW)
sp.verify(is_owner, Errors.SENDER_DOES_NOT_OWN_LOCK)
# Calculate vote share for the vePLY token
token_votes_for_amm = self.data.token_amm_votes.get(
sp.record(token_id=params.token_id, epoch=params.epoch, amm=params.amm),
0,
)
total_votes_for_amm = sp.compute(
self.data.total_amm_votes.get(
sp.record(epoch=params.epoch, amm=params.amm),
0,
)
)
with sp.if_(total_votes_for_amm > 0):
token_vote_share = (token_votes_for_amm * VOTE_SHARE_MULTIPLIER) // total_votes_for_amm
# call the 'claim' entrypoint in bribe contract
param_type = sp.TRecord(
token_id=sp.TNat,
owner=sp.TAddress,
epoch=sp.TNat,
bribe_id=sp.TNat,
vote_share=sp.TNat,
).layout(("token_id", ("owner", ("epoch", ("bribe_id", "vote_share")))))
c = sp.contract(param_type, self.data.amm_to_gauge_bribe[params.amm].bribe, "claim").open_some()
sp.transfer(
sp.record(
token_id=params.token_id,
owner=sp.sender,
epoch=params.epoch,
bribe_id=params.bribe_id,
vote_share=token_vote_share,
),
sp.tez(0),
c,
)
with sp.else_():
# Retutn the bribe to the provider is no votes received by the AMM
c = sp.contract(
sp.TRecord(epoch=sp.TNat, bribe_id=sp.TNat),
self.data.amm_to_gauge_bribe[params.amm].bribe,
"return_bribe",
).open_some()
sp.transfer(
sp.record(
epoch=params.epoch,
bribe_id=params.bribe_id,
),
sp.tez(0),
c,
)
@sp.entry_point
def claim_fee(self, params):
sp.set_type(params, Types.CLAIM_FEE_PARAMS)
# Reject tez
sp.verify(sp.amount == sp.tez(0), Errors.ENTRYPOINT_DOES_NOT_ACCEPT_TEZ)
# Sanity checks
sp.verify(self.data.amm_to_gauge_bribe.contains(params.amm), Errors.AMM_INVALID_OR_NOT_WHITELISTED)
# Verify that the sender owns the specified token / lock
is_owner = sp.view(
"is_owner",
self.data.ve_address,
sp.record(address=sp.sender, token_id=params.token_id),
sp.TBool,
).open_some(Errors.INVALID_VIEW)
sp.verify(is_owner, Errors.SENDER_DOES_NOT_OWN_LOCK)
# Local variable to store through the vote share across the epochs
epoch_vote_shares = sp.local("epoch_vote_shares", sp.list(l=[], t=sp.TRecord(epoch=sp.TNat, share=sp.TNat)))
# Iterate through requested epochs
with sp.for_("epochs", params.epochs) as epoch:
sp.verify(self.data.epoch > epoch, Errors.INVALID_EPOCH)
# Calculate vote share for the vePLY token
token_votes_for_amm = self.data.token_amm_votes[
sp.record(token_id=params.token_id, epoch=epoch, amm=params.amm)
]
total_votes_for_amm = self.data.total_amm_votes[sp.record(epoch=epoch, amm=params.amm)]
token_vote_share = (token_votes_for_amm * VOTE_SHARE_MULTIPLIER) // total_votes_for_amm
epoch_vote_shares.value.push(sp.record(epoch=epoch, share=token_vote_share))
# call the 'claim' entrypoint in Fee Distributor to distribute the fee to the token holder
param_type = sp.TRecord(
token_id=sp.TNat,
owner=sp.TAddress,
amm=sp.TAddress,
epoch_vote_shares=sp.TList(sp.TRecord(epoch=sp.TNat, share=sp.TNat)),
).layout(("token_id", ("owner", ("amm", "epoch_vote_shares"))))
c = sp.contract(param_type, self.data.fee_distributor, "claim").open_some()
sp.transfer(
sp.record(
token_id=params.token_id,
owner=sp.sender,
amm=params.amm,
epoch_vote_shares=epoch_vote_shares.value,
),
sp.tez(0),
c,
)
@sp.entry_point
def pull_amm_fee(self, params):
sp.set_type(params, sp.TRecord(amm=sp.TAddress, epoch=sp.TNat).layout(("amm", "epoch")))
# Reject tez
sp.verify(sp.amount == sp.tez(0), Errors.ENTRYPOINT_DOES_NOT_ACCEPT_TEZ)
# Sanity checks
sp.verify(self.data.epoch > params.epoch, Errors.INVALID_EPOCH)
sp.verify(self.data.amm_to_gauge_bribe.contains(params.amm), Errors.AMM_INVALID_OR_NOT_WHITELISTED)
# Call the 'forwardFee' entrypoint of the amm, passing in fee_distributor & epoch
c = sp.contract(
sp.TRecord(feeDistributor=sp.TAddress, epoch=sp.TNat),
params.amm,
"forwardFee",
).open_some()
sp.transfer(
sp.record(feeDistributor=self.data.fee_distributor, epoch=params.epoch),
sp.tez(0),
c,
)
@sp.entry_point
def recharge_gauge(self, params):
sp.set_type(params, sp.TRecord(amm=sp.TAddress, epoch=sp.TNat).layout(("amm", "epoch")))
# Reject tez
sp.verify(sp.amount == sp.tez(0), Errors.ENTRYPOINT_DOES_NOT_ACCEPT_TEZ)
# Sanity checks
sp.verify(self.data.epoch > params.epoch, Errors.INVALID_EPOCH)
sp.verify(self.data.amm_to_gauge_bribe.contains(params.amm), Errors.AMM_INVALID_OR_NOT_WHITELISTED)
# Calculate the vote share for the amm gauge
total_votes_for_amm = self.data.total_amm_votes[sp.record(epoch=params.epoch, amm=params.amm)]
total_votes_for_epoch = self.data.total_epoch_votes[params.epoch]
amm_vote_share = (total_votes_for_amm * VOTE_SHARE_MULTIPLIER) // total_votes_for_epoch
# Calculate recharge amount based on share
recharge_amount = sp.compute((self.data.emission.real * amm_vote_share) // VOTE_SHARE_MULTIPLIER)
# Mint PLY tokens for the concerned gauge contract
c = sp.contract(
sp.TRecord(address=sp.TAddress, value=sp.TNat),
self.data.ply_address,
"mint",
).open_some()
sp.transfer(
sp.record(address=self.data.amm_to_gauge_bribe[params.amm].gauge, value=recharge_amount),
sp.tez(0),
c,
)
# Call 'recharge' entrypoint in concerned gauge
c_gauge = sp.contract(
sp.TRecord(amount=sp.TNat, epoch=sp.TNat),
self.data.amm_to_gauge_bribe[params.amm].gauge,
"recharge",
).open_some()
sp.transfer(
sp.record(amount=recharge_amount, epoch=params.epoch),
sp.tez(0),
c_gauge,
)
@sp.onchain_view()
def get_current_epoch(self):
sp.result((self.data.epoch, self.data.epoch_end[self.data.epoch]))
@sp.onchain_view()
def get_epoch_end(self, param):
sp.set_type(param, sp.TNat)
sp.result(sp.as_nat(self.data.epoch_end[param] - sp.timestamp(0)))
@sp.onchain_view()
def get_token_amm_votes(self, params):
sp.set_type(params, Types.TOKEN_AMM_VOTES_KEY)
sp.result(self.data.token_amm_votes.get(params, 0))
@sp.onchain_view()
def get_total_amm_votes(self, params):
sp.set_type(params, Types.TOTAL_AMM_VOTES_KEY)
sp.result(self.data.total_amm_votes.get(params, 0))
@sp.onchain_view()
def get_total_token_votes(self, params):
sp.set_type(params, Types.TOTAL_TOKEN_VOTES_KEY)
sp.result(self.data.total_token_votes.get(params, 0))
@sp.onchain_view()
def get_total_epoch_votes(self, param):
sp.set_type(param, sp.TNat)
sp.result(self.data.total_epoch_votes.get(param, 0))
if __name__ == "__main__":
# Test helper
def precision_equal(a, b, dec):
# Check if values are equal after removing precision digits
return (a // dec) == (b // dec)
####################
# vote (valid test)
####################
@sp.add_test(name="vote allows voting across multiple whitelisted AMMs")
def test():
scenario = sp.test_scenario()
# Initialize dummy vote escrow with two tokens of voting powers 100 & 150
ve = VE(powers=sp.big_map(l={1: sp.nat(100), 2: sp.nat(150)}))
# Initialize voter with 3 whitelisted AMMs
voter = Voter(
epoch=1,
epoch_end=sp.big_map(l={1: sp.timestamp(10)}),
amm_to_gauge_bribe=sp.big_map(
l={
Addresses.AMM_1: sp.record(gauge=Addresses.CONTRACT, bribe=Addresses.CONTRACT),
Addresses.AMM_2: sp.record(gauge=Addresses.CONTRACT, bribe=Addresses.CONTRACT),
Addresses.AMM_3: sp.record(gauge=Addresses.CONTRACT, bribe=Addresses.CONTRACT),
}
),
ve_address=ve.address,
)
scenario += ve
scenario += voter
# when ALICE votes for AMM_1 and AMM_2 using token-1
scenario += voter.vote(
sp.record(
token_id=1,
vote_items=sp.list(
[sp.record(amm=Addresses.AMM_1, votes=20), sp.record(amm=Addresses.AMM_2, votes=30)]
),
)
).run(sender=Addresses.ALICE, now=sp.timestamp(5))
# Storage is updated correctly
scenario.verify(voter.data.token_amm_votes[sp.record(token_id=1, amm=Addresses.AMM_1, epoch=1)] == 20)
scenario.verify(voter.data.token_amm_votes[sp.record(token_id=1, amm=Addresses.AMM_2, epoch=1)] == 30)
scenario.verify(voter.data.total_amm_votes[sp.record(amm=Addresses.AMM_1, epoch=1)] == 20)
scenario.verify(voter.data.total_amm_votes[sp.record(amm=Addresses.AMM_2, epoch=1)] == 30)
scenario.verify(voter.data.total_token_votes[sp.record(token_id=1, epoch=1)] == 50)
# when BOB votes for AMM_2 and AMM_3 using token-2
scenario += voter.vote(
sp.record(
token_id=2,
vote_items=sp.list(
[sp.record(amm=Addresses.AMM_2, votes=30), sp.record(amm=Addresses.AMM_3, votes=40)]
),
)
).run(sender=Addresses.BOB, now=sp.timestamp(6))
# Storage is updated correctly
scenario.verify(voter.data.token_amm_votes[sp.record(token_id=2, amm=Addresses.AMM_2, epoch=1)] == 30)
scenario.verify(voter.data.token_amm_votes[sp.record(token_id=2, amm=Addresses.AMM_3, epoch=1)] == 40)
scenario.verify(voter.data.total_amm_votes[sp.record(amm=Addresses.AMM_2, epoch=1)] == 60)
scenario.verify(voter.data.total_amm_votes[sp.record(amm=Addresses.AMM_3, epoch=1)] == 40)
scenario.verify(voter.data.total_token_votes[sp.record(token_id=2, epoch=1)] == 70)
scenario.verify(voter.data.total_epoch_votes[1] == 120)
@sp.add_test(name="vote allows voting multiple times for the same amm using the same token")
def test():
scenario = sp.test_scenario()
# Initialize dummy vote escrow with two tokens of voting powers 100 & 150
ve = VE(powers=sp.big_map(l={1: sp.nat(100), 2: sp.nat(150)}))
# Initialize voter with an AMM
voter = Voter(
epoch=1,
epoch_end=sp.big_map(l={1: sp.timestamp(10)}),
amm_to_gauge_bribe=sp.big_map(
l={
Addresses.AMM_1: sp.record(gauge=Addresses.CONTRACT, bribe=Addresses.CONTRACT),
}
),
ve_address=ve.address,
)
scenario += ve
scenario += voter
# when ALICE votes for AMM_1 twice
scenario += voter.vote(
sp.record(
token_id=1,
vote_items=sp.list([sp.record(amm=Addresses.AMM_1, votes=20)]),
)
).run(sender=Addresses.ALICE, now=sp.timestamp(5))
scenario += voter.vote(
sp.record(
token_id=1,
vote_items=sp.list([sp.record(amm=Addresses.AMM_1, votes=30)]),
)
).run(sender=Addresses.ALICE, now=sp.timestamp(6))
# Storage is updated correctly
scenario.verify(voter.data.token_amm_votes[sp.record(token_id=1, amm=Addresses.AMM_1, epoch=1)] == 50)
scenario.verify(voter.data.total_amm_votes[sp.record(amm=Addresses.AMM_1, epoch=1)] == 50)
scenario.verify(voter.data.total_token_votes[sp.record(token_id=1, epoch=1)] == 50)
scenario.verify(voter.data.total_epoch_votes[1] == 50)
######################
# vote (failure test)
######################
@sp.add_test(name="vote fails if more than the available token voting power is used")
def test():
scenario = sp.test_scenario()
# Initialize dummy vote escrow with two tokens of voting powers 100 & 150
ve = VE(powers=sp.big_map(l={1: sp.nat(100), 2: sp.nat(150)}))
# Initialize voter with an AMM
voter = Voter(
epoch=1,
epoch_end=sp.big_map(l={1: sp.timestamp(10)}),
amm_to_gauge_bribe=sp.big_map(
l={
Addresses.AMM_1: sp.record(gauge=Addresses.CONTRACT, bribe=Addresses.CONTRACT),
Addresses.AMM_2: sp.record(gauge=Addresses.CONTRACT, bribe=Addresses.CONTRACT),
}
),
ve_address=ve.address,
)
scenario += ve
scenario += voter
# ALICE votes for AMM_1
scenario += voter.vote(
sp.record(
token_id=1,
vote_items=sp.list([sp.record(amm=Addresses.AMM_1, votes=50)]),
)
).run(sender=Addresses.ALICE, now=sp.timestamp(5))
# When ALICE votes for AMM_2 with more than the available votes, the transaction fails
scenario += voter.vote(
sp.record(
token_id=1,
vote_items=sp.list([sp.record(amm=Addresses.AMM_2, votes=60)]),
)
).run(
sender=Addresses.ALICE,
now=sp.timestamp(6),
valid=False,
exception=Errors.NOT_ENOUGH_VOTING_POWER_AVAILABLE,
)
@sp.add_test(name="vote fails if zero vote is deposited for a certain amm")
def test():
scenario = sp.test_scenario()
# Initialize dummy vote escrow with two tokens of voting powers 100 & 150
ve = VE(powers=sp.big_map(l={1: sp.nat(100), 2: sp.nat(150)}))
# Initialize voter with an AMM
voter = Voter(
epoch=1,
epoch_end=sp.big_map(l={1: sp.timestamp(10)}),
amm_to_gauge_bribe=sp.big_map(
l={
Addresses.AMM_1: sp.record(gauge=Addresses.CONTRACT, bribe=Addresses.CONTRACT),
Addresses.AMM_2: sp.record(gauge=Addresses.CONTRACT, bribe=Addresses.CONTRACT),
}
),
ve_address=ve.address,
)
scenario += ve
scenario += voter
# When ALICE puts zero vote for AMM_1, the txn fails
scenario += voter.vote(
sp.record(
token_id=1,
vote_items=sp.list([sp.record(amm=Addresses.AMM_1, votes=0)]),
)
).run(
sender=Addresses.ALICE,
now=sp.timestamp(5),
valid=False,
exception=Errors.ZERO_VOTE_NOT_ALLOWED,
)
@sp.add_test(name="vote fails if epoch has already ended")
def test():
scenario = sp.test_scenario()
# Initialize dummy vote escrow with two tokens of voting powers 100 & 150
ve = VE(powers=sp.big_map(l={1: sp.nat(100), 2: sp.nat(150)}))
# Initialize voter with an AMM
voter = Voter(
epoch=1,
epoch_end=sp.big_map(l={1: sp.timestamp(10)}),
amm_to_gauge_bribe=sp.big_map(
l={
Addresses.AMM_1: sp.record(gauge=Addresses.CONTRACT, bribe=Addresses.CONTRACT),
Addresses.AMM_2: sp.record(gauge=Addresses.CONTRACT, bribe=Addresses.CONTRACT),
}
),
ve_address=ve.address,
)
scenario += ve
scenario += voter
# When ALICE votes after the epoch has ended, txn fails
scenario += voter.vote(
sp.record(
token_id=1,
vote_items=sp.list([sp.record(amm=Addresses.AMM_1, votes=0)]),
)
).run(
sender=Addresses.ALICE,
now=sp.timestamp(11),
valid=False,
exception=Errors.EPOCH_ENDED,
)
##########################
# next_epoch (valid test)
##########################
@sp.add_test(name="next_epoch correctly updates first epoch")
def test():
scenario = sp.test_scenario()
voter = Voter()
scenario += voter
# When next_epoch is called for the first time
scenario += voter.next_epoch().run(now=sp.timestamp(8 * DAY))
# Storage is updated correctly
scenario.verify(voter.data.epoch == 1)
scenario.verify(voter.data.epoch_end[1] == sp.timestamp(2 * WEEK))
scenario.verify(voter.data.emission.genesis == 2 * WEEK)
@sp.add_test(name="next_epoch correctly updates epoch during first emission drop")
def test():
scenario = sp.test_scenario()
# Initialize vote escrow with locked supply of 1,00,000 tokens
ve = VE(locked_supply=sp.nat(1_000_000 * DECIMALS))
# Initialize Ply token with total supply 4,00,000 tokens
ply = Ply(total_supply=sp.nat(4_000_000 * DECIMALS))
voter = Voter(
epoch=5,
epoch_end=sp.big_map(l={5: sp.timestamp(6 * WEEK)}),
emission=sp.record(
base=INITIAL_EMISSION,
real=sp.nat(0),
genesis=2 * WEEK,
),
ve_address=ve.address,
ply_address=ply.address,
)
scenario += ve
scenario += ply
scenario += voter
# When next_epoch is called at the end of 4 weeks of emission
scenario += voter.next_epoch().run(now=sp.timestamp(6 * WEEK + 124)) # random offset for testing
# Epoch is updated correctly
scenario.verify(voter.data.epoch == 6)
scenario.verify(voter.data.epoch_end[6] == sp.timestamp(7 * WEEK))
# Predicted values
emission_offset = 375_000 * DECIMALS
real_emission = 2_625_000 * DECIMALS
base_emission = 2_000_000 * DECIMALS
# Emission values are updated correctly
scenario.verify(precision_equal(voter.data.emission.base, base_emission, DECIMALS))
scenario.verify(precision_equal(voter.data.emission.real, real_emission, DECIMALS))
# Predicted locker inflation
locker_inflation = 328_125 * DECIMALS
# Correct inflation is record
scenario.verify(precision_equal(ve.data.inflation, locker_inflation, DECIMALS))
# When next epoch is called again at the end of 7 Weeks
scenario += voter.next_epoch().run(now=sp.timestamp(7 * WEEK + 73))
# Epoch is updated correctly
scenario.verify(voter.data.epoch == 7)
scenario.verify(voter.data.epoch_end[7] == sp.timestamp(8 * WEEK))
# Predicted values
emission_offset = 250_000 * DECIMALS
real_emission = 1_750_000 * DECIMALS
# Emission values are updated correctly
scenario.verify(precision_equal(voter.data.emission.base, base_emission, DECIMALS))
scenario.verify(precision_equal(voter.data.emission.real, real_emission, DECIMALS))
# Predicted locker inflation
locker_inflation = 218_750 * DECIMALS
# Correct inflation is record
scenario.verify(precision_equal(ve.data.inflation, locker_inflation, DECIMALS))
@sp.add_test(name="next_epoch correctly updates epoch during yearly emission drop")
def test():
scenario = sp.test_scenario()
# Initialize vote escrow with locked supply of 1,00,000 tokens
ve = VE(locked_supply=sp.nat(1_000_000 * DECIMALS))
# Initialize Ply token with total supply 4,00,000 tokens
ply = Ply(total_supply=sp.nat(4_000_000 * DECIMALS))
voter = Voter(
epoch=53,
epoch_end=sp.big_map(l={53: sp.timestamp(54 * WEEK)}),
emission=sp.record(
base=2_000_000 * DECIMALS,
real=sp.nat(0),
genesis=2 * WEEK,
),
ve_address=ve.address,
ply_address=ply.address,
)
scenario += ve
scenario += ply
scenario += voter
# When next epoch is called at the end of 1st complete year
scenario += voter.next_epoch().run(now=sp.timestamp(54 * WEEK + 24))
# Epoch is updated correctly
scenario.verify(voter.data.epoch == 54)
scenario.verify(voter.data.epoch_end[54] == sp.timestamp(55 * WEEK))
# Predicted values
emission_offset = 250_000 * DECIMALS
real_emission = 1_750_000 * DECIMALS
base_emission = 1_414_213 * DECIMALS
# Emission values are updated correctly
scenario.verify(precision_equal(voter.data.emission.base, base_emission, DECIMALS))
scenario.verify(precision_equal(voter.data.emission.real, real_emission, DECIMALS))
# Predicted locker inflation
locker_inflation = 218_750 * DECIMALS
# Correct inflation is record
scenario.verify(precision_equal(ve.data.inflation, locker_inflation, DECIMALS))
############################
# next_epoch (failure test)
############################
@sp.add_test(name="next_epoch fails if called before current epoch is over")
def test():
scenario = sp.test_scenario()
voter = Voter(
epoch_end=sp.big_map(
l={
1: sp.timestamp(5),
}
),
epoch=sp.nat(1),
)
scenario += voter
# When next_epoch is called before the current epoch is over, the txn fails
scenario += voter.next_epoch().run(
now=sp.timestamp(4),
valid=False,
exception=Errors.PREVIOUS_EPOCH_YET_TO_END,
)
##########################