diff --git a/client/chain/chain.go b/client/chain/chain.go index 8b9b0720..31dd4a65 100644 --- a/client/chain/chain.go +++ b/client/chain/chain.go @@ -14,7 +14,7 @@ import ( "sync/atomic" "time" - "github.com/InjectiveLabs/sdk-go/client/core" + "github.com/cosmos/cosmos-sdk/types/query" "google.golang.org/grpc/credentials/insecure" @@ -89,8 +89,18 @@ type ChainClient interface { AsyncBroadcastSignedTx(txBytes []byte) (*txtypes.BroadcastTxResponse, error) QueueBroadcastMsg(msgs ...sdk.Msg) error + // Bank Module GetBankBalances(ctx context.Context, address string) (*banktypes.QueryAllBalancesResponse, error) GetBankBalance(ctx context.Context, address string, denom string) (*banktypes.QueryBalanceResponse, error) + GetBankSpendableBalances(ctx context.Context, address string, pagination *query.PageRequest) (*banktypes.QuerySpendableBalancesResponse, error) + GetBankSpendableBalancesByDenom(ctx context.Context, address string, denom string) (*banktypes.QuerySpendableBalanceByDenomResponse, error) + GetBankTotalSupply(ctx context.Context, pagination *query.PageRequest) (*banktypes.QueryTotalSupplyResponse, error) + GetBankSupplyOf(ctx context.Context, denom string) (*banktypes.QuerySupplyOfResponse, error) + GetDenomMetadata(ctx context.Context, denom string) (*banktypes.QueryDenomMetadataResponse, error) + GetDenomsMetadata(ctx context.Context, pagination *query.PageRequest) (*banktypes.QueryDenomsMetadataResponse, error) + GetDenomOwners(ctx context.Context, denom string, pagination *query.PageRequest) (*banktypes.QueryDenomOwnersResponse, error) + GetBankSendEnabled(ctx context.Context, denoms []string, pagination *query.PageRequest) (*banktypes.QuerySendEnabledResponse, error) + GetAuthzGrants(ctx context.Context, req authztypes.QueryGrantsRequest) (*authztypes.QueryGrantsResponse, error) GetAccount(ctx context.Context, address string) (*authtypes.QueryAccountResponse, error) @@ -116,7 +126,9 @@ type ChainClient interface { ComputeOrderHashes(spotOrders []exchangetypes.SpotOrder, derivativeOrders []exchangetypes.DerivativeOrder, subaccountId eth.Hash) (OrderHashes, error) SpotOrder(defaultSubaccountID eth.Hash, network common.Network, d *SpotOrderData) *exchangetypes.SpotOrder + CreateSpotOrder(defaultSubaccountID eth.Hash, network common.Network, d *SpotOrderData, marketsAssistant MarketsAssistant) *exchangetypes.SpotOrder DerivativeOrder(defaultSubaccountID eth.Hash, network common.Network, d *DerivativeOrderData) *exchangetypes.DerivativeOrder + CreateDerivativeOrder(defaultSubaccountID eth.Hash, network common.Network, d *DerivativeOrderData, marketAssistant MarketsAssistant) *exchangetypes.DerivativeOrder OrderCancel(defaultSubaccountID eth.Hash, d *OrderCancelData) *exchangetypes.OrderData SmartContractState( @@ -145,14 +157,13 @@ type ChainClient interface { } type chainClient struct { - ctx client.Context - network common.Network - marketsAssistant core.MarketsAssistant - opts *common.ClientOptions - logger log.Logger - conn *grpc.ClientConn - chainStreamConn *grpc.ClientConn - txFactory tx.Factory + ctx client.Context + network common.Network + opts *common.ClientOptions + logger log.Logger + conn *grpc.ClientConn + chainStreamConn *grpc.ClientConn + txFactory tx.Factory doneC chan bool msgC chan sdk.Msg @@ -182,26 +193,12 @@ type chainClient struct { canSign bool } -// Deprecated: Use NewChainClientWithMarketsAssistant instead. func NewChainClient( ctx client.Context, network common.Network, options ...common.ClientOption, ) (ChainClient, error) { - assistant, err := core.NewMarketsAssistant(network.Name) - if err != nil { - return nil, err - } - return NewChainClientWithMarketsAssistant(ctx, network, assistant, options...) -} - -func NewChainClientWithMarketsAssistant( - ctx client.Context, - network common.Network, - marketsAssistant core.MarketsAssistant, - options ...common.ClientOption, -) (ChainClient, error) { // process options opts := common.DefaultClientOptions() @@ -255,10 +252,9 @@ func NewChainClientWithMarketsAssistant( cancelCtx, cancelFn := context.WithCancel(context.Background()) // build client cc := &chainClient{ - ctx: ctx, - network: network, - marketsAssistant: marketsAssistant, - opts: opts, + ctx: ctx, + network: network, + opts: opts, logger: log.WithFields(log.Fields{ "module": "sdk-go", @@ -453,6 +449,8 @@ func (c *chainClient) Close() { } } +//Bank Module + func (c *chainClient) GetBankBalances(ctx context.Context, address string) (*banktypes.QueryAllBalancesResponse, error) { req := &banktypes.QueryAllBalancesRequest{ Address: address, @@ -460,19 +458,73 @@ func (c *chainClient) GetBankBalances(ctx context.Context, address string) (*ban return c.bankQueryClient.AllBalances(ctx, req) } -func (c *chainClient) GetAccount(ctx context.Context, address string) (*authtypes.QueryAccountResponse, error) { - req := &authtypes.QueryAccountRequest{ +func (c *chainClient) GetBankBalance(ctx context.Context, address string, denom string) (*banktypes.QueryBalanceResponse, error) { + req := &banktypes.QueryBalanceRequest{ Address: address, + Denom: denom, } - return c.authQueryClient.Account(ctx, req) + return c.bankQueryClient.Balance(ctx, req) } -func (c *chainClient) GetBankBalance(ctx context.Context, address string, denom string) (*banktypes.QueryBalanceResponse, error) { - req := &banktypes.QueryBalanceRequest{ +func (c *chainClient) GetBankSpendableBalances(ctx context.Context, address string, pagination *query.PageRequest) (*banktypes.QuerySpendableBalancesResponse, error) { + req := &banktypes.QuerySpendableBalancesRequest{ + Address: address, + Pagination: pagination, + } + return c.bankQueryClient.SpendableBalances(ctx, req) +} + +func (c *chainClient) GetBankSpendableBalancesByDenom(ctx context.Context, address string, denom string) (*banktypes.QuerySpendableBalanceByDenomResponse, error) { + req := &banktypes.QuerySpendableBalanceByDenomRequest{ Address: address, Denom: denom, } - return c.bankQueryClient.Balance(ctx, req) + return c.bankQueryClient.SpendableBalanceByDenom(ctx, req) +} + +func (c *chainClient) GetBankTotalSupply(ctx context.Context, pagination *query.PageRequest) (*banktypes.QueryTotalSupplyResponse, error) { + req := &banktypes.QueryTotalSupplyRequest{Pagination: pagination} + return c.bankQueryClient.TotalSupply(ctx, req) +} + +func (c *chainClient) GetBankSupplyOf(ctx context.Context, denom string) (*banktypes.QuerySupplyOfResponse, error) { + req := &banktypes.QuerySupplyOfRequest{Denom: denom} + return c.bankQueryClient.SupplyOf(ctx, req) +} + +func (c *chainClient) GetDenomMetadata(ctx context.Context, denom string) (*banktypes.QueryDenomMetadataResponse, error) { + req := &banktypes.QueryDenomMetadataRequest{Denom: denom} + return c.bankQueryClient.DenomMetadata(ctx, req) +} + +func (c *chainClient) GetDenomsMetadata(ctx context.Context, pagination *query.PageRequest) (*banktypes.QueryDenomsMetadataResponse, error) { + req := &banktypes.QueryDenomsMetadataRequest{Pagination: pagination} + return c.bankQueryClient.DenomsMetadata(ctx, req) +} + +func (c *chainClient) GetDenomOwners(ctx context.Context, denom string, pagination *query.PageRequest) (*banktypes.QueryDenomOwnersResponse, error) { + req := &banktypes.QueryDenomOwnersRequest{ + Denom: denom, + Pagination: pagination, + } + return c.bankQueryClient.DenomOwners(ctx, req) +} + +func (c *chainClient) GetBankSendEnabled(ctx context.Context, denoms []string, pagination *query.PageRequest) (*banktypes.QuerySendEnabledResponse, error) { + req := &banktypes.QuerySendEnabledRequest{ + Denoms: denoms, + Pagination: pagination, + } + return c.bankQueryClient.SendEnabled(ctx, req) +} + +// Auth Module + +func (c *chainClient) GetAccount(ctx context.Context, address string) (*authtypes.QueryAccountResponse, error) { + req := &authtypes.QueryAccountRequest{ + Address: address, + } + return c.authQueryClient.Account(ctx, req) } // SyncBroadcastMsg sends Tx to chain and waits until Tx is included in block. @@ -899,9 +951,19 @@ func (c *chainClient) GetSubAccountNonce(ctx context.Context, subaccountId eth.H return c.exchangeQueryClient.SubaccountTradeNonce(ctx, req) } +// Deprecated: Use CreateSpotOrder instead func (c *chainClient) SpotOrder(defaultSubaccountID eth.Hash, network common.Network, d *SpotOrderData) *exchangetypes.SpotOrder { + assistant, err := NewMarketsAssistant(network.Name) + if err != nil { + panic(err) + } + + return c.CreateSpotOrder(defaultSubaccountID, network, d, assistant) +} - market, isPresent := c.marketsAssistant.AllSpotMarkets()[d.MarketId] +func (c *chainClient) CreateSpotOrder(defaultSubaccountID eth.Hash, network common.Network, d *SpotOrderData, marketsAssistant MarketsAssistant) *exchangetypes.SpotOrder { + + market, isPresent := marketsAssistant.AllSpotMarkets()[d.MarketId] if !isPresent { panic(errors.Errorf("Invalid spot market id for %s network (%s)", c.network.Name, d.MarketId)) } @@ -922,9 +984,19 @@ func (c *chainClient) SpotOrder(defaultSubaccountID eth.Hash, network common.Net } } +// Deprecated: Use CreateDerivativeOrder instead func (c *chainClient) DerivativeOrder(defaultSubaccountID eth.Hash, network common.Network, d *DerivativeOrderData) *exchangetypes.DerivativeOrder { - market, isPresent := c.marketsAssistant.AllDerivativeMarkets()[d.MarketId] + assistant, err := NewMarketsAssistant(network.Name) + if err != nil { + panic(err) + } + + return c.CreateDerivativeOrder(defaultSubaccountID, network, d, assistant) +} + +func (c *chainClient) CreateDerivativeOrder(defaultSubaccountID eth.Hash, network common.Network, d *DerivativeOrderData, marketAssistant MarketsAssistant) *exchangetypes.DerivativeOrder { + market, isPresent := marketAssistant.AllDerivativeMarkets()[d.MarketId] if !isPresent { panic(errors.Errorf("Invalid derivative market id for %s network (%s)", c.network.Name, d.MarketId)) } diff --git a/client/chain/chain_test.go b/client/chain/chain_test.go index f286819b..ed55ed2a 100644 --- a/client/chain/chain_test.go +++ b/client/chain/chain_test.go @@ -1,14 +1,11 @@ package chain import ( - "context" "os" "testing" "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" rpchttp "github.com/cometbft/cometbft/rpc/client/http" "github.com/cosmos/cosmos-sdk/crypto/keyring" cosmtypes "github.com/cosmos/cosmos-sdk/types" @@ -43,21 +40,9 @@ func createClient(senderAddress cosmtypes.AccAddress, cosmosKeyring keyring.Keyr clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := NewChainClientWithMarketsAssistant( + chainClient, err := NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/client/chain/chain_test_support.go b/client/chain/chain_test_support.go new file mode 100644 index 00000000..402541b8 --- /dev/null +++ b/client/chain/chain_test_support.go @@ -0,0 +1,241 @@ +package chain + +import ( + "context" + "errors" + "time" + + wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" + exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types" + chainstreamtypes "github.com/InjectiveLabs/sdk-go/chain/stream/types" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" + "github.com/cosmos/cosmos-sdk/client" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/query" + txtypes "github.com/cosmos/cosmos-sdk/types/tx" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + authztypes "github.com/cosmos/cosmos-sdk/x/authz" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + eth "github.com/ethereum/go-ethereum/common" + "google.golang.org/grpc" +) + +type MockChainClient struct { + DenomsMetadataResponses []*banktypes.QueryDenomsMetadataResponse +} + +func (c *MockChainClient) CanSignTransactions() bool { + return true +} + +func (c *MockChainClient) FromAddress() sdk.AccAddress { + return sdk.AccAddress{} +} + +func (c *MockChainClient) QueryClient() *grpc.ClientConn { + return &grpc.ClientConn{} +} + +func (c *MockChainClient) ClientContext() client.Context { + return client.Context{} +} + +func (c *MockChainClient) GetAccNonce() (accNum uint64, accSeq uint64) { + return 1, 2 +} + +func (c *MockChainClient) SimulateMsg(clientCtx client.Context, msgs ...sdk.Msg) (*txtypes.SimulateResponse, error) { + return &txtypes.SimulateResponse{}, nil +} + +func (c *MockChainClient) AsyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxResponse, error) { + return &txtypes.BroadcastTxResponse{}, nil +} + +func (c *MockChainClient) SyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxResponse, error) { + return &txtypes.BroadcastTxResponse{}, nil +} + +func (c *MockChainClient) BuildSignedTx(clientCtx client.Context, accNum, accSeq, initialGas uint64, msg ...sdk.Msg) ([]byte, error) { + return *new([]byte), nil +} + +func (c *MockChainClient) SyncBroadcastSignedTx(tyBytes []byte) (*txtypes.BroadcastTxResponse, error) { + return &txtypes.BroadcastTxResponse{}, nil +} + +func (c *MockChainClient) AsyncBroadcastSignedTx(txBytes []byte) (*txtypes.BroadcastTxResponse, error) { + return &txtypes.BroadcastTxResponse{}, nil +} + +func (c *MockChainClient) QueueBroadcastMsg(msgs ...sdk.Msg) error { + return nil +} + +func (c *MockChainClient) GetBankBalances(ctx context.Context, address string) (*banktypes.QueryAllBalancesResponse, error) { + return &banktypes.QueryAllBalancesResponse{}, nil +} + +func (c *MockChainClient) GetBankBalance(ctx context.Context, address string, denom string) (*banktypes.QueryBalanceResponse, error) { + return &banktypes.QueryBalanceResponse{}, nil +} + +func (c *MockChainClient) GetBankSpendableBalances(ctx context.Context, address string, pagination *query.PageRequest) (*banktypes.QuerySpendableBalancesResponse, error) { + return &banktypes.QuerySpendableBalancesResponse{}, nil +} + +func (c *MockChainClient) GetBankSpendableBalancesByDenom(ctx context.Context, address string, denom string) (*banktypes.QuerySpendableBalanceByDenomResponse, error) { + return &banktypes.QuerySpendableBalanceByDenomResponse{}, nil +} + +func (c *MockChainClient) GetBankTotalSupply(ctx context.Context, pagination *query.PageRequest) (*banktypes.QueryTotalSupplyResponse, error) { + return &banktypes.QueryTotalSupplyResponse{}, nil +} + +func (c *MockChainClient) GetBankSupplyOf(ctx context.Context, denom string) (*banktypes.QuerySupplyOfResponse, error) { + return &banktypes.QuerySupplyOfResponse{}, nil +} + +func (c *MockChainClient) GetDenomMetadata(ctx context.Context, denom string) (*banktypes.QueryDenomMetadataResponse, error) { + return &banktypes.QueryDenomMetadataResponse{}, nil +} + +func (c *MockChainClient) GetDenomsMetadata(ctx context.Context, pagination *query.PageRequest) (*banktypes.QueryDenomsMetadataResponse, error) { + var response *banktypes.QueryDenomsMetadataResponse + var localError error + if len(c.DenomsMetadataResponses) > 0 { + response = c.DenomsMetadataResponses[0] + c.DenomsMetadataResponses = c.DenomsMetadataResponses[1:] + localError = nil + } else { + response = &banktypes.QueryDenomsMetadataResponse{} + localError = errors.New("there are no responses configured") + } + + return response, localError +} + +func (c *MockChainClient) GetDenomOwners(ctx context.Context, denom string, pagination *query.PageRequest) (*banktypes.QueryDenomOwnersResponse, error) { + return &banktypes.QueryDenomOwnersResponse{}, nil +} + +func (c *MockChainClient) GetBankSendEnabled(ctx context.Context, denoms []string, pagination *query.PageRequest) (*banktypes.QuerySendEnabledResponse, error) { + return &banktypes.QuerySendEnabledResponse{}, nil +} + +func (c *MockChainClient) GetAuthzGrants(ctx context.Context, req authztypes.QueryGrantsRequest) (*authztypes.QueryGrantsResponse, error) { + return &authztypes.QueryGrantsResponse{}, nil +} + +func (c *MockChainClient) GetAccount(ctx context.Context, address string) (*authtypes.QueryAccountResponse, error) { + return &authtypes.QueryAccountResponse{}, nil +} + +func (c *MockChainClient) BuildGenericAuthz(granter string, grantee string, msgtype string, expireIn time.Time) *authztypes.MsgGrant { + return &authztypes.MsgGrant{} +} + +func (c *MockChainClient) BuildExchangeAuthz(granter string, grantee string, authzType ExchangeAuthz, subaccountId string, markets []string, expireIn time.Time) *authztypes.MsgGrant { + return &authztypes.MsgGrant{} +} + +func (c *MockChainClient) BuildExchangeBatchUpdateOrdersAuthz( + granter string, + grantee string, + subaccountId string, + spotMarkets []string, + derivativeMarkets []string, + expireIn time.Time, +) *authztypes.MsgGrant { + return &authztypes.MsgGrant{} +} + +func (c *MockChainClient) DefaultSubaccount(acc sdk.AccAddress) eth.Hash { + return eth.HexToHash("") +} + +func (c *MockChainClient) Subaccount(account sdk.AccAddress, index int) eth.Hash { + return eth.HexToHash("") +} + +func (c *MockChainClient) GetSubAccountNonce(ctx context.Context, subaccountId eth.Hash) (*exchangetypes.QuerySubaccountTradeNonceResponse, error) { + return &exchangetypes.QuerySubaccountTradeNonceResponse{}, nil +} + +func (c *MockChainClient) GetFeeDiscountInfo(ctx context.Context, account string) (*exchangetypes.QueryFeeDiscountAccountInfoResponse, error) { + return &exchangetypes.QueryFeeDiscountAccountInfoResponse{}, nil +} + +func (c *MockChainClient) UpdateSubaccountNonceFromChain() error { + return nil +} + +func (c *MockChainClient) SynchronizeSubaccountNonce(subaccountId eth.Hash) error { + return nil +} + +func (c *MockChainClient) ComputeOrderHashes(spotOrders []exchangetypes.SpotOrder, derivativeOrders []exchangetypes.DerivativeOrder, subaccountId eth.Hash) (OrderHashes, error) { + return OrderHashes{}, nil +} + +func (c *MockChainClient) SpotOrder(defaultSubaccountID eth.Hash, network common.Network, d *SpotOrderData) *exchangetypes.SpotOrder { + return c.CreateSpotOrder(defaultSubaccountID, network, d, MarketsAssistant{}) +} + +func (c *MockChainClient) CreateSpotOrder(defaultSubaccountID eth.Hash, network common.Network, d *SpotOrderData, marketsAssistant MarketsAssistant) *exchangetypes.SpotOrder { + return &exchangetypes.SpotOrder{} +} + +func (c *MockChainClient) DerivativeOrder(defaultSubaccountID eth.Hash, network common.Network, d *DerivativeOrderData) *exchangetypes.DerivativeOrder { + return c.CreateDerivativeOrder(defaultSubaccountID, network, d, MarketsAssistant{}) +} + +func (c *MockChainClient) CreateDerivativeOrder(defaultSubaccountID eth.Hash, network common.Network, d *DerivativeOrderData, marketAssistant MarketsAssistant) *exchangetypes.DerivativeOrder { + return &exchangetypes.DerivativeOrder{} +} + +func (c *MockChainClient) OrderCancel(defaultSubaccountID eth.Hash, d *OrderCancelData) *exchangetypes.OrderData { + return &exchangetypes.OrderData{} +} + +func (c *MockChainClient) SmartContractState( + ctx context.Context, + contractAddress string, + queryData []byte, +) (*wasmtypes.QuerySmartContractStateResponse, error) { + return &wasmtypes.QuerySmartContractStateResponse{}, nil +} + +func (c *MockChainClient) RawContractState( + ctx context.Context, + contractAddress string, + queryData []byte, +) (*wasmtypes.QueryRawContractStateResponse, error) { + return &wasmtypes.QueryRawContractStateResponse{}, nil +} + +func (c *MockChainClient) GetGasFee() (string, error) { + return "", nil +} + +func (c *MockChainClient) StreamEventOrderFail(sender string, failEventCh chan map[string]uint) {} + +func (c *MockChainClient) StreamEventOrderFailWithWebsocket(sender string, websocket *rpchttp.HTTP, failEventCh chan map[string]uint) { +} + +func (c *MockChainClient) StreamOrderbookUpdateEvents(orderbookType OrderbookType, marketIds []string, orderbookCh chan exchangetypes.Orderbook) { +} + +func (c *MockChainClient) StreamOrderbookUpdateEventsWithWebsocket(orderbookType OrderbookType, marketIds []string, websocket *rpchttp.HTTP, orderbookCh chan exchangetypes.Orderbook) { +} + +func (c *MockChainClient) ChainStream(ctx context.Context, req chainstreamtypes.StreamRequest) (chainstreamtypes.Stream_StreamClient, error) { + return nil, nil +} + +func (c *MockChainClient) GetTx(ctx context.Context, txHash string) (*txtypes.GetTxResponse, error) { + return &txtypes.GetTxResponse{}, nil +} + +func (c *MockChainClient) Close() {} diff --git a/client/core/markets_assistant.go b/client/chain/markets_assistant.go similarity index 52% rename from client/core/markets_assistant.go rename to client/chain/markets_assistant.go index 5f6de9b6..10b2b188 100644 --- a/client/core/markets_assistant.go +++ b/client/chain/markets_assistant.go @@ -1,4 +1,4 @@ -package core +package chain import ( "context" @@ -6,130 +6,139 @@ import ( "path" "runtime" "strings" + "sync" + "github.com/InjectiveLabs/sdk-go/client/core" "github.com/InjectiveLabs/sdk-go/client/exchange" derivativeExchangePB "github.com/InjectiveLabs/sdk-go/exchange/derivative_exchange_rpc/pb" spotExchangePB "github.com/InjectiveLabs/sdk-go/exchange/spot_exchange_rpc/pb" + "github.com/cosmos/cosmos-sdk/types/query" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/shopspring/decimal" - "golang.org/x/exp/maps" "gopkg.in/ini.v1" ) +var legacyMarketAssistantLazyInitialization sync.Once +var legacyMarketAssistant MarketsAssistant + type MarketsAssistant struct { - tokensBySymbol map[string]Token - tokensByDenom map[string]Token - spotMarkets map[string]SpotMarket - derivativeMarkets map[string]DerivativeMarket + tokensBySymbol map[string]core.Token + tokensByDenom map[string]core.Token + spotMarkets map[string]core.SpotMarket + derivativeMarkets map[string]core.DerivativeMarket } func newMarketsAssistant() MarketsAssistant { return MarketsAssistant{ - tokensBySymbol: make(map[string]Token), - tokensByDenom: make(map[string]Token), - spotMarkets: make(map[string]SpotMarket), - derivativeMarkets: make(map[string]DerivativeMarket), + tokensBySymbol: make(map[string]core.Token), + tokensByDenom: make(map[string]core.Token), + spotMarkets: make(map[string]core.SpotMarket), + derivativeMarkets: make(map[string]core.DerivativeMarket), } } -// Deprecated: use NewMarketsAssistantUsingExchangeClient instead +// Deprecated: use NewMarketsAssistantInitializedFromChain instead func NewMarketsAssistant(networkName string) (MarketsAssistant, error) { - assistant := newMarketsAssistant() - fileName := getFileAbsPath(fmt.Sprintf("../metadata/assets/%s.ini", networkName)) - metadataFile, err := ini.Load(fileName) - - if err != nil { - return assistant, err - } - - for _, section := range metadataFile.Sections() { - sectionName := section.Name() - if strings.HasPrefix(sectionName, "0x") { - description := section.Key("description").Value() - decimals, _ := section.Key("quote").Int() - quoteToken := Token{ - Name: "", - Symbol: "", - Denom: "", - Address: "", - Decimals: int32(decimals), - Logo: "", - Updated: -1, - } - - minPriceTickSize := decimal.RequireFromString(section.Key("min_price_tick_size").String()) - minQuantityTickSize := decimal.RequireFromString(section.Key("min_quantity_tick_size").String()) - - if strings.Contains(description, "Spot") { - baseDecimals, _ := section.Key("quote").Int() - baseToken := Token{ - Name: "", - Symbol: "", - Denom: "", - Address: "", - Decimals: int32(baseDecimals), - Logo: "", - Updated: -1, + legacyMarketAssistantLazyInitialization.Do(func() { + assistant := newMarketsAssistant() + fileName := getFileAbsPath(fmt.Sprintf("../metadata/assets/%s.ini", networkName)) + metadataFile, err := ini.Load(fileName) + + if err == nil { + for _, section := range metadataFile.Sections() { + sectionName := section.Name() + if strings.HasPrefix(sectionName, "0x") { + description := section.Key("description").Value() + + decimals, _ := section.Key("quote").Int() + quoteToken := core.Token{ + Name: "", + Symbol: "", + Denom: "", + Address: "", + Decimals: int32(decimals), + Logo: "", + Updated: -1, + } + + minPriceTickSize := decimal.RequireFromString(section.Key("min_price_tick_size").String()) + minQuantityTickSize := decimal.RequireFromString(section.Key("min_quantity_tick_size").String()) + + if strings.Contains(description, "Spot") { + baseDecimals, _ := section.Key("quote").Int() + baseToken := core.Token{ + Name: "", + Symbol: "", + Denom: "", + Address: "", + Decimals: int32(baseDecimals), + Logo: "", + Updated: -1, + } + + market := core.SpotMarket{ + Id: sectionName, + Status: "", + Ticker: description, + BaseToken: baseToken, + QuoteToken: quoteToken, + MakerFeeRate: decimal.NewFromInt32(0), + TakerFeeRate: decimal.NewFromInt32(0), + ServiceProviderFee: decimal.NewFromInt32(0), + MinPriceTickSize: minPriceTickSize, + MinQuantityTickSize: minQuantityTickSize, + } + + assistant.spotMarkets[market.Id] = market + } else { + market := core.DerivativeMarket{ + Id: sectionName, + Status: "", + Ticker: description, + OracleBase: "", + OracleQuote: "", + OracleType: "", + OracleScaleFactor: 1, + InitialMarginRatio: decimal.NewFromInt32(0), + MaintenanceMarginRatio: decimal.NewFromInt32(0), + QuoteToken: quoteToken, + MakerFeeRate: decimal.NewFromInt32(0), + TakerFeeRate: decimal.NewFromInt32(0), + ServiceProviderFee: decimal.NewFromInt32(0), + MinPriceTickSize: minPriceTickSize, + MinQuantityTickSize: minQuantityTickSize, + } + + assistant.derivativeMarkets[market.Id] = market + } + } else { + if sectionName != "DEFAULT" { + tokenDecimals, _ := section.Key("decimals").Int() + newToken := core.Token{ + Name: sectionName, + Symbol: sectionName, + Denom: section.Key("peggy_denom").String(), + Address: "", + Decimals: int32(tokenDecimals), + Logo: "", + Updated: -1, + } + + assistant.tokensByDenom[newToken.Denom] = newToken + assistant.tokensBySymbol[newToken.Symbol] = newToken + } } - - market := SpotMarket{ - Id: sectionName, - Status: "", - Ticker: description, - BaseToken: baseToken, - QuoteToken: quoteToken, - MakerFeeRate: decimal.NewFromInt32(0), - TakerFeeRate: decimal.NewFromInt32(0), - ServiceProviderFee: decimal.NewFromInt32(0), - MinPriceTickSize: minPriceTickSize, - MinQuantityTickSize: minQuantityTickSize, - } - - assistant.spotMarkets[market.Id] = market - } else { - market := DerivativeMarket{ - Id: sectionName, - Status: "", - Ticker: description, - OracleBase: "", - OracleQuote: "", - OracleType: "", - OracleScaleFactor: 1, - InitialMarginRatio: decimal.NewFromInt32(0), - MaintenanceMarginRatio: decimal.NewFromInt32(0), - QuoteToken: quoteToken, - MakerFeeRate: decimal.NewFromInt32(0), - TakerFeeRate: decimal.NewFromInt32(0), - ServiceProviderFee: decimal.NewFromInt32(0), - MinPriceTickSize: minPriceTickSize, - MinQuantityTickSize: minQuantityTickSize, - } - - assistant.derivativeMarkets[market.Id] = market - } - } else { - if sectionName != "DEFAULT" { - tokenDecimals, _ := section.Key("decimals").Int() - newToken := Token{ - Name: sectionName, - Symbol: sectionName, - Denom: section.Key("peggy_denom").String(), - Address: "", - Decimals: int32(tokenDecimals), - Logo: "", - Updated: -1, - } - - assistant.tokensByDenom[newToken.Denom] = newToken - assistant.tokensBySymbol[newToken.Symbol] = newToken } } - } - return assistant, nil + legacyMarketAssistant = assistant + }) + + return legacyMarketAssistant, nil } -func NewMarketsAssistantUsingExchangeClient(ctx context.Context, exchangeClient exchange.ExchangeClient) (MarketsAssistant, error) { +func NewMarketsAssistantInitializedFromChain(ctx context.Context, exchangeClient exchange.ExchangeClient) (MarketsAssistant, error) { assistant := newMarketsAssistant() spotMarketsRequest := spotExchangePB.MarketsRequest{ MarketStatus: "active", @@ -160,7 +169,7 @@ func NewMarketsAssistantUsingExchangeClient(ctx context.Context, exchangeClient minPriceTickSize := decimal.RequireFromString(marketInfo.GetMinPriceTickSize()) minQuantityTickSize := decimal.RequireFromString(marketInfo.GetMinQuantityTickSize()) - market := SpotMarket{ + market := core.SpotMarket{ Id: marketInfo.GetMarketId(), Status: marketInfo.GetMarketStatus(), Ticker: marketInfo.GetTicker(), @@ -200,7 +209,7 @@ func NewMarketsAssistantUsingExchangeClient(ctx context.Context, exchangeClient minPriceTickSize := decimal.RequireFromString(marketInfo.GetMinPriceTickSize()) minQuantityTickSize := decimal.RequireFromString(marketInfo.GetMinQuantityTickSize()) - market := DerivativeMarket{ + market := core.DerivativeMarket{ Id: marketInfo.GetMarketId(), Status: marketInfo.GetMarketStatus(), Ticker: marketInfo.GetTicker(), @@ -225,6 +234,17 @@ func NewMarketsAssistantUsingExchangeClient(ctx context.Context, exchangeClient return assistant, nil } +func NewMarketsAssistantWithAllTokens(ctx context.Context, exchangeClient exchange.ExchangeClient, chainClient ChainClient) (MarketsAssistant, error) { + assistant, err := NewMarketsAssistantInitializedFromChain(ctx, exchangeClient) + if err != nil { + return assistant, err + } + + assistant.initializeTokensFromChainDenoms(ctx, chainClient) + + return assistant, nil +} + func uniqueSymbol(symbol string, denom string, tokenMetaSymbol string, tokenMetaName string, assistant MarketsAssistant) string { uniqueSymbol := denom _, isSymbolPresent := assistant.tokensBySymbol[symbol] @@ -245,13 +265,13 @@ func uniqueSymbol(symbol string, denom string, tokenMetaSymbol string, tokenMeta return uniqueSymbol } -func spotTokenRepresentation(symbol string, tokenMeta *spotExchangePB.TokenMeta, denom string, assistant *MarketsAssistant) Token { +func spotTokenRepresentation(symbol string, tokenMeta *spotExchangePB.TokenMeta, denom string, assistant *MarketsAssistant) core.Token { _, isPresent := assistant.tokensByDenom[denom] if !isPresent { uniqueSymbol := uniqueSymbol(symbol, denom, tokenMeta.GetSymbol(), tokenMeta.GetName(), *assistant) - newToken := Token{ + newToken := core.Token{ Name: tokenMeta.GetName(), Symbol: symbol, Denom: denom, @@ -268,13 +288,13 @@ func spotTokenRepresentation(symbol string, tokenMeta *spotExchangePB.TokenMeta, return assistant.tokensByDenom[denom] } -func derivativeTokenRepresentation(symbol string, tokenMeta *derivativeExchangePB.TokenMeta, denom string, assistant *MarketsAssistant) Token { +func derivativeTokenRepresentation(symbol string, tokenMeta *derivativeExchangePB.TokenMeta, denom string, assistant *MarketsAssistant) core.Token { _, isPresent := assistant.tokensByDenom[denom] if !isPresent { uniqueSymbol := uniqueSymbol(symbol, denom, tokenMeta.GetSymbol(), tokenMeta.GetName(), *assistant) - newToken := Token{ + newToken := core.Token{ Name: tokenMeta.GetName(), Symbol: symbol, Denom: denom, @@ -296,14 +316,67 @@ func getFileAbsPath(relativePath string) string { return path.Join(path.Dir(filename), relativePath) } -func (assistant MarketsAssistant) AllTokens() map[string]Token { - return maps.Clone(assistant.tokensBySymbol) +func (assistant MarketsAssistant) AllTokens() map[string]core.Token { + return assistant.tokensBySymbol +} + +func (assistant MarketsAssistant) AllSpotMarkets() map[string]core.SpotMarket { + return assistant.spotMarkets } -func (assistant MarketsAssistant) AllSpotMarkets() map[string]SpotMarket { - return maps.Clone(assistant.spotMarkets) +func (assistant MarketsAssistant) AllDerivativeMarkets() map[string]core.DerivativeMarket { + return assistant.derivativeMarkets } -func (assistant MarketsAssistant) AllDerivativeMarkets() map[string]DerivativeMarket { - return maps.Clone(assistant.derivativeMarkets) +func (assistant MarketsAssistant) initializeTokensFromChainDenoms(ctx context.Context, chainClient ChainClient) { + var denomsMetadata []banktypes.Metadata + var nextKey []byte + + for readNextPage := true; readNextPage; readNextPage = len(nextKey) > 0 { + pagination := query.PageRequest{Key: nextKey} + result, err := chainClient.GetDenomsMetadata(ctx, &pagination) + + if err != nil { + panic(err) + } + + denomsMetadata = append(denomsMetadata, result.GetMetadatas()...) + } + + for _, denomMetadata := range denomsMetadata { + symbol := denomMetadata.GetSymbol() + denom := denomMetadata.GetBase() + + _, isDenomPresent := assistant.tokensByDenom[denom] + + if symbol != "" && denom != "" && !isDenomPresent { + name := denomMetadata.GetName() + if name == "" { + name = symbol + } + + var decimals int32 = -1 + for _, denomUnit := range denomMetadata.GetDenomUnits() { + exponent := int32(denomUnit.GetExponent()) + if exponent > decimals { + decimals = exponent + } + } + + uniqueSymbol := uniqueSymbol(symbol, denom, symbol, name, assistant) + + newToken := core.Token{ + Name: name, + Symbol: symbol, + Denom: denom, + Address: "", + Decimals: decimals, + Logo: denomMetadata.GetURI(), + Updated: -1, + } + + assistant.tokensByDenom[denom] = newToken + assistant.tokensBySymbol[uniqueSymbol] = newToken + } + } } diff --git a/client/core/markets_assistant_test.go b/client/chain/markets_assistant_test.go similarity index 67% rename from client/core/markets_assistant_test.go rename to client/chain/markets_assistant_test.go index b727c559..12ffb0bd 100644 --- a/client/core/markets_assistant_test.go +++ b/client/chain/markets_assistant_test.go @@ -1,17 +1,19 @@ -package core +package chain import ( "context" "strings" "testing" + "github.com/InjectiveLabs/sdk-go/client/exchange" derivativeExchangePB "github.com/InjectiveLabs/sdk-go/exchange/derivative_exchange_rpc/pb" spotExchangePB "github.com/InjectiveLabs/sdk-go/exchange/spot_exchange_rpc/pb" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/stretchr/testify/assert" ) func TestMarketAssistantCreationUsingMarketsFromExchange(t *testing.T) { - mockExchange := MockExchangeClient{} + mockExchange := exchange.MockExchangeClient{} var spotMarketInfos []*spotExchangePB.SpotMarketInfo var derivativeMarketInfos []*derivativeExchangePB.DerivativeMarketInfo injUsdtSpotMarketInfo := createINJUSDTSpotMarketInfo() @@ -30,7 +32,7 @@ func TestMarketAssistantCreationUsingMarketsFromExchange(t *testing.T) { }) ctx := context.Background() - assistant, err := NewMarketsAssistantUsingExchangeClient(ctx, &mockExchange) + assistant, err := NewMarketsAssistantInitializedFromChain(ctx, &mockExchange) assert.NoError(t, err) @@ -70,3 +72,28 @@ func TestMarketAssistantCreationUsingMarketsFromExchange(t *testing.T) { _, isPresent = derivativeMarkets[btcUsdtDerivativeMarketInfo.MarketId] assert.True(t, isPresent) } + +func TestMarketAssistantCreationWithAllTokens(t *testing.T) { + mockExchange := exchange.MockExchangeClient{} + mockChain := MockChainClient{} + smartDenomMetadata := createSmartDenomMetadata() + + mockExchange.SpotMarketsResponses = append(mockExchange.SpotMarketsResponses, &spotExchangePB.MarketsResponse{}) + mockExchange.DerivativeMarketsResponses = append(mockExchange.DerivativeMarketsResponses, &derivativeExchangePB.MarketsResponse{}) + + mockChain.DenomsMetadataResponses = append(mockChain.DenomsMetadataResponses, &banktypes.QueryDenomsMetadataResponse{ + Metadatas: []banktypes.Metadata{smartDenomMetadata}, + }) + + ctx := context.Background() + assistant, err := NewMarketsAssistantWithAllTokens(ctx, &mockExchange, &mockChain) + + assert.NoError(t, err) + + tokens := assistant.AllTokens() + + assert.Len(t, tokens, 1) + + _, isPresent := tokens[smartDenomMetadata.Symbol] + assert.True(t, isPresent) +} diff --git a/client/chain/markets_assistant_test_support.go b/client/chain/markets_assistant_test_support.go new file mode 100644 index 00000000..d926f585 --- /dev/null +++ b/client/chain/markets_assistant_test_support.go @@ -0,0 +1,159 @@ +package chain + +import ( + derivativeExchangePB "github.com/InjectiveLabs/sdk-go/exchange/derivative_exchange_rpc/pb" + spotExchangePB "github.com/InjectiveLabs/sdk-go/exchange/spot_exchange_rpc/pb" + "github.com/cosmos/cosmos-sdk/x/bank/types" +) + +func createINJTokenMeta() spotExchangePB.TokenMeta { + return spotExchangePB.TokenMeta{ + Name: "Injective Protocol", + Address: "0xe28b3B32B6c345A34Ff64674606124Dd5Aceca30", + Symbol: "INJ", + Logo: "https://static.alchemyapi.io/images/assets/7226.png", + Decimals: 18, + UpdatedAt: 1681739137644, + } +} + +func createAPETokenMeta() spotExchangePB.TokenMeta { + return spotExchangePB.TokenMeta{ + Name: "APE", + Address: "0x0000000000000000000000000000000000000000", + Symbol: "APE", + Logo: "https://assets.coingecko.com/coins/images/24383/small/apecoin.jpg?1647476455", + Decimals: 18, + UpdatedAt: 1681739137646, + } +} + +func createUSDTTokenMeta() spotExchangePB.TokenMeta { + return spotExchangePB.TokenMeta{ + Name: "USDT", + Address: "0x0000000000000000000000000000000000000000", + Symbol: "USDT", + Logo: "https://static.alchemyapi.io/images/assets/825.png", + Decimals: 6, + UpdatedAt: 1681739137645, + } +} + +func createUSDTPerpTokenMeta() derivativeExchangePB.TokenMeta { + return derivativeExchangePB.TokenMeta{ + Name: "Tether", + Address: "0xdAC17F958D2ee523a2206206994597C13D831ec7", + Symbol: "USDTPerp", + Logo: "https://static.alchemyapi.io/images/assets/825.png", + Decimals: 6, + UpdatedAt: 1683929869866, + } +} + +func createINJUSDTSpotMarketInfo() *spotExchangePB.SpotMarketInfo { + injTokenMeta := createINJTokenMeta() + usdtTokenMeta := createUSDTTokenMeta() + marketInfo := spotExchangePB.SpotMarketInfo{ + MarketId: "0x7a57e705bb4e09c88aecfc295569481dbf2fe1d5efe364651fbe72385938e9b0", + MarketStatus: "active", + Ticker: "INJ/USDT", + BaseDenom: "inj", + BaseTokenMeta: &injTokenMeta, + QuoteDenom: "peggy0x87aB3B4C8661e07D6372361211B96ed4Dc36B1B5", + QuoteTokenMeta: &usdtTokenMeta, + MakerFeeRate: "-0.0001", + TakerFeeRate: "0.001", + ServiceProviderFee: "0.4", + MinPriceTickSize: "0.000000000000001", + MinQuantityTickSize: "1000000000000000", + } + + return &marketInfo +} + +func createAPEUSDTSpotMarketInfo() *spotExchangePB.SpotMarketInfo { + apeTokenMeta := createAPETokenMeta() + usdtTokenMeta := createUSDTTokenMeta() + marketInfo := spotExchangePB.SpotMarketInfo{ + MarketId: "0x8b67e705bb4e09c88aecfc295569481dbf2fe1d5efe364651fbe72385938e000", + MarketStatus: "active", + Ticker: "APE/USDT", + BaseDenom: "peggy0x44C21afAaF20c270EBbF5914Cfc3b5022173FEB7", + BaseTokenMeta: &apeTokenMeta, + QuoteDenom: "factory/peggy0x87aB3B4C8661e07D6372361211B96ed4Dc300000", + QuoteTokenMeta: &usdtTokenMeta, + MakerFeeRate: "-0.0001", + TakerFeeRate: "0.001", + ServiceProviderFee: "0.4", + MinPriceTickSize: "0.000000000000001", + MinQuantityTickSize: "1000000000000000", + } + + return &marketInfo +} + +func createBTCUSDTDerivativeMarketInfo() *derivativeExchangePB.DerivativeMarketInfo { + usdtPerpTokenMeta := createUSDTPerpTokenMeta() + + perpetualMarketInfo := derivativeExchangePB.PerpetualMarketInfo{ + HourlyFundingRateCap: "0.0000625", + HourlyInterestRate: "0.00000416666", + NextFundingTimestamp: 1684764000, + FundingInterval: 3600, + } + + perpetualmarketFunding := derivativeExchangePB.PerpetualMarketFunding{ + CumulativeFunding: "6880500093.266083891331674194", + CumulativePrice: "-0.952642601240470199", + LastTimestamp: 1684763442, + } + + marketInfo := derivativeExchangePB.DerivativeMarketInfo{ + MarketId: "0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce", + MarketStatus: "active", + Ticker: "BTC/USDT PERP", + OracleBase: "BTC", + OracleQuote: "USDT", + OracleType: "bandibc", + OracleScaleFactor: 6, + InitialMarginRatio: "0.095", + MaintenanceMarginRatio: "0.025", + QuoteDenom: "peggy0xdAC17F958D2ee523a2206206994597C13D831ec7", + QuoteTokenMeta: &usdtPerpTokenMeta, + MakerFeeRate: "-0.0001", + TakerFeeRate: "0.001", + ServiceProviderFee: "0.4", + IsPerpetual: true, + MinPriceTickSize: "1000000", + MinQuantityTickSize: "0.0001", + PerpetualMarketInfo: &perpetualMarketInfo, + PerpetualMarketFunding: &perpetualmarketFunding, + } + + return &marketInfo +} + +func createSmartDenomMetadata() types.Metadata { + firstDenomUnit := types.DenomUnit{ + Denom: "factory/inj105ujajd95znwjvcy3hwcz80pgy8tc6v77spur0/SMART", + Exponent: 0, + Aliases: []string{"microSMART"}, + } + secondDenomUnit := types.DenomUnit{ + Denom: "SMART", + Exponent: 6, + Aliases: []string{"SMART"}, + } + metadata := types.Metadata{ + Description: "SMART", + DenomUnits: []*types.DenomUnit{&firstDenomUnit, &secondDenomUnit}, + Base: "factory/inj105ujajd95znwjvcy3hwcz80pgy8tc6v77spur0/SMART", + Display: "SMART", + Name: "SMART", + Symbol: "SMART", + URI: "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Flag_of_the_People%27s_Republic_of_China.svg/2560px-Flag_of_the_People%27s_Republic_of_China.svg.png", + URIHash: "", + } + + return metadata +} diff --git a/client/core/markets_assistant_test_support.go b/client/exchange/exchange_test_support.go similarity index 77% rename from client/core/markets_assistant_test_support.go rename to client/exchange/exchange_test_support.go index 9febc8df..2b37623a 100644 --- a/client/core/markets_assistant_test_support.go +++ b/client/exchange/exchange_test_support.go @@ -1,7 +1,8 @@ -package core +package exchange import ( "context" + "errors" accountPB "github.com/InjectiveLabs/sdk-go/exchange/accounts_rpc/pb" auctionPB "github.com/InjectiveLabs/sdk-go/exchange/auction_rpc/pb" @@ -11,137 +12,9 @@ import ( oraclePB "github.com/InjectiveLabs/sdk-go/exchange/oracle_rpc/pb" portfolioExchangePB "github.com/InjectiveLabs/sdk-go/exchange/portfolio_rpc/pb" spotExchangePB "github.com/InjectiveLabs/sdk-go/exchange/spot_exchange_rpc/pb" - "github.com/pkg/errors" "google.golang.org/grpc" ) -func createINJTokenMeta() spotExchangePB.TokenMeta { - return spotExchangePB.TokenMeta{ - Name: "Injective Protocol", - Address: "0xe28b3B32B6c345A34Ff64674606124Dd5Aceca30", - Symbol: "INJ", - Logo: "https://static.alchemyapi.io/images/assets/7226.png", - Decimals: 18, - UpdatedAt: 1681739137644, - } -} - -func createAPETokenMeta() spotExchangePB.TokenMeta { - return spotExchangePB.TokenMeta{ - Name: "APE", - Address: "0x0000000000000000000000000000000000000000", - Symbol: "APE", - Logo: "https://assets.coingecko.com/coins/images/24383/small/apecoin.jpg?1647476455", - Decimals: 18, - UpdatedAt: 1681739137646, - } -} - -func createUSDTTokenMeta() spotExchangePB.TokenMeta { - return spotExchangePB.TokenMeta{ - Name: "USDT", - Address: "0x0000000000000000000000000000000000000000", - Symbol: "USDT", - Logo: "https://static.alchemyapi.io/images/assets/825.png", - Decimals: 6, - UpdatedAt: 1681739137645, - } -} - -func createUSDTPerpTokenMeta() derivativeExchangePB.TokenMeta { - return derivativeExchangePB.TokenMeta{ - Name: "Tether", - Address: "0xdAC17F958D2ee523a2206206994597C13D831ec7", - Symbol: "USDTPerp", - Logo: "https://static.alchemyapi.io/images/assets/825.png", - Decimals: 6, - UpdatedAt: 1683929869866, - } -} - -func createINJUSDTSpotMarketInfo() *spotExchangePB.SpotMarketInfo { - injTokenMeta := createINJTokenMeta() - usdtTokenMeta := createUSDTTokenMeta() - marketInfo := spotExchangePB.SpotMarketInfo{ - MarketId: "0x7a57e705bb4e09c88aecfc295569481dbf2fe1d5efe364651fbe72385938e9b0", - MarketStatus: "active", - Ticker: "INJ/USDT", - BaseDenom: "inj", - BaseTokenMeta: &injTokenMeta, - QuoteDenom: "peggy0x87aB3B4C8661e07D6372361211B96ed4Dc36B1B5", - QuoteTokenMeta: &usdtTokenMeta, - MakerFeeRate: "-0.0001", - TakerFeeRate: "0.001", - ServiceProviderFee: "0.4", - MinPriceTickSize: "0.000000000000001", - MinQuantityTickSize: "1000000000000000", - } - - return &marketInfo -} - -func createAPEUSDTSpotMarketInfo() *spotExchangePB.SpotMarketInfo { - apeTokenMeta := createAPETokenMeta() - usdtTokenMeta := createUSDTTokenMeta() - marketInfo := spotExchangePB.SpotMarketInfo{ - MarketId: "0x8b67e705bb4e09c88aecfc295569481dbf2fe1d5efe364651fbe72385938e000", - MarketStatus: "active", - Ticker: "APE/USDT", - BaseDenom: "peggy0x44C21afAaF20c270EBbF5914Cfc3b5022173FEB7", - BaseTokenMeta: &apeTokenMeta, - QuoteDenom: "factory/peggy0x87aB3B4C8661e07D6372361211B96ed4Dc300000", - QuoteTokenMeta: &usdtTokenMeta, - MakerFeeRate: "-0.0001", - TakerFeeRate: "0.001", - ServiceProviderFee: "0.4", - MinPriceTickSize: "0.000000000000001", - MinQuantityTickSize: "1000000000000000", - } - - return &marketInfo -} - -func createBTCUSDTDerivativeMarketInfo() *derivativeExchangePB.DerivativeMarketInfo { - usdtPerpTokenMeta := createUSDTPerpTokenMeta() - - perpetualMarketInfo := derivativeExchangePB.PerpetualMarketInfo{ - HourlyFundingRateCap: "0.0000625", - HourlyInterestRate: "0.00000416666", - NextFundingTimestamp: 1684764000, - FundingInterval: 3600, - } - - perpetualmarketFunding := derivativeExchangePB.PerpetualMarketFunding{ - CumulativeFunding: "6880500093.266083891331674194", - CumulativePrice: "-0.952642601240470199", - LastTimestamp: 1684763442, - } - - marketInfo := derivativeExchangePB.DerivativeMarketInfo{ - MarketId: "0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce", - MarketStatus: "active", - Ticker: "BTC/USDT PERP", - OracleBase: "BTC", - OracleQuote: "USDT", - OracleType: "bandibc", - OracleScaleFactor: 6, - InitialMarginRatio: "0.095", - MaintenanceMarginRatio: "0.025", - QuoteDenom: "peggy0xdAC17F958D2ee523a2206206994597C13D831ec7", - QuoteTokenMeta: &usdtPerpTokenMeta, - MakerFeeRate: "-0.0001", - TakerFeeRate: "0.001", - ServiceProviderFee: "0.4", - IsPerpetual: true, - MinPriceTickSize: "1000000", - MinQuantityTickSize: "0.0001", - PerpetualMarketInfo: &perpetualMarketInfo, - PerpetualMarketFunding: &perpetualmarketFunding, - } - - return &marketInfo -} - type MockExchangeClient struct { SpotMarketsResponses []*spotExchangePB.MarketsResponse DerivativeMarketsResponses []*derivativeExchangePB.MarketsResponse diff --git a/examples/chain/0_LocalOrderHash/example.go b/examples/chain/0_LocalOrderHash/example.go index 2eb3e8f1..4e36982f 100644 --- a/examples/chain/0_LocalOrderHash/example.go +++ b/examples/chain/0_LocalOrderHash/example.go @@ -6,7 +6,6 @@ import ( "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/google/uuid" @@ -57,15 +56,14 @@ func main() { } ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) + marketsAssistant, err := chainclient.NewMarketsAssistantInitializedFromChain(ctx, exchangeClient) if err != nil { panic(err) } - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -76,24 +74,34 @@ func main() { // prepare tx msg defaultSubaccountID := chainClient.Subaccount(senderAddress, 1) - spotOrder := chainClient.SpotOrder(defaultSubaccountID, network, &chainclient.SpotOrderData{ - OrderType: exchangetypes.OrderType_BUY, - Quantity: decimal.NewFromFloat(2), - Price: decimal.NewFromFloat(22.55), - FeeRecipient: senderAddress.String(), - MarketId: "0x0611780ba69656949525013d947713300f56c37b6175e02f26bffa495c3208fe", - Cid: uuid.NewString(), - }) - - derivativeOrder := chainClient.DerivativeOrder(defaultSubaccountID, network, &chainclient.DerivativeOrderData{ - OrderType: exchangetypes.OrderType_BUY, - Quantity: decimal.NewFromFloat(2), - Price: decimal.RequireFromString("31"), - Leverage: decimal.RequireFromString("2.5"), - FeeRecipient: senderAddress.String(), - MarketId: "0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6", - Cid: uuid.NewString(), - }) + spotOrder := chainClient.CreateSpotOrder( + defaultSubaccountID, + network, + &chainclient.SpotOrderData{ + OrderType: exchangetypes.OrderType_BUY, + Quantity: decimal.NewFromFloat(2), + Price: decimal.NewFromFloat(22.55), + FeeRecipient: senderAddress.String(), + MarketId: "0x0611780ba69656949525013d947713300f56c37b6175e02f26bffa495c3208fe", + Cid: uuid.NewString(), + }, + marketsAssistant, + ) + + derivativeOrder := chainClient.CreateDerivativeOrder( + defaultSubaccountID, + network, + &chainclient.DerivativeOrderData{ + OrderType: exchangetypes.OrderType_BUY, + Quantity: decimal.NewFromFloat(2), + Price: decimal.RequireFromString("31"), + Leverage: decimal.RequireFromString("2.5"), + FeeRecipient: senderAddress.String(), + MarketId: "0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6", + Cid: uuid.NewString(), + }, + marketsAssistant, + ) msg := new(exchangetypes.MsgBatchCreateSpotLimitOrders) msg.Sender = senderAddress.String() diff --git a/examples/chain/10_MsgBatchCancelDerivativeOrders/example.go b/examples/chain/10_MsgBatchCancelDerivativeOrders/example.go index 2b78c7df..d59a188e 100644 --- a/examples/chain/10_MsgBatchCancelDerivativeOrders/example.go +++ b/examples/chain/10_MsgBatchCancelDerivativeOrders/example.go @@ -1,14 +1,11 @@ package main import ( - "context" "fmt" "os" "time" "github.com/InjectiveLabs/sdk-go/client" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/InjectiveLabs/sdk-go/client/common" @@ -51,21 +48,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/11_MsgBatchCreateSpotLimitOrders/example.go b/examples/chain/11_MsgBatchCreateSpotLimitOrders/example.go index fd679dd3..8cb5e33a 100644 --- a/examples/chain/11_MsgBatchCreateSpotLimitOrders/example.go +++ b/examples/chain/11_MsgBatchCreateSpotLimitOrders/example.go @@ -6,7 +6,6 @@ import ( "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/google/uuid" @@ -59,15 +58,14 @@ func main() { } ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) + marketsAssistant, err := chainclient.NewMarketsAssistantInitializedFromChain(ctx, exchangeClient) if err != nil { panic(err) } - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -82,14 +80,18 @@ func main() { amount := decimal.NewFromFloat(2) price := decimal.NewFromFloat(22.5) - order := chainClient.SpotOrder(defaultSubaccountID, network, &chainclient.SpotOrderData{ - OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO - Quantity: amount, - Price: price, - FeeRecipient: senderAddress.String(), - MarketId: marketId, - Cid: uuid.NewString(), - }) + order := chainClient.CreateSpotOrder( + defaultSubaccountID, + network, + &chainclient.SpotOrderData{ + OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO + Quantity: amount, + Price: price, + FeeRecipient: senderAddress.String(), + MarketId: marketId, + Cid: uuid.NewString(), + }, + marketsAssistant) msg := new(exchangetypes.MsgBatchCreateSpotLimitOrders) msg.Sender = senderAddress.String() msg.Orders = []exchangetypes.SpotOrder{*order} diff --git a/examples/chain/12_MsgBatchCreateDerivativeLimitOrders/example.go b/examples/chain/12_MsgBatchCreateDerivativeLimitOrders/example.go index 4ddd83d3..cd6ce01e 100644 --- a/examples/chain/12_MsgBatchCreateDerivativeLimitOrders/example.go +++ b/examples/chain/12_MsgBatchCreateDerivativeLimitOrders/example.go @@ -6,7 +6,6 @@ import ( "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/google/uuid" @@ -59,15 +58,14 @@ func main() { } ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) + marketsAssistant, err := chainclient.NewMarketsAssistantInitializedFromChain(ctx, exchangeClient) if err != nil { panic(err) } - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -83,16 +81,21 @@ func main() { price := decimal.NewFromFloat(5) leverage := decimal.NewFromFloat(1) - order := chainClient.DerivativeOrder(defaultSubaccountID, network, &chainclient.DerivativeOrderData{ - OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO - Quantity: amount, - Price: price, - Leverage: leverage, - FeeRecipient: senderAddress.String(), - MarketId: marketId, - IsReduceOnly: false, - Cid: uuid.NewString(), - }) + order := chainClient.CreateDerivativeOrder( + defaultSubaccountID, + network, + &chainclient.DerivativeOrderData{ + OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO + Quantity: amount, + Price: price, + Leverage: leverage, + FeeRecipient: senderAddress.String(), + MarketId: marketId, + IsReduceOnly: false, + Cid: uuid.NewString(), + }, + marketsAssistant, + ) msg := new(exchangetypes.MsgBatchCreateDerivativeLimitOrders) msg.Sender = senderAddress.String() diff --git a/examples/chain/13_MsgIncreasePositionMargin/example.go b/examples/chain/13_MsgIncreasePositionMargin/example.go index ce5742cd..578d9243 100644 --- a/examples/chain/13_MsgIncreasePositionMargin/example.go +++ b/examples/chain/13_MsgIncreasePositionMargin/example.go @@ -1,14 +1,11 @@ package main import ( - "context" "fmt" "os" "time" "github.com/InjectiveLabs/sdk-go/client" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/InjectiveLabs/sdk-go/client/common" @@ -51,21 +48,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/15_MsgWithdraw/example.go b/examples/chain/15_MsgWithdraw/example.go index 2f05671c..8c49c6e5 100644 --- a/examples/chain/15_MsgWithdraw/example.go +++ b/examples/chain/15_MsgWithdraw/example.go @@ -1,14 +1,10 @@ package main import ( - "context" "fmt" "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" @@ -51,21 +47,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/16_MsgSubaccountTransfer/example.go b/examples/chain/16_MsgSubaccountTransfer/example.go index 06bac9cf..d7580995 100644 --- a/examples/chain/16_MsgSubaccountTransfer/example.go +++ b/examples/chain/16_MsgSubaccountTransfer/example.go @@ -1,14 +1,10 @@ package main import ( - "context" "fmt" "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" @@ -51,21 +47,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/17_MsgBatchUpdateOrders/example.go b/examples/chain/17_MsgBatchUpdateOrders/example.go index 79c9db97..4f10490c 100644 --- a/examples/chain/17_MsgBatchUpdateOrders/example.go +++ b/examples/chain/17_MsgBatchUpdateOrders/example.go @@ -6,7 +6,6 @@ import ( "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/google/uuid" @@ -59,15 +58,14 @@ func main() { } ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) + marketsAssistant, err := chainclient.NewMarketsAssistantInitializedFromChain(ctx, exchangeClient) if err != nil { panic(err) } - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -83,14 +81,19 @@ func main() { sprice := decimal.NewFromFloat(22.5) smarketIds := []string{"0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"} - spot_order := chainClient.SpotOrder(defaultSubaccountID, network, &chainclient.SpotOrderData{ - OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO - Quantity: samount, - Price: sprice, - FeeRecipient: senderAddress.String(), - MarketId: smarketId, - Cid: uuid.NewString(), - }) + spot_order := chainClient.CreateSpotOrder( + defaultSubaccountID, + network, + &chainclient.SpotOrderData{ + OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO + Quantity: samount, + Price: sprice, + FeeRecipient: senderAddress.String(), + MarketId: smarketId, + Cid: uuid.NewString(), + }, + marketsAssistant, + ) dmarketId := "0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce" damount := decimal.NewFromFloat(0.01) @@ -98,16 +101,21 @@ func main() { dleverage := decimal.RequireFromString("2") dmarketIds := []string{"0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce"} - derivative_order := chainClient.DerivativeOrder(defaultSubaccountID, network, &chainclient.DerivativeOrderData{ - OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO - Quantity: damount, - Price: dprice, - Leverage: dleverage, - FeeRecipient: senderAddress.String(), - MarketId: dmarketId, - IsReduceOnly: false, - Cid: uuid.NewString(), - }) + derivative_order := chainClient.CreateDerivativeOrder( + defaultSubaccountID, + network, + &chainclient.DerivativeOrderData{ + OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO + Quantity: damount, + Price: dprice, + Leverage: dleverage, + FeeRecipient: senderAddress.String(), + MarketId: dmarketId, + IsReduceOnly: false, + Cid: uuid.NewString(), + }, + marketsAssistant, + ) msg := new(exchangetypes.MsgBatchUpdateOrders) msg.Sender = senderAddress.String() diff --git a/examples/chain/18_MsgBid/example.go b/examples/chain/18_MsgBid/example.go index 74ac4de2..0012157c 100644 --- a/examples/chain/18_MsgBid/example.go +++ b/examples/chain/18_MsgBid/example.go @@ -1,14 +1,10 @@ package main import ( - "context" "fmt" "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" @@ -51,21 +47,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/19_MsgGrant/example.go b/examples/chain/19_MsgGrant/example.go index 4fa1280b..02f5116e 100644 --- a/examples/chain/19_MsgGrant/example.go +++ b/examples/chain/19_MsgGrant/example.go @@ -1,14 +1,11 @@ package main import ( - "context" "fmt" "os" "time" "github.com/InjectiveLabs/sdk-go/client" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/InjectiveLabs/sdk-go/client/common" @@ -49,21 +46,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/1_MsgSend/example.go b/examples/chain/1_MsgSend/example.go index c90c3c3b..d49591a3 100644 --- a/examples/chain/1_MsgSend/example.go +++ b/examples/chain/1_MsgSend/example.go @@ -1,14 +1,11 @@ package main import ( - "context" "fmt" "os" "time" "github.com/InjectiveLabs/sdk-go/client" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/InjectiveLabs/sdk-go/client/common" @@ -50,21 +47,9 @@ func main() { } clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/20_MsgExec/example.go b/examples/chain/20_MsgExec/example.go index aef4f510..ccb0e8f5 100644 --- a/examples/chain/20_MsgExec/example.go +++ b/examples/chain/20_MsgExec/example.go @@ -6,7 +6,6 @@ import ( "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/InjectiveLabs/sdk-go/client" @@ -74,17 +73,16 @@ func main() { } ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) + marketsAssistant, err := chainclient.NewMarketsAssistantInitializedFromChain(ctx, exchangeClient) if err != nil { panic(err) } txFactory := chainclient.NewTxFactory(clientCtx) txFactory = txFactory.WithGasPrices(client.DefaultGasPriceWithDenom) - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionTxFactory(&txFactory), ) @@ -102,13 +100,18 @@ func main() { amount := decimal.NewFromFloat(2) price := decimal.NewFromFloat(22.55) - order := chainClient.SpotOrder(defaultSubaccountID, network, &chainclient.SpotOrderData{ - OrderType: exchangetypes.OrderType_BUY, - Quantity: amount, - Price: price, - FeeRecipient: senderAddress.String(), - MarketId: marketId, - }) + order := chainClient.CreateSpotOrder( + defaultSubaccountID, + network, + &chainclient.SpotOrderData{ + OrderType: exchangetypes.OrderType_BUY, + Quantity: amount, + Price: price, + FeeRecipient: senderAddress.String(), + MarketId: marketId, + }, + marketsAssistant, + ) // manually pack msg into Any type msg0 := exchangetypes.MsgCreateSpotLimitOrder{ diff --git a/examples/chain/21_MsgRevoke/example.go b/examples/chain/21_MsgRevoke/example.go index 80ecc157..46bb2f3a 100644 --- a/examples/chain/21_MsgRevoke/example.go +++ b/examples/chain/21_MsgRevoke/example.go @@ -1,14 +1,10 @@ package main import ( - "context" "fmt" "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" @@ -50,21 +46,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/22_MsgSendToEth/example.go b/examples/chain/22_MsgSendToEth/example.go index 4c79655f..89623c6b 100644 --- a/examples/chain/22_MsgSendToEth/example.go +++ b/examples/chain/22_MsgSendToEth/example.go @@ -1,14 +1,10 @@ package main import ( - "context" "fmt" "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" @@ -51,21 +47,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/23_MsgRelayPriceFeedPrice/example.go b/examples/chain/23_MsgRelayPriceFeedPrice/example.go index 860e57d6..9718d252 100644 --- a/examples/chain/23_MsgRelayPriceFeedPrice/example.go +++ b/examples/chain/23_MsgRelayPriceFeedPrice/example.go @@ -1,14 +1,10 @@ package main import ( - "context" "fmt" "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" @@ -51,21 +47,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/24_MsgRegisterAsDMM/example.go b/examples/chain/24_MsgRegisterAsDMM/example.go index 7c9ec9ea..ca877e9e 100644 --- a/examples/chain/24_MsgRegisterAsDMM/example.go +++ b/examples/chain/24_MsgRegisterAsDMM/example.go @@ -1,14 +1,10 @@ package main import ( - "context" "fmt" "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" @@ -50,21 +46,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/25_MsgDelegate/example.go b/examples/chain/25_MsgDelegate/example.go index 84c18a20..756a26dd 100644 --- a/examples/chain/25_MsgDelegate/example.go +++ b/examples/chain/25_MsgDelegate/example.go @@ -1,14 +1,10 @@ package main import ( - "context" "fmt" "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" @@ -51,21 +47,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/26_MsgWithdrawDelegatorReward/example.go b/examples/chain/26_MsgWithdrawDelegatorReward/example.go index b93178db..46eed8cb 100644 --- a/examples/chain/26_MsgWithdrawDelegatorReward/example.go +++ b/examples/chain/26_MsgWithdrawDelegatorReward/example.go @@ -1,14 +1,10 @@ package main import ( - "context" "fmt" "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" @@ -50,21 +46,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/27_QueryAuthzGrants/example.go b/examples/chain/27_QueryAuthzGrants/example.go index 25ade850..a50706a2 100644 --- a/examples/chain/27_QueryAuthzGrants/example.go +++ b/examples/chain/27_QueryAuthzGrants/example.go @@ -5,9 +5,6 @@ import ( "encoding/json" "fmt" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" chainclient "github.com/InjectiveLabs/sdk-go/client/chain" @@ -51,21 +48,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -83,6 +68,8 @@ func main() { MsgTypeUrl: msg_type_url, } + ctx := context.Background() + res, err := chainClient.GetAuthzGrants(ctx, req) if err != nil { fmt.Println(err) diff --git a/examples/chain/28_BankBalances/example.go b/examples/chain/28_BankBalances/example.go index fbcabcf0..a183fcc7 100644 --- a/examples/chain/28_BankBalances/example.go +++ b/examples/chain/28_BankBalances/example.go @@ -6,9 +6,6 @@ import ( "fmt" "github.com/InjectiveLabs/sdk-go/client" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - chainclient "github.com/InjectiveLabs/sdk-go/client/chain" "github.com/InjectiveLabs/sdk-go/client/common" rpchttp "github.com/cometbft/cometbft/rpc/client/http" @@ -49,21 +46,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -72,6 +57,7 @@ func main() { } address := "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku" + ctx := context.Background() res, err := chainClient.GetBankBalances(ctx, address) if err != nil { diff --git a/examples/chain/29_BankBalance/example.go b/examples/chain/29_BankBalance/example.go index 1946c666..98c48cca 100644 --- a/examples/chain/29_BankBalance/example.go +++ b/examples/chain/29_BankBalance/example.go @@ -5,9 +5,6 @@ import ( "encoding/json" "fmt" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" chainclient "github.com/InjectiveLabs/sdk-go/client/chain" @@ -50,21 +47,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -74,6 +59,7 @@ func main() { address := "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku" denom := "inj" + ctx := context.Background() res, err := chainClient.GetBankBalance(ctx, address, denom) if err != nil { diff --git a/examples/chain/2_MsgDeposit/example.go b/examples/chain/2_MsgDeposit/example.go index 26bc69f2..abf339a4 100644 --- a/examples/chain/2_MsgDeposit/example.go +++ b/examples/chain/2_MsgDeposit/example.go @@ -1,14 +1,11 @@ package main import ( - "context" "fmt" "os" "time" "github.com/InjectiveLabs/sdk-go/client" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/InjectiveLabs/sdk-go/client/common" @@ -50,21 +47,9 @@ func main() { } clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/30_MsgExternalTransfer/example.go b/examples/chain/30_MsgExternalTransfer/example.go index 2ff30426..4a25192f 100644 --- a/examples/chain/30_MsgExternalTransfer/example.go +++ b/examples/chain/30_MsgExternalTransfer/example.go @@ -1,14 +1,10 @@ package main import ( - "context" "fmt" "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" @@ -51,21 +47,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/31_MsgMultiSend/example.go b/examples/chain/31_MsgMultiSend/example.go index e40018cd..5bf49241 100644 --- a/examples/chain/31_MsgMultiSend/example.go +++ b/examples/chain/31_MsgMultiSend/example.go @@ -1,14 +1,10 @@ package main import ( - "context" "fmt" "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" @@ -53,21 +49,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/34_OfflineSigning/example.go b/examples/chain/34_OfflineSigning/example.go index 6962e625..8b6d50a4 100644 --- a/examples/chain/34_OfflineSigning/example.go +++ b/examples/chain/34_OfflineSigning/example.go @@ -7,7 +7,6 @@ import ( "io/ioutil" "os" - "github.com/InjectiveLabs/sdk-go/client/core" exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/InjectiveLabs/sdk-go/client" @@ -71,15 +70,14 @@ func main() { } ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) + marketsAssistant, err := chainclient.NewMarketsAssistantInitializedFromChain(ctx, exchangeClient) if err != nil { panic(err) } - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -92,13 +90,18 @@ func main() { amount := decimal.NewFromFloat(2) price := decimal.NewFromFloat(1.02) - order := chainClient.SpotOrder(defaultSubaccountID, network, &chainclient.SpotOrderData{ - OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO - Quantity: amount, - Price: price, - FeeRecipient: senderAddress.String(), - MarketId: marketId, - }) + order := chainClient.CreateSpotOrder( + defaultSubaccountID, + network, + &chainclient.SpotOrderData{ + OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO + Quantity: amount, + Price: price, + FeeRecipient: senderAddress.String(), + MarketId: marketId, + }, + marketsAssistant, + ) msg := new(exchangetypes.MsgCreateSpotLimitOrder) msg.Sender = senderAddress.String() diff --git a/examples/chain/35_StreamEventOrderFail/example.go b/examples/chain/35_StreamEventOrderFail/example.go index 4697f761..91be988a 100644 --- a/examples/chain/35_StreamEventOrderFail/example.go +++ b/examples/chain/35_StreamEventOrderFail/example.go @@ -1,12 +1,8 @@ package main import ( - "context" "fmt" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" chainclient "github.com/InjectiveLabs/sdk-go/client/chain" @@ -25,21 +21,9 @@ func main() { panic(err) } - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/36_StreamEventOrderbookUpdate/example.go b/examples/chain/36_StreamEventOrderbookUpdate/example.go index c8394edc..21a669fe 100644 --- a/examples/chain/36_StreamEventOrderbookUpdate/example.go +++ b/examples/chain/36_StreamEventOrderbookUpdate/example.go @@ -1,12 +1,8 @@ package main import ( - "context" "fmt" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types" @@ -26,21 +22,9 @@ func main() { panic(err) } - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/38_MsgLiquidate/example.go b/examples/chain/38_MsgLiquidate/example.go index 2ca94cf6..c43327b7 100644 --- a/examples/chain/38_MsgLiquidate/example.go +++ b/examples/chain/38_MsgLiquidate/example.go @@ -1,14 +1,10 @@ package main import ( - "context" "fmt" "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" @@ -55,21 +51,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmRPC) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/39_GetTx/example.go b/examples/chain/39_GetTx/example.go index aaadc907..c41869e8 100644 --- a/examples/chain/39_GetTx/example.go +++ b/examples/chain/39_GetTx/example.go @@ -6,9 +6,6 @@ import ( "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" - "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" @@ -52,21 +49,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmRPC) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -74,6 +59,8 @@ func main() { panic(err) } + ctx := context.Background() + timeOutCtx, cancelFn := context.WithTimeout(ctx, 30*time.Second) defer cancelFn() diff --git a/examples/chain/3_MsgCreateSpotLimitOrder/example.go b/examples/chain/3_MsgCreateSpotLimitOrder/example.go index fcdc56ab..0f228f36 100644 --- a/examples/chain/3_MsgCreateSpotLimitOrder/example.go +++ b/examples/chain/3_MsgCreateSpotLimitOrder/example.go @@ -6,7 +6,6 @@ import ( "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/google/uuid" @@ -57,15 +56,14 @@ func main() { } ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) + marketsAssistant, err := chainclient.NewMarketsAssistantInitializedFromChain(ctx, exchangeClient) if err != nil { panic(err) } - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -80,14 +78,19 @@ func main() { amount := decimal.NewFromFloat(2) price := decimal.NewFromFloat(22.55) - order := chainClient.SpotOrder(defaultSubaccountID, network, &chainclient.SpotOrderData{ - OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO - Quantity: amount, - Price: price, - FeeRecipient: senderAddress.String(), - MarketId: marketId, - Cid: uuid.NewString(), - }) + order := chainClient.CreateSpotOrder( + defaultSubaccountID, + network, + &chainclient.SpotOrderData{ + OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO + Quantity: amount, + Price: price, + FeeRecipient: senderAddress.String(), + MarketId: marketId, + Cid: uuid.NewString(), + }, + marketsAssistant, + ) msg := new(exchangetypes.MsgCreateSpotLimitOrder) msg.Sender = senderAddress.String() diff --git a/examples/chain/40_ChainStream/example.go b/examples/chain/40_ChainStream/example.go index 3d933761..7279f0e2 100644 --- a/examples/chain/40_ChainStream/example.go +++ b/examples/chain/40_ChainStream/example.go @@ -9,8 +9,6 @@ import ( "github.com/InjectiveLabs/sdk-go/client" chainclient "github.com/InjectiveLabs/sdk-go/client/chain" "github.com/InjectiveLabs/sdk-go/client/common" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" ) func main() { @@ -26,21 +24,9 @@ func main() { } clientCtx = clientCtx.WithNodeURI(network.TmEndpoint) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -90,6 +76,9 @@ func main() { Symbol: []string{"INJ", "USDT"}, }, } + + ctx := context.Background() + stream, err := chainClient.ChainStream(ctx, req) if err != nil { panic(err) diff --git a/examples/chain/41_BroadcastMsgWithoutSimulation/example.go b/examples/chain/41_BroadcastMsgWithoutSimulation/example.go index 7f353b9a..044e2e4f 100644 --- a/examples/chain/41_BroadcastMsgWithoutSimulation/example.go +++ b/examples/chain/41_BroadcastMsgWithoutSimulation/example.go @@ -7,7 +7,6 @@ import ( "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/common" - "github.com/InjectiveLabs/sdk-go/client/core" exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/google/uuid" "github.com/shopspring/decimal" @@ -55,7 +54,7 @@ func main() { } ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) + marketsAssistant, err := chainclient.NewMarketsAssistantInitializedFromChain(ctx, exchangeClient) if err != nil { panic(err) } @@ -64,10 +63,9 @@ func main() { txFactory = txFactory.WithGasPrices(client.DefaultGasPriceWithDenom) txFactory = txFactory.WithGas(uint64(txFactory.GasAdjustment() * 140000)) - clientInstance, err := chainclient.NewChainClientWithMarketsAssistant( + clientInstance, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionTxFactory(&txFactory), ) @@ -82,14 +80,19 @@ func main() { amount := decimal.NewFromFloat(1) price := decimal.NewFromFloat(4.55) - order := clientInstance.SpotOrder(defaultSubaccountID, network, &chainclient.SpotOrderData{ - OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO - Quantity: amount, - Price: price, - FeeRecipient: senderAddress.String(), - MarketId: marketId, - Cid: uuid.NewString(), - }) + order := clientInstance.CreateSpotOrder( + defaultSubaccountID, + network, + &chainclient.SpotOrderData{ + OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO + Quantity: amount, + Price: price, + FeeRecipient: senderAddress.String(), + MarketId: marketId, + Cid: uuid.NewString(), + }, + marketsAssistant, + ) msg := new(exchangetypes.MsgCreateSpotLimitOrder) msg.Sender = senderAddress.String() diff --git a/examples/chain/42_BankSpendableBalances/example.go b/examples/chain/42_BankSpendableBalances/example.go new file mode 100644 index 00000000..46dfb55e --- /dev/null +++ b/examples/chain/42_BankSpendableBalances/example.go @@ -0,0 +1,73 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client" + "github.com/cosmos/cosmos-sdk/types/query" + + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" + + "os" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket") + if err != nil { + panic(err) + } + + senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring( + os.Getenv("HOME")+"/.injectived", + "injectived", + "file", + "inj-user", + "12345678", + "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided + false, + ) + + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + senderAddress.String(), + cosmosKeyring, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + address := "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku" + pagination := query.PageRequest{Limit: 10} + ctx := context.Background() + + res, err := chainClient.GetBankSpendableBalances(ctx, address, &pagination) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) + +} diff --git a/examples/chain/43_BankSpendableBalancesByDenom/example.go b/examples/chain/43_BankSpendableBalancesByDenom/example.go new file mode 100644 index 00000000..e087f283 --- /dev/null +++ b/examples/chain/43_BankSpendableBalancesByDenom/example.go @@ -0,0 +1,71 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client" + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" + + "os" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket") + if err != nil { + panic(err) + } + + senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring( + os.Getenv("HOME")+"/.injectived", + "injectived", + "file", + "inj-user", + "12345678", + "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided + false, + ) + + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + senderAddress.String(), + cosmosKeyring, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + address := "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku" + denom := "peggy0x87aB3B4C8661e07D6372361211B96ed4Dc36B1B5" + ctx := context.Background() + + res, err := chainClient.GetBankSpendableBalancesByDenom(ctx, address, denom) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) + +} diff --git a/examples/chain/44_BankTotalSupply/example.go b/examples/chain/44_BankTotalSupply/example.go new file mode 100644 index 00000000..092bf82f --- /dev/null +++ b/examples/chain/44_BankTotalSupply/example.go @@ -0,0 +1,72 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client" + "github.com/cosmos/cosmos-sdk/types/query" + + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" + + "os" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket") + if err != nil { + panic(err) + } + + senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring( + os.Getenv("HOME")+"/.injectived", + "injectived", + "file", + "inj-user", + "12345678", + "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided + false, + ) + + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + senderAddress.String(), + cosmosKeyring, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + pagination := query.PageRequest{Limit: 10} + ctx := context.Background() + + res, err := chainClient.GetBankTotalSupply(ctx, &pagination) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) + +} diff --git a/examples/chain/45_BankSupplyOf/example.go b/examples/chain/45_BankSupplyOf/example.go new file mode 100644 index 00000000..869c791d --- /dev/null +++ b/examples/chain/45_BankSupplyOf/example.go @@ -0,0 +1,70 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client" + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" + + "os" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket") + if err != nil { + panic(err) + } + + senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring( + os.Getenv("HOME")+"/.injectived", + "injectived", + "file", + "inj-user", + "12345678", + "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided + false, + ) + + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + senderAddress.String(), + cosmosKeyring, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + denom := "peggy0x87aB3B4C8661e07D6372361211B96ed4Dc36B1B5" + ctx := context.Background() + + res, err := chainClient.GetBankSupplyOf(ctx, denom) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) + +} diff --git a/examples/chain/46_DenomMetadata/example.go b/examples/chain/46_DenomMetadata/example.go new file mode 100644 index 00000000..15a37935 --- /dev/null +++ b/examples/chain/46_DenomMetadata/example.go @@ -0,0 +1,70 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client" + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" + + "os" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket") + if err != nil { + panic(err) + } + + senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring( + os.Getenv("HOME")+"/.injectived", + "injectived", + "file", + "inj-user", + "12345678", + "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided + false, + ) + + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + senderAddress.String(), + cosmosKeyring, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + denom := "factory/inj107aqkjc3t5r3l9j4n9lgrma5tm3jav8qgppz6m/position" + ctx := context.Background() + + res, err := chainClient.GetDenomMetadata(ctx, denom) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) + +} diff --git a/examples/chain/47_DenomsMetadata/example.go b/examples/chain/47_DenomsMetadata/example.go new file mode 100644 index 00000000..f456f333 --- /dev/null +++ b/examples/chain/47_DenomsMetadata/example.go @@ -0,0 +1,72 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client" + "github.com/cosmos/cosmos-sdk/types/query" + + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" + + "os" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket") + if err != nil { + panic(err) + } + + senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring( + os.Getenv("HOME")+"/.injectived", + "injectived", + "file", + "inj-user", + "12345678", + "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided + false, + ) + + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + senderAddress.String(), + cosmosKeyring, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + pagination := query.PageRequest{Limit: 10} + ctx := context.Background() + + res, err := chainClient.GetDenomsMetadata(ctx, &pagination) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) + +} diff --git a/examples/chain/48_DenomOwners/example.go b/examples/chain/48_DenomOwners/example.go new file mode 100644 index 00000000..e40b1d82 --- /dev/null +++ b/examples/chain/48_DenomOwners/example.go @@ -0,0 +1,72 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client" + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" + "github.com/cosmos/cosmos-sdk/types/query" + + "os" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket") + if err != nil { + panic(err) + } + + senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring( + os.Getenv("HOME")+"/.injectived", + "injectived", + "file", + "inj-user", + "12345678", + "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided + false, + ) + + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + senderAddress.String(), + cosmosKeyring, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + denom := "factory/inj107aqkjc3t5r3l9j4n9lgrma5tm3jav8qgppz6m/position" + pagination := query.PageRequest{Limit: 10} + ctx := context.Background() + + res, err := chainClient.GetDenomOwners(ctx, denom, &pagination) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) + +} diff --git a/examples/chain/49_DenomOwners/example.go b/examples/chain/49_DenomOwners/example.go new file mode 100644 index 00000000..e40b1d82 --- /dev/null +++ b/examples/chain/49_DenomOwners/example.go @@ -0,0 +1,72 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client" + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" + "github.com/cosmos/cosmos-sdk/types/query" + + "os" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket") + if err != nil { + panic(err) + } + + senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring( + os.Getenv("HOME")+"/.injectived", + "injectived", + "file", + "inj-user", + "12345678", + "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided + false, + ) + + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + senderAddress.String(), + cosmosKeyring, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + denom := "factory/inj107aqkjc3t5r3l9j4n9lgrma5tm3jav8qgppz6m/position" + pagination := query.PageRequest{Limit: 10} + ctx := context.Background() + + res, err := chainClient.GetDenomOwners(ctx, denom, &pagination) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) + +} diff --git a/examples/chain/4_MsgCreateSpotMarketOrder/example.go b/examples/chain/4_MsgCreateSpotMarketOrder/example.go index 8f24974c..8c9f30e6 100644 --- a/examples/chain/4_MsgCreateSpotMarketOrder/example.go +++ b/examples/chain/4_MsgCreateSpotMarketOrder/example.go @@ -6,7 +6,6 @@ import ( "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/google/uuid" @@ -58,15 +57,14 @@ func main() { } ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) + marketsAssistant, err := chainclient.NewMarketsAssistantInitializedFromChain(ctx, exchangeClient) if err != nil { panic(err) } - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -80,14 +78,19 @@ func main() { amount := decimal.NewFromFloat(0.1) price := decimal.NewFromFloat(22) - order := chainClient.SpotOrder(defaultSubaccountID, network, &chainclient.SpotOrderData{ - OrderType: exchangetypes.OrderType_SELL, //BUY SELL - Quantity: amount, - Price: price, - FeeRecipient: senderAddress.String(), - MarketId: marketId, - Cid: uuid.NewString(), - }) + order := chainClient.CreateSpotOrder( + defaultSubaccountID, + network, + &chainclient.SpotOrderData{ + OrderType: exchangetypes.OrderType_SELL, //BUY SELL + Quantity: amount, + Price: price, + FeeRecipient: senderAddress.String(), + MarketId: marketId, + Cid: uuid.NewString(), + }, + marketsAssistant, + ) msg := new(exchangetypes.MsgCreateSpotMarketOrder) msg.Sender = senderAddress.String() diff --git a/examples/chain/50_BankSendEnabled/example.go b/examples/chain/50_BankSendEnabled/example.go new file mode 100644 index 00000000..69ae56fd --- /dev/null +++ b/examples/chain/50_BankSendEnabled/example.go @@ -0,0 +1,72 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client" + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" + "github.com/cosmos/cosmos-sdk/types/query" + + "os" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket") + if err != nil { + panic(err) + } + + senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring( + os.Getenv("HOME")+"/.injectived", + "injectived", + "file", + "inj-user", + "12345678", + "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided + false, + ) + + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + senderAddress.String(), + cosmosKeyring, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + denoms := []string{"factory/inj107aqkjc3t5r3l9j4n9lgrma5tm3jav8qgppz6m/position"} + pagination := query.PageRequest{Limit: 10} + ctx := context.Background() + + res, err := chainClient.GetBankSendEnabled(ctx, denoms, &pagination) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) + +} diff --git a/examples/chain/5_MsgCancelSpotOrder/example.go b/examples/chain/5_MsgCancelSpotOrder/example.go index cb073f02..f42ec5cc 100644 --- a/examples/chain/5_MsgCancelSpotOrder/example.go +++ b/examples/chain/5_MsgCancelSpotOrder/example.go @@ -1,14 +1,11 @@ package main import ( - "context" "fmt" "os" "time" "github.com/InjectiveLabs/sdk-go/client" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/InjectiveLabs/sdk-go/client/common" @@ -50,21 +47,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/6_MsgCreateDerivativeLimitOrder/example.go b/examples/chain/6_MsgCreateDerivativeLimitOrder/example.go index 8ab859cd..57f615e9 100644 --- a/examples/chain/6_MsgCreateDerivativeLimitOrder/example.go +++ b/examples/chain/6_MsgCreateDerivativeLimitOrder/example.go @@ -6,7 +6,6 @@ import ( "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/google/uuid" @@ -58,15 +57,14 @@ func main() { } ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) + marketsAssistant, err := chainclient.NewMarketsAssistantInitializedFromChain(ctx, exchangeClient) if err != nil { panic(err) } - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -81,16 +79,21 @@ func main() { price := decimal.RequireFromString("31000") //31,000 leverage := decimal.RequireFromString("2.5") - order := chainClient.DerivativeOrder(defaultSubaccountID, network, &chainclient.DerivativeOrderData{ - OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO - Quantity: amount, - Price: price, - Leverage: leverage, - FeeRecipient: senderAddress.String(), - MarketId: marketId, - IsReduceOnly: true, - Cid: uuid.NewString(), - }) + order := chainClient.CreateDerivativeOrder( + defaultSubaccountID, + network, + &chainclient.DerivativeOrderData{ + OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO + Quantity: amount, + Price: price, + Leverage: leverage, + FeeRecipient: senderAddress.String(), + MarketId: marketId, + IsReduceOnly: true, + Cid: uuid.NewString(), + }, + marketsAssistant, + ) msg := new(exchangetypes.MsgCreateDerivativeLimitOrder) msg.Sender = senderAddress.String() diff --git a/examples/chain/7_MsgCreateDerivativeMarketOrder/example.go b/examples/chain/7_MsgCreateDerivativeMarketOrder/example.go index 66967194..5eae8eee 100644 --- a/examples/chain/7_MsgCreateDerivativeMarketOrder/example.go +++ b/examples/chain/7_MsgCreateDerivativeMarketOrder/example.go @@ -6,7 +6,6 @@ import ( "os" "time" - "github.com/InjectiveLabs/sdk-go/client/core" exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/google/uuid" @@ -59,15 +58,14 @@ func main() { } ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) + marketsAssistant, err := chainclient.NewMarketsAssistantInitializedFromChain(ctx, exchangeClient) if err != nil { panic(err) } - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) @@ -82,16 +80,21 @@ func main() { price := decimal.RequireFromString("33000") //33,000 leverage := decimal.RequireFromString("2.5") - order := chainClient.DerivativeOrder(defaultSubaccountID, network, &chainclient.DerivativeOrderData{ - OrderType: exchangetypes.OrderType_SELL, //BUY SELL - Quantity: amount, - Price: price, - Leverage: leverage, - FeeRecipient: senderAddress.String(), - MarketId: marketId, - IsReduceOnly: true, - Cid: uuid.NewString(), - }) + order := chainClient.CreateDerivativeOrder( + defaultSubaccountID, + network, + &chainclient.DerivativeOrderData{ + OrderType: exchangetypes.OrderType_SELL, //BUY SELL + Quantity: amount, + Price: price, + Leverage: leverage, + FeeRecipient: senderAddress.String(), + MarketId: marketId, + IsReduceOnly: true, + Cid: uuid.NewString(), + }, + marketsAssistant, + ) msg := new(exchangetypes.MsgCreateDerivativeMarketOrder) msg.Sender = senderAddress.String() diff --git a/examples/chain/8_MsgCancelDerivativeOrder/example.go b/examples/chain/8_MsgCancelDerivativeOrder/example.go index a50656ec..dd5972d2 100644 --- a/examples/chain/8_MsgCancelDerivativeOrder/example.go +++ b/examples/chain/8_MsgCancelDerivativeOrder/example.go @@ -1,14 +1,11 @@ package main import ( - "context" "fmt" "os" "time" "github.com/InjectiveLabs/sdk-go/client" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/InjectiveLabs/sdk-go/client/common" @@ -50,21 +47,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), ) diff --git a/examples/chain/9_MsgBatchCancelSpotOrders/example.go b/examples/chain/9_MsgBatchCancelSpotOrders/example.go index b37f0988..2734d9f8 100644 --- a/examples/chain/9_MsgBatchCancelSpotOrders/example.go +++ b/examples/chain/9_MsgBatchCancelSpotOrders/example.go @@ -1,14 +1,11 @@ package main import ( - "context" "fmt" "os" "time" "github.com/InjectiveLabs/sdk-go/client" - "github.com/InjectiveLabs/sdk-go/client/core" - exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/InjectiveLabs/sdk-go/client/common" @@ -51,21 +48,9 @@ func main() { clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) - exchangeClient, err := exchangeclient.NewExchangeClient(network) - if err != nil { - panic(err) - } - - ctx := context.Background() - marketsAssistant, err := core.NewMarketsAssistantUsingExchangeClient(ctx, exchangeClient) - if err != nil { - panic(err) - } - - chainClient, err := chainclient.NewChainClientWithMarketsAssistant( + chainClient, err := chainclient.NewChainClient( clientCtx, network, - marketsAssistant, common.OptionGasPrices(client.DefaultGasPriceWithDenom), )