From e7a4d13b430490996cfd60b0c57ddd932195d8cb Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 15 Feb 2024 13:24:25 +0900 Subject: [PATCH] GSW-871 feat: use common, consts --- consts/consts.gno | 70 ++++++-------------- position/_RPC_api.gno | 6 +- position/_TEST_INIT_register_tokens_test.gno | 20 +++--- position/_TEST_position_api_test.gnoa | 10 +-- position/_TEST_position_test.gno | 8 +-- position/gno.mod | 21 +++++- position/gno_helper.gno | 4 +- position/liquidity_management.gno | 6 +- position/position.gno | 20 +++--- position/test_helper.gno | 2 +- 10 files changed, 81 insertions(+), 86 deletions(-) diff --git a/consts/consts.gno b/consts/consts.gno index d21588a6..8442e7cb 100644 --- a/consts/consts.gno +++ b/consts/consts.gno @@ -15,64 +15,43 @@ const ( // WRAP & UNWRAP const ( - GNOT = "gnot" - WRAPPED_WUGNOT = "gno.land/r/demo/wugnot" + GNOT string = "gnot" + WRAPPED_WUGNOT string = "gno.land/r/demo/wugnot" ) -// CONTRACT PATH +// CONTRACT PATH & ADDRESS const ( - POOL_PATH = "gno.land/r/demo/pool" - POSITION_PATH = "gno.land/r/demo/position" - STAKER_PATH = "gno.land/r/demo/staker" - ROUTER_PATH = "gno.land/r/demo/router" - GOV_PATH = "gno.land/r/demo/gov" -) + POOL_PATH string = "gno.land/r/demo/pool" + POOL_ADDR std.Address = std.DerivePkgAddr(POOL_PATH) -// CONTRACT ADDRESS -var ( - POOL_ADDR std.Address = "" - POSITION_ADDR std.Address = "" - STAKER_ADDR std.Address = "" - ROUTER_ADDR std.Address = "" - GOV_ADDR std.Address = "" -) + POSITION_PATH string = "gno.land/r/demo/position" + POSITION_ADDR std.Address = std.DerivePkgAddr(POSITION_PATH) -func init() { - POOL_ADDR = std.DerivePkgAddr(POOL_PATH) - POSITION_ADDR = std.DerivePkgAddr(POSITION_PATH) - STAKER_ADDR = std.DerivePkgAddr(STAKER_PATH) - ROUTER_ADDR = std.DerivePkgAddr(ROUTER_PATH) - GOV_ADDR = std.DerivePkgAddr(GOV_PATH) -} + STAKER_PATH string = "gno.land/r/demo/staker" + STAKER_ADDR std.Address = std.DerivePkgAddr(STAKER_PATH) + + ROUTER_ADDR std.Address = std.DerivePkgAddr(ROUTER_PATH) + ROUTER_PATH string = "gno.land/r/demo/router" + + GOV_PATH string = "gno.land/r/demo/gov" + GOV_ADDR std.Address = std.DerivePkgAddr(GOV_PATH) +) // NUMBER const ( - ZERO_ADDRESS std.Address = "" - - // some numbers // calculated by https://mathiasbynens.be/demo/integer-range - MIN_INT8 bigint = -128 - MAX_INT8 bigint = 127 MAX_UINT8 bigint = 255 - MIN_INT16 bigint = -32768 - MAX_INT16 bigint = 32767 MAX_UINT16 bigint = 65535 - MIN_INT32 bigint = -2147483648 - MAX_INT32 bigint = 2147483647 MAX_UINT32 bigint = 4294967295 - MIN_INT64 bigint = -9223372036854775808 - MAX_INT64 bigint = 9223372036854775807 MAX_UINT64 bigint = 18446744073709551615 - MIN_INT128 bigint = -170141183460469231731687303715884105728 - MAX_INT128 bigint = 170141183460469231731687303715884105727 MAX_UINT128 bigint = 340282366920938463463374607431768211455 - MIN_INT256 bigint = -57896044618658097711785492504343953926634992332820282019728792003956564819968 - MAX_INT256 bigint = 57896044618658097711785492504343953926634992332820282019728792003956564819967 + MAX_UINT160 bigint = 1461501637330902918203684832716283019655932542975 + MAX_UINT256 bigint = 115792089237316195423570985008687907853269984665640564039457584007913129639935 // Tick Related @@ -89,14 +68,9 @@ const ( Q96 bigint = 79228162514264337593543950336 // 2 ** 96 Q128 bigint = 340282366920938463463374607431768211456 // 2 ** 128 - // Extra - MIN_INT24 bigint = -8388608 - MAX_INT24 bigint = 8388607 - MAX_UINT24 bigint = 16777215 - - MIN_INT96 bigint = -39614081257132168796771975168 - MAX_INT96 bigint = 39614081257132168796771975167 - MAX_UINT96 bigint = 79228162514264337593543950335 +) - MAX_UINT160 bigint = 1461501637330902918203684832716283019655932542975 +// ETCs +const ( + ZERO_ADDRESS std.Address = "" ) diff --git a/position/_RPC_api.gno b/position/_RPC_api.gno index a298b081..6ddcf0d9 100644 --- a/position/_RPC_api.gno +++ b/position/_RPC_api.gno @@ -8,7 +8,7 @@ import ( "gno.land/r/demo/consts" - p "gno.land/r/demo/pool" + pl "gno.land/r/demo/pool" ) type RpcPosition struct { @@ -68,7 +68,7 @@ func rpcMakePosition(lpTokenId uint64) RpcPosition { panic(ufmt.Sprintf("[POSITION] getter_api.gno__rpcMakePosition() || position not found for lpTokenId(%d)", lpTokenId)) } - pool := p.GetPoolFromPoolPath(position.poolKey) + pool := pl.GetPoolFromPoolPath(position.poolKey) currentX96 := pool.PoolGetSlot0SqrtPriceX96() lowerX96 := common.TickMathGetSqrtRatioAtTick(position.tickLower) upperX96 := common.TickMathGetSqrtRatioAtTick(position.tickUpper) @@ -108,7 +108,7 @@ func unclaimedFee(tokenId uint64) (bigint, bigint) { tickUpper := positions[tokenId].tickUpper poolKey := positions[tokenId].poolKey - pool := p.GetPoolFromPoolPath(poolKey) + pool := pl.GetPoolFromPoolPath(poolKey) currentTick := pool.PoolGetSlot0Tick() diff --git a/position/_TEST_INIT_register_tokens_test.gno b/position/_TEST_INIT_register_tokens_test.gno index 856abb3b..4c1f4c15 100644 --- a/position/_TEST_INIT_register_tokens_test.gno +++ b/position/_TEST_INIT_register_tokens_test.gno @@ -21,7 +21,7 @@ import ( "gno.land/r/demo/users" - "gno.land/r/demo/pool" // only for position `testcase`` to call grc20 by register + pl "gno.land/r/demo/pool" // only for position `testcase`` to call grc20 by register ) type FooToken struct{} @@ -163,13 +163,13 @@ func (GNSToken) Approve() func(spender users.AddressOrName, amount uint64) { } func init() { - pool.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) - pool.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) - pool.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) - pool.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) - pool.RegisterGRC20Interface("gno.land/r/demo/fred", FredToken{}) - pool.RegisterGRC20Interface("gno.land/r/demo/thud", ThudToken{}) - pool.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) - pool.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) - pool.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/fred", FredToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/thud", ThudToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) } diff --git a/position/_TEST_position_api_test.gnoa b/position/_TEST_position_api_test.gnoa index a2a7bd7b..4d90d864 100644 --- a/position/_TEST_position_api_test.gnoa +++ b/position/_TEST_position_api_test.gnoa @@ -8,7 +8,7 @@ import ( "gno.land/r/demo/consts" - p "gno.land/r/demo/pool" + pl "gno.land/r/demo/pool" ) var ( @@ -20,10 +20,10 @@ var ( // 1. Init & Create Pool func TestPoolInitCreatePool(t *testing.T) { std.TestSetOrigCaller(test1) - p.InitManual() + pl.InitManual() std.TestSetOrigCaller(test1) - p.CreatePool(fooPath, barPath, fee500, 130621891405341611593710811006) + pl.CreatePool(fooPath, barPath, fee500, 130621891405341611593710811006) // fee // 500 = 0.05% // USv3 default @@ -32,7 +32,7 @@ func TestPoolInitCreatePool(t *testing.T) { // sqrtPrice // 130621891405341611593710811006 // tick = 10000 - shouldPanic(t, func() { p.CreatePool(fooPath, barPath, 500, 130621891405341611593710811006) }) + shouldPanic(t, func() { pl.CreatePool(fooPath, barPath, 500, 130621891405341611593710811006) }) } // 2. Mint LP and Get GNFT @@ -50,7 +50,7 @@ func TestMint(t *testing.T) { func TestSwap(t *testing.T) { std.TestSetPrevRealm("gno.land/r/demo/router") std.TestSetOrigCaller(test1) - p.Swap( + pl.Swap( fooPath, barPath, fee500, diff --git a/position/_TEST_position_test.gno b/position/_TEST_position_test.gno index 4c9296c1..aedd9a2d 100644 --- a/position/_TEST_position_test.gno +++ b/position/_TEST_position_test.gno @@ -6,7 +6,7 @@ import ( "encoding/gjson" - p "gno.land/r/demo/pool" + pl "gno.land/r/demo/pool" ) var ( @@ -18,10 +18,10 @@ var ( // 1. Init & Create Pool func TestPoolInitCreatePool(t *testing.T) { std.TestSetOrigCaller(test1) - p.InitManual() + pl.InitManual() std.TestSetOrigCaller(test1) - p.CreatePool(fooPath, barPath, fee500, 130621891405341611593710811006) + pl.CreatePool(fooPath, barPath, fee500, 130621891405341611593710811006) // fee // 500 = 0.05% // USv3 default @@ -30,7 +30,7 @@ func TestPoolInitCreatePool(t *testing.T) { // sqrtPrice // 130621891405341611593710811006 // tick = 10000 - shouldPanic(t, func() { p.CreatePool(fooPath, barPath, fee500, 130621891405341611593710811006) }) + shouldPanic(t, func() { pl.CreatePool(fooPath, barPath, fee500, 130621891405341611593710811006) }) } // 2. Mint LP and Get GNFT diff --git a/position/gno.mod b/position/gno.mod index 108623e0..76442d99 100644 --- a/position/gno.mod +++ b/position/gno.mod @@ -1 +1,20 @@ -module gno.land/r/demo/position \ No newline at end of file +module gno.land/r/demo/position + +require ( + gno.land/p/demo/common v0.0.0-latest + gno.land/p/demo/grc/grc721 v0.0.0-latest + gno.land/p/demo/ufmt v0.0.0-latest + gno.land/r/demo/bar v0.0.0-latest + gno.land/r/demo/baz v0.0.0-latest + gno.land/r/demo/consts v0.0.0-latest + gno.land/r/demo/foo v0.0.0-latest + gno.land/r/demo/fred v0.0.0-latest + gno.land/r/demo/gnft v0.0.0-latest + gno.land/r/demo/gns v0.0.0-latest + gno.land/r/demo/obl v0.0.0-latest + gno.land/r/demo/pool v0.0.0-latest + gno.land/r/demo/qux v0.0.0-latest + gno.land/r/demo/thud v0.0.0-latest + gno.land/r/demo/users v0.0.0-latest + gno.land/r/demo/wugnot v0.0.0-latest +) diff --git a/position/gno_helper.gno b/position/gno_helper.gno index b3a49387..4958d681 100644 --- a/position/gno_helper.gno +++ b/position/gno_helper.gno @@ -3,6 +3,8 @@ package position import ( "std" "time" + + "gno.land/r/demo/consts" ) func GetChainID() string { @@ -18,7 +20,7 @@ func GetTimestamp() int64 { } func GetOrigPkgAddr() std.Address { - return std.DerivePkgAddr("gno.land/r/demo/position") + return consts.POSITION_ADDR // return std.GetOrigPkgAddr() } diff --git a/position/liquidity_management.gno b/position/liquidity_management.gno index 1b6c4976..0604ac36 100644 --- a/position/liquidity_management.gno +++ b/position/liquidity_management.gno @@ -3,11 +3,11 @@ package position import ( "gno.land/p/demo/common" - p "gno.land/r/demo/pool" + pl "gno.land/r/demo/pool" ) func addLiquidity(params AddLiquidityParams) (bigint, bigint, bigint) { - pool := p.GetPoolFromPoolPath(params.poolKey) + pool := pl.GetPoolFromPoolPath(params.poolKey) sqrtPriceX96 := pool.PoolGetSlot0SqrtPriceX96() sqrtRatioAX96 := common.TickMathGetSqrtRatioAtTick(params.tickLower) @@ -23,7 +23,7 @@ func addLiquidity(params AddLiquidityParams) (bigint, bigint, bigint) { liquidity += 1 // adjust for rounding errors pToken0, pToken1, pFee := poolKeyDivide(params.poolKey) - amount0, amount1 := p.Mint( + amount0, amount1 := pl.Mint( pToken0, pToken1, pFee, diff --git a/position/position.gno b/position/position.gno index 0b25b076..7be2c1ef 100644 --- a/position/position.gno +++ b/position/position.gno @@ -5,7 +5,7 @@ import ( "gno.land/r/demo/consts" - p "gno.land/r/demo/pool" + pl "gno.land/r/demo/pool" "gno.land/r/demo/gnft" ) @@ -57,10 +57,10 @@ func Mint( func mint(params MintParams) (uint64, bigint, bigint, bigint) { checkDeadline(params.deadline) - pool := p.GetPool(params.token0, params.token1, params.fee) + pool := pl.GetPool(params.token0, params.token1, params.fee) liquidity, amount0, amount1 := addLiquidity( AddLiquidityParams{ - poolKey: p.GetPoolPath(params.token0, params.token1, params.fee), + poolKey: pl.GetPoolPath(params.token0, params.token1, params.fee), recipient: GetOrigPkgAddr(), // hardcoded tickLower: params.tickLower, tickUpper: params.tickUpper, @@ -82,7 +82,7 @@ func mint(params MintParams) (uint64, bigint, bigint, bigint) { position := Position{ nonce: 0, operator: PrevRealmAddr(), - poolKey: p.GetPoolPath(params.token0, params.token1, params.fee), + poolKey: pl.GetPoolPath(params.token0, params.token1, params.fee), tickLower: params.tickLower, tickUpper: params.tickUpper, liquidity: liquidity, @@ -107,10 +107,10 @@ func CollectFee(tokenId uint64) (uint64, bigint, bigint, string) { // tokenId, t tokensOwed0, tokensOwed1 := position.tokensOwed0, position.tokensOwed1 pToken0, pToken1, pFee := poolKeyDivide(position.poolKey) - p.Burn(pToken0, pToken1, pFee, position.tickLower, position.tickUpper, 0) // burn '0' liquidity to collect fee + pl.Burn(pToken0, pToken1, pFee, position.tickLower, position.tickUpper, 0) // burn '0' liquidity to collect fee positionKey := positionKeyCompute(GetOrigPkgAddr(), position.tickLower, position.tickUpper) - pool := p.GetPoolFromPoolPath(position.poolKey) + pool := pl.GetPoolFromPoolPath(position.poolKey) feeGrowthInside0LastX128, feeGrowthInside1LastX128 := pool.PoolGetPositionFeeGrowthInside0LastX128(positionKey), pool.PoolGetPositionFeeGrowthInside1LastX128(positionKey) tokensOwed0 += (feeGrowthInside0LastX128 - position.feeGrowthInside0LastX128) * position.liquidity / consts.Q128 @@ -119,7 +119,7 @@ func CollectFee(tokenId uint64) (uint64, bigint, bigint, string) { // tokenId, t position.feeGrowthInside0LastX128 = feeGrowthInside0LastX128 position.feeGrowthInside1LastX128 = feeGrowthInside1LastX128 - amount0, amount1 := p.Collect( + amount0, amount1 := pl.Collect( pToken0, pToken1, pFee, @@ -147,10 +147,10 @@ func Burn(tokenId uint64) (uint64, bigint, bigint, bigint, string) { // tokenId, position := positions[tokenId] positionLiquidity := position.liquidity - pool := p.GetPoolFromPoolPath(position.poolKey) // poolKey == poolPath + pool := pl.GetPoolFromPoolPath(position.poolKey) // poolKey == poolPath pToken0, pToken1, pFee := poolKeyDivide(position.poolKey) - burnAmount0, burnAmount1 := p.Burn(pToken0, pToken1, pFee, position.tickLower, position.tickUpper, positionLiquidity) + burnAmount0, burnAmount1 := pl.Burn(pToken0, pToken1, pFee, position.tickLower, position.tickUpper, positionLiquidity) positionKey := positionKeyCompute(GetOrigPkgAddr(), position.tickLower, position.tickUpper) feeGrowthInside0LastX128, feeGrowthInside1LastX128 := pool.PoolGetPositionFeeGrowthInside0LastX128(positionKey), pool.PoolGetPositionFeeGrowthInside1LastX128(positionKey) @@ -164,7 +164,7 @@ func Burn(tokenId uint64) (uint64, bigint, bigint, bigint, string) { // tokenId, position.liquidity = 0 positions[tokenId] = position - collectAmount0, collectAmount1 := p.Collect( + collectAmount0, collectAmount1 := pl.Collect( pToken0, pToken1, pFee, diff --git a/position/test_helper.gno b/position/test_helper.gno index 11939f73..ff623327 100644 --- a/position/test_helper.gno +++ b/position/test_helper.gno @@ -27,6 +27,6 @@ func tid(tokenId interface{}) grc721.TokenID { case grc721.TokenID: return tokenId.(grc721.TokenID) default: - panic("[STAKER] utils.gno__tid() || unsupported tokenId type") + panic("[POSITION] utils.gno__tid() || unsupported tokenId type") } }