Skip to content

Commit 02cb6ed

Browse files
authored
Merge pull request #202 from InjectiveLabs/feat/add_chain_values_converters
feat/add chain values converters
2 parents bf3ea70 + a6f2d0c commit 02cb6ed

File tree

55 files changed

+5799
-1510
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+5799
-1510
lines changed

Makefile

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,31 @@ all:
22

33
copy-exchange-client:
44
rm -rf exchange/*
5-
mkdir -p exchange/meta_rpc
6-
mkdir -p exchange/exchange_rpc
75
mkdir -p exchange/accounts_rpc
86
mkdir -p exchange/auction_rpc
9-
mkdir -p exchange/oracle_rpc
10-
mkdir -p exchange/insurance_rpc
11-
mkdir -p exchange/explorer_rpc
12-
mkdir -p exchange/spot_exchange_rpc
7+
mkdir -p exchange/campaign_rpc
138
mkdir -p exchange/derivative_exchange_rpc
9+
mkdir -p exchange/exchange_rpc
10+
mkdir -p exchange/explorer_rpc
11+
mkdir -p exchange/insurance_rpc
12+
mkdir -p exchange/meta_rpc
13+
mkdir -p exchange/oracle_rpc
1414
mkdir -p exchange/portfolio_rpc
15+
mkdir -p exchange/spot_exchange_rpc
16+
mkdir -p exchange/trading_rpc
1517

16-
cp -r ../injective-indexer/api/gen/grpc/injective_meta_rpc/pb exchange/meta_rpc/pb
17-
cp -r ../injective-indexer/api/gen/grpc/injective_exchange_rpc/pb exchange/exchange_rpc/pb
1818
cp -r ../injective-indexer/api/gen/grpc/injective_accounts_rpc/pb exchange/accounts_rpc/pb
1919
cp -r ../injective-indexer/api/gen/grpc/injective_auction_rpc/pb exchange/auction_rpc/pb
20-
cp -r ../injective-indexer/api/gen/grpc/injective_oracle_rpc/pb exchange/oracle_rpc/pb
21-
cp -r ../injective-indexer/api/gen/grpc/injective_insurance_rpc/pb exchange/insurance_rpc/pb
22-
cp -r ../injective-indexer/api/gen/grpc/injective_explorer_rpc/pb exchange/explorer_rpc/pb
23-
cp -r ../injective-indexer/api/gen/grpc/injective_spot_exchange_rpc/pb exchange/spot_exchange_rpc/pb
20+
cp -r ../injective-indexer/api/gen/grpc/injective_campaign_rpc/pb exchange/campaign_rpc/pb
2421
cp -r ../injective-indexer/api/gen/grpc/injective_derivative_exchange_rpc/pb exchange/derivative_exchange_rpc/pb
22+
cp -r ../injective-indexer/api/gen/grpc/injective_exchange_rpc/pb exchange/exchange_rpc/pb
23+
cp -r ../injective-indexer/api/gen/grpc/injective_explorer_rpc/pb exchange/explorer_rpc/pb
24+
cp -r ../injective-indexer/api/gen/grpc/injective_insurance_rpc/pb exchange/insurance_rpc/pb
25+
cp -r ../injective-indexer/api/gen/grpc/injective_meta_rpc/pb exchange/meta_rpc/pb
26+
cp -r ../injective-indexer/api/gen/grpc/injective_oracle_rpc/pb exchange/oracle_rpc/pb
2527
cp -r ../injective-indexer/api/gen/grpc/injective_portfolio_rpc/pb exchange/portfolio_rpc/pb
28+
cp -r ../injective-indexer/api/gen/grpc/injective_spot_exchange_rpc/pb exchange/spot_exchange_rpc/pb
29+
cp -r ../injective-indexer/api/gen/grpc/injective_trading_rpc/pb exchange/trading_rpc/pb
2630

2731
.PHONY: copy-exchange-client tests coverage
2832

client/common/util.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"os"
99
"strings"
1010

11+
"github.com/shopspring/decimal"
12+
1113
chaintypes "github.com/InjectiveLabs/sdk-go/chain/types"
1214
"google.golang.org/grpc/credentials"
1315
)
@@ -56,3 +58,7 @@ func MsgResponse(data []byte) []*chaintypes.TxResponseGenericMessage {
5658
}
5759
return response.Messages
5860
}
61+
62+
func RemoveExtraDecimals(value decimal.Decimal, decimalsToRemove int32) decimal.Decimal {
63+
return value.Div(decimal.New(1, decimalsToRemove))
64+
}

client/core/market.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core
22

33
import (
4+
"github.com/InjectiveLabs/sdk-go/client/common"
45
cosmtypes "github.com/cosmos/cosmos-sdk/types"
56
"github.com/shopspring/decimal"
67
)
@@ -46,6 +47,14 @@ func (spotMarket SpotMarket) PriceFromChainFormat(chainValue cosmtypes.Dec) deci
4647
return decimal.RequireFromString(chainValue.String()).Mul(decimal.New(1, decimals))
4748
}
4849

50+
func (spotMarket SpotMarket) QuantityFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal {
51+
return common.RemoveExtraDecimals(spotMarket.QuantityFromChainFormat(chainValue), AdditionalChainFormatDecimals)
52+
}
53+
54+
func (spotMarket SpotMarket) PriceFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal {
55+
return common.RemoveExtraDecimals(spotMarket.PriceFromChainFormat(chainValue), AdditionalChainFormatDecimals)
56+
}
57+
4958
type DerivativeMarket struct {
5059
Id string
5160
Status string
@@ -116,3 +125,15 @@ func (derivativeMarket DerivativeMarket) MarginFromChainFormat(chainValue cosmty
116125
decimals := -derivativeMarket.QuoteToken.Decimals
117126
return decimal.RequireFromString(chainValue.String()).Mul(decimal.New(1, decimals))
118127
}
128+
129+
func (derivativeMarket DerivativeMarket) QuantityFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal {
130+
return common.RemoveExtraDecimals(derivativeMarket.QuantityFromChainFormat(chainValue), AdditionalChainFormatDecimals)
131+
}
132+
133+
func (derivativeMarket DerivativeMarket) PriceFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal {
134+
return common.RemoveExtraDecimals(derivativeMarket.PriceFromChainFormat(chainValue), AdditionalChainFormatDecimals)
135+
}
136+
137+
func (derivativeMarket DerivativeMarket) MarginFromExtendedChainFormat(chainValue cosmtypes.Dec) decimal.Decimal {
138+
return common.RemoveExtraDecimals(derivativeMarket.MarginFromChainFormat(chainValue), AdditionalChainFormatDecimals)
139+
}

client/core/market_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ func TestConvertPriceFromChainFormatForSpotMarket(t *testing.T) {
112112
assert.Assert(t, expectedPrice.Equal(humanReadablePrice))
113113
}
114114

115+
func TestConvertQuantityFromExtendedChainFormatForSpotMarket(t *testing.T) {
116+
spotMarket := createINJUSDTSpotMarket()
117+
expectedQuantity := decimal.RequireFromString("123.456")
118+
119+
chainFormatQuantity := expectedQuantity.Mul(decimal.New(1, spotMarket.BaseToken.Decimals)).Mul(decimal.New(1, AdditionalChainFormatDecimals))
120+
humanReadableQuantity := spotMarket.QuantityFromExtendedChainFormat(types.MustNewDecFromStr(chainFormatQuantity.String()))
121+
122+
assert.Assert(t, expectedQuantity.Equal(humanReadableQuantity))
123+
}
124+
125+
func TestConvertPriceFromExtendedChainFormatForSpotMarket(t *testing.T) {
126+
spotMarket := createINJUSDTSpotMarket()
127+
expectedPrice := decimal.RequireFromString("123.456")
128+
129+
priceDecimals := spotMarket.QuoteToken.Decimals - spotMarket.BaseToken.Decimals
130+
chainFormatPrice := expectedPrice.Mul(decimal.New(1, priceDecimals)).Mul(decimal.New(1, AdditionalChainFormatDecimals))
131+
humanReadablePrice := spotMarket.PriceFromExtendedChainFormat(types.MustNewDecFromStr(chainFormatPrice.String()))
132+
133+
assert.Assert(t, expectedPrice.Equal(humanReadablePrice))
134+
}
135+
115136
//Derivative markets tests
116137

117138
func TestConvertQuantityToChainFormatForDerivativeMarket(t *testing.T) {
@@ -197,3 +218,35 @@ func TestConvertMarginFromChainFormatForDerivativeMarket(t *testing.T) {
197218

198219
assert.Assert(t, expectedMargin.Equal(humanReadablePrice))
199220
}
221+
222+
func TestConvertQuantityFromExtendedChainFormatForDerivativeMarket(t *testing.T) {
223+
derivativeMarket := createBTCUSDTPerpMarket()
224+
expectedQuantity := decimal.RequireFromString("123.456")
225+
226+
chainFormatQuantity := expectedQuantity.Mul(decimal.New(1, AdditionalChainFormatDecimals))
227+
humanReadableQuantity := derivativeMarket.QuantityFromExtendedChainFormat(types.MustNewDecFromStr(chainFormatQuantity.String()))
228+
229+
assert.Assert(t, expectedQuantity.Equal(humanReadableQuantity))
230+
}
231+
232+
func TestConvertPriceFromExtendedChainFormatForDerivativeMarket(t *testing.T) {
233+
derivativeMarket := createBTCUSDTPerpMarket()
234+
expectedPrice := decimal.RequireFromString("123.456")
235+
236+
priceDecimals := derivativeMarket.QuoteToken.Decimals
237+
chainFormatPrice := expectedPrice.Mul(decimal.New(1, priceDecimals)).Mul(decimal.New(1, AdditionalChainFormatDecimals))
238+
humanReadablePrice := derivativeMarket.PriceFromExtendedChainFormat(types.MustNewDecFromStr(chainFormatPrice.String()))
239+
240+
assert.Assert(t, expectedPrice.Equal(humanReadablePrice))
241+
}
242+
243+
func TestConvertMarginFromExtendedChainFormatForDerivativeMarket(t *testing.T) {
244+
derivativeMarket := createBTCUSDTPerpMarket()
245+
expectedMargin := decimal.RequireFromString("123.456")
246+
247+
marginDecimals := derivativeMarket.QuoteToken.Decimals
248+
chainFormatMargin := expectedMargin.Mul(decimal.New(1, marginDecimals)).Mul(decimal.New(1, AdditionalChainFormatDecimals))
249+
humanReadablePrice := derivativeMarket.MarginFromExtendedChainFormat(types.MustNewDecFromStr(chainFormatMargin.String()))
250+
251+
assert.Assert(t, expectedMargin.Equal(humanReadablePrice))
252+
}

0 commit comments

Comments
 (0)