Skip to content

Commit

Permalink
tests: add e2e tests for ibc_status params (#1978)
Browse files Browse the repository at this point in the history
* chore: add tests in flow
1. check the quota after ibc_transfer
2. send the tokens to umee to gaia and back
3. disable the quota check
4. check the prices effected or not

* chore: fix the lint

* review: address the pr comments

* Apply suggestions from code review

* review: fix the lint

---------

Co-authored-by: Robert Zaremba <robert@zaremba.ch>
  • Loading branch information
gsk967 and robert-zaremba committed Apr 12, 2023
1 parent f67ec3d commit 7f05ad4
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 88 deletions.
14 changes: 7 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ type Client struct {
}

// NewClient constructs Client object.
// Accounts are generated using the list of mnemonics. Each string must be a sequence of words,
// eg: `["w11 w12 w13", "w21 w22 w23"]`. Keyring names for created accounts will be: val1, val2....
func NewClient(
chainID,
tmrpcEndpoint,
grpcEndpoint,
accountName,
accountMnemonic string,
grpcEndpoint string,
mnemonics []string,
gasAdjustment float64,
encCfg sdkparams.EncodingConfig,
) (Client, error) {
c, err := sdkclient.NewClient(chainID, tmrpcEndpoint, grpcEndpoint,
accountName, accountMnemonic, gasAdjustment, encCfg)
c, err := sdkclient.NewClient(chainID, tmrpcEndpoint, grpcEndpoint, mnemonics, gasAdjustment, encCfg)
if err != nil {
return Client{}, err
}
Expand All @@ -32,10 +32,10 @@ func NewClient(
}, nil
}

func (c *Client) NewQCtx() (context.Context, context.CancelFunc) {
func (c Client) NewQCtx() (context.Context, context.CancelFunc) {
return c.Query.NewCtx()
}

func (c *Client) NewQCtxWithCancel() (context.Context, context.CancelFunc) {
func (c Client) NewQCtxWithCancel() (context.Context, context.CancelFunc) {
return c.Query.NewCtxWithCancel()
}
20 changes: 20 additions & 0 deletions client/gov.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package client

import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
)

func (c Client) GovQClient() govtypes.QueryClient {
return govtypes.NewQueryClient(c.Query.GrpcConn)
}

func (c Client) GovProposal(proposalID uint64) (*govtypes.Proposal, error) {
ctx, cancel := c.NewQCtx()
defer cancel()

queryResponse, err := c.GovQClient().Proposal(ctx, &govtypes.QueryProposalRequest{ProposalId: proposalID})
if err != nil {
return nil, err
}
return queryResponse.Proposal, nil
}
10 changes: 5 additions & 5 deletions client/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@ import (
oracletypes "github.com/umee-network/umee/v4/x/oracle/types"
)

func (c *Client) OracleQueryClient() oracletypes.QueryClient {
func (c Client) OracleQueryClient() oracletypes.QueryClient {
return oracletypes.NewQueryClient(c.Query.GrpcConn)
}

func (c *Client) QueryOracleParams() (oracletypes.Params, error) {
func (c Client) QueryOracleParams() (oracletypes.Params, error) {
ctx, cancel := c.NewQCtx()
defer cancel()

queryResponse, err := c.OracleQueryClient().Params(ctx, &oracletypes.QueryParams{})
return queryResponse.Params, err
}

func (c *Client) QueryExchangeRates() ([]sdk.DecCoin, error) {
func (c Client) QueryExchangeRates() ([]sdk.DecCoin, error) {
ctx, cancel := c.NewQCtx()
defer cancel()

queryResponse, err := c.OracleQueryClient().ExchangeRates(ctx, &oracletypes.QueryExchangeRates{})
return queryResponse.ExchangeRates, err
}

func (c *Client) QueryMedians() ([]oracletypes.Price, error) {
func (c Client) QueryMedians() ([]oracletypes.Price, error) {
ctx, cancel := c.NewQCtx()
defer cancel()

resp, err := c.OracleQueryClient().Medians(ctx, &oracletypes.QueryMedians{})
return resp.Medians, err
}

func (c *Client) QueryMedianDeviations() ([]oracletypes.Price, error) {
func (c Client) QueryMedianDeviations() ([]oracletypes.Price, error) {
ctx, cancel := c.NewQCtx()
defer cancel()

Expand Down
20 changes: 20 additions & 0 deletions client/uibc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package client

import (
"github.com/umee-network/umee/v4/x/uibc"
)

func (c Client) UIBCQueryClient() uibc.QueryClient {
return uibc.NewQueryClient(c.Query.GrpcConn)
}

func (c Client) QueryUIBCParams() (uibc.Params, error) {
ctx, cancel := c.NewQCtx()
defer cancel()

queryResponse, err := c.UIBCQueryClient().Params(ctx, &uibc.QueryParams{})
if err != nil {
return uibc.Params{}, err
}
return queryResponse.Params, nil
}
9 changes: 5 additions & 4 deletions sdkclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
// transactions and queries. The object should be extended by another struct to provide
// chain specific transactions and queries. Example:
// https://github.com/umee-network/umee/blob/main/client
// Accounts are generated using the list of mnemonics. Each string must be a sequence of words,
// eg: `["w11 w12 w13", "w21 w22 w23"]`. Keyring names for created accounts will be: val1, val2....
type Client struct {
Query *query.Client
Tx *tx.Client
Expand All @@ -23,9 +25,8 @@ type Client struct {
func NewClient(
chainID,
tmrpcEndpoint,
grpcEndpoint,
accountName,
accountMnemonic string,
grpcEndpoint string,
mnemonics []string,
gasAdjustment float64,
encCfg sdkparams.EncodingConfig,
) (uc Client, err error) {
Expand All @@ -34,7 +35,7 @@ func NewClient(
if err != nil {
return Client{}, err
}
uc.Tx, err = tx.NewClient(chainID, tmrpcEndpoint, accountName, accountMnemonic, gasAdjustment, encCfg)
uc.Tx, err = tx.NewClient(chainID, tmrpcEndpoint, mnemonics, gasAdjustment, encCfg)
return uc, err
}

Expand Down
26 changes: 19 additions & 7 deletions sdkclient/tx/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tx

import (
"fmt"
"os"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -23,18 +24,18 @@ type Client struct {
gasAdjustment float64

keyringKeyring keyring.Keyring
keyringRecord *keyring.Record
keyringRecord []*keyring.Record
txFactory *tx.Factory
encCfg sdkparams.EncodingConfig
}

// Initializes a cosmos sdk client context and transaction factory for
// signing and broadcasting transactions
// Note: For signing the transactions accounts are created by names like this val0, val1....
func NewClient(
chainID string,
tmrpcEndpoint string,
accountName string,
accountMnemonic string,
mnemonics []string,
gasAdjustment float64,
encCfg sdkparams.EncodingConfig,
) (c *Client, err error) {
Expand All @@ -45,11 +46,19 @@ func NewClient(
encCfg: encCfg,
}

c.keyringRecord, c.keyringKeyring, err = CreateAccountFromMnemonic(accountName, accountMnemonic, encCfg.Codec)
c.keyringKeyring, err = keyring.New(keyringAppName, keyring.BackendTest, "", nil, encCfg.Codec)
if err != nil {
return nil, err
}

for index, menomic := range mnemonics {
kr, err := CreateAccountFromMnemonic(c.keyringKeyring, fmt.Sprintf("val%d", index), menomic)
c.keyringRecord = append(c.keyringRecord, kr)
if err != nil {
return nil, err
}
}

err = c.initClientCtx()
if err != nil {
return nil, err
Expand All @@ -60,7 +69,7 @@ func NewClient(
}

func (c *Client) initClientCtx() error {
fromAddress, _ := c.keyringRecord.GetAddress()
fromAddress, _ := c.keyringRecord[0].GetAddress()

tmHTTPClient, err := tmjsonclient.DefaultHTTPClient(c.TMRPCEndpoint)
if err != nil {
Expand All @@ -85,8 +94,8 @@ func (c *Client) initClientCtx() error {
Client: tmRPCClient,
Keyring: c.keyringKeyring,
FromAddress: fromAddress,
FromName: c.keyringRecord.Name,
From: c.keyringRecord.Name,
FromName: c.keyringRecord[0].Name,
From: c.keyringRecord[0].Name,
OutputFormat: "json",
UseLedger: false,
Simulate: false,
Expand All @@ -110,5 +119,8 @@ func (c *Client) initTxFactory() {
}

func (c *Client) BroadcastTx(msgs ...sdk.Msg) (*sdk.TxResponse, error) {
c.ClientContext.From = c.keyringRecord[0].Name
c.ClientContext.FromName = c.keyringRecord[0].Name
c.ClientContext.FromAddress, _ = c.keyringRecord[0].GetAddress()
return BroadcastTx(*c.ClientContext, *c.txFactory, msgs...)
}
84 changes: 63 additions & 21 deletions sdkclient/tx/gov.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
package tx

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
proposal "github.com/cosmos/cosmos-sdk/x/params/types/proposal"
)

func (c *Client) GovVoteYes(proposalID uint64) (*sdk.TxResponse, error) {
voter, err := c.keyringRecord.GetAddress()
if err != nil {
return nil, err
}

voteType, err := govtypes.VoteOptionFromString("VOTE_OPTION_YES")
if err != nil {
return nil, err
}

msg := govtypes.NewMsgVote(
voter,
proposalID,
voteType,
)
return c.BroadcastTx(msg)
}

func (c *Client) GovParamChange(title, description string, changes []proposal.ParamChange, deposit sdk.Coins,
) (*sdk.TxResponse, error) {
content := proposal.NewParameterChangeProposal(title, description, changes)
fromAddr, err := c.keyringRecord.GetAddress()
fromAddr, err := c.keyringRecord[0].GetAddress()
if err != nil {
return nil, err
}
Expand All @@ -47,7 +31,7 @@ func (c *Client) GovSubmitProposal(changes []proposal.ParamChange, deposit sdk.C
changes,
)

fromAddr, err := c.keyringRecord.GetAddress()
fromAddr, err := c.keyringRecord[0].GetAddress()
if err != nil {
return nil, err
}
Expand All @@ -58,3 +42,61 @@ func (c *Client) GovSubmitProposal(changes []proposal.ParamChange, deposit sdk.C

return c.BroadcastTx(msg)
}

func (c *Client) TxSubmitProposalWithMsg(msgs []sdk.Msg) (*sdk.TxResponse, error) {
deposit, err := sdk.ParseCoinsNormalized("1000uumee")
if err != nil {
return nil, err
}

fromAddr, err := c.keyringRecord[0].GetAddress()
if err != nil {
return nil, err
}

submitProposal, err := v1.NewMsgSubmitProposal(msgs, deposit, fromAddr.String(), "")
if err != nil {
return nil, err
}

return c.BroadcastTx(submitProposal)
}

// TxGovVoteYesAll creates transactions (one for each registered account) to approve a given proposal.
func (c *Client) TxGovVoteYesAll(proposalID uint64) error {
for index := range c.keyringRecord {
voter, err := c.keyringRecord[index].GetAddress()
if err != nil {
return err
}

voteType, err := govtypes.VoteOptionFromString("VOTE_OPTION_YES")
if err != nil {
return err
}

msg := govtypes.NewMsgVote(
voter,
proposalID,
voteType,
)

c.ClientContext.From = c.keyringRecord[index].Name
c.ClientContext.FromName = c.keyringRecord[index].Name
c.ClientContext.FromAddress, _ = c.keyringRecord[index].GetAddress()

for retry := 0; retry < 5; retry++ {
// retry if txs fails, because sometimes account sequence mismatch occurs due to txs pending
if _, err = BroadcastTx(*c.ClientContext, *c.txFactory, []sdk.Msg{msg}...); err == nil {
break
}
time.Sleep(time.Second * 1)
}

if err != nil {
return err
}
}

return nil
}
14 changes: 4 additions & 10 deletions sdkclient/tx/key.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tx

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -12,22 +11,17 @@ const (
keyringAppName = "testnet"
)

func CreateAccountFromMnemonic(name, mnemonic string, cdc codec.Codec) (*keyring.Record, keyring.Keyring, error) {
kb, err := keyring.New(keyringAppName, keyring.BackendTest, "", nil, cdc)
if err != nil {
return nil, nil, err
}

func CreateAccountFromMnemonic(kb keyring.Keyring, name, mnemonic string) (*keyring.Record, error) {
keyringAlgos, _ := kb.SupportedAlgorithms()
algo, err := keyring.NewSigningAlgoFromString(string(hd.Secp256k1Type), keyringAlgos)
if err != nil {
return nil, nil, err
return nil, err
}

account, err := kb.NewAccount(name, mnemonic, "", sdk.FullFundraiserPath, algo)
if err != nil {
return nil, nil, err
return nil, err
}

return account, kb, nil
return account, nil
}
Loading

0 comments on commit 7f05ad4

Please sign in to comment.