Search Apps Documentation Source Content File Folder Download Copy Actions Download

assert.gno

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