Skip to content

Commit

Permalink
[Tradeshield]: Handle error and improve execution logs (#974)
Browse files Browse the repository at this point in the history
* handle error and improve execution logs

* fix
  • Loading branch information
amityadav0 authored Nov 20, 2024
1 parent dcaf0c3 commit 557affe
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
18 changes: 15 additions & 3 deletions x/tradeshield/keeper/msg_server_execute_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package keeper

import (
"context"
"fmt"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/tradeshield/types"
Expand All @@ -13,6 +15,7 @@ func (k msgServer) ExecuteOrders(goCtx context.Context, msg *types.MsgExecuteOrd
}
ctx := sdk.UnwrapSDKContext(goCtx)

spotLog := []string{}
// loop through the spot orders and execute them
for _, spotOrderId := range msg.SpotOrderIds {
// get the spot order
Expand All @@ -39,12 +42,14 @@ func (k msgServer) ExecuteOrders(goCtx context.Context, msg *types.MsgExecuteOrd
err = k.ExecuteMarketBuyOrder(ctx, spotOrder)
}

// return the error if any
// log the error if any
if err != nil {
return nil, err
// 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()))
}
}

perpLog := []string{}
// loop through the perpetual orders and execute them
for _, perpetualOrderId := range msg.PerpetualOrderIds {
// get the perpetual order
Expand All @@ -67,10 +72,17 @@ func (k msgServer) ExecuteOrders(goCtx context.Context, msg *types.MsgExecuteOrd
}

// return the error if any
// log the error if any
if err != nil {
return nil, err
// Add log about error or not executed
perpLog = append(perpLog, fmt.Sprintf("Perpetual order Id:%d cannot be executed due to err: %s", perpetualOrderId, err.Error()))
}
}

ctx.EventManager().EmitEvent(sdk.NewEvent(types.TypeEvtExecuteOrders,
sdk.NewAttribute("spot_orders", strings.Join(spotLog, "\n")),
sdk.NewAttribute("perpetual_orders", strings.Join(perpLog, "\n")),
))

return &types.MsgExecuteOrdersResponse{}, nil
}
4 changes: 3 additions & 1 deletion x/tradeshield/keeper/msg_server_perpetual_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package keeper

import (
"context"
errorsmod "cosmossdk.io/errors"
"fmt"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
perpetualtypes "github.com/elys-network/elys/x/perpetual/types"
Expand Down Expand Up @@ -80,6 +81,7 @@ func (k msgServer) CreatePerpetualOpenOrder(goCtx context.Context, msg *types.Ms
Collateral: msg.Collateral,
TakeProfitPrice: msg.TakeProfitPrice,
PoolId: msg.PoolId,
LimitPrice: msg.TriggerPrice.Rate,
})
if err != nil {
return nil, err
Expand Down
11 changes: 10 additions & 1 deletion x/tradeshield/keeper/pending_perpetual_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,18 @@ func (k Keeper) ConstructPerpetualOrderExtraInfo(ctx sdk.Context, order types.Pe
Collateral: order.Collateral,
TakeProfitPrice: order.TakeProfitPrice,
PoolId: order.PoolId,
LimitPrice: order.TriggerPrice.Rate,
})

// If error use zero values
if err != nil {
return nil, err
return &types.PerpetualOrderExtraInfo{
PerpetualOrder: &order,
PositionSize: sdk.Coin{},
LiquidationPrice: sdkmath.LegacyZeroDec(),
FundingRate: sdkmath.LegacyZeroDec(),
BorrowInterestRate: sdkmath.LegacyZeroDec(),
}, nil
}

return &types.PerpetualOrderExtraInfo{
Expand Down
1 change: 1 addition & 0 deletions x/tradeshield/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
const (
TypeEvtCloseSpotOrder = "close_spot_order"
TypeEvtCancelPerpetualOrder = "cancel_perpetual_order"
TypeEvtExecuteOrders = "execute_orders"
)

func EmitCloseSpotOrderEvent(ctx sdk.Context, order SpotOrder) {
Expand Down

0 comments on commit 557affe

Please sign in to comment.