assert.gno
2.20 Kb · 86 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, err := governance.GetOldestActiveProposalSnapshotTime()
58 if err != nil {
59 panic(err)
60 }
61 if !hasActiveProposal {
62 // No active proposals, cleanup is safe
63 return
64 }
65
66 if cleanupSnapshotTime > oldestActiveSnapshotTime {
67 panic(makeErrorWithDetails(
68 errInvalidSnapshotTime,
69 ufmt.Sprintf(
70 "cannot cleanup delegation history: active proposal requires data from snapshot time %d, but cleanup would remove data before %d",
71 oldestActiveSnapshotTime,
72 cleanupSnapshotTime,
73 ),
74 ))
75 }
76}
77
78func assertNoSameDelegatee(delegatee, newDelegatee address) {
79 if delegatee == newDelegatee {
80 panic(errors.New(errSameDelegatee))
81 }
82}
83
84func assertNotImplementYet() {
85 panic("NotImplementYet")
86}