@@ -10,15 +10,6 @@ import (
1010 "github.com/sideprotocol/ibcswap/v6/modules/apps/101-interchain-swap/types"
1111)
1212
13- // // SetInterchainLiquidityPool set a specific interchainLiquidityPool in the store from its index
14- // func (k Keeper) SetInterchainLiquidityPool(ctx sdk.Context, interchainLiquidityPool types.InterchainLiquidityPool) {
15- // store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
16- // b := k.cdc.MustMarshal(&interchainLiquidityPool)
17- // store.Set(types.InterchainLiquidityPoolKey(
18- // interchainLiquidityPool.Id,
19- // ), b)
20- // }
21-
2213// SetInterchainLiquidityPool set a specific interchainLiquidityPool in the store from its index
2314func (k Keeper ) SetInitialPoolAssets (ctx sdk.Context , poolId string , tokens sdk.Coins ) {
2415 store := prefix .NewStore (ctx .KVStore (k .storeKey ), types .KeyPrefix (poolId ))
@@ -71,24 +62,24 @@ func (k Keeper) GetInitialPoolAssets(ctx sdk.Context, poolId string) sdk.Coins {
7162 return tokens
7263}
7364
74- // GetInterchainLiquidityPool returns a interchainLiquidityPool from its index
75- func (k Keeper ) GetInterchainLiquidityPool (
76- ctx sdk.Context ,
77- poolId string ,
65+ // // GetInterchainLiquidityPool returns a interchainLiquidityPool from its index
66+ // func (k Keeper) GetInterchainLiquidityPool(
67+ // ctx sdk.Context,
68+ // poolId string,
7869
79- ) (val types.InterchainLiquidityPool , found bool ) {
80- store := prefix .NewStore (ctx .KVStore (k .storeKey ), types .KeyPrefix (types .InterchainLiquidityPoolKeyPrefix ))
70+ // ) (val types.InterchainLiquidityPool, found bool) {
8171
82- b := store .Get (types .InterchainLiquidityPoolKey (
83- poolId ,
84- ))
85- if b == nil {
86- return val , false
87- }
72+ // store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
73+ // b := store.Get(types.InterchainLiquidityPoolKey(
74+ // poolId,
75+ // ))
76+ // if b == nil {
77+ // return val, false
78+ // }
8879
89- k .cdc .MustUnmarshal (b , & val )
90- return val , true
91- }
80+ // k.cdc.MustUnmarshal(b, &val)
81+ // return val, true
82+ // }
9283
9384// RemoveInterchainLiquidityPool removes a interchainLiquidityPool from the store
9485func (k Keeper ) RemoveInterchainLiquidityPool (
@@ -102,50 +93,32 @@ func (k Keeper) RemoveInterchainLiquidityPool(
10293 ))
10394}
10495
105- // // GetAllInterchainLiquidityPool returns all interchainLiquidityPool
106- // func (k Keeper) GetAllInterchainLiquidityPool(ctx sdk.Context) (list []types.InterchainLiquidityPool) {
96+ // func (k Keeper) SetInterchainLiquidityPool(ctx sdk.Context, interchainLiquidityPool types.InterchainLiquidityPool) {
10797// store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
108- // iterator := sdk.KVStorePrefixIterator(store, []byte{})
109-
110- // defer iterator.Close()
111-
112- // for ; iterator.Valid(); iterator.Next() {
113- // var val types.InterchainLiquidityPool
114- // k.cdc.MustUnmarshal(iterator.Value(), &val)
115- // list = append(list, val)
116- // }
117- // return
118- // }
119-
120- // CurrentPoolCountKey stores the current number of pools.
121- var CurrentPoolCountKey = []byte ("CurrentPoolCount" )
122-
123- func (k Keeper ) SetInterchainLiquidityPool (ctx sdk.Context , interchainLiquidityPool types.InterchainLiquidityPool ) {
124- store := prefix .NewStore (ctx .KVStore (k .storeKey ), types .KeyPrefix (types .InterchainLiquidityPoolKeyPrefix ))
12598
126- // Get current pool count
127- poolCount := k .GetPoolCount (ctx )
99+ // // Get current pool count
100+ // poolCount := k.GetPoolCount(ctx)
128101
129- // Increment the count
130- poolCount ++
102+ // // Increment the count
103+ // poolCount++
131104
132- // Set the new count
133- k .SetPoolCount (ctx , poolCount )
105+ // // Set the new count
106+ // k.SetPoolCount(ctx, poolCount)
134107
135- // Marshal the pool and set in store
136- b := k .cdc .MustMarshal (& interchainLiquidityPool )
137- store .Set (GetInterchainLiquidityPoolKey (poolCount ), b )
108+ // // Marshal the pool and set in store
109+ // b := k.cdc.MustMarshal(&interchainLiquidityPool)
110+ // store.Set(GetInterchainLiquidityPoolKey(poolCount), b)
138111
139- // Check if we exceed max pools
140- if poolCount > types .MaxPoolCount {
141- // Remove the oldest pool
142- store .Delete (GetInterchainLiquidityPoolKey (poolCount - types .MaxPoolCount ))
143- }
144- }
112+ // // Check if we exceed max pools
113+ // if poolCount > types.MaxPoolCount {
114+ // // Remove the oldest pool
115+ // store.Delete(GetInterchainLiquidityPoolKey(poolCount - types.MaxPoolCount))
116+ // }
117+ // }
145118
146119func (k Keeper ) GetPoolCount (ctx sdk.Context ) uint64 {
147120 store := prefix .NewStore (ctx .KVStore (k .storeKey ), types .KeyPrefix (types .InterchainLiquidityPoolKeyPrefix ))
148- b := store .Get (CurrentPoolCountKey )
121+ b := store .Get (types . CurrentPoolCountKey )
149122 if b == nil {
150123 return 0
151124 }
@@ -156,7 +129,7 @@ func (k Keeper) SetPoolCount(ctx sdk.Context, count uint64) {
156129 store := prefix .NewStore (ctx .KVStore (k .storeKey ), types .KeyPrefix (types .InterchainLiquidityPoolKeyPrefix ))
157130 b := make ([]byte , 8 )
158131 binary .BigEndian .PutUint64 (b , count )
159- store .Set (CurrentPoolCountKey , b )
132+ store .Set (types . CurrentPoolCountKey , b )
160133}
161134
162135func GetInterchainLiquidityPoolKey (count uint64 ) []byte {
@@ -180,3 +153,66 @@ func (k Keeper) GetAllInterchainLiquidityPool(ctx sdk.Context) (list []types.Int
180153 }
181154 return
182155}
156+
157+ // Sets the mapping between poolId and its count index
158+ func (k Keeper ) SetPoolIdToCountMapping (ctx sdk.Context , poolId string , count uint64 ) {
159+ store := prefix .NewStore (ctx .KVStore (k .storeKey ), types .PoolIdToCountKeyPrefix )
160+ b := make ([]byte , 8 )
161+ binary .BigEndian .PutUint64 (b , count )
162+ store .Set ([]byte (poolId ), b )
163+ }
164+
165+ // Gets the count index of the poolId
166+ func (k Keeper ) GetCountByPoolId (ctx sdk.Context , poolId string ) (count uint64 , found bool ) {
167+ store := prefix .NewStore (ctx .KVStore (k .storeKey ), types .PoolIdToCountKeyPrefix )
168+ b := store .Get ([]byte (poolId ))
169+ if b == nil {
170+ return 0 , false
171+ }
172+ return binary .BigEndian .Uint64 (b ), true
173+ }
174+
175+ // Modified SetInterchainLiquidityPool
176+ func (k Keeper ) SetInterchainLiquidityPool (ctx sdk.Context , interchainLiquidityPool types.InterchainLiquidityPool ) {
177+ store := prefix .NewStore (ctx .KVStore (k .storeKey ), types .KeyPrefix (types .InterchainLiquidityPoolKeyPrefix ))
178+
179+ // Get current pool count
180+ poolCount := k .GetPoolCount (ctx )
181+
182+ // Increment the count
183+ poolCount ++
184+
185+ // Set the new count
186+ k .SetPoolCount (ctx , poolCount )
187+
188+ // Set the poolId to count mapping
189+ k .SetPoolIdToCountMapping (ctx , interchainLiquidityPool .Id , poolCount )
190+
191+ // Marshal the pool and set in store
192+ b := k .cdc .MustMarshal (& interchainLiquidityPool )
193+ store .Set (GetInterchainLiquidityPoolKey (poolCount ), b )
194+
195+ // Check if we exceed max pools
196+ if poolCount > types .MaxPoolCount {
197+ // Remove the oldest pool
198+ store .Delete (GetInterchainLiquidityPoolKey (poolCount - types .MaxPoolCount ))
199+ }
200+ }
201+
202+ // Modified GetInterchainLiquidityPool
203+ func (k Keeper ) GetInterchainLiquidityPool (ctx sdk.Context , poolId string ) (val types.InterchainLiquidityPool , found bool ) {
204+ store := prefix .NewStore (ctx .KVStore (k .storeKey ), types .KeyPrefix (types .InterchainLiquidityPoolKeyPrefix ))
205+
206+ count , found := k .GetCountByPoolId (ctx , poolId )
207+ if ! found {
208+ return val , false
209+ }
210+
211+ b := store .Get (GetInterchainLiquidityPoolKey (count ))
212+ if b == nil {
213+ return val , false
214+ }
215+
216+ k .cdc .MustUnmarshal (b , & val )
217+ return val , true
218+ }
0 commit comments