-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontract.py
1111 lines (961 loc) · 34 KB
/
contract.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 typing
from algopy import (
ARC4Contract,
Account,
Bytes,
Global,
OnCompleteAction,
TemplateVar,
Txn,
UInt64,
arc4,
itxn,
op,
subroutine,
)
from contract_mab import calculate_mab_pure
from utils import require_payment, get_available_balance, close_offline_on_delete
Bytes32: typing.TypeAlias = arc4.StaticArray[arc4.Byte, typing.Literal[32]]
Bytes64: typing.TypeAlias = arc4.StaticArray[arc4.Byte, typing.Literal[64]]
class PartKeyInfo(arc4.Struct):
address: arc4.Address
vote_key: Bytes32
selection_key: Bytes32
vote_first: arc4.UInt64
vote_last: arc4.UInt64
vote_key_dilution: arc4.UInt64
state_proof_key: Bytes64
class MessagePartKeyInfo(arc4.Struct):
who: arc4.Address
partkey: PartKeyInfo
##################################################
# Ownable
# allows contract to be owned
##################################################
class OwnableInterface(ARC4Contract):
"""
Interface for all abimethods operated by owner.
"""
def __init__(self) -> None:
self.owner = Account()
@arc4.abimethod
def transfer(self, new_owner: arc4.Address) -> None:
"""
Transfer ownership of the contract to a new owner.
"""
pass
class Ownable(OwnableInterface):
def __init__(self) -> None:
super().__init__()
@arc4.abimethod
def transfer(self, new_owner: arc4.Address) -> None:
assert Txn.sender == self.owner, "must be owner"
self.owner = new_owner.native
##################################################
# Fundable
# allows contract to be funded
##################################################
class FundableInterface(ARC4Contract):
"""
Interface for all methods called by funder.
This class defines the basic interface for all types of vehicles,
including methods for starting and stopping the engine. Subclasses
must implement these methods to provide concrete behavior
Global State:
- funder, who funded the contract
- funding, when funded
"""
def __init__(self) -> None:
self.funder = Account()
self.funding = UInt64()
self.total = UInt64()
@arc4.abimethod
def fill(self) -> None:
"""
Add funds to the contract and increments total.
"""
pass
@arc4.abimethod
def set_funding(self, funding: arc4.UInt64) -> None:
"""
Extend the funding period. Should be called before the funding deadline.
Should not allow setting the funding deadline back. Can be called multiple
times.
"""
pass
@arc4.abimethod(allow_actions=[OnCompleteAction.DeleteApplication])
def abort_funding(self) -> None:
"""
Abort funding. Must be called when funding not initialized.
"""
pass
class Fundable(FundableInterface):
def __init__(self) -> None:
super().__init__()
@arc4.abimethod
def fill(self) -> None:
#########################################
assert Txn.sender == self.funder, "must be funder"
#########################################
payment_amount = require_payment(self.funder)
assert payment_amount >= UInt64(0), "payment amount accurate"
#########################################
self.total = self.total + payment_amount
@arc4.abimethod
def set_funding(self, funding: arc4.UInt64) -> None:
#########################################
assert Txn.sender == self.funder, "must be funder"
#########################################
assert (
self.funding == UInt64(0) or self.funding > Global.latest_timestamp
), "funding not be initialized or can be extended"
#########################################
self.funding = funding.native
@arc4.abimethod(allow_actions=[OnCompleteAction.DeleteApplication])
def abort_funding(self) -> None:
#########################################
assert Txn.sender == self.funder, "must be funder"
#########################################
assert self.funding == UInt64(0)
#########################################
close_offline_on_delete(self.funder)
##################################################
# Stakeable
# allows contract to participate in consensus,
# stake
##################################################
class StakeableInterface(ARC4Contract):
"""
Interface for all abimethods of stakeable contract.
"""
def __init__(self) -> None:
self.delegate = Account()
self.stakeable = bool(1)
@arc4.abimethod
def set_delegate(self, delegate: arc4.Address) -> None:
"""
Set delegate.
"""
pass
@arc4.abimethod
def participate(
self,
vote_k: Bytes32,
sel_k: Bytes32,
vote_fst: arc4.UInt64,
vote_lst: arc4.UInt64,
vote_kd: arc4.UInt64,
sp_key: Bytes64,
) -> None:
"""
Participate in consensus.
"""
pass
class Stakeable(StakeableInterface, OwnableInterface):
def __init__(self) -> None:
# ownable state
self.owner = Account()
# stakeable state
self.delegate = Account() # zero address
self.stakeable = bool(1) # 1 (Default unlocked)
@arc4.abimethod
def set_delegate(self, delegate: arc4.Address) -> None:
assert Txn.sender == self.owner, "must be owner"
self.delegate = delegate.native
@arc4.abimethod
def participate(
self,
vote_k: Bytes32,
sel_k: Bytes32,
vote_fst: arc4.UInt64,
vote_lst: arc4.UInt64,
vote_kd: arc4.UInt64,
sp_key: Bytes64,
) -> None:
###########################################
assert (
Txn.sender == self.owner or Txn.sender == self.delegate
), "must be owner or delegate"
###########################################
key_reg_fee = Global.min_txn_fee
# require payment of min fee to prevent draining
assert require_payment(Txn.sender) == key_reg_fee, "payment amout accurate"
###########################################
itxn.KeyRegistration(
vote_key=vote_k.bytes,
selection_key=sel_k.bytes,
vote_first=vote_fst.native,
vote_last=vote_lst.native,
vote_key_dilution=vote_kd.native,
state_proof_key=sp_key.bytes,
fee=key_reg_fee,
).submit()
##################################################
# Deleteable
# allows contract to be deleted
##################################################
class DeleteableInterface(ARC4Contract):
"""
Interface for all abimethods of deletable contract.
"""
def __init__(self) -> None:
self.deletable = bool(1)
@arc4.abimethod
def on_delete(self) -> None:
"""
Delete the contract.
"""
pass
class Deleteable(DeleteableInterface):
def __init__(self) -> None:
super().__init__()
@arc4.baremethod(allow_actions=["DeleteApplication"])
def on_delete(self) -> None:
##########################################
# WARNING: This app can be deleted by the creator (Development)
##########################################
assert Txn.sender == Global.creator_address, "must be creator"
assert self.deletable == UInt64(1), "not approved"
##########################################
##################################################
# Upgradeable
# allows contract to be updated
##################################################
class UpgradeableInterface(ARC4Contract):
"""
Interface for all abimethods of upgradeable contract.
"""
def __init__(self) -> None:
self.contract_version = UInt64()
self.deployment_version = UInt64()
self.updatable = bool(1)
@arc4.abimethod
def set_version(
self, contract_version: arc4.UInt64, deployment_version: arc4.UInt64
) -> None:
"""
Set contract and deployment version.
"""
pass
@arc4.abimethod
def on_update(self) -> None:
"""
On update.
"""
pass
@arc4.abimethod
def approve_update(self, approval: arc4.Bool) -> None:
"""
Approve update.
"""
pass
class Upgradeable(UpgradeableInterface, OwnableInterface):
def __init__(self) -> None:
# ownable state
self.owner = Account()
# upgradeable state
self.contract_version = UInt64()
self.deployment_version = UInt64()
self.updatable = bool(1)
@arc4.abimethod
def set_version(
self, contract_version: arc4.UInt64, deployment_version: arc4.UInt64
) -> None:
assert Txn.sender == Global.creator_address, "must be creator"
self.contract_version = contract_version.native
self.deployment_version = deployment_version.native
@arc4.baremethod(allow_actions=["UpdateApplication"])
def on_update(self) -> None:
##########################################
# WARNING: This app can be updated by the creator
##########################################
assert Txn.sender == Global.creator_address, "must be creator"
assert self.updatable == UInt64(1), "not approved"
##########################################
##############################################
# function: approve_update
# arguments:
# - approval, approval status
# purpose: approve update
# pre-conditions
# - only callable by owner
# post-conditions:
# - updatable set to approval
##############################################
@arc4.abimethod
def approve_update(self, approval: arc4.Bool) -> None:
assert Txn.sender == self.owner, "must be owner"
self.updatable = approval.native
##############################################
##################################################
# Lockable
# allows contract to be lock network tokens
##################################################
class LockableInterface(ARC4Contract):
"""
Interface for all methods of lockable contracts.
This class defines the basic interface for all types of lockable contracts.
Subclasses must implement these methods to provide concrete behavior
Global State:
- period, lockup period
- initial, initial balance
- deadline, deadline for lockup period configuration
- period_seconds, seconds in a period
- lockup_delay, lockup delay
- vesting_delay, vesting delay
- period_limit, period limit
"""
def __init__(self) -> None:
self.period = UInt64() # 0
self.initial = UInt64() # 0
self.deadline = UInt64() # 0
self.period_seconds = TemplateVar[UInt64]("PERIOD_SECONDS") # ex) 2592000
self.lockup_delay = TemplateVar[UInt64]("LOCKUP_DELAY") # ex) 12
self.vesting_delay = TemplateVar[UInt64]("VESTING_DELAY") # ex) 12
self.period_limit = TemplateVar[UInt64]("PERIOD_LIMIT") # ex) 5
self.distribution_count = TemplateVar[UInt64]("DISTRIBUTION_COUNT") # ex) 12
self.distribution_seconds = TemplateVar[UInt64](
"DISTRIBUTION_SECONDS"
) # ex) 2592000
@arc4.abimethod
def preconfigure(self, period: arc4.UInt64, deadline: arc4.UInt64) -> None:
"""
Preconfigure lockup period and deadline.
"""
pass
@arc4.abimethod
def set_vesting_delay(self, vesting_delay: arc4.UInt64) -> None:
"""
Set vesting delay. Should be called by creator.
"""
pass
@arc4.abimethod
def set_total(self, funding: arc4.UInt64) -> None:
"""
Set total funding. Should be called by creator.
"""
pass
@arc4.abimethod
def setup(
self, owner: arc4.Address, funder: arc4.Address, initial: arc4.UInt64
) -> None:
"""
Setup lockup. Should be called by creator.
"""
pass
@arc4.abimethod
def configure(self, period: arc4.UInt64) -> None:
"""
Configure lockup period. Should be called by owner.
"""
pass
@arc4.abimethod
def withdraw(self, amount: arc4.UInt64) -> UInt64:
"""
Withdraw funds from contract. Should be called by owner.
"""
return UInt64()
@arc4.abimethod
def close(self) -> None:
"""
Close contract. Should be called by owner or funder.
"""
pass
@subroutine
def calculate_min_balance(self) -> UInt64:
"""
Calculate minimum balance.
"""
return UInt64()
##################################################
# Lockable
# allows contract to be lock network tokens
# State:
# hot state
# - funder, who funded the contract
# - period, lockup period
# - funding, when funded
# - total, total amount funded
# - initial, initial balance
# - deadline, funding deadline
# warm state
# - vesting_delay, vesting delay
# periods from funding until lockup
# vesting delay in early staking rewards
# depends on period
# ex)
# vd = (p + 1) 2 / 3
# , where p is between 1 and 18
# otherwise cold
# cold state
# - parent_id, parent id
# - period_seconds, period seconds
# - lockup_delay, lockup delay
# - period_limit, period limit
##################################################
class Lockable(LockableInterface, OwnableInterface, FundableInterface):
def __init__(self) -> None:
# ownable state
self.owner = Account()
# lockable state
self.period = UInt64()
self.initial = UInt64()
self.deadline = UInt64()
self.period_seconds = TemplateVar[UInt64]("PERIOD_SECONDS")
self.lockup_delay = TemplateVar[UInt64]("LOCKUP_DELAY")
self.vesting_delay = TemplateVar[UInt64]("VESTING_DELAY")
self.period_limit = TemplateVar[UInt64]("PERIOD_LIMIT")
self.distribution_count = TemplateVar[UInt64]("DISTRIBUTION_COUNT")
self.distribution_seconds = TemplateVar[UInt64]("DISTRIBUTION_SECONDS")
# prevent division by zero in calculate_mab_pure
assert self.distribution_seconds > 0, "distribution seconds must be positive"
# fundable state
self.funder = Account()
self.funding = UInt64()
self.total = UInt64()
@arc4.abimethod
def preconfigure(self, period: arc4.UInt64, deadline: arc4.UInt64) -> None:
"""
Preconfigure lockup period and deadline.
"""
##########################################
assert self.owner == Global.zero_address, "owner not initialized"
assert self.funder == Global.zero_address, "funder not initialized"
##########################################
assert Txn.sender == Global.creator_address, "must be creator"
##########################################
self.period = period.native
self.deadline = deadline.native
@arc4.abimethod
def set_vesting_delay(self, vesting_delay: arc4.UInt64) -> None:
"""
Set vesting delay.
"""
##########################################
assert self.owner == Global.zero_address, "owner not initialized"
assert self.funder == Global.zero_address, "funder not initialized"
##########################################
assert Txn.sender == Global.creator_address, "must be creator"
##########################################
self.vesting_delay = vesting_delay.native
@arc4.abimethod
def set_total(self, funding: arc4.UInt64) -> None:
"""
Set total funding.
"""
##########################################
assert self.owner == Global.zero_address, "owner not initialized"
assert self.funder == Global.zero_address, "funder not initialized"
##########################################
assert Txn.sender == Global.creator_address, "must be creator"
##########################################
self.total = funding.native
@arc4.abimethod
def setup(
self,
owner: arc4.Address,
funder: arc4.Address,
initial: arc4.UInt64,
) -> None:
##########################################
assert self.owner == Global.zero_address, "owner not initialized"
assert self.funder == Global.zero_address, "funder not initialized"
##########################################
assert Txn.sender == Global.creator_address, "must be creator"
##########################################
self.owner = owner.native
self.funder = funder.native
self.initial = initial.native
@arc4.abimethod
def configure(self, period: arc4.UInt64) -> None:
##########################################
assert self.funding == 0, "funding not initialized"
assert self.total == 0, "total not initialized"
##########################################
assert Txn.sender == self.owner, "must be owner"
##########################################
assert period <= TemplateVar[UInt64]("PERIOD_LIMIT")
assert period >= 0
##########################################
assert self.deadline > Global.latest_timestamp, "deadline not passed"
##########################################
self.period = period.native
##############################################
# function: withdraw
# arguments:
# - amount
# returns: min balance
# purpose: extract funds from contract
# pre-conditions
# - only callable by owner
# - let balance be the current balance of the
# contract
# - balance - amount >= min_balance
# (fee paid in appl txn)
# post-conditions:
# - transfer amount from the contract account
# to owner
# notes: costs 2 fees
##############################################
@arc4.abimethod
def withdraw(self, amount: arc4.UInt64) -> UInt64:
"""
Withdraw funds from contract.
"""
##########################################
assert Txn.sender == self.owner, "must be owner"
##########################################
if self.funding > 0:
min_balance = self.calculate_min_balance()
available_balance = get_available_balance()
assert available_balance - amount.native >= min_balance, "balance available"
if amount > 0:
itxn.Payment(amount=amount.native, receiver=Txn.sender, fee=0).submit()
return min_balance
else:
min_balance = self.total
available_balance = get_available_balance()
assert available_balance - amount.native >= min_balance, "balance available"
if amount > 0:
itxn.Payment(amount=amount.native, receiver=Txn.sender, fee=0).submit()
return min_balance
##############################################
# function: close
# purpose: deletes contract
# pre-conditions:
# - min balance is 0
# post-conditions:
# - contract is deleted
# - account closed out to owner if it has a balance
# notes:
# - should be alled with onCompletion
# deleteApplication
##############################################
@arc4.abimethod(allow_actions=[OnCompleteAction.DeleteApplication])
def close(self) -> None:
###########################################
assert self.funding > 0, "funded"
###########################################
assert self.calculate_min_balance() == 0, "min balance not zero"
###########################################
assert (
Txn.sender == self.owner or Txn.sender == self.funder
), "must be owner or funder"
###########################################
close_offline_on_delete(self.owner)
##############################################
# function: min_balance (internal)
# arguments: None
# purpose: calcualte minimum balance
# pre-conditions: None
# post-conditions: None
# notes:
# - let period = number of months to to lockup
# total = total amount intially funded (airdrop + lockup bonus)
# y = vesting delay in months
# p = 1 / (self.period x 12) or 1 / (period)
# - mimumum balance =
# total x min(1, p x max(0, (period - (now() - funding + y x seconds-in-month)) / seconds-in-month))
##############################################
@subroutine
def calculate_min_balance(self) -> UInt64:
now: UInt64 = Global.latest_timestamp
min_balance: UInt64 = calculate_mab_pure(
now,
self.vesting_delay,
self.period_seconds,
self.lockup_delay,
self.period,
self.funding,
self.total,
self.distribution_count,
self.distribution_seconds,
)
return min_balance
##################################################
# Receiver
# reference to messenger
##################################################
class ReceiverInterface(ARC4Contract):
"""
Interface for all abimethods of receiver contract.
"""
def __init__(self) -> None:
self.messenger_id = UInt64()
class Receiver(ReceiverInterface):
def __init__(self) -> None:
self.messenger_id = TemplateVar[UInt64]("MESSENGER_ID")
##################################################
# Messenger
# emits events
##################################################
class MessengerInterface(ARC4Contract):
"""
Interface for all abimethods of messenger contract.
"""
@arc4.abimethod
def partkey_broastcast(
self,
address: arc4.Address,
vote_k: Bytes32,
sel_k: Bytes32,
vote_fst: arc4.UInt64,
vote_lst: arc4.UInt64,
vote_kd: arc4.UInt64,
sp_key: Bytes64,
) -> None:
"""
Broastcast partkey information.
"""
pass
class Messenger(MessengerInterface, Upgradeable):
def __init__(self) -> None:
# upgradeable state
self.contract_version = UInt64() # 0
self.deployment_version = UInt64() # 0
self.updatable = bool(1) # 1 (Default unlocked)
@arc4.abimethod
def partkey_broastcast(
self,
address: arc4.Address,
vote_k: Bytes32,
sel_k: Bytes32,
vote_fst: arc4.UInt64,
vote_lst: arc4.UInt64,
vote_kd: arc4.UInt64,
sp_key: Bytes64,
) -> None:
arc4.emit(
MessagePartKeyInfo(
arc4.Address(Txn.sender),
PartKeyInfo(
address, vote_k, sel_k, vote_fst, vote_lst, vote_kd, sp_key
),
)
)
##################################################
# Deployable
# ensures that contract is created by factory
# and recorded
##################################################
class DeployableInterface(ARC4Contract):
"""
Interface for all abimethods of deployable contract.
"""
def __init__(self) -> None:
self.parent_id = UInt64()
@arc4.abimethod(create="require")
def on_create(self) -> None:
"""
Execute on create.
"""
pass
class Deployable(DeployableInterface):
def __init__(self) -> None:
super().__init__()
@arc4.baremethod(create="require")
def on_create(self) -> None:
caller_id = Global.caller_application_id
assert caller_id > 0, "must be created by factory"
self.parent_id = caller_id
##################################################
# Airdrop
# facilitates airdrop staking
##################################################
class Airdrop(
Lockable, Ownable, Fundable, Deployable, Stakeable, Upgradeable, Receiver
):
def __init__(self) -> None:
# deployable state
self.parent_id = UInt64()
# stakeable state
self.delegate = Account()
self.stakeable = bool(1)
# upgradeable state
self.contract_version = UInt64()
self.deployment_version = UInt64()
self.updatable = bool(1)
# ownable state
self.owner = Account()
# lockable state
self.period = UInt64()
self.initial = UInt64()
self.deadline = UInt64()
self.period_seconds = TemplateVar[UInt64]("PERIOD_SECONDS")
self.lockup_delay = TemplateVar[UInt64]("LOCKUP_DELAY")
self.vesting_delay = TemplateVar[UInt64]("VESTING_DELAY")
self.period_limit = TemplateVar[UInt64]("PERIOD_LIMIT")
self.distribution_count = TemplateVar[UInt64]("DISTRIBUTION_COUNT")
self.distribution_seconds = TemplateVar[UInt64]("DISTRIBUTION_SECONDS")
# fundable state
self.funder = Account()
self.funding = UInt64()
self.total = UInt64()
# receiver state
self.messenger_id = TemplateVar[UInt64]("MESSENGER_ID")
# override fundable abort_funding abimethod
# close offline on delete to owner
@arc4.abimethod(allow_actions=[OnCompleteAction.DeleteApplication])
def abort_funding(self) -> None:
##########################################
assert (
Txn.sender == self.funder or Txn.sender == self.owner
), "must be funder or owner"
##########################################
assert self.funding == UInt64(0)
##########################################
close_offline_on_delete(self.owner)
##################################################
# BaseFactory
# factory for airdrop also serves as a base for
# upgrading contracts if applicable
##################################################
class BaseFactory(Upgradeable):
"""
Base factory for all factories.
"""
def __init__(self) -> None:
"""
Initialize factory.
"""
# upgradeable state
self.contract_version = UInt64() # 0
self.deployment_version = UInt64() # 0
self.updatable = bool(1) # 1 (Default unlocked)
##############################################
# @arc4.abimethod
# def update(self) -> None:
# pass
##############################################
# @arc4.abimethod
# def remote_update(self, app_id: arc4.UInt64) -> None:
# pass
##############################################
# @arc4.abimethod
# def create(self, *args) -> UInt64:
# return UInt64()
##############################################
@subroutine
def get_initial_payment(self) -> UInt64:
"""
Get initial payment.
"""
payment_amount = require_payment(Txn.sender)
mbr_increase = UInt64(677500)
min_balance = op.Global.min_balance # 100000
assert (
payment_amount >= mbr_increase + min_balance
), "payment amount accurate" # 777500
initial = payment_amount - mbr_increase - min_balance
return initial
##################################################
# AirdropFactory
# factory for airdrop also serves as a base for
# upgrading contracts if applicable
##################################################
class AirdropFactory(BaseFactory):
"""
Factory for airdrop requiring lockup period
configuration and funding.
"""
def __init__(self) -> None:
super().__init__()
@arc4.abimethod
def create(
self,
owner: arc4.Address,
funder: arc4.Address,
deadline: arc4.UInt64,
initial: arc4.UInt64,
) -> UInt64:
"""
Create airdrop.
Arguments:
- owner, who is the beneficiary
- funder, who funded the contract
- deadline, funding deadline
- initial, initial funded value not including lockup bonus
Returns:
- app id
"""
##########################################
self.get_initial_payment()
##########################################
base_app = arc4.arc4_create(Airdrop).created_app
arc4.abi_call(Airdrop.preconfigure, UInt64(0), deadline, app_id=base_app)
itxn.Payment(
receiver=base_app.address, amount=op.Global.min_balance, fee=0 # 100000
).submit()
arc4.abi_call(Airdrop.setup, owner, funder, initial, app_id=base_app)
# configured by owner
# funder
# fill
# set funding
# vesting
# lockup
# done
##########################################
return base_app.id
##################################################
# VanillaFactory
# factory for staking contract vesting and lockup
##################################################
# class VanillaFactory(BaseFactory):
# """
# Factory for staking contract without vesting and lockup.
# """
# def __init__(self) -> None:
# super().__init__()
# @arc4.abimethod
# def create(self, delegate: arc4.Address) -> UInt64:
# ##########################################
# owner = Txn.sender
# initial = self.get_initial_payment()
# ##########################################
# base_app = arc4.arc4_create(Airdrop).created_app
# itxn.Payment(
# receiver=base_app.address,
# amount=initial + op.Global.min_balance, # initial + 100000
# fee=0,
# ).submit()
# arc4.abi_call(
# Airdrop.preconfigure, UInt64(0), Global.latest_timestamp, app_id=base_app
# )
# arc4.abi_call(Airdrop.set_delegate, delegate, app_id=base_app)
# arc4.abi_call(Airdrop.set_total, initial, app_id=base_app)
# arc4.abi_call(
# Airdrop.setup,
# owner, # owner
# Global.current_application_address, # funder
# initial,
# app_id=base_app,
# )
# arc4.abi_call(Airdrop.set_funding, Global.latest_timestamp, app_id=base_app)
# # withdraw particpate
# # close
# ##########################################
# return base_app.id
##################################################
# StakeRewardFactory
# factory for stake reward
##################################################
# class StakeRewardFactory(BaseFactory):
# """
# Factory for stake reward.
# """
# def __init__(self) -> None:
# super().__init__()
# @arc4.abimethod
# def create(
# self,
# owner: arc4.Address,
# funder: arc4.Address,
# delegate: arc4.Address,
# period: arc4.UInt64,
# ) -> UInt64:
# """
# Create stake reward.
# Arguments: