Skip to content

Commit

Permalink
fix: Display order price event attribute in JSON (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond authored Dec 13, 2024
1 parent e02a025 commit 81797c1
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/setup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
)

// make sure to update these when you upgrade the version
var NextVersion = "v0.55.0"
var NextVersion = "v1.0.1"

func (app *ElysApp) setUpgradeHandler() {
app.UpgradeKeeper.SetUpgradeHandler(
Expand Down
111 changes: 106 additions & 5 deletions x/tradeshield/keeper/msg_server_execute_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
oracletypes "github.com/elys-network/elys/x/oracle/types"
ptypes "github.com/elys-network/elys/x/parameter/types"
keeper "github.com/elys-network/elys/x/tradeshield/keeper"
"github.com/elys-network/elys/x/tradeshield/types"
)
Expand All @@ -12,9 +13,10 @@ func (suite *TradeshieldKeeperTestSuite) TestMsgServerExecuteOrder() {
addr := suite.AddAccounts(3, nil)

testCases := []struct {
name string
expectErrMsg string
prerequisiteFunction func() *types.MsgExecuteOrders
name string
expectErrMsg string
prerequisiteFunction func() *types.MsgExecuteOrders
postrequisiteFunction func()
}{
{
"Spot Order not found",
Expand All @@ -26,6 +28,7 @@ func (suite *TradeshieldKeeperTestSuite) TestMsgServerExecuteOrder() {
PerpetualOrderIds: []uint64{1},
}
},
func() {},
},
{
"Perpetual order not found",
Expand All @@ -52,9 +55,77 @@ func (suite *TradeshieldKeeperTestSuite) TestMsgServerExecuteOrder() {
PerpetualOrderIds: []uint64{1},
}
},
func() {},
},
{
"Success: Execute Orders",
"Success: Execute Spot Order",
"",
func() *types.MsgExecuteOrders {
suite.SetupCoinPrices()

_ = suite.CreateNewAmmPool(addr[0], true, math.LegacyZeroDec(), math.LegacyZeroDec(), ptypes.ATOM, math.NewInt(100000000000).MulRaw(10), math.NewInt(100000000000).MulRaw(10))

openOrderMsg := &types.MsgCreateSpotOrder{
OwnerAddress: addr[2].String(),
OrderType: types.SpotOrderType_LIMITBUY,
OrderPrice: types.OrderPrice{
BaseDenom: "uusdc",
QuoteDenom: "uatom",
Rate: math.LegacyNewDec(10),
},
OrderAmount: sdk.NewCoin("uusdc", math.NewInt(100000)),
OrderTargetDenom: "uatom",
}
msgSrvr := keeper.NewMsgServerImpl(suite.app.TradeshieldKeeper)
_, err := msgSrvr.CreateSpotOrder(suite.ctx, openOrderMsg)
suite.Require().NoError(err)

suite.app.OracleKeeper.SetPrice(suite.ctx, oracletypes.Price{
Asset: "ATOM",
Price: math.LegacyNewDec(5),
Source: "elys",
Provider: oracleProvider.String(),
Timestamp: uint64(suite.ctx.BlockTime().Unix()),
})

return &types.MsgExecuteOrders{
Creator: addr[2].String(),
SpotOrderIds: []uint64{1},
PerpetualOrderIds: []uint64{},
}
},
func() {
// Get events from context
events := suite.ctx.EventManager().Events()

// Find the specific event we're looking for
var foundEvent sdk.Event
for _, event := range events {
if event.Type == types.TypeEvtExecuteLimitBuySpotOrder {
foundEvent = event
break
}
}

// Assert event was emitted
suite.Require().NotNil(foundEvent)

// Check event attributes
suite.Require().Equal(types.TypeEvtExecuteLimitBuySpotOrder, foundEvent.Type)

// Check specific attributes
for _, attr := range foundEvent.Attributes {
switch string(attr.Key) {
case "order_id":
suite.Require().Equal("1", string(attr.Value))
case "order_price":
suite.Require().Equal(string(attr.Value), "{\"base_denom\":\"uusdc\",\"quote_denom\":\"uatom\",\"rate\":\"5.000000000000000000\"}")
}
}
},
},
{
"Success: Execute Perpetual Order",
"",
func() *types.MsgExecuteOrders {
_, _, _ = suite.SetPerpetualPool(1)
Expand Down Expand Up @@ -86,10 +157,39 @@ func (suite *TradeshieldKeeperTestSuite) TestMsgServerExecuteOrder() {

return &types.MsgExecuteOrders{
Creator: addr[2].String(),
SpotOrderIds: []uint64{1},
SpotOrderIds: []uint64{},
PerpetualOrderIds: []uint64{1},
}
},
func() {
// Get events from context
events := suite.ctx.EventManager().Events()

// Find the specific event we're looking for
var foundEvent sdk.Event
for _, event := range events {
if event.Type == types.TypeEvtExecuteLimitOpenPerpetualOrder {
foundEvent = event
break
}
}

// Assert event was emitted
suite.Require().NotNil(foundEvent)

// Check event attributes
suite.Require().Equal(types.TypeEvtExecuteLimitOpenPerpetualOrder, foundEvent.Type)

// Check specific attributes
for _, attr := range foundEvent.Attributes {
switch string(attr.Key) {
case "order_id":
suite.Require().Equal("1", string(attr.Value))
case "trigger_price":
suite.Require().Equal(string(attr.Value), "{\"trading_asset_denom\":\"uatom\",\"rate\":\"10.000000000000000000\"}")
}
}
},
},
}

Expand All @@ -104,6 +204,7 @@ func (suite *TradeshieldKeeperTestSuite) TestMsgServerExecuteOrder() {
} else {
suite.Require().NoError(err)
}
tc.postrequisiteFunction()
})
}
}
12 changes: 4 additions & 8 deletions x/tradeshield/keeper/msg_server_execute_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
ammtypes "github.com/elys-network/elys/x/amm/types"
"github.com/elys-network/elys/x/tradeshield/types"
)

