Skip to content

Commit

Permalink
GSW-871 feat: use common, consts
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Feb 19, 2024
1 parent 614798e commit e7a4d13
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 86 deletions.
70 changes: 22 additions & 48 deletions consts/consts.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = ""
)
6 changes: 3 additions & 3 deletions position/_RPC_api.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down
20 changes: 10 additions & 10 deletions position/_TEST_INIT_register_tokens_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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{})
}
10 changes: 5 additions & 5 deletions position/_TEST_position_api_test.gnoa
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"gno.land/r/demo/consts"

p "gno.land/r/demo/pool"
pl "gno.land/r/demo/pool"
)

var (
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions position/_TEST_position_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"encoding/gjson"

p "gno.land/r/demo/pool"
pl "gno.land/r/demo/pool"
)

var (
Expand All @@ -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
Expand All @@ -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
Expand Down
21 changes: 20 additions & 1 deletion position/gno.mod
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
module gno.land/r/demo/position
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
)
4 changes: 3 additions & 1 deletion position/gno_helper.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package position
import (
"std"
"time"

"gno.land/r/demo/consts"
)

func GetChainID() string {
Expand All @@ -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()
}

Expand Down
6 changes: 3 additions & 3 deletions position/liquidity_management.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions position/position.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion position/test_helper.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit e7a4d13

Please sign in to comment.