From 091877cdac4f5f22745fadbe9e11d746ea6492bb Mon Sep 17 00:00:00 2001 From: abel Date: Tue, 16 Jan 2024 11:19:51 -0300 Subject: [PATCH] (fix) Addressed comments in PR review --- client/common/util.go | 5 +++++ client/core/market.go | 19 ++++++------------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/client/common/util.go b/client/common/util.go index f7e0c9bc..e840804a 100644 --- a/client/common/util.go +++ b/client/common/util.go @@ -5,6 +5,7 @@ import ( "crypto/x509" "encoding/hex" "fmt" + "github.com/shopspring/decimal" "os" "strings" @@ -56,3 +57,7 @@ func MsgResponse(data []byte) []*chaintypes.TxResponseGenericMessage { } return response.Messages } + +func RemoveExtraDecimals(value decimal.Decimal, decimalsToRemove int32) decimal.Decimal { + return value.Div(decimal.New(1, decimalsToRemove)) +} diff --git a/client/core/market.go b/client/core/market.go index 81356b73..e6f9fe88 100644 --- a/client/core/market.go +++ b/client/core/market.go @@ -1,6 +1,7 @@ package core import ( + "github.com/InjectiveLabs/sdk-go/client/common" cosmtypes "github.com/cosmos/cosmos-sdk/types" "github.com/shopspring/decimal" ) @@ -47,15 +48,11 @@ func (spotMarket SpotMarket) PriceFromChainFormat(chainValue cosmtypes.Dec) deci } func (spotMarket SpotMarket) QuantityFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal { - return spotMarket.fromExtendedChainformat(spotMarket.QuantityFromChainFormat(chainValue)) + return common.RemoveExtraDecimals(spotMarket.QuantityFromChainFormat(chainValue), AdditionalChainFormatDecimals) } func (spotMarket SpotMarket) PriceFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal { - return spotMarket.fromExtendedChainformat(spotMarket.PriceFromChainFormat(chainValue)) -} - -func (spotMarket SpotMarket) fromExtendedChainformat(chainValue decimal.Decimal) decimal.Decimal { - return chainValue.Div(decimal.New(1, AdditionalChainFormatDecimals)) + return common.RemoveExtraDecimals(spotMarket.PriceFromChainFormat(chainValue), AdditionalChainFormatDecimals) } type DerivativeMarket struct { @@ -130,17 +127,13 @@ func (derivativeMarket DerivativeMarket) MarginFromChainFormat(chainValue cosmty } func (derivativeMarket DerivativeMarket) QuantityFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal { - return derivativeMarket.fromExtendedChainformat(derivativeMarket.QuantityFromChainFormat(chainValue)) + return common.RemoveExtraDecimals(derivativeMarket.QuantityFromChainFormat(chainValue), AdditionalChainFormatDecimals) } func (derivativeMarket DerivativeMarket) PriceFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal { - return derivativeMarket.fromExtendedChainformat(derivativeMarket.PriceFromChainFormat(chainValue)) + return common.RemoveExtraDecimals(derivativeMarket.PriceFromChainFormat(chainValue), AdditionalChainFormatDecimals) } func (derivativeMarket DerivativeMarket) MarginFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal { - return derivativeMarket.fromExtendedChainformat(derivativeMarket.MarginFromChainFormat(chainValue)) -} - -func (derivativeMarket DerivativeMarket) fromExtendedChainformat(chainValue decimal.Decimal) decimal.Decimal { - return chainValue.Div(decimal.New(1, AdditionalChainFormatDecimals)) + return common.RemoveExtraDecimals(derivativeMarket.MarginFromChainFormat(chainValue), AdditionalChainFormatDecimals) }