Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ var upgradesList = []string{
"1.2.1beta",
// 1.2.2beta
"1.2.2beta",
// 1.2.3beta
"1.2.3beta",
// 1.2.4beta
"1.2.4beta",
// 1.2.2beta-postfix
"1.2.2beta-postfix",
}

func (app App) RegisterUpgradeHandlers() {
Expand Down
13 changes: 4 additions & 9 deletions x/dex/cache/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,12 @@ func (o *BlockOrders) GetSortedMarketOrders(direction types.PositionDirection, i
o.mu.Lock()
defer o.mu.Unlock()

orderTypes := map[types.OrderType]bool{
types.OrderType_MARKET: true,
types.OrderType_FOKMARKET: true,
types.OrderType_FOKMARKETBYVALUE: true,
}
res := o.getOrdersByCriteria(types.OrderType_MARKET, direction)
res = append(res, o.getOrdersByCriteria(types.OrderType_FOKMARKET, direction)...)
if includeLiquidationOrders {
orderTypes[types.OrderType_LIQUIDATION] = true
res = append(res, o.getOrdersByCriteria(types.OrderType_LIQUIDATION, direction)...)
}
res := o.getOrdersByCriteriaMap(orderTypes, map[types.PositionDirection]bool{direction: true})

sort.Slice(res, func(i, j int) bool {
sort.SliceStable(res, func(i, j int) bool {
// a price of 0 indicates that there is no worst price for the order, so it should
// always be ranked at the top.
if res[i].Price.IsZero() {
Expand Down
2 changes: 1 addition & 1 deletion x/dex/keeper/utils/order_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func PopulateOrderbook(
}

func sortOrderBookEntries(entries []types.OrderBookEntry) {
sort.Slice(entries, func(i, j int) bool {
sort.SliceStable(entries, func(i, j int) bool {
return entries[i].GetPrice().LT(entries[j].GetPrice())
})
}
Expand Down
22 changes: 7 additions & 15 deletions x/dex/types/match_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,16 @@ func NewMatchResult(
cancellations []*Cancellation,
settlements []*SettlementEntry,
) *MatchResult {
sort.Slice(orders, func(i, j int) bool {
if i != j && orders[i].Id == orders[j].Id {
panic("orders have identical IDs")
}
return orders[i].Id < orders[j].Id
// Note that we use string comparator since it is more robust. E.g. in the case that 2 orders match
// on the same orderId, we will then sort on the next field
sort.SliceStable(orders, func(i, j int) bool {
return orders[i].String() < orders[j].String()
})
sort.Slice(cancellations, func(i, j int) bool {
if i != j && cancellations[i].Id == cancellations[j].Id {
panic("cancnellations have identical IDs")
}
return cancellations[i].Id < cancellations[j].Id
sort.SliceStable(cancellations, func(i, j int) bool {
return cancellations[i].String() < cancellations[j].String()
})
sort.SliceStable(settlements, func(i, j int) bool {
// settlements for the same order ID are always populated
// by the same goroutine, so the ordering among those
// settlements are already deterministic as long as the
// sorting is stable.
return settlements[i].OrderId < settlements[j].OrderId
return settlements[i].String() < settlements[j].String()
})
return &MatchResult{
Orders: orders,
Expand Down
54 changes: 54 additions & 0 deletions x/dex/types/match_result_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package types_test

import (
"testing"

"github.com/sei-protocol/sei-chain/x/dex/types"
"github.com/stretchr/testify/require"
)

func TestMatchResultSorted(t *testing.T) {
// Test stable sorting
order1 := &types.Order{
Id: 1,
}
order2 := &types.Order{
Id: 1,
}
order3 := &types.Order{
Id: 2,
}

// Test sort on different field
cancellation1 := &types.Cancellation{
Id: 1,
AssetDenom: "a",
}
cancellation2 := &types.Cancellation{
Id: 1,
AssetDenom: "b",
}

// Test sort on string
settlement1 := &types.SettlementEntry{
Account: "a",
}
settlement2 := &types.SettlementEntry{
Account: "b",
}

orders := []*types.Order{order3, order1, order2}
cancellations := []*types.Cancellation{cancellation2, cancellation1}
settlements := []*types.SettlementEntry{settlement2, settlement1}

matchResult := types.NewMatchResult(orders, cancellations, settlements)
expectedOrders := []*types.Order{order1, order2, order3}
expectedCancellations := []*types.Cancellation{cancellation1, cancellation2}
expectedSettlements := []*types.SettlementEntry{settlement1, settlement2}
require.Equal(t, expectedOrders, matchResult.Orders)
// Check actual elements, since slice match above don't seem to capture if the orders point to different objects
require.True(t, order1 == matchResult.Orders[0])
require.True(t, order2 == matchResult.Orders[1])
require.Equal(t, expectedCancellations, matchResult.Cancellations)
require.Equal(t, expectedSettlements, matchResult.Settlements)
}