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
17 changes: 5 additions & 12 deletions x/dex/keeper/match_result.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package keeper

import (
"encoding/binary"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/x/dex/types"
)

const MatchResultKey = "match-result"

func (k Keeper) SetMatchResult(ctx sdk.Context, contractAddr string, result *types.MatchResult) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
Expand All @@ -20,22 +20,15 @@ func (k Keeper) SetMatchResult(ctx sdk.Context, contractAddr string, result *typ
if err != nil {
panic(err)
}
key := make([]byte, 8)
binary.BigEndian.PutUint64(key, uint64(height))
store.Set(key, bz)
store.Set([]byte(MatchResultKey), bz)
}

func (k Keeper) GetMatchResultState(ctx sdk.Context, contractAddr string, height int64) (*types.MatchResult, bool) {
func (k Keeper) GetMatchResultState(ctx sdk.Context, contractAddr string) (*types.MatchResult, bool) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.MatchResultPrefix(contractAddr),
)
key := make([]byte, 8)
binary.BigEndian.PutUint64(key, uint64(height))
if !store.Has(key) {
return nil, false
}
bz := store.Get(key)
bz := store.Get([]byte(MatchResultKey))
result := types.MatchResult{}
if err := result.Unmarshal(bz); err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion x/dex/keeper/query/grpc_query_match_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (k KeeperWrapper) GetMatchResult(c context.Context, req *types.QueryGetMatc
}
ctx := sdk.UnwrapSDKContext(c)

result, found := k.GetMatchResultState(ctx, req.ContractAddr, req.Height)
result, found := k.GetMatchResultState(ctx, req.ContractAddr)
if !found {
return nil, status.Error(codes.NotFound, "result not found")
}
Expand Down
20 changes: 10 additions & 10 deletions x/dex/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestEndBlockMarketOrder(t *testing.T) {
_, found = dexkeeper.GetLongBookByPrice(ctx, contractAddr.String(), sdk.MustNewDecFromStr("1"), pair.PriceDenom, pair.AssetDenom)
require.True(t, found)

matchResults, _ := dexkeeper.GetMatchResultState(ctx, contractAddr.String(), 2)
matchResults, _ := dexkeeper.GetMatchResultState(ctx, contractAddr.String())
require.Equal(t, 1, len(matchResults.Orders))
require.Equal(t, 2, len(matchResults.Settlements))

Expand All @@ -168,7 +168,7 @@ func TestEndBlockMarketOrder(t *testing.T) {
ctx = ctx.WithBlockHeight(3)
testApp.EndBlocker(ctx, abci.RequestEndBlock{})

matchResults, _ = dexkeeper.GetMatchResultState(ctx, contractAddr.String(), 3)
matchResults, _ = dexkeeper.GetMatchResultState(ctx, contractAddr.String())
require.Equal(t, 1, len(matchResults.Orders))
require.Equal(t, 0, len(matchResults.Settlements))
}
Expand Down Expand Up @@ -305,7 +305,7 @@ func TestEndBlockLimitOrder(t *testing.T) {
_, found = dexkeeper.GetLongBookByPrice(ctx, contractAddr.String(), sdk.MustNewDecFromStr("3"), pair.PriceDenom, pair.AssetDenom)
require.False(t, found)

matchResults, _ := dexkeeper.GetMatchResultState(ctx, contractAddr.String(), 2)
matchResults, _ := dexkeeper.GetMatchResultState(ctx, contractAddr.String())
require.Equal(t, 2, len(matchResults.Orders))
require.Equal(t, 4, len(matchResults.Settlements))

Expand Down Expand Up @@ -335,7 +335,7 @@ func TestEndBlockLimitOrder(t *testing.T) {
_, found = dexkeeper.GetShortBookByPrice(ctx, contractAddr.String(), sdk.MustNewDecFromStr("3"), pair.PriceDenom, pair.AssetDenom)
require.False(t, found)

matchResults, _ = dexkeeper.GetMatchResultState(ctx, contractAddr.String(), 3)
matchResults, _ = dexkeeper.GetMatchResultState(ctx, contractAddr.String())
require.Equal(t, 1, len(matchResults.Orders))
require.Equal(t, 2, len(matchResults.Settlements))
}
Expand Down Expand Up @@ -366,8 +366,8 @@ func TestEndBlockRollback(t *testing.T) {
ctx = ctx.WithBlockHeight(1)
testApp.EndBlocker(ctx, abci.RequestEndBlock{})
// No state change should've been persisted
_, found := dexkeeper.GetMatchResultState(ctx, keepertest.TestContract, 2)
require.False(t, found)
matchResult, _ := dexkeeper.GetMatchResultState(ctx, keepertest.TestContract)
require.Equal(t, &types.MatchResult{}, matchResult)
}

func TestEndBlockPartialRollback(t *testing.T) {
Expand Down Expand Up @@ -444,12 +444,12 @@ func TestEndBlockPartialRollback(t *testing.T) {
ctx = ctx.WithBlockHeight(1)
testApp.EndBlocker(ctx, abci.RequestEndBlock{})
// No state change should've been persisted for bad contract
_, found := dexkeeper.GetMatchResultState(ctx, keepertest.TestContract, 1)
require.False(t, found)
matchResult, _ := dexkeeper.GetMatchResultState(ctx, keepertest.TestContract)
require.Equal(t, &types.MatchResult{}, matchResult)
// state change should've been persisted for good contract
matchResult, _ := dexkeeper.GetMatchResultState(ctx, contractAddr.String(), 1)
matchResult, _ = dexkeeper.GetMatchResultState(ctx, contractAddr.String())
require.Equal(t, 1, len(matchResult.Orders))
_, found = dexkeeper.GetLongBookByPrice(ctx, contractAddr.String(), sdk.MustNewDecFromStr("0.0001"), pair.PriceDenom, pair.AssetDenom)
_, found := dexkeeper.GetLongBookByPrice(ctx, contractAddr.String(), sdk.MustNewDecFromStr("0.0001"), pair.PriceDenom, pair.AssetDenom)
require.True(t, found)
}

Expand Down