@@ -21,11 +21,13 @@ import (
2121	codectypes "github.com/cosmos/cosmos-sdk/codec/types" 
2222	cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" 
2323	sdktypes "github.com/cosmos/cosmos-sdk/types" 
24+ 	txtypes "github.com/cosmos/cosmos-sdk/types/tx" 
2425	"github.com/cosmos/cosmos-sdk/types/tx/signing" 
2526	authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" 
2627	authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" 
2728	banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" 
2829	staking "github.com/cosmos/cosmos-sdk/x/staking/types" 
30+ 	gogogrpc "github.com/gogo/protobuf/grpc" 
2931	"github.com/gogo/protobuf/proto" 
3032	prototypes "github.com/gogo/protobuf/types" 
3133	"github.com/pkg/errors" 
@@ -57,20 +59,35 @@ const (
5759	defaultFaucetMinAmount  =  100 
5860)
5961
62+ // FaucetClient allows to mock the cosmosfaucet.Client. 
63+ type  FaucetClient  interface  {
64+ 	Transfer (context.Context , cosmosfaucet.TransferRequest ) (cosmosfaucet.TransferResponse , error )
65+ }
66+ 
67+ // Gasometer allows to mock the tx.CalculateGas func. 
68+ type  Gasometer  interface  {
69+ 	CalculateGas (clientCtx  gogogrpc.ClientConn , txf  tx.Factory , msgs  ... sdktypes.Msg ) (* txtypes.SimulateResponse , uint64 , error )
70+ }
71+ 
6072// Client is a client to access your chain by querying and broadcasting transactions. 
6173type  Client  struct  {
6274	// RPC is Tendermint RPC. 
6375	RPC  rpcclient.Client 
6476
65- 	// Factory  is a Cosmos SDK tx factory. 
66- 	Factory  tx.Factory 
77+ 	// TxFactory  is a Cosmos SDK tx factory. 
78+ 	TxFactory  tx.Factory 
6779
6880	// context is a Cosmos SDK client context. 
6981	context  client.Context 
7082
7183	// AccountRegistry is the retistry to access accounts. 
7284	AccountRegistry  cosmosaccount.Registry 
7385
86+ 	accountRetriever  client.AccountRetriever 
87+ 	bankQueryClient   banktypes.QueryClient 
88+ 	faucetClient      FaucetClient 
89+ 	gasometer         Gasometer 
90+ 
7491	addressPrefix  string 
7592
7693	nodeAddress  string 
@@ -184,19 +201,53 @@ func WithBroadcastMode(broadcastMode string) Option {
184201	}
185202}
186203
204+ // WithGenerateOnly tells if txs will be generated only. 
187205func  WithGenerateOnly (generateOnly  bool ) Option  {
188206	return  func (c  * Client ) {
189207		c .generateOnly  =  generateOnly 
190208	}
191209}
192210
193211// WithRPCClient sets a tendermint RPC client. 
212+ // Already set by default. 
194213func  WithRPCClient (rpc  rpcclient.Client ) Option  {
195214	return  func (c  * Client ) {
196215		c .RPC  =  rpc 
197216	}
198217}
199218
219+ // WithAccountRetriever sets the account retriever 
220+ // Already set by default. 
221+ func  WithAccountRetriever (accountRetriever  client.AccountRetriever ) Option  {
222+ 	return  func (c  * Client ) {
223+ 		c .accountRetriever  =  accountRetriever 
224+ 	}
225+ }
226+ 
227+ // WithBankQueryClient sets the bank query client. 
228+ // Already set by default. 
229+ func  WithBankQueryClient (bankQueryClient  banktypes.QueryClient ) Option  {
230+ 	return  func (c  * Client ) {
231+ 		c .bankQueryClient  =  bankQueryClient 
232+ 	}
233+ }
234+ 
235+ // WithFaucetClient sets the faucet client. 
236+ // Already set by default. 
237+ func  WithFaucetClient (faucetClient  FaucetClient ) Option  {
238+ 	return  func (c  * Client ) {
239+ 		c .faucetClient  =  faucetClient 
240+ 	}
241+ }
242+ 
243+ // WithGasometer sets the gasometer. 
244+ // Already set by default. 
245+ func  WithGasometer (gasometer  Gasometer ) Option  {
246+ 	return  func (c  * Client ) {
247+ 		c .gasometer  =  gasometer 
248+ 	}
249+ }
250+ 
200251// New creates a new client with given options. 
201252func  New (ctx  context.Context , options  ... Option ) (Client , error ) {
202253	c  :=  Client {
@@ -252,8 +303,20 @@ func New(ctx context.Context, options ...Option) (Client, error) {
252303	}
253304
254305	c .context  =  c .newContext ()
255- 	c .Factory  =  newFactory (c .context )
306+ 	c .TxFactory  =  newFactory (c .context )
256307
308+ 	if  c .accountRetriever  ==  nil  {
309+ 		c .accountRetriever  =  authtypes.AccountRetriever {}
310+ 	}
311+ 	if  c .bankQueryClient  ==  nil  {
312+ 		c .bankQueryClient  =  banktypes .NewQueryClient (c .context )
313+ 	}
314+ 	if  c .faucetClient  ==  nil  {
315+ 		c .faucetClient  =  cosmosfaucet .NewClient (c .faucetAddress )
316+ 	}
317+ 	if  c .gasometer  ==  nil  {
318+ 		c .gasometer  =  gasometer {}
319+ 	}
257320	// set address prefix in SDK global config 
258321	c .SetConfigAddressPrefix ()
259322
@@ -444,7 +507,7 @@ func (c Client) CreateTx(account cosmosaccount.Account, msgs ...sdktypes.Msg) (T
444507		WithFromName (account .Name ).
445508		WithFromAddress (sdkaddr )
446509
447- 	txf , err  :=  prepareFactory (ctx ,  c . Factory )
510+ 	txf , err  :=  c . prepareFactory (ctx )
448511	if  err  !=  nil  {
449512		return  TxService {}, err 
450513	}
@@ -456,7 +519,7 @@ func (c Client) CreateTx(account cosmosaccount.Account, msgs ...sdktypes.Msg) (T
456519			return  TxService {}, err 
457520		}
458521	} else  {
459- 		_ , gas , err  =  tx .CalculateGas (ctx , txf , msgs ... )
522+ 		_ , gas , err  =  c . gasometer .CalculateGas (ctx , txf , msgs ... )
460523		if  err  !=  nil  {
461524			return  TxService {}, err 
462525		}
@@ -494,8 +557,7 @@ func (c *Client) makeSureAccountHasTokens(ctx context.Context, address string) e
494557	}
495558
496559	// request coins from the faucet. 
497- 	fc  :=  cosmosfaucet .NewClient (c .faucetAddress )
498- 	faucetResp , err  :=  fc .Transfer (ctx , cosmosfaucet.TransferRequest {AccountAddress : address })
560+ 	faucetResp , err  :=  c .faucetClient .Transfer (ctx , cosmosfaucet.TransferRequest {AccountAddress : address })
499561	if  err  !=  nil  {
500562		return  errors .Wrap (errCannotRetrieveFundsFromFaucet , err .Error ())
501563	}
@@ -513,7 +575,7 @@ func (c *Client) makeSureAccountHasTokens(ctx context.Context, address string) e
513575}
514576
515577func  (c  * Client ) checkAccountBalance (ctx  context.Context , address  string ) error  {
516- 	resp , err  :=  banktypes . NewQueryClient ( c . context ) .Balance (ctx , & banktypes.QueryBalanceRequest {
578+ 	resp , err  :=  c . bankQueryClient .Balance (ctx , & banktypes.QueryBalanceRequest {
517579		Address : address ,
518580		Denom :   c .faucetDenom ,
519581	})
@@ -544,16 +606,19 @@ func handleBroadcastResult(resp *sdktypes.TxResponse, err error) error {
544606	return  nil 
545607}
546608
547- func  prepareFactory (clientCtx  client.Context , txf  tx.Factory ) (tx.Factory , error ) {
548- 	from  :=  clientCtx .GetFromAddress ()
609+ func  (c  * Client ) prepareFactory (clientCtx  client.Context ) (tx.Factory , error ) {
610+ 	var  (
611+ 		from  =  clientCtx .GetFromAddress ()
612+ 		txf   =  c .TxFactory 
613+ 	)
549614
550- 	if  err  :=  txf . AccountRetriever () .EnsureExists (clientCtx , from ); err  !=  nil  {
615+ 	if  err  :=  c . accountRetriever .EnsureExists (clientCtx , from ); err  !=  nil  {
551616		return  txf , err 
552617	}
553618
554619	initNum , initSeq  :=  txf .AccountNumber (), txf .Sequence ()
555620	if  initNum  ==  0  ||  initSeq  ==  0  {
556- 		num , seq , err  :=  txf . AccountRetriever () .GetAccountNumberSequence (clientCtx , from )
621+ 		num , seq , err  :=  c . accountRetriever .GetAccountNumberSequence (clientCtx , from )
557622		if  err  !=  nil  {
558623			return  txf , err 
559624		}
@@ -593,7 +658,7 @@ func (c Client) newContext() client.Context {
593658		WithLegacyAmino (amino ).
594659		WithInput (os .Stdin ).
595660		WithOutput (c .out ).
596- 		WithAccountRetriever (authtypes. AccountRetriever {} ).
661+ 		WithAccountRetriever (c . accountRetriever ).
597662		WithBroadcastMode (c .broadcastMode ).
598663		WithHomeDir (c .homePath ).
599664		WithClient (c .RPC ).
0 commit comments