Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/add chain values converters #202

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ all:

copy-exchange-client:
rm -rf exchange/*
mkdir -p exchange/meta_rpc
mkdir -p exchange/exchange_rpc
mkdir -p exchange/accounts_rpc
mkdir -p exchange/auction_rpc
mkdir -p exchange/oracle_rpc
mkdir -p exchange/insurance_rpc
mkdir -p exchange/explorer_rpc
mkdir -p exchange/spot_exchange_rpc
mkdir -p exchange/campaign_rpc
mkdir -p exchange/derivative_exchange_rpc
mkdir -p exchange/exchange_rpc
mkdir -p exchange/explorer_rpc
mkdir -p exchange/insurance_rpc
mkdir -p exchange/meta_rpc
mkdir -p exchange/oracle_rpc
mkdir -p exchange/portfolio_rpc
mkdir -p exchange/spot_exchange_rpc
mkdir -p exchange/trading_rpc

cp -r ../injective-indexer/api/gen/grpc/injective_meta_rpc/pb exchange/meta_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_exchange_rpc/pb exchange/exchange_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_accounts_rpc/pb exchange/accounts_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_auction_rpc/pb exchange/auction_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_oracle_rpc/pb exchange/oracle_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_insurance_rpc/pb exchange/insurance_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_explorer_rpc/pb exchange/explorer_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_spot_exchange_rpc/pb exchange/spot_exchange_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_campaign_rpc/pb exchange/campaign_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_derivative_exchange_rpc/pb exchange/derivative_exchange_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_exchange_rpc/pb exchange/exchange_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_explorer_rpc/pb exchange/explorer_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_insurance_rpc/pb exchange/insurance_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_meta_rpc/pb exchange/meta_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_oracle_rpc/pb exchange/oracle_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_portfolio_rpc/pb exchange/portfolio_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_spot_exchange_rpc/pb exchange/spot_exchange_rpc/pb
cp -r ../injective-indexer/api/gen/grpc/injective_trading_rpc/pb exchange/trading_rpc/pb

.PHONY: copy-exchange-client tests coverage

Expand Down
6 changes: 6 additions & 0 deletions client/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"os"
"strings"

"github.com/shopspring/decimal"

chaintypes "github.com/InjectiveLabs/sdk-go/chain/types"
"google.golang.org/grpc/credentials"
)
Expand Down Expand Up @@ -56,3 +58,7 @@
}
return response.Messages
}

func RemoveExtraDecimals(value decimal.Decimal, decimalsToRemove int32) decimal.Decimal {
return value.Div(decimal.New(1, decimalsToRemove))

Check warning on line 63 in client/common/util.go

View check run for this annotation

Codecov / codecov/patch

client/common/util.go#L62-L63

Added lines #L62 - L63 were not covered by tests
}
21 changes: 21 additions & 0 deletions client/core/market.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -46,6 +47,14 @@ func (spotMarket SpotMarket) PriceFromChainFormat(chainValue cosmtypes.Dec) deci
return decimal.RequireFromString(chainValue.String()).Mul(decimal.New(1, decimals))
}

func (spotMarket SpotMarket) QuantityFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal {
return common.RemoveExtraDecimals(spotMarket.QuantityFromChainFormat(chainValue), AdditionalChainFormatDecimals)
}

func (spotMarket SpotMarket) PriceFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal {
return common.RemoveExtraDecimals(spotMarket.PriceFromChainFormat(chainValue), AdditionalChainFormatDecimals)
}

type DerivativeMarket struct {
Id string
Status string
Expand Down Expand Up @@ -116,3 +125,15 @@ func (derivativeMarket DerivativeMarket) MarginFromChainFormat(chainValue cosmty
decimals := -derivativeMarket.QuoteToken.Decimals
return decimal.RequireFromString(chainValue.String()).Mul(decimal.New(1, decimals))
}

func (derivativeMarket DerivativeMarket) QuantityFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal {
return common.RemoveExtraDecimals(derivativeMarket.QuantityFromChainFormat(chainValue), AdditionalChainFormatDecimals)
}

func (derivativeMarket DerivativeMarket) PriceFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal {
return common.RemoveExtraDecimals(derivativeMarket.PriceFromChainFormat(chainValue), AdditionalChainFormatDecimals)
}

func (derivativeMarket DerivativeMarket) MarginFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal {
return common.RemoveExtraDecimals(derivativeMarket.MarginFromChainFormat(chainValue), AdditionalChainFormatDecimals)
}
53 changes: 53 additions & 0 deletions client/core/market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ func TestConvertPriceFromChainFormatForSpotMarket(t *testing.T) {
assert.Assert(t, expectedPrice.Equal(humanReadablePrice))
}

func TestConvertQuantityFromExtendedChainFormatForSpotMarket(t *testing.T) {
spotMarket := createINJUSDTSpotMarket()
expectedQuantity := decimal.RequireFromString("123.456")

chainFormatQuantity := expectedQuantity.Mul(decimal.New(1, spotMarket.BaseToken.Decimals)).Mul(decimal.New(1, AdditionalChainFormatDecimals))
humanReadableQuantity := spotMarket.QuantityFromExtendedChainFormat(types.MustNewDecFromStr(chainFormatQuantity.String()))

assert.Assert(t, expectedQuantity.Equal(humanReadableQuantity))
}

func TestConvertPriceFromExtendedChainFormatForSpotMarket(t *testing.T) {
spotMarket := createINJUSDTSpotMarket()
expectedPrice := decimal.RequireFromString("123.456")

priceDecimals := spotMarket.QuoteToken.Decimals - spotMarket.BaseToken.Decimals
chainFormatPrice := expectedPrice.Mul(decimal.New(1, priceDecimals)).Mul(decimal.New(1, AdditionalChainFormatDecimals))
humanReadablePrice := spotMarket.PriceFromExtendedChainFormat(types.MustNewDecFromStr(chainFormatPrice.String()))

assert.Assert(t, expectedPrice.Equal(humanReadablePrice))
}

//Derivative markets tests

func TestConvertQuantityToChainFormatForDerivativeMarket(t *testing.T) {
Expand Down Expand Up @@ -197,3 +218,35 @@ func TestConvertMarginFromChainFormatForDerivativeMarket(t *testing.T) {

assert.Assert(t, expectedMargin.Equal(humanReadablePrice))
}

func TestConvertQuantityFromExtendedChainFormatForDerivativeMarket(t *testing.T) {
derivativeMarket := createBTCUSDTPerpMarket()
expectedQuantity := decimal.RequireFromString("123.456")

chainFormatQuantity := expectedQuantity.Mul(decimal.New(1, AdditionalChainFormatDecimals))
humanReadableQuantity := derivativeMarket.QuantityFromExtendedChainFormat(types.MustNewDecFromStr(chainFormatQuantity.String()))

assert.Assert(t, expectedQuantity.Equal(humanReadableQuantity))
}

func TestConvertPriceFromExtendedChainFormatForDerivativeMarket(t *testing.T) {
derivativeMarket := createBTCUSDTPerpMarket()
expectedPrice := decimal.RequireFromString("123.456")

priceDecimals := derivativeMarket.QuoteToken.Decimals
chainFormatPrice := expectedPrice.Mul(decimal.New(1, priceDecimals)).Mul(decimal.New(1, AdditionalChainFormatDecimals))
humanReadablePrice := derivativeMarket.PriceFromExtendedChainFormat(types.MustNewDecFromStr(chainFormatPrice.String()))

assert.Assert(t, expectedPrice.Equal(humanReadablePrice))
}

func TestConvertMarginFromExtendedChainFormatForDerivativeMarket(t *testing.T) {
derivativeMarket := createBTCUSDTPerpMarket()
expectedMargin := decimal.RequireFromString("123.456")

marginDecimals := derivativeMarket.QuoteToken.Decimals
chainFormatMargin := expectedMargin.Mul(decimal.New(1, marginDecimals)).Mul(decimal.New(1, AdditionalChainFormatDecimals))
humanReadablePrice := derivativeMarket.MarginFromExtendedChainFormat(types.MustNewDecFromStr(chainFormatMargin.String()))

assert.Assert(t, expectedMargin.Equal(humanReadablePrice))
}
Loading
Loading