Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GSW-1839 Refactor/position contract utils #433

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading