Skip to content

Commit adec74f

Browse files
committed
feat: use avl.Tree instead of map
1 parent 89350f8 commit adec74f

18 files changed

+235
-215
lines changed

pool/_helper_test.gno

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import (
44
"std"
55
"testing"
66

7+
"gno.land/p/demo/avl"
8+
"gno.land/p/demo/testutils"
9+
"gno.land/p/demo/uassert"
10+
pusers "gno.land/p/demo/users"
11+
12+
"gno.land/r/demo/users"
13+
714
"gno.land/r/demo/wugnot"
815
"gno.land/r/gnoswap/v1/gns"
916
"gno.land/r/onbloc/bar"
@@ -13,10 +20,6 @@ import (
1320
"gno.land/r/onbloc/qux"
1421
"gno.land/r/onbloc/usdc"
1522

16-
"gno.land/p/demo/testutils"
17-
"gno.land/p/demo/uassert"
18-
pusers "gno.land/p/demo/users"
19-
"gno.land/r/demo/users"
2023
"gno.land/r/gnoswap/v1/consts"
2124
pn "gno.land/r/gnoswap/v1/position"
2225
)
@@ -66,8 +69,7 @@ func InitialisePoolTest(t *testing.T) {
6669
std.TestSetOrigCaller(users.Resolve(admin))
6770
TokenApprove(t, gnsPath, admin, pool, maxApprove)
6871
poolPath := GetPoolPath(wugnotPath, gnsPath, fee3000)
69-
_, exist := pools[poolPath]
70-
if !exist {
72+
if !DoesPoolPathExist(poolPath) {
7173
CreatePool(wugnotPath, gnsPath, fee3000, "79228162514264337593543950336")
7274
}
7375

@@ -361,7 +363,7 @@ func ugnotDeposit(t *testing.T, addr std.Address, amount uint64) {
361363

362364
// resetObject resets the object state(clear or make it default values)
363365
func resetObject(t *testing.T) {
364-
pools = make(poolMap)
366+
pools = avl.NewTree()
365367
slot0FeeProtocol = 0
366368
poolCreationFee = 100_000_000
367369
withdrawalFeeBPS = 100
@@ -414,11 +416,11 @@ func burnUsdc(addr pusers.AddressOrName) {
414416

415417
func TestBeforeResetObject(t *testing.T) {
416418
// make some data
417-
pools = make(poolMap)
418-
pools["gno.land/r/gnoswap/v1/gns:gno.land/r/onbloc/usdc"] = &Pool{
419+
pools = avl.NewTree()
420+
pools.Set("gno.land/r/gnoswap/v1/gns:gno.land/r/onbloc/usdc", &Pool{
419421
token0Path: "gno.land/r/gnoswap/v1/gns",
420422
token1Path: "gno.land/r/onbloc/usdc",
421-
}
423+
})
422424

423425
slot0FeeProtocol = 1
424426
poolCreationFee = 100_000_000
@@ -435,7 +437,7 @@ func TestBeforeResetObject(t *testing.T) {
435437

436438
func TestResetObject(t *testing.T) {
437439
resetObject(t)
438-
uassert.Equal(t, len(pools), 0)
440+
uassert.Equal(t, pools.Size(), 0)
439441
uassert.Equal(t, slot0FeeProtocol, uint8(0))
440442
uassert.Equal(t, poolCreationFee, uint64(100_000_000))
441443
uassert.Equal(t, withdrawalFeeBPS, uint64(100))

pool/api.gno

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package pool
22

33
import (
44
b64 "encoding/base64"
5-
6-
"gno.land/p/demo/json"
7-
85
"std"
96
"strconv"
107
"strings"
118
"time"
129

10+
"gno.land/p/demo/json"
1311
"gno.land/p/demo/ufmt"
12+
13+
u256 "gno.land/p/gnoswap/uint256"
1414
)
1515

1616
type RpcPool struct {
@@ -81,10 +81,11 @@ type RpcPosition struct {
8181

8282
func ApiGetPools() string {
8383
rpcPools := []RpcPool{}
84-
for poolPath, _ := range pools {
84+
pools.Iterate("", "", func(poolPath string, value interface{}) bool {
8585
rpcPool := rpcMakePool(poolPath)
8686
rpcPools = append(rpcPools, rpcPool)
87-
}
87+
return false
88+
})
8889

8990
responses := json.ArrayNode("", []*json.Node{})
9091
for _, pool := range rpcPools {
@@ -122,10 +123,10 @@ func ApiGetPools() string {
122123
}
123124

124125
func ApiGetPool(poolPath string) string {
125-
_, exist := pools[poolPath]
126-
if !exist {
126+
if getPoolFromPoolPath(poolPath) == nil {
127127
return ""
128128
}
129+
129130
rpcPool := rpcMakePool(poolPath)
130131

131132
responseNode := json.ObjectNode("", map[string]*json.Node{
@@ -198,8 +199,10 @@ func rpcMakePool(poolPath string) RpcPool {
198199
rpcPool.Liquidity = pool.liquidity.ToString()
199200

200201
rpcPool.Ticks = RpcTicks{}
201-
for tick, tickInfo := range pool.ticks {
202-
rpcPool.Ticks[tick] = RpcTickInfo{
202+
pool.ticks.Iterate("", "", func(strTick string, value interface{}) bool {
203+
tick, _ := strconv.Atoi(strTick)
204+
tickInfo := value.(TickInfo)
205+
rpcPool.Ticks[int32(tick)] = RpcTickInfo{
203206
LiquidityGross: tickInfo.liquidityGross.ToString(),
204207
LiquidityNet: tickInfo.liquidityNet.ToString(),
205208
FeeGrowthOutside0X128: tickInfo.feeGrowthOutside0X128.ToString(),
@@ -209,18 +212,24 @@ func rpcMakePool(poolPath string) RpcPool {
209212
SecondsOutside: tickInfo.secondsOutside,
210213
Initialized: tickInfo.initialized,
211214
}
212-
}
215+
216+
return false
217+
})
213218

214219
rpcPool.TickBitmaps = RpcTickBitmaps{}
215-
for tick, tickBitmap := range pool.tickBitmaps {
216-
rpcPool.TickBitmaps[tick] = tickBitmap.ToString()
217-
}
220+
pool.tickBitmaps.Iterate("", "", func(strTick string, value interface{}) bool {
221+
tick, _ := strconv.Atoi(strTick)
222+
tickBitmap := value.(*u256.Uint)
223+
rpcPool.TickBitmaps[int16(tick)] = tickBitmap.ToString()
224+
225+
return false
226+
})
218227

219-
Positions := pool.positions
220228
rpcPositions := []RpcPosition{}
221-
for posKey, posInfo := range Positions {
229+
pool.positions.Iterate("", "", func(posKey string, value interface{}) bool {
222230
owner, tickLower, tickUpper := posKeyDivide(posKey)
223231

232+
posInfo := value.(PositionInfo)
224233
rpcPositions = append(rpcPositions, RpcPosition{
225234
Owner: owner,
226235
TickLower: tickLower,
@@ -229,7 +238,9 @@ func rpcMakePool(poolPath string) RpcPool {
229238
Token0Owed: posInfo.tokensOwed0.ToString(),
230239
Token1Owed: posInfo.tokensOwed1.ToString(),
231240
})
232-
}
241+
242+
return false
243+
})
233244
rpcPool.Positions = rpcPositions
234245

235246
return rpcPool

pool/api_test.gno

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestInitTwoPools(t *testing.T) {
2121
// bar:baz
2222
CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") // tick 10000
2323

24-
uassert.Equal(t, len(pools), 2)
24+
uassert.Equal(t, pools.Size(), 2)
2525
}
2626

2727
func TestApiGetPools(t *testing.T) {

pool/getter.gno

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package pool
33
// pool
44
func PoolGetPoolList() []string {
55
poolPaths := []string{}
6-
for poolPath, _ := range pools {
6+
7+
pools.Iterate("", "", func(poolPath string, value interface{}) bool {
78
poolPaths = append(poolPaths, poolPath)
8-
}
9+
return false
10+
})
911

1012
return poolPaths
1113
}

pool/getter_test.gno

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package pool
33
import (
44
"testing"
55

6+
"gno.land/p/demo/avl"
7+
68
i256 "gno.land/p/gnoswap/int256"
79
u256 "gno.land/p/gnoswap/uint256"
810
)
@@ -35,8 +37,8 @@ func TestInitData(t *testing.T) {
3537
liquidity: u256.NewUint(1000000),
3638
}
3739

38-
mockTicks := Ticks{}
39-
mockTicks[0] = TickInfo{
40+
mockTicks := avl.NewTree()
41+
mockTicks.Set("0", TickInfo{
4042
liquidityGross: u256.NewUint(1000000),
4143
liquidityNet: i256.NewInt(2000000),
4244
feeGrowthOutside0X128: u256.NewUint(3000000),
@@ -45,20 +47,20 @@ func TestInitData(t *testing.T) {
4547
secondsPerLiquidityOutsideX128: u256.NewUint(6000000),
4648
secondsOutside: 7,
4749
initialized: true,
48-
}
50+
})
4951
mockPool.ticks = mockTicks
5052

51-
mockPositions := Positions{}
52-
mockPositions["test_position"] = PositionInfo{
53+
mockPositions := avl.NewTree()
54+
mockPositions.Set("test_position", PositionInfo{
5355
liquidity: u256.NewUint(1000000),
5456
feeGrowthInside0LastX128: u256.NewUint(2000000),
5557
feeGrowthInside1LastX128: u256.NewUint(3000000),
5658
tokensOwed0: u256.NewUint(4000000),
5759
tokensOwed1: u256.NewUint(5000000),
58-
}
60+
})
5961
mockPool.positions = mockPositions
6062

61-
pools["token0:token1:3000"] = mockPool
63+
pools.Set("token0:token1:3000", mockPool)
6264
}
6365

6466
func TestPoolGetters(t *testing.T) {

pool/pool.gno

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func Burn(
104104
}
105105

106106
positionKey := positionGetKey(caller, tickLower, tickUpper)
107-
pool.positions[positionKey] = position
107+
pool.positions.Set(positionKey, position)
108108

109109
// actual token transfer happens in Collect()
110110
return amount0.ToString(), amount1.ToString()
@@ -141,13 +141,14 @@ func Collect(
141141
pool := GetPool(token0Path, token1Path, fee)
142142

143143
positionKey := positionGetKey(std.PrevRealm().Addr(), tickLower, tickUpper)
144-
position, exist := pool.positions[positionKey]
144+
iposition, exist := pool.positions.Get(positionKey)
145145
if !exist {
146146
panic(addDetailToError(
147147
errDataNotFound,
148148
ufmt.Sprintf("pool.gno__Collect() || positionKey(%s) does not exist", positionKey),
149149
))
150150
}
151+
position := iposition.(PositionInfo)
151152

152153
var amount0, amount1 *u256.Uint
153154

@@ -167,7 +168,7 @@ func Collect(
167168
token1 := common.GetTokenTeller(pool.token1Path)
168169
checkTransferError(token1.Transfer(recipient, amount1.Uint64()))
169170

170-
pool.positions[positionKey] = position
171+
pool.positions.Set(positionKey, position)
171172

172173
return amount0.ToString(), amount1.ToString()
173174
}
@@ -709,9 +710,11 @@ func setFeeProtocol(feeProtocol0, feeProtocol1 uint8) uint8 {
709710
newFee := feeProtocol0 + (feeProtocol1 << 4) // ( << 4 ) = ( * 16 )
710711

711712
// iterate all pool
712-
for _, pool := range pools {
713+
pools.Iterate("", "", func(poolPath string, value interface{}) bool {
714+
pool := value.(*Pool)
713715
pool.slot0.feeProtocol = newFee
714-
}
716+
return false
717+
})
715718

716719
// update slot0
717720
slot0FeeProtocol = newFee
@@ -1066,10 +1069,20 @@ func (p *Pool) PoolGetLiquidity() *u256.Uint {
10661069
}
10671070

10681071
func mustGetPool(poolPath string) *Pool {
1069-
pool, exist := pools[poolPath]
1070-
if !exist {
1071-
panic(addDetailToError(errDataNotFound,
1072-
ufmt.Sprintf("poolPath(%s) does not exist", poolPath)))
1072+
pool := getPoolFromPoolPath(poolPath)
1073+
if pool == nil {
1074+
panic(addDetailToError(
1075+
errDataNotFound,
1076+
ufmt.Sprintf("pool.gno__mustGetPool() || expected poolPath(%s) to exist", poolPath),
1077+
))
10731078
}
10741079
return pool
10751080
}
1081+
1082+
func getPoolFromPoolPath(poolPath string) *Pool {
1083+
pool, exist := pools.Get(poolPath)
1084+
if !exist {
1085+
return nil
1086+
}
1087+
return pool.(*Pool)
1088+
}

0 commit comments

Comments
 (0)