Skip to content

Commit

Permalink
fix: 🐛 fix orderId incorrection problem after move to bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
soring323 committed Aug 9, 2023
1 parent a71e82c commit c740a00
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
18 changes: 13 additions & 5 deletions modules/apps/100-atomic-swap/keeper/atomic_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func GetBidIDFromBytes(bz []byte) uint64 {
return binary.BigEndian.Uint64(bz)
}

// MoveOrderToBottom moves the atomic order with the given ID to the bottom of the list.
func (k Keeper) MoveOrderToBottom(ctx sdk.Context, orderId string) error {
// Step 1: Retrieve the item based on the given ID.
order, found := k.GetAtomicOrder(ctx, orderId)
Expand All @@ -131,12 +130,21 @@ func (k Keeper) MoveOrderToBottom(ctx sdk.Context, orderId string) error {
}
// Step 2: Remove the item from its current position.
k.RemoveOrder(ctx, orderId)
// Step 3: Append the item to the end.
count := k.GetAtomicOrderCount(ctx)

// 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(count), bz)
k.SetAtomicOrderCount(ctx, count+1)
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)

return nil
}

Expand Down
5 changes: 5 additions & 0 deletions modules/apps/100-atomic-swap/keeper/atomic_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func (suite *KeeperTestSuite) TestMoveOrderToBottom() {
ordersAfterMove := k.GetAllOrder(ctx)
lastOrder := ordersAfterMove[len(ordersAfterMove)-1]
suite.Require().Equal(lastOrder.Id, orderIDs[2])

// Additional check for the updated `orderId -> count` relationship
movedOrderCount := k.GetAtomicOrderCountByOrderId(ctx, orderIDs[2])
lastOrderCount := k.GetAtomicOrderCount(ctx) - 1
suite.Require().Equal(movedOrderCount, lastOrderCount)
}

func (suite *KeeperTestSuite) TestTrimExcessOrders() {
Expand Down

0 comments on commit c740a00

Please sign in to comment.