Gno.land
| 1 | package position |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "time" |
| 6 | |
| 7 | u256 "gno.land/p/gnoswap/uint256/v1" |
| 8 | ufmt "gno.land/p/nt/ufmt/v0" |
| 9 | |
| 10 | prbac "gno.land/p/gnoswap/rbac/v1" |
| 11 | "gno.land/r/gnoswap/access/v1" |
| 12 | ) |
| 13 | |
| 14 | // assertIsNotExpired panics if the deadline is expired. |
| 15 | func 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. |
| 27 | func 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. |
| 46 | func 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. |
| 56 | func 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. |
| 66 | func 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. |
| 78 | func 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. |
| 90 | func 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. |
| 104 | func 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. |
| 111 | func assertIsNotMintToStaker(mintTo address) { |
| 112 | stakerAddr := access.MustGetAddress(prbac.ROLE_STAKER.String()) |
| 113 | if mintTo == stakerAddr { |
| 114 | panic(errors.New(errCannotMintToStaker)) |
| 115 | } |
| 116 | } |
| Function | Parameters |
|---|---|
| NewPositionV1 | (positionStore interface {GetPosition func(uint64) (gno.land/r/gnoswap/position.Position, bool); GetPositionNextID func() uint64; GetPositions func() *gno.land/p/nt/bptree/v0.BPTree; HasPosition func(uint64) bool; HasPositionNextIDStoreKey func() bool; HasPositionsStoreKey func() bool; RemovePosition func(int, .uverse.realm, uint64) .uverse.error; SetPosition func(int, .uverse.realm, uint64, gno.land/r/gnoswap/position.Position) .uverse.error; SetPositionNextID func(int, .uverse.realm, uint64) .uverse.error; SetPositions func(int, .uverse.realm, *gno.land/p/nt/bptree/v0.BPTree) .uverse.error}, accessor interface {Approve func(int, .uverse.realm, .uverse.address, gno.land/p/nt/grc721/v0.TokenID) .uverse.error; Burn func(int, .uverse.realm, gno.land/p/nt/grc721/v0.TokenID); Exists func(gno.land/p/nt/grc721/v0.TokenID) bool; Mint func(int, .uverse.realm, .uverse.address, gno.land/p/nt/grc721/v0.TokenID) gno.land/p/nt/grc721/v0.TokenID; OwnerOf func(gno.land/p/nt/grc721/v0.TokenID) (.uverse.address, .uverse.error); TotalSupply func() int64}) |
N/A
| Function | Caller | Age |
|---|
None