diff --git a/x/dex/keeper/match_result.go b/x/dex/keeper/match_result.go index 12fe1032d1..ca15ef12eb 100644 --- a/x/dex/keeper/match_result.go +++ b/x/dex/keeper/match_result.go @@ -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), @@ -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) diff --git a/x/dex/keeper/query/grpc_query_match_result.go b/x/dex/keeper/query/grpc_query_match_result.go index 0a75329ab5..95226904e6 100644 --- a/x/dex/keeper/query/grpc_query_match_result.go +++ b/x/dex/keeper/query/grpc_query_match_result.go @@ -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") } diff --git a/x/dex/module_test.go b/x/dex/module_test.go index b96402e574..720d6102c3 100644 --- a/x/dex/module_test.go +++ b/x/dex/module_test.go @@ -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)) @@ -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)) } @@ -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)) @@ -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)) } @@ -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) { @@ -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) }