Skip to content

Commit

Permalink
GSW-1839 Refactor/position contract utils (#433)
Browse files Browse the repository at this point in the history
* GSW-1839 refactor: integrated helper and test code
- integrated helper with nft helper
- add test helper code
- add test code for helper
- change file filename

* GSW-1839 refactor: utils
- add assert functions
- refactor original util functions

* Update position/utils_test.gno
* test: Update to use the correct test values

---------

Co-authored-by: Blake <[email protected]>
  • Loading branch information
onlyhyde and r3v4s authored Dec 16, 2024
1 parent 0cb7ce0 commit 61f36d4
Show file tree
Hide file tree
Showing 3 changed files with 434 additions and 25 deletions.
20 changes: 10 additions & 10 deletions position/position.gno
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ func Mint(

poolSqrtPriceX96 := pl.PoolGetSlot0SqrtPriceX96(poolPath)

prevAddr, prevRealm := getPrev()
prevAddr, prevPkgPath := getPrevAsString()

std.Emit(
"Mint",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"prevRealm", prevPkgPath,
"tickLower", ufmt.Sprintf("%d", tickLower),
"tickUpper", ufmt.Sprintf("%d", tickUpper),
"poolPath", poolPath,
Expand Down Expand Up @@ -265,12 +265,12 @@ func IncreaseLiquidity(

poolSqrtPriceX96 := pl.PoolGetSlot0SqrtPriceX96(poolPath)

prevAddr, prevRealm := getPrev()
prevAddr, prevPkgPath := getPrevAsString()

std.Emit(
"IncreaseLiquidity",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"prevRealm", prevPkgPath,
"lpTokenId", ufmt.Sprintf("%d", tokenId),
"internal_poolPath", poolPath,
"internal_liquidity", liquidity.ToString(),
Expand Down Expand Up @@ -386,12 +386,12 @@ func DecreaseLiquidity(

poolSqrtPriceX96 := pl.PoolGetSlot0SqrtPriceX96(poolPath)

prevAddr, prevRealm := getPrev()
prevAddr, prevPkgPath := getPrevAsString()

std.Emit(
"DecreaseLiquidity",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"prevRealm", prevPkgPath,
"lpTokenId", ufmt.Sprintf("%d", tokenId),
"liquidityRatio", ufmt.Sprintf("%d", liquidityRatio),
"internal_poolPath", poolPath,
Expand Down Expand Up @@ -615,12 +615,12 @@ func Reposition(

poolSqrtPriceX96 := pl.PoolGetSlot0SqrtPriceX96(position.poolKey)

prevAddr, prevRealm := getPrev()
prevAddr, prevPkgPath := getPrevAsString()

std.Emit(
"Reposition",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"prevRealm", prevPkgPath,
"lpTokenId", ufmt.Sprintf("%d", tokenId),
"tickLower", ufmt.Sprintf("%d", tickLower),
"tickUpper", ufmt.Sprintf("%d", tickUpper),
Expand Down Expand Up @@ -736,12 +736,12 @@ func CollectFee(tokenId uint64, unwrapResult bool) (uint64, string, string, stri
}
}

prevAddr, prevRealm := getPrev()
prevAddr, prevPkgPath := getPrevAsString()

std.Emit(
"CollectSwapFee",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"prevRealm", prevPkgPath,
"lpTokenId", ufmt.Sprintf("%d", tokenId),
"internal_fee0", withoutFee0,
"internal_fee1", withoutFee1,
Expand Down
95 changes: 80 additions & 15 deletions position/utils.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,86 @@ import (

"gno.land/p/demo/ufmt"
pusers "gno.land/p/demo/users"
"gno.land/r/gnoswap/v1/common"
"gno.land/r/gnoswap/v1/consts"
)

func checkDeadline(deadline int64) {
now := time.Now().Unix()
if now > deadline {
panic(addDetailToError(
errExpired,
ufmt.Sprintf("utils.gno__checkDeadline() || transaction too old, now(%d) > deadline(%d)", now, deadline),
))
}
}

// a2u converts std.Address to pusers.AddressOrName.
// pusers is a package that contains the user-related functions.
//
// Input:
// - addr: the address to convert
//
// Output:
// - pusers.AddressOrName: the converted address
func a2u(addr std.Address) pusers.AddressOrName {
return pusers.AddressOrName(addr)
}

func prevRealm() string {
return std.PrevRealm().PkgPath()
// derivePkgAddr derives the Realm address from it's pkgpath parameter
func derivePkgAddr(pkgPath string) std.Address {
return std.DerivePkgAddr(pkgPath)
}

func isUserCall() bool {
return std.PrevRealm().IsUser()
// getOrigPkgAddr returns the original package address.
// In position contract, original package address is the position address.
func getOrigPkgAddr() std.Address {
return consts.POSITION_ADDR
}

func getPrev() (string, string) {
// getPrevRealm returns object of the previous realm.
func getPrevRealm() std.Realm {
return std.PrevRealm()
}

// getPrevAddr returns the address of the previous realm.
func getPrevAddr() std.Address {
return std.PrevRealm().Addr()
}

// getPrev returns the address and package path of the previous realm.
func getPrevAsString() (string, string) {
prev := std.PrevRealm()
return prev.Addr().String(), prev.PkgPath()
}

// isUserCall returns true if the caller is a user.
func isUserCall() bool {
return std.PrevRealm().IsUser()
}

// checkDeadline checks if the deadline is expired.
// If the deadline is expired, it panics.
// The deadline is expired if the current time is greater than the deadline.
// Input:
// - deadline: the deadline to check
func checkDeadline(deadline int64) {
now := time.Now().Unix()
if now > deadline {
panic(newErrorWithDetail(
errExpired,
ufmt.Sprintf("transaction too old, now(%d) > deadline(%d)", now, deadline),
))
}
}

// assertOnlyUserOrStaker panics if the caller is not a user or staker.
func assertOnlyUserOrStaker(caller std.Realm) {
if !caller.IsUser() {
if err := common.StakerOnly(caller.Addr()); err != nil {
panic(newErrorWithDetail(
errNoPermission,
ufmt.Sprintf("from (%s)", caller.Addr()),
))
}
}
}

// assertOnlyNotHalted panics if the contract is halted.
func assertOnlyNotHalted() {
common.IsHalted()
}

// assertOnlyValidAddress panics if the address is invalid.
func assertOnlyValidAddress(addr std.Address) {
if !addr.IsValid() {
Expand All @@ -44,3 +95,17 @@ func assertOnlyValidAddress(addr std.Address) {
))
}
}

// assertOnlyValidAddress panics if the address is invalid or previous address is not
// different from the other address.
func assertOnlyValidAddressWith(prevAddr, otherAddr std.Address) {
assertOnlyValidAddress(prevAddr)
assertOnlyValidAddress(otherAddr)

if prevAddr != otherAddr {
panic(newErrorWithDetail(
errInvalidAddress,
ufmt.Sprintf("(%s, %s)", prevAddr, otherAddr),
))
}
}
Loading

0 comments on commit 61f36d4

Please sign in to comment.