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/sync dec with master v1 51 1 #233

Merged
merged 13 commits into from
Aug 7, 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
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
- name: Run test and calculate coverage
run: make coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Clone the repository locally and install needed dependencies
```bash
$ git clone [email protected]:InjectiveLabs/sdk-go.git
$ cd sdk-go
$ go install ./...
$ go mod download
```

## Run examples
Expand All @@ -20,10 +20,10 @@ $ go install ./...
injectived keys unsafe-import-eth-key inj-user 5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e

# run chain example
go run examples/chain/0_MsgSend.go
go run examples/chain/bank/1_MsgSend/example.go

# run exchange example
go run examples/exchange/derivative_exchange_rpc/0_GetOrderbook.go
go run examples/exchange/derivatives/4_Orderbook/example.go
```

---
Expand Down
10 changes: 10 additions & 0 deletions client/chain/markets_assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func NewMarketsAssistant(networkName string) (MarketsAssistant, error) {

minPriceTickSize := decimal.RequireFromString(section.Key("min_price_tick_size").String())
minQuantityTickSize := decimal.RequireFromString(section.Key("min_quantity_tick_size").String())
minNotional := decimal.Zero
if section.HasKey("min_notional") {
minNotional = decimal.RequireFromString(section.Key("min_notional").String())
}

if strings.Contains(description, "Spot") {
baseDecimals, _ := section.Key("quote").Int()
Expand All @@ -97,6 +101,7 @@ func NewMarketsAssistant(networkName string) (MarketsAssistant, error) {
ServiceProviderFee: decimal.NewFromInt32(0),
MinPriceTickSize: minPriceTickSize,
MinQuantityTickSize: minQuantityTickSize,
MinNotional: minNotional,
}

assistant.spotMarkets[market.Id] = market
Expand All @@ -117,6 +122,7 @@ func NewMarketsAssistant(networkName string) (MarketsAssistant, error) {
ServiceProviderFee: decimal.NewFromInt32(0),
MinPriceTickSize: minPriceTickSize,
MinQuantityTickSize: minQuantityTickSize,
MinNotional: minNotional,
}

assistant.derivativeMarkets[market.Id] = market
Expand Down Expand Up @@ -190,6 +196,7 @@ func NewMarketsAssistantInitializedFromChain(ctx context.Context, exchangeClient
serviceProviderFee := decimal.RequireFromString(marketInfo.GetServiceProviderFee())
minPriceTickSize := decimal.RequireFromString(marketInfo.GetMinPriceTickSize())
minQuantityTickSize := decimal.RequireFromString(marketInfo.GetMinQuantityTickSize())
minNotional := decimal.RequireFromString(marketInfo.GetMinNotional())

market := core.SpotMarket{
Id: marketInfo.GetMarketId(),
Expand All @@ -202,6 +209,7 @@ func NewMarketsAssistantInitializedFromChain(ctx context.Context, exchangeClient
ServiceProviderFee: serviceProviderFee,
MinPriceTickSize: minPriceTickSize,
MinQuantityTickSize: minQuantityTickSize,
MinNotional: minNotional,
}

assistant.spotMarkets[market.Id] = market
Expand Down Expand Up @@ -232,6 +240,7 @@ func NewMarketsAssistantInitializedFromChain(ctx context.Context, exchangeClient
serviceProviderFee := decimal.RequireFromString(marketInfo.GetServiceProviderFee())
minPriceTickSize := decimal.RequireFromString(marketInfo.GetMinPriceTickSize())
minQuantityTickSize := decimal.RequireFromString(marketInfo.GetMinQuantityTickSize())
minNotional := decimal.RequireFromString(marketInfo.GetMinNotional())

market := core.DerivativeMarket{
Id: marketInfo.GetMarketId(),
Expand All @@ -249,6 +258,7 @@ func NewMarketsAssistantInitializedFromChain(ctx context.Context, exchangeClient
ServiceProviderFee: serviceProviderFee,
MinPriceTickSize: minPriceTickSize,
MinQuantityTickSize: minQuantityTickSize,
MinNotional: minNotional,
}

assistant.derivativeMarkets[market.Id] = market
Expand Down
3 changes: 3 additions & 0 deletions client/chain/markets_assistant_test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func createINJUSDTSpotMarketInfo() *spotExchangePB.SpotMarketInfo {
ServiceProviderFee: "0.4",
MinPriceTickSize: "0.000000000000001",
MinQuantityTickSize: "1000000000000000",
MinNotional: "1000000",
}

return &marketInfo
Expand All @@ -87,6 +88,7 @@ func createAPEUSDTSpotMarketInfo() *spotExchangePB.SpotMarketInfo {
ServiceProviderFee: "0.4",
MinPriceTickSize: "0.000000000000001",
MinQuantityTickSize: "1000000000000000",
MinNotional: "1000000",
}

return &marketInfo
Expand Down Expand Up @@ -128,6 +130,7 @@ func createBTCUSDTDerivativeMarketInfo() *derivativeExchangePB.DerivativeMarketI
MinQuantityTickSize: "0.0001",
PerpetualMarketInfo: &perpetualMarketInfo,
PerpetualMarketFunding: &perpetualmarketFunding,
MinNotional: "1000000",
}

return &marketInfo
Expand Down
42 changes: 36 additions & 6 deletions client/core/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type SpotMarket struct {
ServiceProviderFee decimal.Decimal
MinPriceTickSize decimal.Decimal
MinQuantityTickSize decimal.Decimal
MinNotional decimal.Decimal
}

func (spotMarket SpotMarket) QuantityToChainFormat(humanReadableValue decimal.Decimal) sdkmath.LegacyDec {
Expand All @@ -38,6 +39,14 @@ func (spotMarket SpotMarket) PriceToChainFormat(humanReadableValue decimal.Decim
return valueInChainFormat
}

func (spotMarket SpotMarket) NotionalToChainFormat(humanReadableValue decimal.Decimal) sdkmath.LegacyDec {
decimals := spotMarket.QuoteToken.Decimals
chainFormattedValue := humanReadableValue.Mul(decimal.New(1, decimals))
valueInChainFormat, _ := sdkmath.LegacyNewDecFromStr(chainFormattedValue.String())

return valueInChainFormat
}

func (spotMarket SpotMarket) QuantityFromChainFormat(chainValue sdkmath.LegacyDec) decimal.Decimal {
return decimal.RequireFromString(chainValue.String()).Div(decimal.New(1, spotMarket.BaseToken.Decimals))
}
Expand All @@ -47,6 +56,11 @@ func (spotMarket SpotMarket) PriceFromChainFormat(chainValue sdkmath.LegacyDec)
return decimal.RequireFromString(chainValue.String()).Mul(decimal.New(1, decimals))
}

func (spotMarket SpotMarket) NotionalFromChainFormat(chainValue sdkmath.LegacyDec) decimal.Decimal {
decimals := -spotMarket.QuoteToken.Decimals
return decimal.RequireFromString(chainValue.String()).Mul(decimal.New(1, decimals))
}

func (spotMarket SpotMarket) QuantityFromExtendedChainFormat(chainValue sdkmath.LegacyDec) decimal.Decimal {
return common.RemoveExtraDecimals(spotMarket.QuantityFromChainFormat(chainValue), AdditionalChainFormatDecimals)
}
Expand All @@ -55,6 +69,10 @@ func (spotMarket SpotMarket) PriceFromExtendedChainFormat(chainValue sdkmath.Leg
return common.RemoveExtraDecimals(spotMarket.PriceFromChainFormat(chainValue), AdditionalChainFormatDecimals)
}

func (spotMarket SpotMarket) NotionalFromExtendedChainFormat(chainValue sdkmath.LegacyDec) decimal.Decimal {
return common.RemoveExtraDecimals(spotMarket.NotionalFromChainFormat(chainValue), AdditionalChainFormatDecimals)
}

type DerivativeMarket struct {
Id string
Status string
Expand All @@ -71,6 +89,7 @@ type DerivativeMarket struct {
ServiceProviderFee decimal.Decimal
MinPriceTickSize decimal.Decimal
MinQuantityTickSize decimal.Decimal
MinNotional decimal.Decimal
}

func (derivativeMarket DerivativeMarket) QuantityToChainFormat(humanReadableValue decimal.Decimal) sdkmath.LegacyDec {
Expand All @@ -91,12 +110,7 @@ func (derivativeMarket DerivativeMarket) PriceToChainFormat(humanReadableValue d
}

func (derivativeMarket DerivativeMarket) MarginToChainFormat(humanReadableValue decimal.Decimal) sdkmath.LegacyDec {
decimals := derivativeMarket.QuoteToken.Decimals
chainFormattedValue := humanReadableValue.Mul(decimal.New(1, decimals))
quantizedValue := chainFormattedValue.DivRound(derivativeMarket.MinQuantityTickSize, 0).Mul(derivativeMarket.MinQuantityTickSize)
valueInChainFormat, _ := sdkmath.LegacyNewDecFromStr(quantizedValue.String())

return valueInChainFormat
return derivativeMarket.NotionalToChainFormat(humanReadableValue)
}

func (derivativeMarket DerivativeMarket) CalculateMarginInChainFormat(humanReadableQuantity, humanReadablePrice, leverage decimal.Decimal) sdkmath.LegacyDec {
Expand All @@ -112,6 +126,14 @@ func (derivativeMarket DerivativeMarket) CalculateMarginInChainFormat(humanReada
return valueInChainFormat
}

func (derivativeMarket DerivativeMarket) NotionalToChainFormat(humanReadableValue decimal.Decimal) sdkmath.LegacyDec {
decimals := derivativeMarket.QuoteToken.Decimals
chainFormattedValue := humanReadableValue.Mul(decimal.New(1, decimals))
valueInChainFormat, _ := sdkmath.LegacyNewDecFromStr(chainFormattedValue.String())

return valueInChainFormat
}

func (derivativeMarket DerivativeMarket) QuantityFromChainFormat(chainValue sdkmath.LegacyDec) decimal.Decimal {
return decimal.RequireFromString(chainValue.String())
}
Expand All @@ -122,6 +144,10 @@ func (derivativeMarket DerivativeMarket) PriceFromChainFormat(chainValue sdkmath
}

func (derivativeMarket DerivativeMarket) MarginFromChainFormat(chainValue sdkmath.LegacyDec) decimal.Decimal {
return derivativeMarket.NotionalFromChainFormat(chainValue)
}

func (derivativeMarket DerivativeMarket) NotionalFromChainFormat(chainValue sdkmath.LegacyDec) decimal.Decimal {
decimals := -derivativeMarket.QuoteToken.Decimals
return decimal.RequireFromString(chainValue.String()).Mul(decimal.New(1, decimals))
}
Expand All @@ -137,3 +163,7 @@ func (derivativeMarket DerivativeMarket) PriceFromExtendedChainFormat(chainValue
func (derivativeMarket DerivativeMarket) MarginFromExtendedChainFormat(chainValue sdkmath.LegacyDec) decimal.Decimal {
return common.RemoveExtraDecimals(derivativeMarket.MarginFromChainFormat(chainValue), AdditionalChainFormatDecimals)
}

func (derivativeMarket DerivativeMarket) NotionalFromExtendedChainFormat(chainValue sdkmath.LegacyDec) decimal.Decimal {
return common.RemoveExtraDecimals(derivativeMarket.NotionalFromChainFormat(chainValue), AdditionalChainFormatDecimals)
}
72 changes: 72 additions & 0 deletions client/core/market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func createINJUSDTSpotMarket() SpotMarket {
serviceProviderFee := decimal.RequireFromString("0.4")
minPriceTickSize := decimal.RequireFromString("0.000000000000001")
minQuantityTickSize := decimal.RequireFromString("1000000000000000")
minNotional := decimal.RequireFromString("1000000")

market := SpotMarket{
Id: "0x7a57e705bb4e09c88aecfc295569481dbf2fe1d5efe364651fbe72385938e9b0",
Expand All @@ -30,6 +31,7 @@ func createINJUSDTSpotMarket() SpotMarket {
ServiceProviderFee: serviceProviderFee,
MinPriceTickSize: minPriceTickSize,
MinQuantityTickSize: minQuantityTickSize,
MinNotional: minNotional,
}
return market
}
Expand All @@ -44,6 +46,7 @@ func createBTCUSDTPerpMarket() DerivativeMarket {
serviceProviderFee := decimal.RequireFromString("0.4")
minPriceTickSize := decimal.RequireFromString("1000000")
minQuantityTickSize := decimal.RequireFromString("0.0001")
minNotional := decimal.RequireFromString("1000000")

market := DerivativeMarket{
Id: "0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce",
Expand All @@ -61,6 +64,7 @@ func createBTCUSDTPerpMarket() DerivativeMarket {
ServiceProviderFee: serviceProviderFee,
MinPriceTickSize: minPriceTickSize,
MinQuantityTickSize: minQuantityTickSize,
MinNotional: minNotional,
}
return market
}
Expand Down Expand Up @@ -92,6 +96,18 @@ func TestConvertPriceToChainFormatForSpotMarket(t *testing.T) {
assert.Assert(t, quantizedChainFormatValue.Equal(chainValue))
}

func TestConvertNotionalToChainFormatForSpotMarket(t *testing.T) {
spotMarket := createINJUSDTSpotMarket()
originalNotional := decimal.RequireFromString("123.456789")

chainValue := spotMarket.NotionalToChainFormat(originalNotional)
notionalDecimals := spotMarket.QuoteToken.Decimals
expectedValue := originalNotional.Mul(decimal.New(1, notionalDecimals))
chainFormatValue := sdkmath.LegacyMustNewDecFromStr(expectedValue.String())

assert.Assert(t, chainFormatValue.Equal(chainValue))
}

func TestConvertQuantityFromChainFormatForSpotMarket(t *testing.T) {
spotMarket := createINJUSDTSpotMarket()
expectedQuantity := decimal.RequireFromString("123.456")
Expand All @@ -113,6 +129,17 @@ func TestConvertPriceFromChainFormatForSpotMarket(t *testing.T) {
assert.Assert(t, expectedPrice.Equal(humanReadablePrice))
}

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

notionalDecimals := spotMarket.QuoteToken.Decimals
chainFormatPrice := expectedNotional.Mul(decimal.New(1, notionalDecimals))
humanReadableNotional := spotMarket.NotionalFromChainFormat(sdkmath.LegacyMustNewDecFromStr(chainFormatPrice.String()))

assert.Assert(t, expectedNotional.Equal(humanReadableNotional))
}

func TestConvertQuantityFromExtendedChainFormatForSpotMarket(t *testing.T) {
spotMarket := createINJUSDTSpotMarket()
expectedQuantity := decimal.RequireFromString("123.456")
Expand All @@ -134,6 +161,17 @@ func TestConvertPriceFromExtendedChainFormatForSpotMarket(t *testing.T) {
assert.Assert(t, expectedPrice.Equal(humanReadablePrice))
}

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

notionalDecimals := spotMarket.QuoteToken.Decimals
chainFormatNotional := expectedNotional.Mul(decimal.New(1, notionalDecimals)).Mul(decimal.New(1, AdditionalChainFormatDecimals))
humanReadableNotional := spotMarket.NotionalFromExtendedChainFormat(sdkmath.LegacyMustNewDecFromStr(chainFormatNotional.String()))

assert.Assert(t, expectedNotional.Equal(humanReadableNotional))
}

// Derivative markets tests

func TestConvertQuantityToChainFormatForDerivativeMarket(t *testing.T) {
Expand Down Expand Up @@ -188,6 +226,18 @@ func TestCalculateMarginInChainFormatForDerivativeMarket(t *testing.T) {
assert.Assert(t, chainValue.Equal(legacyDecimalQuantizedValue))
}

func TestConvertNotionalToChainFormatForDerivativeMarket(t *testing.T) {
derivativeMarket := createBTCUSDTPerpMarket()
originalNotional := decimal.RequireFromString("123.456789")

chainValue := derivativeMarket.NotionalToChainFormat(originalNotional)
notionalDecimals := derivativeMarket.QuoteToken.Decimals
expectedValue := originalNotional.Mul(decimal.New(1, notionalDecimals))
expectedChainFormatValue := sdkmath.LegacyMustNewDecFromStr(expectedValue.String())

assert.Assert(t, expectedChainFormatValue.Equal(chainValue))
}

func TestConvertQuantityFromChainFormatForDerivativeMarket(t *testing.T) {
derivativeMarket := createBTCUSDTPerpMarket()
expectedQuantity := decimal.RequireFromString("123.456")
Expand Down Expand Up @@ -220,6 +270,17 @@ func TestConvertMarginFromChainFormatForDerivativeMarket(t *testing.T) {
assert.Assert(t, expectedMargin.Equal(humanReadablePrice))
}

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

notionalDecimals := derivativeMarket.QuoteToken.Decimals
chainFormatPrice := expectedNotional.Mul(decimal.New(1, notionalDecimals))
humanReadableNotional := derivativeMarket.NotionalFromChainFormat(sdkmath.LegacyMustNewDecFromStr(chainFormatPrice.String()))

assert.Assert(t, expectedNotional.Equal(humanReadableNotional))
}

func TestConvertQuantityFromExtendedChainFormatForDerivativeMarket(t *testing.T) {
derivativeMarket := createBTCUSDTPerpMarket()
expectedQuantity := decimal.RequireFromString("123.456")
Expand Down Expand Up @@ -251,3 +312,14 @@ func TestConvertMarginFromExtendedChainFormatForDerivativeMarket(t *testing.T) {

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

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

notionalDecimals := derivativeMarket.QuoteToken.Decimals
chainFormatNotional := expectedNotional.Mul(decimal.New(1, notionalDecimals)).Mul(decimal.New(1, AdditionalChainFormatDecimals))
humanReadableNotional := derivativeMarket.NotionalFromExtendedChainFormat(sdkmath.LegacyMustNewDecFromStr(chainFormatNotional.String()))

assert.Assert(t, expectedNotional.Equal(humanReadableNotional))
}
Loading
Loading