Skip to content

Commit

Permalink
Feat/initialize tokens from chain denoms (#197)
Browse files Browse the repository at this point in the history
* (feat) Added missing messages from the Bank MOdule

* (feat) Added logic in MarketsAssistant to enable the use of all tokens from the chain denoms metadata

* (fix) Added lazy initialization for the legacy markets assistant (the one initialized from the .ini files).

---------

Co-authored-by: abel <[email protected]>
  • Loading branch information
aarmoa and abel authored Dec 30, 2023
1 parent ed9d10e commit c618b06
Show file tree
Hide file tree
Showing 54 changed files with 1,602 additions and 884 deletions.
142 changes: 107 additions & 35 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/InjectiveLabs/sdk-go/client/core"
"github.com/cosmos/cosmos-sdk/types/query"
"math"
"os"
"strconv"
Expand Down Expand Up @@ -86,8 +86,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)

Expand All @@ -113,7 +123,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(
Expand Down Expand Up @@ -142,14 +154,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

fromAddress sdk.AccAddress
doneC chan bool
Expand Down Expand Up @@ -180,26 +191,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()

Expand Down Expand Up @@ -253,10 +250,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",
Expand Down Expand Up @@ -448,26 +444,82 @@ func (c *chainClient) Close() {
}
}

//Bank Module

func (c *chainClient) GetBankBalances(ctx context.Context, address string) (*banktypes.QueryAllBalancesResponse, error) {
req := &banktypes.QueryAllBalancesRequest{
Address: address,
}
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.
Expand Down Expand Up @@ -894,9 +946,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))
}
Expand All @@ -917,9 +979,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))
}
Expand Down
17 changes: 1 addition & 16 deletions client/chain/chain_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package chain

import (
"context"
"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"
Expand Down Expand Up @@ -42,21 +39,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),
)

Expand Down
Loading

0 comments on commit c618b06

Please sign in to comment.