Skip to content

Commit

Permalink
chore: unify variable name
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Feb 7, 2024
1 parent 3b334c8 commit 0a30238
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 49 deletions.
6 changes: 3 additions & 3 deletions pool/_RPC_api.gno
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func ApiGetPools() string {
return string(rr)
}

func rpcMakePool(poolKey string) RpcPool {
func rpcMakePool(poolPath string) RpcPool {
rpcPool := RpcPool{}
pool := GetPoolFromPoolKey(poolKey)
pool := GetPoolFromPoolPath(poolPath)

rpcPool.PoolPath = poolKey
rpcPool.PoolPath = poolPath

rpcPool.Token0Path = pool.token0Path
rpcPool.Token1Path = pool.token1Path
Expand Down
6 changes: 3 additions & 3 deletions pool/_RPC_dry.gno
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func DrySwap(
pToken0Path string,
pToken1Path string,
token0Path string,
token1Path string,
pFee uint16,
recipient std.Address,
zeroForOne bool,
Expand All @@ -22,7 +22,7 @@ func DrySwap(
return 0, 0, false
}

pool := GetPool(pToken0Path, pToken1Path, pFee)
pool := GetPool(token0Path, token1Path, pFee)
slot0Start := pool.slot0

if zeroForOne {
Expand Down
1 change: 0 additions & 1 deletion pool/bit_math.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pool

import (
"gno.land/p/demo/ufmt"

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

Expand Down
1 change: 0 additions & 1 deletion pool/emergency_halt.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"std"

"gno.land/p/demo/ufmt"

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

Expand Down
55 changes: 28 additions & 27 deletions pool/pool_manager.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import (

"gno.land/p/demo/ufmt"

"gno.land/r/demo/consts"

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

var (
admins []std.Address
initialized bool = false

feeAmountTickSpacing map[uint16]int32 = make(map[uint16]int32) // map[fee_amount]tick_spacing
pools map[string]*Pool = make(map[string]*Pool) // map[pool_key]*Pool
feeAmountTickSpacing map[uint16]int32 = make(map[uint16]int32) // map[feeAmount]tick_spacing
pools map[string]*Pool = make(map[string]*Pool) // map[poolPath]*Pool
)

func InitManual() {
Expand All @@ -28,16 +30,16 @@ func InitManual() {
}

func CreatePool(
tokenAPath string,
tokenBPath string,
token0Path string,
token1Path string,
fee uint16,
sqrtPriceX96 bigint,
) *Pool {
require(initialized, "[POOl] pool_manager.gno__CreatePool() || contract must be initialized")
require(tokenAPath != tokenBPath, ufmt.Sprintf("[POOl] pool_manager.gno__CreatePool() || token pair cannot be the same__tokenAPath(%s) != tokenBPath(%s)", tokenAPath, tokenBPath))
require(token0Path != token1Path, ufmt.Sprintf("[POOl] pool_manager.gno__CreatePool() || token pair cannot be the same__token0Path(%s) != token1Path(%s)", token0Path, token1Path))

if tokenBPath < tokenAPath {
tokenAPath, tokenBPath = tokenBPath, tokenAPath
if token1Path < token0Path {
token0Path, token1Path = token1Path, token0Path
tick := -(TickMathGetTickAtSqrtRatio(sqrtPriceX96))
sqrtPriceX96 = TickMathGetSqrtRatioAtTick(tick)
}
Expand All @@ -46,42 +48,41 @@ func CreatePool(
tickSpacing := feeAmountTickSpacing[fee]
require(tickSpacing > 0, ufmt.Sprintf("[POOL] pool_manager.gno__CreatePool() || tickSpacing(%d) > 0", tickSpacing))

// calculate poolKey
poolKey := GetPoolKey(tokenAPath, tokenBPath, fee)
// calculate poolPath
poolPath := GetPoolPath(token0Path, token1Path, fee)

// check whether the pool already exist
pool, exist := pools[poolKey]
require(!exist, ufmt.Sprintf("[POOl] pool_manager.gno__CreatePool() || pool(%s) already exist", poolKey))
pool, exist := pools[poolPath]
require(!exist, ufmt.Sprintf("[POOl] pool_manager.gno__CreatePool() || pool(%s) already exist", poolPath))

if !exist {
// 500 GNS as creation fee
// recipent is same address that receives protocol fee
// r3v4_xxx: change address when publish
gns.TransferFrom(a2u(std.GetOrigCaller()), a2u(std.Address("g1vaekzh6lta047h6lta047h6lta047h6lutmjdk")), 500)
// recipient is same address that receives protocol fee
// r3v4_xxx: change admin address when publish
gns.TransferFrom(a2u(std.GetOrigCaller()), a2u(consts.GNOSWAP_ADMIN), consts.POOL_CREATION_FEE)

pool = newPool(tokenAPath, tokenBPath, fee, tickSpacing, sqrtPriceX96)
pools[poolKey] = pool
pool = newPool(token0Path, token1Path, fee, tickSpacing, sqrtPriceX96)
pools[poolPath] = pool
}

return pool
}

func GetPool(token0, token1 string, fee uint16) *Pool {
poolKey := GetPoolKey(token0, token1, fee)
pool, exist := pools[poolKey]
require(exist, ufmt.Sprintf("[POOL] pool_manager.gno__GetPool() || pool(%s) not found", poolKey))
func GetPool(token0Path, token1Path string, fee uint16) *Pool {
poolPath := GetPoolPath(token0Path, token1Path, fee)
pool, exist := pools[poolPath]
require(exist, ufmt.Sprintf("[POOL] pool_manager.gno__GetPool() || pool(%s) not found", poolPath))

return pool
}

func GetPoolFromPoolKey(poolKey string) *Pool {
pool, exist := pools[poolKey]
require(exist, ufmt.Sprintf("[POOL] pool_manager.gno__GetPoolFromPoolKey() || pool(%s) not found", poolKey))
func GetPoolFromPoolPath(poolPath string) *Pool {
pool, exist := pools[poolPath]
require(exist, ufmt.Sprintf("[POOL] pool_manager.gno__GetPoolFromPoolPath() || pool(%s) not found", poolPath))

return pool
}

func GetPoolKey(token0Path, token1Path string, fee uint16) string {
func GetPoolPath(token0Path, token1Path string, fee uint16) string {
if token0Path < token1Path {
return token0Path + ":" + token1Path + ":" + strconv.Itoa(int(fee))
} else {
Expand All @@ -94,7 +95,7 @@ func AddAdmin(addr std.Address) {
if isAdmin(caller) {
admins = append(admins, addr)
} else {
panic("[POOL] pool_manager.gno__AddAdmin() || caller is not admin") // r3v4_xx: detailed error message ??
panic("[POOL] pool_manager.gno__AddAdmin() || caller is not admin")
}
}

Expand All @@ -112,7 +113,7 @@ func RemoveAdmin(addr std.Address) {
}
}
} else {
panic("[POOL] pool_manager.gno__RemoveAdmin() || caller is not admin") // r3v4_xx: detailed error message ??
panic("[POOL] pool_manager.gno__RemoveAdmin() || caller is not admin")
}
}

Expand Down
1 change: 0 additions & 1 deletion pool/position.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"std"

"gno.land/p/demo/ufmt"

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

Expand Down
1 change: 0 additions & 1 deletion pool/sqrt_price_math.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pool

import (
"gno.land/p/demo/ufmt"

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

Expand Down
1 change: 0 additions & 1 deletion pool/tick.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pool

import (
"gno.land/p/demo/ufmt"

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

Expand Down
1 change: 0 additions & 1 deletion pool/tick_bitmap.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pool

import (
"gno.land/p/demo/ufmt"

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

Expand Down
1 change: 0 additions & 1 deletion pool/tick_math.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pool

import (
"gno.land/p/demo/ufmt"

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

Expand Down
4 changes: 2 additions & 2 deletions position/_RPC_api.gno
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func rpcMakePosition(lpTokenId uint64) RpcPosition {
panic(ufmt.Sprintf("[POSITION] getter_api.gno__rpcMakePosition() || position not found for lpTokenId(%d)", lpTokenId))
}

pool := p.GetPoolFromPoolKey(position.poolKey)
pool := p.GetPoolFromPoolPath(position.poolKey)
currentX96 := pool.PoolGetSlot0SqrtPriceX96()
lowerX96 := p.TickMathGetSqrtRatioAtTick(position.tickLower)
upperX96 := p.TickMathGetSqrtRatioAtTick(position.tickUpper)
Expand Down Expand Up @@ -105,7 +105,7 @@ func unclaimedFee(tokenId uint64) (bigint, bigint) {
tickUpper := positions[tokenId].tickUpper

poolKey := positions[tokenId].poolKey
pool := p.GetPoolFromPoolKey(poolKey)
pool := p.GetPoolFromPoolPath(poolKey)

currentTick := pool.PoolGetSlot0Tick()

Expand Down
2 changes: 1 addition & 1 deletion position/liquidity_management.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func addLiquidity(params AddLiquidityParams) (bigint, bigint, bigint) {
pool := p.GetPoolFromPoolKey(params.poolKey)
pool := p.GetPoolFromPoolPath(params.poolKey)

sqrtPriceX96 := pool.PoolGetSlot0SqrtPriceX96()
sqrtRatioAX96 := p.TickMathGetSqrtRatioAtTick(params.tickLower)
Expand Down
8 changes: 4 additions & 4 deletions position/position.gno
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func mint(params MintParams) (uint64, bigint, bigint, bigint) {
pool := p.GetPool(params.token0, params.token1, params.fee)
liquidity, amount0, amount1 := addLiquidity(
AddLiquidityParams{
poolKey: p.GetPoolKey(params.token0, params.token1, params.fee),
poolKey: p.GetPoolPath(params.token0, params.token1, params.fee),
recipient: GetOrigPkgAddr(), // hardcoded
tickLower: params.tickLower,
tickUpper: params.tickUpper,
Expand All @@ -80,7 +80,7 @@ func mint(params MintParams) (uint64, bigint, bigint, bigint) {
position := Position{
nonce: 0,
operator: PrevRealmAddr(),
poolKey: p.GetPoolKey(params.token0, params.token1, params.fee),
poolKey: p.GetPoolPath(params.token0, params.token1, params.fee),
tickLower: params.tickLower,
tickUpper: params.tickUpper,
liquidity: liquidity,
Expand Down Expand Up @@ -108,7 +108,7 @@ func CollectFee(tokenId uint64) (uint64, bigint, bigint, string) { // tokenId, t
p.Burn(pToken0, pToken1, pFee, position.tickLower, position.tickUpper, 0) // burn '0' liquidity to collect fee

positionKey := positionKeyCompute(GetOrigPkgAddr(), position.tickLower, position.tickUpper)
pool := p.GetPoolFromPoolKey(position.poolKey)
pool := p.GetPoolFromPoolPath(position.poolKey)
feeGrowthInside0LastX128, feeGrowthInside1LastX128 := pool.PoolGetPositionFeeGrowthInside0LastX128(positionKey), pool.PoolGetPositionFeeGrowthInside1LastX128(positionKey)

tokensOwed0 += (feeGrowthInside0LastX128 - position.feeGrowthInside0LastX128) * position.liquidity / Q128
Expand Down Expand Up @@ -145,7 +145,7 @@ func Burn(tokenId uint64) (uint64, bigint, bigint, bigint, string) { // tokenId,
position := positions[tokenId]
positionLiquidity := position.liquidity

pool := p.GetPoolFromPoolKey(position.poolKey) // poolKey == poolPath
pool := p.GetPoolFromPoolPath(position.poolKey) // poolKey == poolPath

pToken0, pToken1, pFee := poolKeyDivide(position.poolKey)
burnAmount0, burnAmount1 := p.Burn(pToken0, pToken1, pFee, position.tickLower, position.tickUpper, positionLiquidity)
Expand Down
2 changes: 1 addition & 1 deletion router/_RPC_api.gno
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func getRatiosFromBase(maxHops int) []TokenPathPrice {

func calculateTokenPrice(currentToken, swapPath string, numPools, proceed int, currentPriceX96 bigint) bigint {
currentPoolPathKey := makePoolPath(swapPath, proceed)
currentPool := p.GetPoolFromPoolKey(currentPoolPathKey)
currentPool := p.GetPoolFromPoolPath(currentPoolPathKey)

poolToken0 := currentPool.PoolGetToken0Path()
poolToken1 := currentPool.PoolGetToken1Path()
Expand Down
2 changes: 1 addition & 1 deletion staker/staker.gno
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func deleteFromIncentives(m map[string]Incentive, key string) map[string]Incenti
func getTokenPairBalanceFromPosition(tokenId uint64) (bigint, bigint) {
poolKey := pos.PositionGetPositionPoolKey(tokenId)

pool := pl.GetPoolFromPoolKey(poolKey)
pool := pl.GetPoolFromPoolPath(poolKey)
currentX96 := pool.PoolGetSlot0SqrtPriceX96()
lowerX96 := pl.TickMathGetSqrtRatioAtTick(pos.PositionGetPositionTickLower(tokenId))
upperX96 := pl.TickMathGetSqrtRatioAtTick(pos.PositionGetPositionTickUpper(tokenId))
Expand Down

0 comments on commit 0a30238

Please sign in to comment.