Skip to content

Commit 6735233

Browse files
onlyhyder3v4s
andcommitted
GSW-1839 Refactor/position contract utils (#433)
* 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]>
1 parent 7b62f12 commit 6735233

File tree

3 files changed

+434
-25
lines changed

3 files changed

+434
-25
lines changed

Diff for: position/position.gno

+10-10
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ func Mint(
105105

106106
poolSqrtPriceX96 := pl.PoolGetSlot0SqrtPriceX96(poolPath)
107107

108-
prevAddr, prevRealm := getPrev()
108+
prevAddr, prevPkgPath := getPrevAsString()
109109

110110
std.Emit(
111111
"Mint",
112112
"prevAddr", prevAddr,
113-
"prevRealm", prevRealm,
113+
"prevRealm", prevPkgPath,
114114
"tickLower", ufmt.Sprintf("%d", tickLower),
115115
"tickUpper", ufmt.Sprintf("%d", tickUpper),
116116
"poolPath", poolPath,
@@ -265,12 +265,12 @@ func IncreaseLiquidity(
265265

266266
poolSqrtPriceX96 := pl.PoolGetSlot0SqrtPriceX96(poolPath)
267267

268-
prevAddr, prevRealm := getPrev()
268+
prevAddr, prevPkgPath := getPrevAsString()
269269

270270
std.Emit(
271271
"IncreaseLiquidity",
272272
"prevAddr", prevAddr,
273-
"prevRealm", prevRealm,
273+
"prevRealm", prevPkgPath,
274274
"lpTokenId", ufmt.Sprintf("%d", tokenId),
275275
"internal_poolPath", poolPath,
276276
"internal_liquidity", liquidity.ToString(),
@@ -386,12 +386,12 @@ func DecreaseLiquidity(
386386

387387
poolSqrtPriceX96 := pl.PoolGetSlot0SqrtPriceX96(poolPath)
388388

389-
prevAddr, prevRealm := getPrev()
389+
prevAddr, prevPkgPath := getPrevAsString()
390390

391391
std.Emit(
392392
"DecreaseLiquidity",
393393
"prevAddr", prevAddr,
394-
"prevRealm", prevRealm,
394+
"prevRealm", prevPkgPath,
395395
"lpTokenId", ufmt.Sprintf("%d", tokenId),
396396
"liquidityRatio", ufmt.Sprintf("%d", liquidityRatio),
397397
"internal_poolPath", poolPath,
@@ -615,12 +615,12 @@ func Reposition(
615615

616616
poolSqrtPriceX96 := pl.PoolGetSlot0SqrtPriceX96(position.poolKey)
617617

618-
prevAddr, prevRealm := getPrev()
618+
prevAddr, prevPkgPath := getPrevAsString()
619619

620620
std.Emit(
621621
"Reposition",
622622
"prevAddr", prevAddr,
623-
"prevRealm", prevRealm,
623+
"prevRealm", prevPkgPath,
624624
"lpTokenId", ufmt.Sprintf("%d", tokenId),
625625
"tickLower", ufmt.Sprintf("%d", tickLower),
626626
"tickUpper", ufmt.Sprintf("%d", tickUpper),
@@ -736,12 +736,12 @@ func CollectFee(tokenId uint64, unwrapResult bool) (uint64, string, string, stri
736736
}
737737
}
738738

739-
prevAddr, prevRealm := getPrev()
739+
prevAddr, prevPkgPath := getPrevAsString()
740740

741741
std.Emit(
742742
"CollectSwapFee",
743743
"prevAddr", prevAddr,
744-
"prevRealm", prevRealm,
744+
"prevRealm", prevPkgPath,
745745
"lpTokenId", ufmt.Sprintf("%d", tokenId),
746746
"internal_fee0", withoutFee0,
747747
"internal_fee1", withoutFee1,

Diff for: position/utils.gno

+80-15
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,86 @@ import (
66

77
"gno.land/p/demo/ufmt"
88
pusers "gno.land/p/demo/users"
9+
"gno.land/r/gnoswap/v1/common"
10+
"gno.land/r/gnoswap/v1/consts"
911
)
1012

11-
func checkDeadline(deadline int64) {
12-
now := time.Now().Unix()
13-
if now > deadline {
14-
panic(addDetailToError(
15-
errExpired,
16-
ufmt.Sprintf("utils.gno__checkDeadline() || transaction too old, now(%d) > deadline(%d)", now, deadline),
17-
))
18-
}
19-
}
20-
13+
// a2u converts std.Address to pusers.AddressOrName.
14+
// pusers is a package that contains the user-related functions.
15+
//
16+
// Input:
17+
// - addr: the address to convert
18+
//
19+
// Output:
20+
// - pusers.AddressOrName: the converted address
2121
func a2u(addr std.Address) pusers.AddressOrName {
2222
return pusers.AddressOrName(addr)
2323
}
2424

25-
func prevRealm() string {
26-
return std.PrevRealm().PkgPath()
25+
// derivePkgAddr derives the Realm address from it's pkgpath parameter
26+
func derivePkgAddr(pkgPath string) std.Address {
27+
return std.DerivePkgAddr(pkgPath)
2728
}
2829

29-
func isUserCall() bool {
30-
return std.PrevRealm().IsUser()
30+
// getOrigPkgAddr returns the original package address.
31+
// In position contract, original package address is the position address.
32+
func getOrigPkgAddr() std.Address {
33+
return consts.POSITION_ADDR
3134
}
3235

33-
func getPrev() (string, string) {
36+
// getPrevRealm returns object of the previous realm.
37+
func getPrevRealm() std.Realm {
38+
return std.PrevRealm()
39+
}
40+
41+
// getPrevAddr returns the address of the previous realm.
42+
func getPrevAddr() std.Address {
43+
return std.PrevRealm().Addr()
44+
}
45+
46+
// getPrev returns the address and package path of the previous realm.
47+
func getPrevAsString() (string, string) {
3448
prev := std.PrevRealm()
3549
return prev.Addr().String(), prev.PkgPath()
3650
}
3751

52+
// isUserCall returns true if the caller is a user.
53+
func isUserCall() bool {
54+
return std.PrevRealm().IsUser()
55+
}
56+
57+
// checkDeadline checks if the deadline is expired.
58+
// If the deadline is expired, it panics.
59+
// The deadline is expired if the current time is greater than the deadline.
60+
// Input:
61+
// - deadline: the deadline to check
62+
func checkDeadline(deadline int64) {
63+
now := time.Now().Unix()
64+
if now > deadline {
65+
panic(newErrorWithDetail(
66+
errExpired,
67+
ufmt.Sprintf("transaction too old, now(%d) > deadline(%d)", now, deadline),
68+
))
69+
}
70+
}
71+
72+
// assertOnlyUserOrStaker panics if the caller is not a user or staker.
73+
func assertOnlyUserOrStaker(caller std.Realm) {
74+
if !caller.IsUser() {
75+
if err := common.StakerOnly(caller.Addr()); err != nil {
76+
panic(newErrorWithDetail(
77+
errNoPermission,
78+
ufmt.Sprintf("from (%s)", caller.Addr()),
79+
))
80+
}
81+
}
82+
}
83+
84+
// assertOnlyNotHalted panics if the contract is halted.
85+
func assertOnlyNotHalted() {
86+
common.IsHalted()
87+
}
88+
3889
// assertOnlyValidAddress panics if the address is invalid.
3990
func assertOnlyValidAddress(addr std.Address) {
4091
if !addr.IsValid() {
@@ -44,3 +95,17 @@ func assertOnlyValidAddress(addr std.Address) {
4495
))
4596
}
4697
}
98+
99+
// assertOnlyValidAddress panics if the address is invalid or previous address is not
100+
// different from the other address.
101+
func assertOnlyValidAddressWith(prevAddr, otherAddr std.Address) {
102+
assertOnlyValidAddress(prevAddr)
103+
assertOnlyValidAddress(otherAddr)
104+
105+
if prevAddr != otherAddr {
106+
panic(newErrorWithDetail(
107+
errInvalidAddress,
108+
ufmt.Sprintf("(%s, %s)", prevAddr, otherAddr),
109+
))
110+
}
111+
}

0 commit comments

Comments
 (0)