Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(position): Extract JSON From Position #495

Merged
merged 17 commits into from
Feb 5, 2025
20 changes: 10 additions & 10 deletions contract/r/gnoswap/common/errors.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
)

var (
errNoPermission = errors.New("[GNOSWAP-COMMON-001] caller has no permission")
errHalted = errors.New("[GNOSWAP-COMMON-002] halted")
errOutOfRange = errors.New("[GNOSWAP-COMMON-003] value out of range")
errNotRegistered = errors.New("[GNOSWAP-COMMON-004] token is not registered")
errInvalidAddr = errors.New("[GNOSWAP-COMMON-005] invalid address")
errOverflow = errors.New("[GNOSWAP-COMMON-006] overflow")
errInvalidTokenId = errors.New("[GNOSWAP-COMMON-007] invalid tokenId")
errInvalidInput = errors.New("[GNOSWAP-COMMON-008] invalid input data")
errOverFlow = errors.New("[GNOSWAP-COMMON-009] overflow")
errIdenticalTicks = errors.New("[GNOSWAP-COMMON-010] identical ticks")
errNoPermission = errors.New("[GNOSWAP-COMMON-001] caller has no permission")
errHalted = errors.New("[GNOSWAP-COMMON-002] halted")
errOutOfRange = errors.New("[GNOSWAP-COMMON-003] value out of range")
errNotRegistered = errors.New("[GNOSWAP-COMMON-004] token is not registered")
errInvalidAddr = errors.New("[GNOSWAP-COMMON-005] invalid address")
errOverflow = errors.New("[GNOSWAP-COMMON-006] overflow")
errInvalidPositionId = errors.New("[GNOSWAP-COMMON-007] invalid positionId")
errInvalidInput = errors.New("[GNOSWAP-COMMON-008] invalid input data")
errOverFlow = errors.New("[GNOSWAP-COMMON-009] overflow")
errIdenticalTicks = errors.New("[GNOSWAP-COMMON-010] identical ticks")
)

// newErrorWithDetail appends additional context or details to an existing error message.
Expand Down
41 changes: 41 additions & 0 deletions contract/r/gnoswap/common/grc721_position_id.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package common

import (
"strconv"

"gno.land/p/demo/ufmt"

"gno.land/p/demo/grc/grc721"
)

