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

utils.gno

3.46 Kb · 114 lines
  1package position
  2
  3import (
  4	"strconv"
  5	"strings"
  6
  7	"gno.land/p/gnoswap/deps/grc721"
  8	prabc "gno.land/p/gnoswap/rbac"
  9	u256 "gno.land/p/gnoswap/uint256"
 10	ufmt "gno.land/p/nt/ufmt/v0"
 11	"gno.land/r/gnoswap/access"
 12)
 13
 14const MAX_UINT256 string = "115792089237316195423570985008687907853269984665640564039457584007913129639935"
 15
 16func mustGetStakerAddress() address {
 17	return access.MustGetAddress(prabc.ROLE_STAKER.String())
 18}
 19
 20// positionIdFrom converts positionId to grc721.TokenID type
 21// input: positionId uint64
 22// output: grc721.TokenID
 23func positionIdFrom(positionId uint64) grc721.TokenID {
 24	return grc721.TokenID(strconv.FormatUint(positionId, 10))
 25}
 26
 27// exists checks whether positionId exists
 28// If positionId doesn't exist, return false, otherwise return true
 29// input: positionId uint64
 30// output: bool
 31func (p *positionV1) exists(positionId uint64) bool {
 32	return p.nftAccessor.Exists(positionIdFrom(positionId))
 33}
 34
 35// isOwner checks whether the caller is the owner of the positionId
 36// If the caller is the owner of the positionId, return true, otherwise return false
 37// input: positionId uint64, addr std.Address
 38// output: bool
 39func (p *positionV1) isOwner(positionId uint64, addr address) bool {
 40	owner, err := p.nftAccessor.OwnerOf(positionIdFrom(positionId))
 41	if err != nil {
 42		return false
 43	}
 44	return owner == addr
 45}
 46
 47// isOperator checks whether the caller is the approved operator of the positionId
 48// If the caller is the approved operator of the positionId, return true, otherwise return false
 49// input: positionId uint64, addr std.Address
 50// output: bool
 51func (p *positionV1) isOperator(positionId uint64, addr address) bool {
 52	return p.GetPositionOperator(positionId) == addr
 53}
 54
 55// isStaked checks whether positionId is staked
 56// If positionId is staked, owner of positionId is staker contract
 57// If positionId is staked, return true, otherwise return false
 58// input: positionId grc721.TokenID
 59// output: bool
 60func (p *positionV1) isStaked(positionId grc721.TokenID) bool {
 61	exist := p.nftAccessor.Exists(positionId)
 62	if !exist {
 63		return false
 64	}
 65	owner, err := p.nftAccessor.OwnerOf(positionId)
 66	if err == nil && owner == mustGetStakerAddress() {
 67		return true
 68	}
 69	return false
 70}
 71
 72// isOwnerOrOperator checks whether the caller is the owner or approved operator of the positionId
 73// If the caller is the owner or approved operator of the positionId, return true, otherwise return false
 74// input: addr std.Address, positionId uint64
 75// output: bool
 76func (p *positionV1) isOwnerOrOperator(positionId uint64, addr address) bool {
 77	if !addr.IsValid() || !p.exists(positionId) {
 78		return false
 79	}
 80
 81	if p.isStaked(positionIdFrom(positionId)) {
 82		return p.isOperator(positionId, addr)
 83	}
 84
 85	return p.isOwner(positionId, addr)
 86}
 87
 88// splitOf divides poolKey into pToken0, pToken1, and pFee
 89// If poolKey is invalid, it will panic
 90//
 91// input: poolKey string
 92// output:
 93// - token0Path string
 94// - token1Path string
 95// - fee uint32
 96func splitOf(poolKey string) (string, string, uint32) {
 97	res := strings.Split(poolKey, ":")
 98	if len(res) != 3 {
 99		panic(newErrorWithDetail(errInvalidInput, ufmt.Sprintf("invalid poolKey(%s)", poolKey)))
100	}
101
102	pToken0, pToken1, pFeeStr := res[0], res[1], res[2]
103
104	pFee, err := strconv.Atoi(pFeeStr)
105	if err != nil {
106		panic(newErrorWithDetail(errInvalidInput, ufmt.Sprintf("invalid fee(%s)", pFeeStr)))
107	}
108
109	return pToken0, pToken1, uint32(pFee)
110}
111
112func isSlippageExceeded(amount0, amount1, amount0Min, amount1Min *u256.Uint) bool {
113	return !(amount0.Gte(amount0Min) && amount1.Gte(amount1Min))
114}