Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

assert.gno

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