Skip to content

Commit 275bda4

Browse files
notJoon0xTopaz
and
0xTopaz
authored
refactor(position): Extract JSON From Position (#495)
* refactor: position api json * gsw-2082 refactor: tokenId to positionId Co-authored-by: 0xTopaz <[email protected]>
1 parent cc5622e commit 275bda4

File tree

100 files changed

+1489
-1657
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+1489
-1657
lines changed

contract/r/gnoswap/common/errors.gno

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import (
77
)
88

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

2222
// newErrorWithDetail appends additional context or details to an existing error message.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package common
2+
3+
import (
4+
"strconv"
5+
6+
"gno.land/p/demo/ufmt"
7+
8+
"gno.land/p/demo/grc/grc721"
9+
)
10+
11+
// PositionIdFrom converts positionId to grc721.TokenID type
12+
// NOTE: input parameter positionId can be string, int, uint64, or grc721.TokenID
13+
// if positionId is nil or not supported, it will panic
14+
// if input type is not supported, it will panic
15+
// input: positionId interface{}
16+
// output: grc721.TokenID
17+
func PositionIdFrom(positionId interface{}) grc721.TokenID {
18+
if positionId == nil {
19+
panic(newErrorWithDetail(
20+
errInvalidPositionId,
21+
"can not be nil",
22+
))
23+
}
24+
25+
switch positionId.(type) {
26+
case string:
27+
return grc721.TokenID(positionId.(string))
28+
case int:
29+
return grc721.TokenID(strconv.Itoa(positionId.(int)))
30+
case uint64:
31+
return grc721.TokenID(strconv.Itoa(int(positionId.(uint64))))
32+
case grc721.TokenID:
33+
return positionId.(grc721.TokenID)
34+
default:
35+
estimatedType := ufmt.Sprintf("%T", positionId)
36+
panic(newErrorWithDetail(
37+
errInvalidPositionId,
38+
ufmt.Sprintf("unsupported positionId type: %s", estimatedType),
39+
))
40+
}
41+
}

contract/r/gnoswap/common/grc721_token_id_test.gno renamed to contract/r/gnoswap/common/grc721_position_id_test.gno

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"gno.land/p/demo/grc/grc721"
77
)
88

9-
func TestTokenIdFrom(t *testing.T) {
9+
func TestPositionIdFrom(t *testing.T) {
1010
tests := []struct {
1111
name string
1212
input interface{}
@@ -50,14 +50,14 @@ func TestTokenIdFrom(t *testing.T) {
5050
if tt.wantPanic {
5151
defer func() {
5252
if r := recover(); r == nil {
53-
t.Errorf("TokenIdFrom() should have panicked")
53+
t.Errorf("PositionIdFrom() should have panicked")
5454
}
5555
}()
5656
}
5757

58-
got := TokenIdFrom(tt.input)
58+
got := PositionIdFrom(tt.input)
5959
if got != tt.want {
60-
t.Errorf("TokenIdFrom() = %v, want %v", got, tt.want)
60+
t.Errorf("PositionIdFrom() = %v, want %v", got, tt.want)
6161
}
6262
})
6363
}

contract/r/gnoswap/common/grc721_token_id.gno

Lines changed: 0 additions & 41 deletions
This file was deleted.

contract/r/gnoswap/launchpad/tests/launchpad_reward_and_gov_reward_02_test.gnoA

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ func testPositionMint(t *testing.T) {
7777
bar.Approve(consts.POOL_ADDR, consts.UINT64_MAX)
7878
foo.Approve(consts.POOL_ADDR, consts.UINT64_MAX)
7979

80-
tokenId, liquidity, amount0, amount1 := pn.Mint(barPath, fooPath, fee500, int32(-8000), int32(8000), "10000000000", "10000000000", "0", "0", max_timeout, admin, admin)
81-
uassert.Equal(t, tokenId, uint64(1))
80+
positionId, liquidity, amount0, amount1 := pn.Mint(barPath, fooPath, fee500, int32(-8000), int32(8000), "10000000000", "10000000000", "0", "0", max_timeout, admin, admin)
81+
uassert.Equal(t, positionId, uint64(1))
8282
uassert.Equal(t, amount0, "10000000000")
8383
uassert.Equal(t, amount1, "10000000000")
8484
})

