Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ignite/pkg/cosmosclient/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (c Client) BankBalances(ctx context.Context, address string, pagination *qu
Pagination: pagination,
}

resp, err := banktypes.NewQueryClient(c.context).AllBalances(ctx, req)
resp, err := c.bankQueryClient.AllBalances(ctx, req)
if err != nil {
return nil, err
}
Expand Down
41 changes: 41 additions & 0 deletions ignite/pkg/cosmosclient/bank_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cosmosclient_test

import (
"context"
"testing"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestClientBankBalances(t *testing.T) {
var (
ctx = context.Background()
address = "address"
pagination = &query.PageRequest{Offset: 1}
expectedBalances = sdk.NewCoins(
sdk.NewCoin("token", math.NewInt(1000)),
sdk.NewCoin("stake", math.NewInt(2000)),
)
)
c := newClient(t, func(s suite) {
req := &banktypes.QueryAllBalancesRequest{
Address: address,
Pagination: pagination,
}

s.bankQueryClient.EXPECT().AllBalances(ctx, req).
Return(&banktypes.QueryAllBalancesResponse{
Balances: expectedBalances,
}, nil)
})

balances, err := c.BankBalances(ctx, address, pagination)

require.NoError(t, err)
assert.Equal(t, expectedBalances, balances)
}
91 changes: 78 additions & 13 deletions ignite/pkg/cosmosclient/cosmosclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdktypes "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
gogogrpc "github.com/gogo/protobuf/grpc"
"github.com/gogo/protobuf/proto"
prototypes "github.com/gogo/protobuf/types"
"github.com/pkg/errors"
Expand Down Expand Up @@ -57,20 +59,35 @@ const (
defaultFaucetMinAmount = 100
)

// FaucetClient allows to mock the cosmosfaucet.Client.
type FaucetClient interface {
Transfer(context.Context, cosmosfaucet.TransferRequest) (cosmosfaucet.TransferResponse, error)
}

// Gasometer allows to mock the tx.CalculateGas func.
type Gasometer interface {
CalculateGas(clientCtx gogogrpc.ClientConn, txf tx.Factory, msgs ...sdktypes.Msg) (*txtypes.SimulateResponse, uint64, error)
}

// Client is a client to access your chain by querying and broadcasting transactions.
type Client struct {
// RPC is Tendermint RPC.
RPC rpcclient.Client

// Factory is a Cosmos SDK tx factory.
Factory tx.Factory
// TxFactory is a Cosmos SDK tx factory.
TxFactory tx.Factory

// context is a Cosmos SDK client context.
context client.Context

// AccountRegistry is the retistry to access accounts.
AccountRegistry cosmosaccount.Registry

accountRetriever client.AccountRetriever
bankQueryClient banktypes.QueryClient
faucetClient FaucetClient
gasometer Gasometer

addressPrefix string

nodeAddress string
Expand Down Expand Up @@ -184,19 +201,53 @@ func WithBroadcastMode(broadcastMode string) Option {
}
}

// WithGenerateOnly tells if txs will be generated only.
func WithGenerateOnly(generateOnly bool) Option {
return func(c *Client) {
c.generateOnly = generateOnly
}
}

// WithRPCClient sets a tendermint RPC client.
// Already set by default.
func WithRPCClient(rpc rpcclient.Client) Option {
return func(c *Client) {
c.RPC = rpc
}
}

// WithAccountRetriever sets the account retriever
// Already set by default.
func WithAccountRetriever(accountRetriever client.AccountRetriever) Option {
return func(c *Client) {
c.accountRetriever = accountRetriever
}
}

// WithBankQueryClient sets the bank query client.
// Already set by default.
func WithBankQueryClient(bankQueryClient banktypes.QueryClient) Option {
return func(c *Client) {
c.bankQueryClient = bankQueryClient
}
}

// WithFaucetClient sets the faucet client.
// Already set by default.
func WithFaucetClient(faucetClient FaucetClient) Option {
return func(c *Client) {
c.faucetClient = faucetClient
}
}

// WithGasometer sets the gasometer.
// Already set by default.
func WithGasometer(gasometer Gasometer) Option {
return func(c *Client) {
c.gasometer = gasometer
}
}

// New creates a new client with given options.
func New(ctx context.Context, options ...Option) (Client, error) {
c := Client{
Expand Down Expand Up @@ -252,8 +303,20 @@ func New(ctx context.Context, options ...Option) (Client, error) {
}

c.context = c.newContext()
c.Factory = newFactory(c.context)
c.TxFactory = newFactory(c.context)

if c.accountRetriever == nil {
c.accountRetriever = authtypes.AccountRetriever{}
}
if c.bankQueryClient == nil {
c.bankQueryClient = banktypes.NewQueryClient(c.context)
}
if c.faucetClient == nil {
c.faucetClient = cosmosfaucet.NewClient(c.faucetAddress)
}
if c.gasometer == nil {
c.gasometer = gasometer{}
}
// set address prefix in SDK global config
c.SetConfigAddressPrefix()

Expand Down Expand Up @@ -444,7 +507,7 @@ func (c Client) CreateTx(account cosmosaccount.Account, msgs ...sdktypes.Msg) (T
WithFromName(account.Name).
WithFromAddress(sdkaddr)

txf, err := prepareFactory(ctx, c.Factory)
txf, err := c.prepareFactory(ctx)
if err != nil {
return TxService{}, err
}
Expand All @@ -456,7 +519,7 @@ func (c Client) CreateTx(account cosmosaccount.Account, msgs ...sdktypes.Msg) (T
return TxService{}, err
}
} else {
_, gas, err = tx.CalculateGas(ctx, txf, msgs...)
_, gas, err = c.gasometer.CalculateGas(ctx, txf, msgs...)
if err != nil {
return TxService{}, err
}
Expand Down Expand Up @@ -494,8 +557,7 @@ func (c *Client) makeSureAccountHasTokens(ctx context.Context, address string) e
}

// request coins from the faucet.
fc := cosmosfaucet.NewClient(c.faucetAddress)
faucetResp, err := fc.Transfer(ctx, cosmosfaucet.TransferRequest{AccountAddress: address})
faucetResp, err := c.faucetClient.Transfer(ctx, cosmosfaucet.TransferRequest{AccountAddress: address})
if err != nil {
return errors.Wrap(errCannotRetrieveFundsFromFaucet, err.Error())
}
Expand All @@ -513,7 +575,7 @@ func (c *Client) makeSureAccountHasTokens(ctx context.Context, address string) e
}

func (c *Client) checkAccountBalance(ctx context.Context, address string) error {
resp, err := banktypes.NewQueryClient(c.context).Balance(ctx, &banktypes.QueryBalanceRequest{
resp, err := c.bankQueryClient.Balance(ctx, &banktypes.QueryBalanceRequest{
Address: address,
Denom: c.faucetDenom,
})
Expand Down Expand Up @@ -544,16 +606,19 @@ func handleBroadcastResult(resp *sdktypes.TxResponse, err error) error {
return nil
}

func prepareFactory(clientCtx client.Context, txf tx.Factory) (tx.Factory, error) {
from := clientCtx.GetFromAddress()
func (c *Client) prepareFactory(clientCtx client.Context) (tx.Factory, error) {
var (
from = clientCtx.GetFromAddress()
txf = c.TxFactory
)

if err := txf.AccountRetriever().EnsureExists(clientCtx, from); err != nil {
if err := c.accountRetriever.EnsureExists(clientCtx, from); err != nil {
return txf, err
}

initNum, initSeq := txf.AccountNumber(), txf.Sequence()
if initNum == 0 || initSeq == 0 {
num, seq, err := txf.AccountRetriever().GetAccountNumberSequence(clientCtx, from)
num, seq, err := c.accountRetriever.GetAccountNumberSequence(clientCtx, from)
if err != nil {
return txf, err
}
Expand Down Expand Up @@ -593,7 +658,7 @@ func (c Client) newContext() client.Context {
WithLegacyAmino(amino).
WithInput(os.Stdin).
WithOutput(c.out).
WithAccountRetriever(authtypes.AccountRetriever{}).
WithAccountRetriever(c.accountRetriever).
WithBroadcastMode(c.broadcastMode).
WithHomeDir(c.homePath).
WithClient(c.RPC).
Expand Down
Loading