-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkadena-stake-fungiv2-vesting.pact
1468 lines (1347 loc) · 70.3 KB
/
kadena-stake-fungiv2-vesting.pact
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
;(enforce-pact-version "3.7")
(namespace (read-msg 'ns))
(module kadena-stake-fungiv2-vesting GOVERNANCE "Stake Factory Fungible-v2 Vesting Pool"
;\|/ (__) factory-stake-vesting-fungiv2
; `\------(oo) creates vesting pools where
; || (__) fungible-v2 tokens are emitted over time
; ||w--|| \|/ to beneficiary accounts
;Stake Factory Fungible-v2 Vesting Pools distribute tokens over time amongst pool members
;;;;; CONSTANTS
(defconst ACCOUNT_ID_CHARSET CHARSET_LATIN1
" Allowed character set for Account IDs. ")
(defconst ACCOUNT_ID_MIN_LENGTH 3
" Minimum character length for account IDs. ")
(defconst ACCOUNT_ID_MAX_LENGTH 256
" Maximum character length for account IDs. ")
(defconst NAME_MIN_LENGTH 3
" Minimum character length for account IDs. ")
(defconst NAME_MAX_LENGTH 30
" Maximum character length for account IDs. ")
;;;;; CAPABILITIES
(defcap GOVERNANCE ()
@doc " Give the admin full access to call and upgrade the module. "
(enforce-keyset 'admin-kadena-stake)
)
(defcap ACCOUNT_GUARD(account:string)
@doc "Verifies account meets format and belongs to caller"
(enforce-guard
(at "guard" (coin.details account))
)
)
(defcap POOL_CREATOR_GUARD(pool-id:string)
@doc "Verifies account belongs to pool creator"
(let
(
(pool-data (read pools pool-id ["account"]))
)
(enforce-guard
(at "guard" (coin.details (at "account" pool-data) ))
)
)
)
(defcap PRIVATE_RESERVE
(id:string pair-key:string)
true)
(defun enforce-private-reserve:bool
(id:string pair-key:string)
(require-capability (PRIVATE_RESERVE id pair-key)))
(defun create-pool-guard:guard
(id:string pair-key:string)
(create-user-guard (enforce-private-reserve id pair-key )))
;;;;;;;;;; SCHEMAS AND TABLES ;;;;;;;;;;;;;;
(defschema pools-schema
@doc "Pool information, a unique id is the key"
id:string
name:string
description:string
pool_balance:decimal
reward-token:module{fungible-v2}
account:string
active:bool
max-reward-per-account:decimal
claim-wait-seconds:decimal
start-time:time
reward-duration:decimal
reward-amount:decimal
stakers:decimal
withdraw-duration:decimal
start-balance:decimal
initialized:bool
end-time:time
locked:bool
)
(defschema pools-usage-schema
@doc "Pool usage data"
tokens-locked:decimal
placeholder-tokens:decimal
last-updated:time
paid:decimal
multiplier:decimal
next-multiplier:decimal
has-vesting-connection:bool
vesting-pool-id:string
)
;list of accounts in pool
(defschema pool-accounts-schema
@doc "key pool-id"
ids:[string]
)
(defschema pools-user-stats-schema
@doc "User total reward earnings in a pool"
total-earned:decimal)
(defschema stakes-schema
@doc "Stores beneficiary account information, key is account + pool name"
id:string
pool-id:string
balance:decimal
last-updated:time
account:string
rewards:decimal
last-claimed:time
withdraw-duration:decimal
multiplier:decimal
start-time:time
max-rewards:decimal
)
(deftable pools:{pools-schema})
(deftable pools-usage:{pools-usage-schema})
(deftable pool-user-stats:{pools-user-stats-schema})
(deftable stakes:{stakes-schema})
(deftable pool-accounts:{pool-accounts-schema})
;;;;; Pool Creator Related
;create pool(): for creating a vesting pool where fungible-v2 tokens are emitted to beneficiary accounts
;id: id of pool, ex: "test-pool"
;name: name of pool, ex: "Test Pool"
;balance: amount of distribution token given by pool owner to be distributed to beneficiary accounts, ex: 200
;reward-token: name of fungible-v2 reward token provided by pool creator for emissions, ex: coin
;account: pool creator account, ex: k:mykaccount
;max-reward-per-account: max rewards a beneficiary account can ever claim in the pool, ex: 200
;claim-wait-seconds: minimum number of seconds between beneficary allocation claims, ex: 0
;reward-duration: time it takes for reward amount to become emitted and available to beneficiarys, ex: 86400
;reward-amount: the amount of rewards available each reward-duration, ex: 10
;withdraw-duration: the amount of time in seconds until anyone can begin withdrawing vested tokens from this pool, ex: 0
;stakeables: a list of accounts to add to the pool upon creation, ex [{ "pool-id" : "test-pool", "account" : "k:mykaccount", "amount" : 100.0, "set-withdraw-duration" : true, "withdraw-duration" : 0.0, "max-rewards" : 200.0}]
(defun create-pool (id:string
name:string
description:string
balance:decimal
reward-token:module{fungible-v2}
account:string
max-reward-per-account:decimal
claim-wait-seconds:decimal
reward-duration:decimal
reward-amount:decimal
withdraw-duration:decimal
stakeables:[object:{mintable}] )
@doc "Creates a new vesting pool"
(with-capability (ACCOUNT_GUARD account)
;Enforce rules
(enforce-pool-id id)
(enforce-valid-id account)
(enforce-valid-id id)
(enforce-valid-name name)
(enforce-unit balance (reward-token::precision))
(enforce (>= (length stakeables) 1) "Must add alteast 1 pool member when creating a pool")
;Create reward token account
(reward-token::create-account id (create-pool-guard id (get-token-key reward-token)))
;Transfer reward token
(reward-token::transfer-create account id (create-pool-guard id (get-token-key reward-token)) balance)
;Insert pool
(insert pools id
{
"id": id,
"name": name,
"description": description,
"pool_balance": balance,
"reward-token": reward-token,
"account": account,
"active": true,
"max-reward-per-account": max-reward-per-account,
"claim-wait-seconds": claim-wait-seconds,
"start-time": (at "block-time" (chain-data)),
"reward-duration": reward-duration,
"reward-amount": reward-amount,
"stakers": 0.0,
"withdraw-duration": withdraw-duration,
"start-balance": balance,
"initialized": false,
"end-time": (at "block-time" (chain-data)),
"locked": false
}
)
;Insert pool blank usage
(insert pools-usage id {
"tokens-locked": 0.0,
"placeholder-tokens": 0.0,
"last-updated": (at "block-time" (chain-data)),
"paid": 0.0,
"multiplier": 1.0,
"next-multiplier": 1.0,
"has-vesting-connection": false,
"vesting-pool-id": "none"
})
(mass-stake stakeables)
;Return a message
(format "Created pool {} with {} {}" [name balance reward-token])
)
)
(defun add-balance-to-pool (pool-id:string account:string amount:decimal)
@doc "Adds more tokens to a vesting pool for it to distribute to pool members"
(let
(
(pool-data (read pools pool-id ["account" "reward-token" "pool_balance" "start-balance" "active"]))
)
(let
(
(token:module{fungible-v2} (at "reward-token" pool-data))
)
;Enforce active pool
(enforce (= (at "active" pool-data) true) "You cannot add rewards to exhausted pools.")
;Enforce token precision
(enforce-unit amount (token::precision))
;Transfer token to pool
(token::transfer account pool-id amount)
;Update pool
(update pools pool-id
{
"balance": (+ (at "pool_balance" pool-data) amount),
"start-balance": (+ (at "start-balance" pool-data) amount),
"end-time": (calculate-pool-end-time pool-id false true amount)
}
)
(format "Added {} {} distribution tokens to {}" [amount token pool-id])
)
)
)
(defun deactivate-pool (pool-id:string account:string)
@doc "Deactivates a pool and recovers all undistributed tokens back to pool creator account"
(with-capability (POOL_CREATOR_GUARD pool-id )
(let
(
(pool-data (read pools pool-id))
(pool-usage-data (read pools-usage pool-id))
)
(let
(
(token:module{fungible-v2} (at "reward-token" pool-data))
)
(let
(
(to-pay (token::get-balance pool-id) )
)
;Enforce pool owner
(enforce (= (at "account" pool-data) account) "Access prohibited.")
;Enforce unlocked pool
(enforce (= (at "locked" pool-data) false) "Locked pools may not be recovered.")
;Transfer pool starting stake
(install-capability (token::TRANSFER pool-id account to-pay) )
(with-capability (PRIVATE_RESERVE pool-id (get-token-key token)) (token::transfer pool-id account to-pay) )
;Update pool
(update pools pool-id
(+
{
"active": false,
"pool_balance": (- (at "balance" pool-data) to-pay)
}
pool-data
)
)
;Update pool usage
(update pools-usage pool-id
(+
{
"last-updated": (at "block-time" (chain-data))
}
pool-usage-data
)
)
;Return a message
(format "Returned {} {} and deactivated pool {}" [to-pay token pool-id])
)
)
)
)
)
(defun withdraw-funds (pool-id:string account:string amount:decimal)
@doc "For withdrawing funds from the vesting pool - Pool Creator Only"
(with-capability (POOL_CREATOR_GUARD pool-id )
(let
(
(pool-data (read pools pool-id))
(pool-usage-data (read pools-usage pool-id))
)
(let
(
(token:module{fungible-v2} (at "reward-token" pool-data))
)
(let
(
(canpay:decimal amount)
)
;Enforce pool owner
(enforce (= (at "account" pool-data) account) "Access prohibited.")
;Enforce unlocked pool
(enforce (= (at "locked" pool-data) false) "Locked pools may not be recovered.")
;Transfer amount to account
(install-capability (token::TRANSFER pool-id account canpay) )
(with-capability (PRIVATE_RESERVE pool-id (get-token-key token)) (token::transfer pool-id account canpay) )
;Update pool
(update pools pool-id
(+
{
"pool_balance": (- (at "pool_balance" (read pools pool-id)) canpay),
"start-balance": (- (at "start-balance" pool-data) canpay),
"end-time": (calculate-pool-end-time pool-id false true (* canpay -1.0) )
}
pool-data
)
)
;Return a message
(format "Withdrew {} {} from {} to {}" [canpay token pool-id account])
)
)
)
)
)
(defun lock-pool (pool-id:string account:string)
@doc "Permanently locks a vesting pool from being recovered by pool creator"
(with-capability (POOL_CREATOR_GUARD pool-id )
(let
(
(pool-data (read pools pool-id))
)
;Enforce pool owner
(enforce (= (at "account" pool-data) account) "Access prohibited.")
;Update pool
(update pools pool-id
{
"locked": true
}
)
;Return a message
(format "Locked pool {}" [pool-id])
)
)
)
(defun absorb-new-tokens (pool-id:string)
@doc "Absorbs new tokens from a pools account into the pools distribution schedule"
(let
(
(pool-data (read pools pool-id ["account" "reward-token" "pool_balance" "start-balance" "active"]))
(pool-usage-data (read pools-usage pool-id ["tokens-locked"] ))
)
(let
(
(reward-token:module{fungible-v2} (at "reward-token" pool-data))
)
(let
(
(new-tokens (- (reward-token::get-balance pool-id) (at "pool_balance" pool-data) )
)
)
;Enforce active pool
(enforce (= (at "active" pool-data) true) "You cannot add rewards to exhausted pools.")
;Update pool
(update pools pool-id
{
"pool_balance": (+ (at "pool_balance" pool-data) new-tokens),
"start-balance": (+ (at "start-balance" pool-data) new-tokens),
"end-time": (calculate-pool-end-time pool-id false true new-tokens)
}
)
(format "Absorbed {} reward-tokens into pool {}" [new-tokens pool-id])
)
)
)
)
(defun migrate-vesting-allocations (pool-id:string)
@doc "Migrates vesting pool allocations"
(let
(
(pool-data (read pools pool-id))
(pool-usage-data (read pools-usage pool-id))
)
(claim-vest (at "vesting-pool-id" pool-usage-data) pool-id)
)
)
(defun connect-vesting-pool (pool-id:string vesting-pool-id:string set-active:bool)
@doc "Connects a vesting pool to this staking pool"
(with-capability (POOL_CREATOR_GUARD pool-id)
(update pools-usage pool-id
{
"has-vesting-connection": set-active,
"vesting-pool-id": vesting-pool-id
}
)
)
)
;;;;; User Related
;stakeable schemea
(defschema stakeable
@doc "Mass edit helper object"
pool-id:string
account:string
amount:decimal
set-withdraw-duration:bool
withdraw-duration:decimal
max-rewards:decimal
)
(defun mass-staker (stakeable:object{stakeable})
@doc " Mass edit helper funciton "
(bind stakeable {
"pool-id" := s_poolid,
"account" := s_account,
"amount" := s_amount,
"set-withdraw-duration" := s_setwithdrawduration,
"withdraw-duration" := s_withdrawduration,
"max-rewards" := s_maxrewards
}
(create-stake s_poolid s_account s_amount s_setwithdrawduration s_withdrawduration s_maxrewards)
)
)
;mass-stale: runs multiple create-stake() at once
;useful for setting up an entire pool at once, or adjusting all pool member allocations at once so weights are always balanced properly
(defun mass-stake
(
stakeables:[object:{mintable}]
)
@doc " Mass edit pool "
(map (mass-staker) stakeables )
)
;create-stake: adds account to pool, automatically adjusting other pool members rewards
;pool-id: pool to add account to
;account: account to add to pool
;amount: weight of account in pool - all weights add up to 100% of the pools vest
;set-withdraw-duration: true/false if to update this users withdraw-duration (always set to true at first and set a starting withdraw duration, even if its only 0 seconds)
;withdraw-duration: time in seconds until this user can begin withdrawing, ex: 0 (always set a withdraw duration unless updating the users allocation after it was already set previously)
;max-rewards: maximum number of tokens this account will ever be able to withdraw from this pool
(defun create-stake (
pool-id:string
account:string
amount:decimal
set-withdraw-duration:bool
withdraw-duration:decimal
max-rewards:decimal
)
@doc " Creates or adds more distribution allocations to a pool member account, transfering the accounts current allocations before so if there are any"
(with-capability (POOL_CREATOR_GUARD pool-id )
(if (= (at "has-vesting-connection" (read pools-usage pool-id)) true )
(let
(
(hasVEST true)
)
(migrate-vesting-allocations pool-id)
(absorb-new-tokens pool-id)
)
true
)
(let
(
(pool-data (read pools pool-id))
(stake-id (get-stake-id-key account pool-id))
)
(if (and (= (at "active" pool-data) true) (> (at "pool_balance" pool-data) 0.0) )
(let
(
(pool-usage-data (read pools-usage pool-id))
)
;Claim rewards for this user
(claim pool-id account)
;Enforce active pool
(enforce (= (at "active" pool-data) true) "Vesting pool is not active.")
;Insert stake data
(with-default-read stakes stake-id
{ "id" : stake-id, "pool-id" : pool-id, "balance" : 0.0, "last-updated" : (at "block-time" (chain-data)), "account" : account, "rewards" : 0.0, "last-claimed" : (at "block-time" (chain-data)), "withdraw-duration": withdraw-duration }
{ "id" := t_id, "pool-id" := t_pool-id, "balance" := t_balance, "last-updated" := t_last-updated, "account" := t_account, "rewards" := t_rewards, "last-claimed" := t_last-claimed, "withdraw-duration" := t_withdraw-duration }
;If this is not the first beneficiary, update the pools emissions multiplier
(if (> (at "stakers" pool-data) 0.0)
(update pools-usage pool-id
{
"last-updated": (at "block-time" (chain-data)),
"multiplier": (calculate-multiplier pool-id )
}
)
true
)
;Update pool usage data
(update pools-usage pool-id
{
"tokens-locked": (+ (- (- (at "tokens-locked" pool-usage-data) t_balance ) (at "placeholder-tokens" pool-usage-data) ) amount),
"placeholder-tokens": 0.0,
"last-updated": (if (= (at "active" (read pools pool-id)) true ) (at "block-time" (chain-data)) (at "last-updated" pool-usage-data) )
}
)
;Update user stake information
(write stakes stake-id {
"id": t_id,
"pool-id": t_pool-id,
"balance": amount,
"last-updated": (at "block-time" (chain-data)),
"account": t_account,
"rewards": t_rewards,
"last-claimed": t_last-claimed,
"withdraw-duration": (if (= set-withdraw-duration true) withdraw-duration t_withdraw-duration ),
"multiplier": (if (> (at "stakers" pool-data) 0.0)
(calculate-multiplier pool-id)
(at "multiplier" pool-usage-data)
),
"start-time": (at "block-time" (chain-data)),
"max-rewards": max-rewards
})
;Record account to pool-accounts
(if (= (check-if-account-in-pool t_pool-id account) false)
(with-default-read pool-accounts t_pool-id
{ "ids" : [] }
{ "ids" := t_ids }
(write pool-accounts t_pool-id {"ids": (+ [account] t_ids ) } )
)
true)
;If this pool is not initialized or if all beneficiarys left, set it's start-time + end-time now
(if (or (= (at "initialized" pool-data) false) (= (at "stakers" (read pools pool-id)) 0.0 ) )
(update pools pool-id
{
"initialized": true,
"start-time": (if (> (at "stakers" pool-data) 0.0)
(at "start-time" pool-data)
(at "block-time" (chain-data))
),
"end-time": (if (= (at "stakers" (read pools pool-id)) 0.0 ) (calculate-pool-end-time pool-id false false 0.0) (calculate-pool-end-time pool-id true false 0.0) )
}
)
true
)
;If this pool has no emissions to distribute then we stop and deactivate the pool
(let
(
(time-since-end-time (diff-time (at "block-time" (chain-data)) (at "end-time" (read pools pool-id)) ))
)
(if (>= time-since-end-time 0.0 )
;If pool can not pay this new beneficiary
(let
(
(EXHAUSTED true)
)
(update pools pool-id
{
"active": false
}
)
(format "Pool {} is exhausted and can not distribute allocations any further." [pool-id])
)
;If pool can pay new beneficiary
(let
(
(ALLOCATION (* (at "reward-amount" pool-data) (/ (at "balance"(read stakes stake-id)) (at "tokens-locked" (read pools-usage pool-id) ) ) ) )
)
;If this beneficiarys balance is 0, this beneficiary is new, and we add +1 staker count to the pools data and set the pool start time:
(if (= t_balance 0.0)
(update pools pool-id
{
"stakers": (+ (at "stakers" pool-data) 1.0)
}
)
true
)
(format "Allocated {}% of emissions to {} for {} {} every {} seconds" [(* (/ (at "balance"(read stakes stake-id)) (at "tokens-locked" (read pools-usage pool-id) ) ) 100) account ALLOCATION (at "reward-token" pool-data) (at "reward-duration" pool-data) ])
)
)
)
)
)
(format "The vesting pool {} is deactivated." [pool-id]))
)
)
)
;Vesting pools can be connected to other vesting pools to auto feed tokens from 1 pool to the other
;Only 2 vesting pools can be connected together at a time - 3 vesting pools cannot be linked together
(defun claim-vest (pool-id:string account:string)
@doc "For pool to pool vesting claims"
(with-default-read stakes (get-stake-id-key account pool-id)
{ "account" : 'nonulls, "balance" : 0.0 }
{ "account" := t_account, "balance" := t_balance }
(if (= (at "has-vesting-connection" (read pools-usage pool-id)) true )
(let
(
(hasVEST true)
)
;(migrate-vesting-allocations pool-id)
(absorb-new-tokens pool-id)
)
true
)
(if (and (= account t_account) (> t_balance 0.0) )
(let
(
(stake-id (get-stake-id-key account pool-id))
(pool-data (read pools pool-id))
(pool-usage-data (read pools-usage pool-id))
)
(if (> (at "stakers" pool-data) 0.0)
(let
(
(stake (read stakes stake-id))
(reward-token:module{fungible-v2} (at "reward-token" pool-data))
(to-pay-max (calculate-rewards pool-id account) )
(available (at "pool_balance" pool-data))
)
(let
(
(to-pay-total (if (>= (- available to-pay-max ) 0.0)
to-pay-max
available
)
)
(can-pay (- (at "max-rewards" stake) (at "rewards" stake) ))
)
(let
(
(to-pay (if (>= (- can-pay to-pay-total ) 0.0)
to-pay-total
(- (at "max-rewards" stake) (at "rewards" stake) )
)
)
)
;Enforce account
(enforce (= account (at "account" stake)) "Authorization failed")
;Enforce balance
(if (>= to-pay 0.0)
(let
(
(hasCLAIM true)
)
;Enforce claim wait duration
(if (>= (diff-time (at "block-time" (chain-data)) (at 'last-claimed stake)) (at "claim-wait-seconds" pool-data) )
(let
(
(canCLAIM true)
)
;Enforce pool withdraw wait duration
(if (>= (diff-time (at "block-time" (chain-data)) (at 'start-time pool-data)) (at "withdraw-duration" pool-data) )
(let
(
(canWITHDRAW true)
)
;Enforce user withdraw wait duration
(if (>= (diff-time (at "block-time" (chain-data)) (at 'start-time stake)) (at "withdraw-duration" stake) )
(let
(
(userWITHDRAW true)
)
;Compose allocation transfer
(if (> to-pay 0.0)
(if (>= (- (at "pool_balance" pool-data) to-pay ) 0.0)
(install-capability (reward-token::TRANSFER pool-id account to-pay))
(install-capability (reward-token::TRANSFER pool-id account (at "pool_balance" pool-data)))
)
true)
;Transfer back the allocation amount composed above
(if (> to-pay 0.0)
(if (>= (- (at "pool_balance" pool-data) to-pay ) 0.0)
(with-capability (PRIVATE_RESERVE pool-id (get-token-key reward-token)) (reward-token::transfer pool-id account to-pay) )
(with-capability (PRIVATE_RESERVE pool-id (get-token-key reward-token)) (reward-token::transfer pool-id account (at "pool_balance" pool-data) ) )
)
true)
;Update pool usage data
(update pools-usage pool-id
{
"last-updated": (at "block-time" (chain-data)),
"paid": (+ (at "paid" pool-usage-data) to-pay) ,
"multiplier": (calculate-multiplier pool-id)
}
)
;Update pool data
(update pools pool-id
{
"pool_balance": (- (at "pool_balance" pool-data) (if (> to-pay 0.0) to-pay 0.0) ),
"active": (if (<= (- (at "pool_balance" pool-data) to-pay) 0.0) false (at "active" pool-data) )
}
)
;Update user stake data
(update stakes stake-id
{
"last-updated": (at "block-time" (chain-data)),
"rewards": (+ (at "rewards" stake) (if (> to-pay 0.0) to-pay 0.0)),
"last-claimed": (at "block-time" (chain-data)),
"multiplier": (at "multiplier" (read pools-usage pool-id))
}
)
;If this account has claimed its max rewards and can no longer claim rewards, then remove its allocation and recycle what it was claiming back into the pool for distribution
(if (or (= (- (at "max-rewards" stake) (at "rewards" stake) ) 0.0) (= (- (at "max-rewards" (read stakes stake-id)) (at "rewards" (read stakes stake-id)) ) 0.0) )
(let
(
(userEXPIRED true)
)
(update pools pool-id
{
"end-time": (calculate-pool-end-time pool-id false true to-pay-max),
"stakers": (- (at "stakers" pool-data) 1.0)
}
)
(update pools-usage pool-id
{
"tokens-locked": (- (at "tokens-locked" pool-usage-data) (at "balance" stake) )
}
)
(update stakes stake-id
{
"balance": (-(at "balance" stake) (at "balance" stake) )
}
)
(with-default-read pool-accounts pool-id
{ "ids" : [] }
{ "ids" := t_ids }
(let*
(
(newlist:[string] (filter (compose (composelist) (!= account)) t_ids) )
)
(write pool-accounts pool-id {"ids": newlist } )
)
)
)
true)
;Return message
(format "Awarded {} with {} {}" [account to-pay reward-token])
)
(format "Withdraw actions for {} are still locked under a wait-limit in pool {}" [account pool-id])
)
)
(format "Withdraw actions are still locked under a wait-limit in pool {}" [pool-id])
)
)
(format "{} must wait the full wait-limit in {} before claiming emissions" [account pool-id])
)
)
(format "{} has no emissions to claim right now in pool {}" [account pool-id])
)
)
)
)
(format "{} has no token emissions to claim in {}" [account pool-id]))
)
(format "{} has no token emissions to claim in {}" [account pool-id]))
)
)
(defun claim (pool-id:string account:string)
@doc "Claims an accounts current rewards from a pool"
(with-default-read stakes (get-stake-id-key account pool-id)
{ "account" : 'nonulls, "balance" : 0.0 }
{ "account" := t_account, "balance" := t_balance }
(if (= (at "has-vesting-connection" (read pools-usage pool-id)) true )
(let
(
(hasVEST true)
)
(migrate-vesting-allocations pool-id)
(absorb-new-tokens pool-id)
)
true
)
(if (and (= account t_account) (> t_balance 0.0) )
(let
(
(stake-id (get-stake-id-key account pool-id))
(pool-data (read pools pool-id))
(pool-usage-data (read pools-usage pool-id))
)
(if (> (at "stakers" pool-data) 0.0)
(let
(
(stake (read stakes stake-id))
(reward-token:module{fungible-v2} (at "reward-token" pool-data))
(to-pay-max (calculate-rewards pool-id account) )
(available (at "pool_balance" pool-data))
)
(let
(
(to-pay-total (if (>= (- available to-pay-max ) 0.0)
to-pay-max
available
)
)
(can-pay (- (at "max-rewards" stake) (at "rewards" stake) ))
)
(let
(
(to-pay (if (>= (- can-pay to-pay-total ) 0.0)
to-pay-total
(- (at "max-rewards" stake) (at "rewards" stake) )
)
)
)
;Enforce account
(enforce (= account (at "account" stake)) "Authorization failed")
;Enforce balance
(if (>= to-pay 0.0)
(let
(
(hasCLAIM true)
)
;Enforce claim wait duration
(if (>= (diff-time (at "block-time" (chain-data)) (at 'last-claimed stake)) (at "claim-wait-seconds" pool-data) )
(let
(
(canCLAIM true)
)
;Enforce pool withdraw wait duration
(if (>= (diff-time (at "block-time" (chain-data)) (at 'start-time pool-data)) (at "withdraw-duration" pool-data) )
(let
(
(canWITHDRAW true)
)
;Enforce user withdraw wait duration
(if (>= (diff-time (at "block-time" (chain-data)) (at 'start-time stake)) (at "withdraw-duration" stake) )
(let
(
(userWITHDRAW true)
)
;Compose allocation transfer
(if (> to-pay 0.0)
(if (>= (- (at "pool_balance" pool-data) to-pay ) 0.0)
(install-capability (reward-token::TRANSFER pool-id account to-pay))
(install-capability (reward-token::TRANSFER pool-id account (at "balance" pool-data)))
)
true)
;Transfer back the allocation amount composed above
(if (> to-pay 0.0)
(if (>= (- (at "pool_balance" pool-data) to-pay ) 0.0)
(with-capability (PRIVATE_RESERVE pool-id (get-token-key reward-token)) (reward-token::transfer pool-id account to-pay) )
(with-capability (PRIVATE_RESERVE pool-id (get-token-key reward-token)) (reward-token::transfer pool-id account (at "pool_balance" pool-data) ) )
)
true)
;Update pool usage data
(update pools-usage pool-id
{
"last-updated": (at "block-time" (chain-data)),
"paid": (+ (at "paid" pool-usage-data) to-pay) ,
"multiplier": (calculate-multiplier pool-id)
}
)
;Update pool data
(update pools pool-id
{
"pool_balance": (- (at "pool_balance" pool-data) (if (> to-pay 0.0) to-pay 0.0) ),
"active": (if (<= (- (at "pool_balance" pool-data) to-pay) 0.0) false (at "active" pool-data) )
}
)
;Update user stake data
(update stakes stake-id
{
"last-updated": (at "block-time" (chain-data)),
"rewards": (+ (at "rewards" stake) (if (> to-pay 0.0) to-pay 0.0)),
"last-claimed": (at "block-time" (chain-data)),
"multiplier": (at "multiplier" (read pools-usage pool-id))
}
)
;If this account has claimed its max rewards and can no longer claim rewards, then remove its allocation and recycle what it was claiming back into the pool for distribution
(if (or (= (- (at "max-rewards" stake) (at "rewards" stake) ) 0.0) (= (- (at "max-rewards" (read stakes stake-id)) (at "rewards" (read stakes stake-id)) ) 0.0) )
(let
(
(userEXPIRED true)
)
(update pools pool-id
{
"end-time": (calculate-pool-end-time pool-id false true to-pay-max),
"stakers": (- (at "stakers" pool-data) 1.0)
}
)
(update pools-usage pool-id
{
"tokens-locked": (- (at "tokens-locked" pool-usage-data) (at "balance" stake) )
}
)
(update stakes stake-id
{
"balance": (-(at "balance" stake) (at "balance" stake) )
}
)
(with-default-read pool-accounts pool-id
{ "ids" : [] }
{ "ids" := t_ids }
(let*
(
(newlist:[string] (filter (compose (composelist) (!= account)) t_ids) )
)
(write pool-accounts pool-id {"ids": newlist } )
)
)
)
true)
;Return message
(format "Awarded {} with {} {}" [account to-pay reward-token])
)
(format "Withdraw actions for {} are still locked under a wait-limit in pool {}" [account pool-id])
)
)
(format "Withdraw actions are still locked under a wait-limit in pool {}" [pool-id])
)
)
(format "{} must wait the full wait-limit in {} before claiming emissions" [account pool-id])
)
)
(format "{} has no emissions to claim right now in pool {}" [account pool-id])
)
)
)
)
(format "{} has no token emissions to claim in {}" [account pool-id]))
)
(format "{} has no token emissions to claim in {}" [account pool-id]))
)
)
(defun remove-account (pool-id:string account:string pay-out:bool)
@doc "Deletes an account from a vesting pool, with option to pay out what is owed or not"
(with-capability (POOL_CREATOR_GUARD pool-id)
(with-default-read stakes (get-stake-id-key account pool-id)
{ "account" : 'nonulls, "balance" : 0.0 }
{ "account" := t_account, "balance" := t_balance }
(if (and (= account t_account) (> t_balance 0.0) )
(let
(
(stake-id (get-stake-id-key account pool-id))
(pool-data (read pools pool-id))
(pool-usage-data (read pools-usage pool-id))
)
(if (> (at "stakers" pool-data) 0.0)
(let
(
(stake (read stakes stake-id))
(reward-token:module{fungible-v2} (at "reward-token" pool-data))
(to-pay-max (calculate-rewards pool-id account) )
(available (at "pool_balance" pool-data))
)
(let
(
(to-pay (if (>= (- available to-pay-max ) 0.0)
to-pay-max
available
)
)
(to-pay-stake (at "balance" stake))
)
;Enforce balance
(enforce (>= to-pay 0.0) "There are no allocations to claim for this account.")
;Update pool data