From 55f83a9cabdcce9b5ff54e0131d2a67ab362910b Mon Sep 17 00:00:00 2001 From: Mario Karagiorgas Date: Mon, 20 Sep 2021 21:22:14 +0300 Subject: [PATCH] Rely on ctx's event manager --- x/market/keeper/keeper.go | 22 ++++++++++------------ x/market/keeper/keeper_test.go | 26 ++++++++++++++------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/x/market/keeper/keeper.go b/x/market/keeper/keeper.go index 7bdf7e1c..beb3c11d 100644 --- a/x/market/keeper/keeper.go +++ b/x/market/keeper/keeper.go @@ -6,16 +6,15 @@ package keeper import ( "fmt" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "math" "sync" "time" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/e-money/em-ledger/x/market/types" - "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/e-money/em-ledger/x/market/types" ) const ( @@ -141,6 +140,9 @@ func (k *Keeper) GetSrcFromSlippage( } func (k *Keeper) NewOrderSingle(ctx sdk.Context, aggressiveOrder types.Order) (*sdk.Result, error) { + // save caller's event manager + retEvManager := ctx.EventManager() + // Use a fixed gas amount ctx.GasMeter().ConsumeGas(gasPriceNewOrder, "NewOrderSingle") ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) @@ -150,6 +152,8 @@ func (k *Keeper) NewOrderSingle(ctx sdk.Context, aggressiveOrder types.Order) (* ctx, commitTrade := ctx.CacheContext() defer func() { + retEvManager.EmitEvents(ctx.EventManager().Events()) + if KillOrder { return } @@ -345,7 +349,7 @@ func (k *Keeper) NewOrderSingle(ctx sdk.Context, aggressiveOrder types.Order) (* } } - return &sdk.Result{Events: ctx.EventManager().ABCIEvents()}, nil + return &sdk.Result{}, nil } // Check whether an asset even exists on the chain at the moment. @@ -396,13 +400,7 @@ func (k *Keeper) CancelReplaceLimitOrder(ctx sdk.Context, newOrder types.Order, newOrder.TimeInForce = origOrder.TimeInForce - resAdd, err := k.NewOrderSingle(ctx, newOrder) - if err != nil { - return nil, err - } - - evts := append(ctx.EventManager().ABCIEvents(), resAdd.Events...) - return &sdk.Result{Events: evts}, nil + return k.NewOrderSingle(ctx, newOrder) } func (k *Keeper) GetOrderByOwnerAndClientOrderId(ctx sdk.Context, owner, clientOrderId string) *types.Order { diff --git a/x/market/keeper/keeper_test.go b/x/market/keeper/keeper_test.go index 2c870c1e..7306fc9a 100644 --- a/x/market/keeper/keeper_test.go +++ b/x/market/keeper/keeper_test.go @@ -7,6 +7,11 @@ package keeper import ( "encoding/json" "fmt" + "math" + "strings" + "testing" + "time" + "github.com/cosmos/cosmos-sdk/client" clienttx "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/codec" @@ -36,10 +41,6 @@ import ( tmrand "github.com/tendermint/tendermint/libs/rand" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" dbm "github.com/tendermint/tm-db" - "math" - "strings" - "testing" - "time" ) func init() { @@ -538,16 +539,16 @@ func TestFillOrKillMarketOrder1(t *testing.T) { // Create a market for eur o = order(ctx.BlockTime(), acc2, "100eur", "100gbp") - res, err := k.NewOrderSingle(ctx, o) + _, err = k.NewOrderSingle(ctx, o) require.NoError(t, err) require.Equal( t, "accept", - string(res.Events[0].Attributes[0].GetValue()), + string(ctx.EventManager().Events()[0].Attributes[0].GetValue()), ) require.Equal( t, ctx.BlockTime().Format(time.RFC3339), - string(res.Events[0].Attributes[len(res.Events[0].Attributes)-1].GetValue()), + string(ctx.EventManager().Events()[0].Attributes[len(ctx.EventManager().Events()[0].Attributes)-1].GetValue()), ) // Create a fill or kill order that cannot be satisfied by the current market @@ -559,12 +560,13 @@ func TestFillOrKillMarketOrder1(t *testing.T) { require.NoError(t, err) limitOrder := order(ctx.BlockTime(), acc1, slippageSource.String(), dest.String()) limitOrder.TimeInForce = types.TimeInForce_FillOrKill - result, err := k.NewOrderSingle(ctx, limitOrder) + ctx = ctx.WithEventManager(sdk.NewEventManager()) + _, err = k.NewOrderSingle(ctx, limitOrder) require.NoError(t, err) - require.Len(t, result.Events, 1) - require.Equal(t, types.EventTypeMarket, result.Events[0].Type) - require.Equal(t, "action", string(result.Events[0].Attributes[0].GetKey())) - require.Equal(t, "expire", string(result.Events[0].Attributes[0].GetValue())) + require.Len(t, ctx.EventManager().Events(), 1) + require.Equal(t, types.EventTypeMarket, ctx.EventManager().Events()[0].Type) + require.Equal(t, "action", string(ctx.EventManager().Events()[0].Attributes[0].GetKey())) + require.Equal(t, "expire", string(ctx.EventManager().Events()[0].Attributes[0].GetValue())) // Last order must fail completely due to not being fillable acc1Bal := bk.GetAllBalances(ctx, acc1.GetAddress())