assert.gno
2.16 Kb · 83 lines
1package staker
2
3import (
4 "errors"
5 "time"
6
7 ufmt "gno.land/p/nt/ufmt/v0"
8
9 "gno.land/r/gnoswap/gov/governance"
10)
11
12// assertIsValidDelegateAmount validates that the delegation amount meets system requirements.
13// This function checks minimum amount and multiple requirements.
14//
15// Parameters:
16// - amount: amount to validate
17//
18// Returns:
19// - error: nil if valid, error describing validation failure
20func assertIsValidDelegateAmount(amount int64) {
21 if amount < minimumAmount {
22 panic(makeErrorWithDetails(
23 errLessThanMinimum,
24 ufmt.Sprintf("minimum amount to delegate is %d (requested:%d)", minimumAmount, amount),
25 ))
26 }
27
28 if amount%minimumAmount != 0 {
29 panic(makeErrorWithDetails(
30 errInvalidAmount,
31 ufmt.Sprintf("amount must be multiple of %d", minimumAmount),
32 ))
33 }
34}
35
36func assertIsValidSnapshotTime(snapshotTime int64) {
37 if snapshotTime < 0 {
38 panic(makeErrorWithDetails(
39 errInvalidSnapshotTime,
40 ufmt.Sprintf("snapshot time must be greater than 0 (requested:%d)", snapshotTime),
41 ))
42 }
43
44 currentTime := time.Now().Unix()
45 maxSmoothingPeriod := governance.GetMaxSmoothingPeriod()
46
47 if snapshotTime > currentTime-maxSmoothingPeriod {
48 panic(makeErrorWithDetails(
49 errInvalidSnapshotTime,
50 ufmt.Sprintf("snapshot time must be less than %d (requested:%d)", currentTime-maxSmoothingPeriod, snapshotTime),
51 ))
52 }
53}
54
55// assertIsAvailableCleanupSnapshotTime checks that no active proposals need
56func assertIsAvailableCleanupSnapshotTime(cleanupSnapshotTime int64) {
57 oldestActiveSnapshotTime, hasActiveProposal := governance.GetOldestActiveProposalSnapshotTime()
58 if !hasActiveProposal {
59 // No active proposals, cleanup is safe
60 return
61 }
62
63 if cleanupSnapshotTime > oldestActiveSnapshotTime {
64 panic(makeErrorWithDetails(
65 errInvalidSnapshotTime,
66 ufmt.Sprintf(
67 "cannot cleanup delegation history: active proposal requires data from snapshot time %d, but cleanup would remove data before %d",
68 oldestActiveSnapshotTime,
69 cleanupSnapshotTime,
70 ),
71 ))
72 }
73}
74
75func assertNoSameDelegatee(delegatee, newDelegatee address) {
76 if delegatee == newDelegatee {
77 panic(errors.New(errSameDelegatee))
78 }
79}
80
81func assertNotImplementYet() {
82 panic("NotImplementYet")
83}