55 "encoding/hex"
66 "encoding/json"
77 "math/big"
8+ "strconv"
89 common "github.com/InjectiveLabs/sdk-go/client/common"
910 chaintypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
1011 txtypes "github.com/cosmos/cosmos-sdk/types/tx"
@@ -55,13 +56,9 @@ type ChainClient interface {
5556 GetBankBalances (ctx context.Context , address string ) (* banktypes.QueryAllBalancesResponse , error )
5657
5758 DefaultSubaccount (acc cosmtypes.AccAddress ) eth.Hash
58- GetSpotQuantity (value decimal.Decimal , minTickSize cosmtypes.Dec , baseDecimals int ) (qty cosmtypes.Dec )
59- GetSpotPrice (price decimal.Decimal , baseDecimals int , quoteDecimals int , minPriceTickSize cosmtypes.Dec ) cosmtypes.Dec
60- GetDerivativeQuantity (value decimal.Decimal , minTickSize cosmtypes.Dec ) (qty cosmtypes.Dec )
61- GetDerivativePrice (value , tickSize cosmtypes.Dec ) cosmtypes.Dec
6259
63- SpotOrder (defaultSubaccountID eth.Hash , d * SpotOrderData ) * exchangetypes.SpotOrder
64- DerivativeOrder (defaultSubaccountID eth.Hash , d * DerivativeOrderData ) * exchangetypes.DerivativeOrder
60+ SpotOrder (defaultSubaccountID eth.Hash , network common. Network , d * SpotOrderData ) * exchangetypes.SpotOrder
61+ DerivativeOrder (defaultSubaccountID eth.Hash , network common. Network , d * DerivativeOrderData ) * exchangetypes.DerivativeOrder
6562 OrderCancel (defaultSubaccountID eth.Hash , d * OrderCancelData ) * exchangetypes.OrderData
6663
6764 Close ()
@@ -543,7 +540,7 @@ func formatPriceToTickSize(value, tickSize cosmtypes.Dec) cosmtypes.Dec {
543540}
544541
545542
546- func ( c * chainClient ) GetSpotQuantity (value decimal.Decimal , minTickSize cosmtypes.Dec , baseDecimals int ) (qty cosmtypes.Dec ) {
543+ func GetSpotQuantity (value decimal.Decimal , minTickSize cosmtypes.Dec , baseDecimals int ) (qty cosmtypes.Dec ) {
547544 mid , _ := cosmtypes .NewDecFromStr (value .String ())
548545 bStr := decimal .New (1 , int32 (baseDecimals )).String ()
549546 baseDec , _ := cosmtypes .NewDecFromStr (bStr )
@@ -553,7 +550,7 @@ func (c *chainClient) GetSpotQuantity(value decimal.Decimal, minTickSize cosmtyp
553550 return qty
554551}
555552
556- func ( c * chainClient ) GetSpotPrice (price decimal.Decimal , baseDecimals int , quoteDecimals int , minPriceTickSize cosmtypes.Dec ) cosmtypes.Dec {
553+ func GetSpotPrice (price decimal.Decimal , baseDecimals int , quoteDecimals int , minPriceTickSize cosmtypes.Dec ) cosmtypes.Dec {
557554 scale := decimal .New (1 , int32 (quoteDecimals - baseDecimals ))
558555 priceStr := scale .Mul (price ).StringFixed (18 )
559556 decPrice , err := cosmtypes .NewDecFromStr (priceStr )
@@ -566,7 +563,7 @@ func (c *chainClient) GetSpotPrice(price decimal.Decimal, baseDecimals int, quot
566563 return realPrice
567564}
568565
569- func ( c * chainClient ) GetDerivativeQuantity (value decimal.Decimal , minTickSize cosmtypes.Dec ) (qty cosmtypes.Dec ) {
566+ func GetDerivativeQuantity (value decimal.Decimal , minTickSize cosmtypes.Dec ) (qty cosmtypes.Dec ) {
570567 mid := cosmtypes .MustNewDecFromStr (value .StringFixed (18 ))
571568 baseDec := cosmtypes .OneDec ()
572569 scale := baseDec .Quo (minTickSize )
@@ -575,7 +572,7 @@ func (c *chainClient) GetDerivativeQuantity(value decimal.Decimal, minTickSize c
575572 return qty
576573}
577574
578- func ( c * chainClient ) GetDerivativePrice (value , tickSize cosmtypes.Dec ) cosmtypes.Dec {
575+ func GetDerivativePrice (value , tickSize cosmtypes.Dec ) cosmtypes.Dec {
579576 residue := new (big.Int ).Mod (value .BigInt (), tickSize .BigInt ())
580577 formattedValue := new (big.Int ).Sub (value .BigInt (), residue )
581578 p := decimal .NewFromBigInt (formattedValue , - 18 ).String ()
@@ -584,29 +581,47 @@ func (c *chainClient) GetDerivativePrice(value, tickSize cosmtypes.Dec) cosmtype
584581}
585582
586583
587- func (c * chainClient ) SpotOrder (defaultSubaccountID eth.Hash , d * SpotOrderData ) * exchangetypes.SpotOrder {
584+ func (c * chainClient ) SpotOrder (defaultSubaccountID eth.Hash , network common.Network , d * SpotOrderData ) * exchangetypes.SpotOrder {
585+
586+ baseDecimals := common .LoadMetadata (network , d .MarketId ).Base
587+ quoteDecimals := common .LoadMetadata (network , d .MarketId ).Quote
588+ minPriceTickSize := common .LoadMetadata (network , d .MarketId ).MinPriceTickSize
589+ minQuantityTickSize := common .LoadMetadata (network , d .MarketId ).MinQuantityTickSize
590+
591+ orderSize := GetSpotQuantity (d .Quantity , cosmtypes .MustNewDecFromStr (strconv .FormatFloat (minQuantityTickSize , 'f' , - 1 , 64 )), baseDecimals )
592+ orderPrice := GetSpotPrice (d .Price , baseDecimals , quoteDecimals , cosmtypes .MustNewDecFromStr (strconv .FormatFloat (minPriceTickSize , 'f' , - 1 , 64 )))
593+
588594 return & exchangetypes.SpotOrder {
589595 MarketId : d .MarketId ,
590596 OrderType : d .OrderType ,
591597 OrderInfo : exchangetypes.OrderInfo {
592598 SubaccountId : defaultSubaccountID .Hex (),
593599 FeeRecipient : d .FeeRecipient ,
594- Price : d . Price ,
595- Quantity : d . Quantity ,
600+ Price : orderPrice ,
601+ Quantity : orderSize ,
596602 },
597603 }
598604}
599605
600- func (c * chainClient ) DerivativeOrder (defaultSubaccountID eth.Hash , d * DerivativeOrderData ) * exchangetypes.DerivativeOrder {
606+ func (c * chainClient ) DerivativeOrder (defaultSubaccountID eth.Hash , network common.Network , d * DerivativeOrderData ) * exchangetypes.DerivativeOrder {
607+
608+ minPriceTickSize := common .LoadMetadata (network , d .MarketId ).MinPriceTickSize
609+ minQuantityTickSize := common .LoadMetadata (network , d .MarketId ).MinQuantityTickSize
610+
611+ margin := cosmtypes .MustNewDecFromStr (fmt .Sprint (d .Quantity )).Mul (d .Price ).Quo (d .Leverage )
612+ orderSize := GetDerivativeQuantity (d .Quantity , cosmtypes .MustNewDecFromStr (strconv .FormatFloat (minQuantityTickSize , 'f' , - 1 , 64 )))
613+ orderPrice := GetDerivativePrice (d .Price , cosmtypes .MustNewDecFromStr (strconv .FormatFloat (minPriceTickSize , 'f' , - 1 , 64 )))
614+ orderMargin := GetDerivativePrice (margin , cosmtypes .MustNewDecFromStr (strconv .FormatFloat (minPriceTickSize , 'f' , - 1 , 64 )))
615+
601616 return & exchangetypes.DerivativeOrder {
602617 MarketId : d .MarketId ,
603618 OrderType : d .OrderType ,
604- Margin : d . Margin ,
619+ Margin : orderMargin ,
605620 OrderInfo : exchangetypes.OrderInfo {
606621 SubaccountId : defaultSubaccountID .Hex (),
607622 FeeRecipient : d .FeeRecipient ,
608- Price : d . Price ,
609- Quantity : d . Quantity ,
623+ Price : orderPrice ,
624+ Quantity : orderSize ,
610625 },
611626 }
612627}
@@ -624,8 +639,7 @@ func (c *chainClient) OrderCancel(defaultSubaccountID eth.Hash, d *OrderCancelDa
624639type DerivativeOrderData struct {
625640 OrderType exchangetypes.OrderType
626641 Price cosmtypes.Dec
627- Margin cosmtypes.Dec
628- Quantity cosmtypes.Dec
642+ Quantity decimal.Decimal
629643 Leverage cosmtypes.Dec
630644 FeeRecipient string
631645 MarketId string
@@ -634,8 +648,8 @@ type DerivativeOrderData struct {
634648
635649type SpotOrderData struct {
636650 OrderType exchangetypes.OrderType
637- Price cosmtypes. Dec
638- Quantity cosmtypes. Dec
651+ Price decimal. Decimal
652+ Quantity decimal. Decimal
639653 FeeRecipient string
640654 MarketId string
641655}
0 commit comments