contract/r/gnoswap/launchpad/tests/launchpad_reward_and_gov_reward_03_test.gnoA

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ func testMint(t *testing.T) {
120120
wugnot.Approve(consts.POOL_ADDR, consts.UINT64_MAX)
121121
gns.Approve(consts.POOL_ADDR, consts.UINT64_MAX)
122122

123-
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)
124-
uassert.Equal(t, tokenId, uint64(1))
123+
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)
124+
uassert.Equal(t, positionId, uint64(1))
125125
uassert.Equal(t, amount0, "10000000000")
126126
uassert.Equal(t, amount1, "10000000000")
127127

contract/r/gnoswap/pool/protocol_fee_withdrawal.gno

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const (
2626
// HandleWithdrawalFee withdraws the fee from the user and returns the amount after the fee
2727
// Only position contract can call this function
2828
// Input:
29-
// - tokenId: the id of the LP token
29+
// - positionId: the id of the LP token
3030
// - token0Path: the path of the token0
3131
// - amount0: the amount of token0
3232
// - token1Path: the path of the token1
@@ -39,7 +39,7 @@ const (
3939
//
4040
// ref: https://docs.gnoswap.io/contracts/pool/protocol_fee_withdrawal.gno#handlewithdrawalfee
4141
func HandleWithdrawalFee(
42-
tokenId uint64,
42+
positionId uint64,
4343
token0Path string,
4444
amount0 string, // uint256
4545
token1Path string,
@@ -81,7 +81,7 @@ func HandleWithdrawalFee(
8181
"WithdrawalFee",
8282
"prevAddr", prevAddr,
8383
"prevRealm", prevPkgPath,
84-
"lpTokenId", formatUint(tokenId),
84+
"lpTokenId", formatUint(positionId),
8585
"poolPath", poolPath,
8686
"feeAmount0", feeAmount0.ToString(),
8787
"feeAmount1", feeAmount1.ToString(),

contract/r/gnoswap/position/_helper_test.gno

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -436,28 +436,27 @@ func MakeMintPositionWithoutFee(t *testing.T) (uint64, string, string, string) {
436436
)
437437
}
438438

439-
func LPTokenApprove(t *testing.T, owner, operator std.Address, tokenId uint64) {
439+
func LPTokenApprove(t *testing.T, owner, operator std.Address, positionId uint64) {
440440
t.Helper()
441441
std.TestSetRealm(std.NewUserRealm(owner))
442-
gnft.Approve(operator, tokenIdFrom(tokenId))
442+
gnft.Approve(operator, positionIdFrom(positionId))
443443
}
444444

445-
func LPTokenStake(t *testing.T, owner std.Address, tokenId uint64) {
445+
func LPTokenStake(t *testing.T, owner std.Address, positionId uint64) {
446446
t.Helper()
447447
std.TestSetRealm(std.NewUserRealm(owner))
448-
sr.StakeToken(tokenId)
448+
sr.StakeToken(positionId)
449449
}
450450

451-
func LPTokenUnStake(t *testing.T, owner std.Address, tokenId uint64, unwrap bool) {
451+
func LPTokenUnStake(t *testing.T, owner std.Address, positionId uint64, unwrap bool) {
452452
t.Helper()
453453
std.TestSetRealm(std.NewUserRealm(owner))
454-
sr.UnStakeToken(tokenId, unwrap)
454+
sr.UnStakeToken(positionId, unwrap)
455455
}
456456

457-
func getPoolFromLpTokenId(t *testing.T, lpTokenId uint64) *pl.Pool {
457+
func getPoolFromPositionId(t *testing.T, positionId uint64) *pl.Pool {
458458
t.Helper()
459-
460-
position := MustGetPosition(lpTokenId)
459+
position := MustGetPosition(positionId)
461460
return pl.GetPoolFromPoolPath(position.poolKey)
462461
}
463462

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

659658
std.TestSetRealm(std.NewCodeRealm(consts.POSITION_PATH))
660659
for i := uint64(1); i <= gnft.TotalSupply(); i++ {
661-
gnft.Burn(tokenIdFrom(i))
660+
gnft.Burn(positionIdFrom(i))
662661
}
663662
}
664663

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

670-
tokenId, liquidity, amount0, amount1 := MakeMintPositionWithoutFee(t)
671-
uassert.Equal(t, tokenId, uint64(1), "tokenId should be 1")
669+
positionId, liquidity, amount0, amount1 := MakeMintPositionWithoutFee(t)
670+
uassert.Equal(t, positionId, uint64(1), "positionId should be 1")
672671
uassert.Equal(t, liquidity, "50000", "liquidity should be 50000")
673672
uassert.Equal(t, amount0, "50000", "amount0 should be 50000")
674673
uassert.Equal(t, amount1, "50000", "amount1 should be 50000")

0 commit comments

Comments
 (0)