Skip to content

Commit

Permalink
Rely on ctx's event manager
Browse files Browse the repository at this point in the history
  • Loading branch information
blewater committed Sep 20, 2021
1 parent 5fc1176 commit 55f83a9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
22 changes: 10 additions & 12 deletions x/market/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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())
Expand All @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 14 additions & 12 deletions x/market/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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())
Expand Down

0 comments on commit 55f83a9

Please sign in to comment.