assert.gno
3.21 Kb · 115 lines
1package v1
2
3import (
4 "time"
5
6 u256 "gno.land/p/gnoswap/uint256"
7 ufmt "gno.land/p/nt/ufmt/v0"
8
9 prbac "gno.land/p/gnoswap/rbac"
10 "gno.land/r/gnoswap/access"
11)
12
13// assertIsNotExpired panics if the deadline is expired.
14func assertIsNotExpired(deadline int64) {
15 now := time.Now().Unix()
16
17 if now > deadline {
18 panic(makeErrorWithDetails(
19 errExpired,
20 ufmt.Sprintf("transaction too old, now(%d) > deadline(%d)", now, deadline),
21 ))
22 }
23}
24
25// assertValidNumberString panics if the input string does not represent a valid integer.
26func assertValidNumberString(input string) {
27 if len(input) == 0 {
28 panic(newErrorWithDetail(errInvalidInput, "input is empty"))
29 }
30
31 bytes := []byte(input)
32 for i, b := range bytes {
33 if i == 0 && b == '-' {
34 continue // Allow if the first character is a negative sign (-)
35 }
36 if b < '0' || b > '9' {
37 panic(newErrorWithDetail(
38 errInvalidInput,
39 ufmt.Sprintf("input string : %s", input)))
40 }
41 }
42}
43
44// assertValidLiquidityAmount panics if the liquidity amount is zero.
45func assertValidLiquidityAmount(liquidity string) {
46 if u256.MustFromDecimal(liquidity).IsZero() {
47 panic(newErrorWithDetail(
48 errZeroLiquidity,
49 ufmt.Sprintf("liquidity amount must be greater than 0, got %s", liquidity),
50 ))
51 }
52}
53
54// assertExistsPosition panics if the position does not exist.
55func assertExistsPosition(p *positionV1, positionId uint64) {
56 if !p.exists(positionId) {
57 panic(newErrorWithDetail(
58 errPositionDoesNotExist,
59 ufmt.Sprintf("position with position ID(%d) doesn't exist", positionId),
60 ))
61 }
62}
63
64// assertIsOwnerForToken panics if caller is not the owner of the position.
65func assertIsOwnerForToken(p *positionV1, positionId uint64, caller address) {
66 assertExistsPosition(p, positionId)
67
68 if !p.isOwner(positionId, caller) {
69 panic(newErrorWithDetail(
70 errNoPermission,
71 ufmt.Sprintf("caller(%s) is not owner of positionId(%d)", caller, positionId),
72 ))
73 }
74}
75
76// assertIsOwnerOrOperatorForToken panics if caller is not the owner or operator of the position.
77func assertIsOwnerOrOperatorForToken(p *positionV1, positionId uint64, caller address) {
78 assertExistsPosition(p, positionId)
79
80 if !p.isOwnerOrOperator(positionId, caller) {
81 panic(newErrorWithDetail(
82 errNoPermission,
83 ufmt.Sprintf("caller(%s) is not owner or approved operator of positionId(%d)", caller, positionId),
84 ))
85 }
86}
87
88// assertEqualsAddress panics if addresses are invalid or not equal.
89func assertEqualsAddress(prevAddr, otherAddr address) {
90 access.AssertIsValidAddress(prevAddr)
91 access.AssertIsValidAddress(otherAddr)
92
93 if prevAddr != otherAddr {
94 panic(newErrorWithDetail(
95 errInvalidAddress,
96 ufmt.Sprintf("(%s, %s)", prevAddr, otherAddr),
97 ))
98 }
99}
100
101// assertValidOperatorAddress validates operator address.
102// Empty address is allowed (for operator removal), but non-empty addresses must be valid.
103func assertValidOperatorAddress(operator address) {
104 if operator != address("") && !operator.IsValid() {
105 panic(newErrorWithDetail(errInvalidAddress, ufmt.Sprintf("operator(%s)", operator)))
106 }
107}
108
109// assertIsNotMintToStaker panics if the mintTo address is staker.
110func assertIsNotMintToStaker(mintTo address) {
111 stakerAddr := access.MustGetAddress(prbac.ROLE_STAKER.String())
112 if mintTo == stakerAddr {
113 panic(errCannotMintToStaker)
114 }
115}