Search Apps Documentation Source Content File Folder Download Copy Actions Download

assert.gno

4.41 Kb ยท 156 lines
  1package v1
  2
  3import (
  4	"time"
  5
  6	u256 "gno.land/p/gnoswap/uint256"
  7	"gno.land/p/nt/ufmt"
  8
  9	"gno.land/r/gnoswap/access"
 10	"gno.land/r/gnoswap/common"
 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// assertSlippageIsNotExceeded panics if slippage tolerance is exceeded.
102func assertSlippageIsNotExceeded(amount0, amount1, amount0Min, amount1Min *u256.Uint) {
103	if !(amount0.Gte(amount0Min) && amount1.Gte(amount1Min)) {
104		panic(newErrorWithDetail(
105			errSlippage,
106			ufmt.Sprintf("amount0(%s) >= amount0Min(%s) && amount1(%s) >= amount1Min(%s)",
107				amount0.ToString(), amount0Min.ToString(), amount1.ToString(), amount1Min.ToString()),
108		))
109	}
110}
111
112// assertIsValidUserCoinSendWithTokenPair asserts that the user has sent the correct amount of native coin.
113func assertIsValidUserCoinSendWithTokenPair(token0, token1 string, amount0, amount1 string) {
114	if common.IsGNOTNativePath(token0) {
115		common.AssertIsUserSendGNOTAmount(mustParseInt64(amount0))
116
117		return
118	}
119
120	if common.IsGNOTNativePath(token1) {
121		common.AssertIsUserSendGNOTAmount(mustParseInt64(amount1))
122
123		return
124	}
125
126	common.AssertIsNotHandleNativeCoin()
127}
128
129// assertIsValidUserCoinSendWithWrappedTokenPair asserts that the user has sent the correct amount of native coin.
130func assertIsValidUserCoinSendWithWrappedTokenPair(token0, token1 string, amount0, amount1 string) {
131	isExistsUserSendCoins := common.ExistsUserSendCoins()
132
133	if isExistsUserSendCoins {
134		if common.IsGNOTPath(token0) {
135			common.AssertIsUserSendGNOTAmount(mustParseInt64(amount0))
136
137			return
138		}
139
140		if common.IsGNOTPath(token1) {
141			common.AssertIsUserSendGNOTAmount(mustParseInt64(amount1))
142
143			return
144		}
145	}
146
147	common.AssertIsNotHandleNativeCoin()
148}
149
150// assertValidOperatorAddress validates operator address.
151// Empty address is allowed (for operator removal), but non-empty addresses must be valid.
152func assertValidOperatorAddress(operator address) {
153	if operator != address("") && !operator.IsValid() {
154		panic(newErrorWithDetail(errInvalidAddress, ufmt.Sprintf("operator(%s)", operator)))
155	}
156}