Skip to content

Commit

Permalink
fix: 🎨 move to bottom function invalid orderId -> count relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
soring323 committed Aug 9, 2023
1 parent c740a00 commit 8757dd1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 50 deletions.
23 changes: 6 additions & 17 deletions modules/apps/100-atomic-swap/keeper/atomic_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,17 @@ func GetBidIDFromBytes(bz []byte) uint64 {
}

func (k Keeper) MoveOrderToBottom(ctx sdk.Context, orderId string) error {
// Step 1: Retrieve the item based on the given ID.
// Step 1: Retrieve the order based on the given ID.
order, found := k.GetAtomicOrder(ctx, orderId)
if !found {
return types.ErrNotFoundOrder
}
// Step 2: Remove the item from its current position.
// Step 2: Remove the original order from its position.
k.RemoveOrder(ctx, orderId)

// Step 3: Get the current count (which will be the new position for the order).
newCount := k.GetAtomicOrderCount(ctx)

// Step 4: Set the order at its new position.
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OTCOrderBookKey)
bz := k.cdc.MustMarshal(&order)
store.Set(GetOrderIDBytes(newCount), bz)

// Step 5: Update the orderId -> count relationship.
k.SetAtomicOrderCountToOrderID(ctx, orderId, newCount)

// Step 6: Increment the total order count.
k.SetAtomicOrderCount(ctx, newCount+1)

// Step 3: Append the order to the end of the list.
// (Since AppendAtomicOrder manages the order count and index mapping,
// we can reuse this function to simplify our logic.)
k.AppendAtomicOrder(ctx, order)
return nil
}

Expand Down
10 changes: 9 additions & 1 deletion modules/apps/100-atomic-swap/keeper/atomic_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func (suite *KeeperTestSuite) TestMoveOrderToBottom() {
CancelTimestamp: int64(i),
})
}

// Move the third order to the bottom
err := k.MoveOrderToBottom(ctx, orderIDs[2])
suite.Require().NoError(err)
Expand All @@ -73,6 +72,15 @@ func (suite *KeeperTestSuite) TestMoveOrderToBottom() {
movedOrderCount := k.GetAtomicOrderCountByOrderId(ctx, orderIDs[2])
lastOrderCount := k.GetAtomicOrderCount(ctx) - 1
suite.Require().Equal(movedOrderCount, lastOrderCount)

// Verify that all other orders have maintained their original sequence
for i := 0; i < len(orderIDs)-1; i++ { // Exclude the last order (since it was moved)
if i < 2 { // For orders before the moved order
suite.Require().Equal(ordersAfterMove[i].Id, orderIDs[i])
} else { // For orders after the moved order
suite.Require().Equal(ordersAfterMove[i].Id, orderIDs[i+1])
}
}
}

func (suite *KeeperTestSuite) TestTrimExcessOrders() {
Expand Down
3 changes: 1 addition & 2 deletions modules/apps/100-atomic-swap/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ func (k Keeper) OnReceivedMake(ctx sdk.Context, packet channeltypes.Packet, orde
Maker: msg,
}

k.SetAtomicOrder(ctx, order)

k.AppendAtomicOrder(ctx, order)
ctx.EventManager().EmitTypedEvents(msg)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ import (
"github.com/sideprotocol/ibcswap/v6/modules/apps/101-interchain-swap/types"
)

// // SetInterchainLiquidityPool set a specific interchainLiquidityPool in the store from its index
// func (k Keeper) SetInterchainLiquidityPool(ctx sdk.Context, interchainLiquidityPool types.InterchainLiquidityPool) {
// store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
// b := k.cdc.MustMarshal(&interchainLiquidityPool)
// store.Set(types.InterchainLiquidityPoolKey(
// interchainLiquidityPool.Id,
// ), b)
// }

// SetInterchainLiquidityPool set a specific interchainLiquidityPool in the store from its index
func (k Keeper) SetInitialPoolAssets(ctx sdk.Context, poolId string, tokens sdk.Coins) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(poolId))
Expand Down Expand Up @@ -102,24 +93,6 @@ func (k Keeper) RemoveInterchainLiquidityPool(
))
}

// // GetAllInterchainLiquidityPool returns all interchainLiquidityPool
// func (k Keeper) GetAllInterchainLiquidityPool(ctx sdk.Context) (list []types.InterchainLiquidityPool) {
// store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
// iterator := sdk.KVStorePrefixIterator(store, []byte{})

// defer iterator.Close()

// for ; iterator.Valid(); iterator.Next() {
// var val types.InterchainLiquidityPool
// k.cdc.MustUnmarshal(iterator.Value(), &val)
// list = append(list, val)
// }
// return
// }

// CurrentPoolCountKey stores the current number of pools.
var CurrentPoolCountKey = []byte("CurrentPoolCount")

func (k Keeper) SetInterchainLiquidityPool(ctx sdk.Context, interchainLiquidityPool types.InterchainLiquidityPool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))

Expand All @@ -145,7 +118,7 @@ func (k Keeper) SetInterchainLiquidityPool(ctx sdk.Context, interchainLiquidityP

func (k Keeper) GetPoolCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
b := store.Get(CurrentPoolCountKey)
b := store.Get(types.CurrentPoolCountKey)
if b == nil {
return 0
}
Expand All @@ -156,7 +129,7 @@ func (k Keeper) SetPoolCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, count)
store.Set(CurrentPoolCountKey, b)
store.Set(types.CurrentPoolCountKey, b)
}

func GetInterchainLiquidityPoolKey(count uint64) []byte {
Expand Down
3 changes: 2 additions & 1 deletion modules/apps/101-interchain-swap/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const (

var (
// PortKey defines the key to store the port ID in store
PortKey = []byte{0x01}
PortKey = []byte{0x01}
CurrentPoolCountKey = []byte{0x02}
)

func KeyPrefix(p string) []byte {
Expand Down

0 comments on commit 8757dd1

Please sign in to comment.