Expand All @@ -23,30 +22,27 @@ func (k msgServer) ExecuteOrders(goCtx context.Context, msg *types.MsgExecuteOrd
}

var err error
var res *ammtypes.MsgSwapByDenomResponse

// dispatch based on the order type
switch spotOrder.OrderType {
case types.SpotOrderType_STOPLOSS:
// execute the stop loss order
res, err = k.ExecuteStopLossOrder(ctx, spotOrder)
_, err = k.ExecuteStopLossOrder(ctx, spotOrder)
case types.SpotOrderType_LIMITSELL:
// execute the limit sell order
res, err = k.ExecuteLimitSellOrder(ctx, spotOrder)
_, err = k.ExecuteLimitSellOrder(ctx, spotOrder)
case types.SpotOrderType_LIMITBUY:
// execute the limit buy order
res, err = k.ExecuteLimitBuyOrder(ctx, spotOrder)
_, err = k.ExecuteLimitBuyOrder(ctx, spotOrder)
case types.SpotOrderType_MARKETBUY:
// execute the market buy order
res, err = k.ExecuteMarketBuyOrder(ctx, spotOrder)
_, err = k.ExecuteMarketBuyOrder(ctx, spotOrder)
}

// log the error if any
if err != nil {
// Add log about error or not executed
spotLog = append(spotLog, fmt.Sprintf("Spot order Id:%d cannot be executed due to err: %s", spotOrderId, err.Error()))
} else {
ctx.EventManager().EmitEvent(types.NewExecuteSpotOrderEvt(spotOrder, res))
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/tradeshield/keeper/msg_server_spot_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (k msgServer) CreateSpotOrder(goCtx context.Context, msg *types.MsgCreateSp
return nil, err
}

ctx.EventManager().EmitEvent(types.NewExecuteSpotOrderEvt(pendingSpotOrder, res))
ctx.EventManager().EmitEvent(types.NewExecuteMarketBuySpotOrderEvt(pendingSpotOrder, res))

return &types.MsgCreateSpotOrderResponse{
OrderId: pendingSpotOrder.OrderId,
Expand Down
16 changes: 16 additions & 0 deletions x/tradeshield/keeper/pending_spot_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (k Keeper) ExecuteStopLossOrder(ctx sdk.Context, order types.SpotOrder) (*a
DenomIn: order.OrderPrice.BaseDenom,
DenomOut: order.OrderPrice.QuoteDenom,
MinAmount: sdk.NewCoin(order.OrderTargetDenom, sdkmath.ZeroInt()),
MaxAmount: order.OrderAmount,
})
if err != nil {
return res, err
Expand All @@ -220,6 +221,9 @@ func (k Keeper) ExecuteStopLossOrder(ctx sdk.Context, order types.SpotOrder) (*a
// Remove the order from the pending order list
k.RemovePendingSpotOrder(ctx, order.OrderId)

// emit the event
ctx.EventManager().EmitEvent(types.NewExecuteStopLossSpotOrderEvt(order, res))

return res, nil
}

Expand Down Expand Up @@ -253,6 +257,7 @@ func (k Keeper) ExecuteLimitSellOrder(ctx sdk.Context, order types.SpotOrder) (*
DenomIn: order.OrderPrice.BaseDenom,
DenomOut: order.OrderPrice.QuoteDenom,
MinAmount: sdk.NewCoin(order.OrderTargetDenom, sdkmath.ZeroInt()),
MaxAmount: order.OrderAmount,
})
if err != nil {
return res, err
Expand All @@ -261,6 +266,9 @@ func (k Keeper) ExecuteLimitSellOrder(ctx sdk.Context, order types.SpotOrder) (*
// Remove the order from the pending order list
k.RemovePendingSpotOrder(ctx, order.OrderId)

// emit the event
ctx.EventManager().EmitEvent(types.NewExecuteLimitSellSpotOrderEvt(order, res))

return res, nil
}

Expand Down Expand Up @@ -294,6 +302,7 @@ func (k Keeper) ExecuteLimitBuyOrder(ctx sdk.Context, order types.SpotOrder) (*a
DenomIn: order.OrderPrice.BaseDenom,
DenomOut: order.OrderPrice.QuoteDenom,
MinAmount: sdk.NewCoin(order.OrderTargetDenom, sdkmath.ZeroInt()),
MaxAmount: order.OrderAmount,
})
if err != nil {
return res, err
Expand All @@ -302,6 +311,9 @@ func (k Keeper) ExecuteLimitBuyOrder(ctx sdk.Context, order types.SpotOrder) (*a
// Remove the order from the pending order list
k.RemovePendingSpotOrder(ctx, order.OrderId)

// emit the event
ctx.EventManager().EmitEvent(types.NewExecuteLimitBuySpotOrderEvt(order, res))

return res, nil
}

Expand All @@ -315,10 +327,14 @@ func (k Keeper) ExecuteMarketBuyOrder(ctx sdk.Context, order types.SpotOrder) (*
DenomIn: order.OrderAmount.Denom,
DenomOut: order.OrderTargetDenom,
MinAmount: sdk.NewCoin(order.OrderTargetDenom, sdkmath.ZeroInt()),
MaxAmount: order.OrderAmount,
})
if err != nil {
return res, err
}

// emit the event
ctx.EventManager().EmitEvent(types.NewExecuteMarketBuySpotOrderEvt(order, res))

return res, nil
}
Loading

0 comments on commit 81797c1

Please sign in to comment.