// PositionIdFrom converts positionId to grc721.TokenID type
// NOTE: input parameter positionId can be string, int, uint64, or grc721.TokenID
// if positionId is nil or not supported, it will panic
// if input type is not supported, it will panic
// input: positionId interface{}
// output: grc721.TokenID
func PositionIdFrom(positionId interface{}) grc721.TokenID {
if positionId == nil {
panic(newErrorWithDetail(
errInvalidPositionId,
"can not be nil",
))
}

switch positionId.(type) {
case string:
return grc721.TokenID(positionId.(string))
case int:
return grc721.TokenID(strconv.Itoa(positionId.(int)))
case uint64:
return grc721.TokenID(strconv.Itoa(int(positionId.(uint64))))
case grc721.TokenID:
return positionId.(grc721.TokenID)
default:
estimatedType := ufmt.Sprintf("%T", positionId)
panic(newErrorWithDetail(
errInvalidPositionId,
ufmt.Sprintf("unsupported positionId type: %s", estimatedType),
))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"gno.land/p/demo/grc/grc721"
)

func TestTokenIdFrom(t *testing.T) {
func TestPositionIdFrom(t *testing.T) {
tests := []struct {
name string
input interface{}
Expand Down Expand Up @@ -50,14 +50,14 @@ func TestTokenIdFrom(t *testing.T) {
if tt.wantPanic {
defer func() {
if r := recover(); r == nil {
t.Errorf("TokenIdFrom() should have panicked")
t.Errorf("PositionIdFrom() should have panicked")
}
}()
}

got := TokenIdFrom(tt.input)
got := PositionIdFrom(tt.input)
if got != tt.want {
t.Errorf("TokenIdFrom() = %v, want %v", got, tt.want)
t.Errorf("PositionIdFrom() = %v, want %v", got, tt.want)
}
})
}
Expand Down
41 changes: 0 additions & 41 deletions contract/r/gnoswap/common/grc721_token_id.gno

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func testPositionMint(t *testing.T) {
bar.Approve(consts.POOL_ADDR, consts.UINT64_MAX)
foo.Approve(consts.POOL_ADDR, consts.UINT64_MAX)

tokenId, liquidity, amount0, amount1 := pn.Mint(barPath, fooPath, fee500, int32(-8000), int32(8000), "10000000000", "10000000000", "0", "0", max_timeout, admin, admin)
uassert.Equal(t, tokenId, uint64(1))
positionId, liquidity, amount0, amount1 := pn.Mint(barPath, fooPath, fee500, int32(-8000), int32(8000), "10000000000", "10000000000", "0", "0", max_timeout, admin, admin)
uassert.Equal(t, positionId, uint64(1))
uassert.Equal(t, amount0, "10000000000")
uassert.Equal(t, amount1, "10000000000")
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func testMint(t *testing.T) {
wugnot.Approve(consts.POOL_ADDR, consts.UINT64_MAX)
gns.Approve(consts.POOL_ADDR, consts.UINT64_MAX)

tokenId, liquidity, amount0, amount1 := pn.Mint(consts.WUGNOT_PATH, consts.GNS_PATH, fee3000, int32(-8040), int32(8040), "10000000000", "10000000000", "0", "0", max_timeout, admin, admin)
uassert.Equal(t, tokenId, uint64(1))
positionId, liquidity, amount0, amount1 := pn.Mint(consts.WUGNOT_PATH, consts.GNS_PATH, fee3000, int32(-8040), int32(8040), "10000000000", "10000000000", "0", "0", max_timeout, admin, admin)
uassert.Equal(t, positionId, uint64(1))
uassert.Equal(t, amount0, "10000000000")
uassert.Equal(t, amount1, "10000000000")

Expand Down
6 changes: 3 additions & 3 deletions contract/r/gnoswap/pool/protocol_fee_withdrawal.gno
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
// HandleWithdrawalFee withdraws the fee from the user and returns the amount after the fee
// Only position contract can call this function
// Input:
// - tokenId: the id of the LP token
// - positionId: the id of the LP token
// - token0Path: the path of the token0
// - _amount0: the amount of token0
// - token1Path: the path of the token1
Expand All @@ -39,7 +39,7 @@ const (
//
// ref: https://docs.gnoswap.io/contracts/pool/protocol_fee_withdrawal.gno#handlewithdrawalfee
func HandleWithdrawalFee(
tokenId uint64,
positionId uint64,
token0Path string,
amount0 string, // uint256
token1Path string,
Expand Down Expand Up @@ -81,7 +81,7 @@ func HandleWithdrawalFee(
"WithdrawalFee",
"prevAddr", prevAddr,
"prevRealm", prevPkgPath,
"lpTokenId", formatUint(tokenId),
"lpTokenId", formatUint(positionId),
"poolPath", poolPath,
"feeAmount0", feeAmount0.ToString(),
"feeAmount1", feeAmount1.ToString(),
Expand Down
23 changes: 11 additions & 12 deletions contract/r/gnoswap/position/_helper_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -436,28 +436,27 @@ func MakeMintPositionWithoutFee(t *testing.T) (uint64, string, string, string) {
)
}

func LPTokenApprove(t *testing.T, owner, operator std.Address, tokenId uint64) {
func LPTokenApprove(t *testing.T, owner, operator std.Address, positionId uint64) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(owner))
gnft.Approve(operator, tokenIdFrom(tokenId))
gnft.Approve(operator, positionIdFrom(positionId))
}

func LPTokenStake(t *testing.T, owner std.Address, tokenId uint64) {
func LPTokenStake(t *testing.T, owner std.Address, positionId uint64) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(owner))
sr.StakeToken(tokenId)
sr.StakeToken(positionId)
}

func LPTokenUnStake(t *testing.T, owner std.Address, tokenId uint64, unwrap bool) {
func LPTokenUnStake(t *testing.T, owner std.Address, positionId uint64, unwrap bool) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(owner))
sr.UnStakeToken(tokenId, unwrap)
sr.UnStakeToken(positionId, unwrap)
}

func getPoolFromLpTokenId(t *testing.T, lpTokenId uint64) *pl.Pool {
func getPoolFromPositionId(t *testing.T, positionId uint64) *pl.Pool {
t.Helper()

position := MustGetPosition(lpTokenId)
position := MustGetPosition(positionId)
return pl.GetPoolFromPoolPath(position.poolKey)
}

Expand Down Expand Up @@ -658,7 +657,7 @@ func burnAllNFT(t *testing.T) {

std.TestSetRealm(std.NewCodeRealm(consts.POSITION_PATH))
for i := uint64(1); i <= gnft.TotalSupply(); i++ {
gnft.Burn(tokenIdFrom(i))
gnft.Burn(positionIdFrom(i))
}
}

Expand All @@ -667,8 +666,8 @@ func TestBeforeResetPositionObject(t *testing.T) {
// make actual data to test resetting not only position's state but also pool's state
std.TestSetRealm(adminRealm)

tokenId, liquidity, amount0, amount1 := MakeMintPositionWithoutFee(t)
uassert.Equal(t, tokenId, uint64(1), "tokenId should be 1")
positionId, liquidity, amount0, amount1 := MakeMintPositionWithoutFee(t)
uassert.Equal(t, positionId, uint64(1), "positionId should be 1")
uassert.Equal(t, liquidity, "50000", "liquidity should be 50000")
uassert.Equal(t, amount0, "50000", "amount0 should be 50000")
uassert.Equal(t, amount1, "50000", "amount1 should be 50000")
Expand Down
Loading
Loading