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 1 commit
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
28 changes: 28 additions & 0 deletions client/core/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ 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 spotMarket.fromExtendedChainformat(spotMarket.QuantityFromChainFormat(chainValue))
}

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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to do this in utils file, this one doesn't depend on spotMarket or derivativeMarket

fromExtendedChainformat(chainValue decimal.Decimal) decimal.Decimal

But why chainValue has decimal.Decimal type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will move it (unfortunately moving it to utils makes it less OOP, something that I don't really like)

Regarding the chainValue, it was not originally decimal.Decimal. But the function that calls this one first translates the value according to market tokens decimals, making it a decimal.Decimal

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately moving it to utils makes it less OOP, something that I don't really like

Yeah it's a tool and we need flexibility to choose which to use

Regarding the chainValue, it was not originally decimal.Decimal. But the function that calls this one first translates the value according to market tokens decimals, making it a decimal.Decimal

Oh I thought using sdk.Dec is enough when chain streamed the value

}

type DerivativeMarket struct {
Id string
Status string
Expand Down Expand Up @@ -116,3 +128,19 @@ 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 derivativeMarket.fromExtendedChainformat(derivativeMarket.QuantityFromChainFormat(chainValue))
}

func (derivativeMarket DerivativeMarket) PriceFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal {
return derivativeMarket.fromExtendedChainformat(derivativeMarket.PriceFromChainFormat(chainValue))
}

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))
}
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