Skip to content

Commit

Permalink
[DEC-2159] - add generic json printer to help format customtypes in p…
Browse files Browse the repository at this point in the history
…rotos (#702)

* add generic json printer to help format customtypes in protos

* remove unreachable check for nil jsonData
  • Loading branch information
jakob-dydx authored Oct 25, 2023
1 parent 71529ce commit e47a416
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
17 changes: 17 additions & 0 deletions protocol/lib/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lib

import (
"encoding/json"
"fmt"
)

// MaybeGetStructJsonString returns the json representation of a struct, or a formatted string using
// %+v if the json conversion encounters an error.
func MaybeGetJsonString(i interface{}) string {
jsonData, err := json.Marshal(i)
if err != nil {
return fmt.Sprintf("%+v", i)
}

return string(jsonData)
}
11 changes: 5 additions & 6 deletions protocol/x/clob/keeper/deleveraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
errorsmod "cosmossdk.io/errors"

gometrics "github.com/armon/go-metrics"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
Expand Down Expand Up @@ -269,8 +268,8 @@ func (k Keeper) OffsetSubaccountPerpetualPosition(
"checkTx", ctx.IsCheckTx(),
"perpetualId", perpetualId,
"deltaQuantums", deltaQuantums,
"liquidatedSubaccount", log.NewLazySprintf("%+v", liquidatedSubaccount),
"offsettingSubaccount", log.NewLazySprintf("%+v", offsettingSubaccount),
"liquidatedSubaccount", liquidatedSubaccount,
"offsettingSubaccount", offsettingSubaccount,
)
numSubaccountsWithNonOverlappingBankruptcyPrices++
}
Expand Down Expand Up @@ -345,9 +344,9 @@ func (k Keeper) ProcessDeleveraging(
offsettingPositionQuantums.CmpAbs(deltaQuantums) == -1 {
return errorsmod.Wrapf(
types.ErrInvalidPerpetualPositionSizeDelta,
"ProcessDeleveraging: liquidated = (%+v), offsetting = (%+v), perpetual id = (%d), deltaQuantums = (%+v)",
liquidatedSubaccount,
offsettingSubaccount,
"ProcessDeleveraging: liquidated = (%s), offsetting = (%s), perpetual id = (%d), deltaQuantums = (%+v)",
lib.MaybeGetJsonString(liquidatedSubaccount),
lib.MaybeGetJsonString(offsettingSubaccount),
perpetualId,
deltaQuantums,
)
Expand Down

0 comments on commit e47a416

Please sign in to comment.