2020-05-04 06:55:16 -07:00
|
|
|
package testutil
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
import (
|
2020-07-02 06:02:28 -07:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-04-29 08:52:30 -07:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-04-29 19:36:34 -07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-07-02 06:02:28 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-04-29 08:52:30 -07:00
|
|
|
clientkeys "github.com/cosmos/cosmos-sdk/client/keys"
|
|
|
|
"github.com/cosmos/cosmos-sdk/tests"
|
2020-05-01 13:16:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/tests/cli"
|
2020-04-29 08:52:30 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-06-17 11:42:27 -07:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2020-07-02 06:02:28 -07:00
|
|
|
bankcli "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
|
2020-04-29 08:52:30 -07:00
|
|
|
)
|
|
|
|
|
2020-07-02 06:02:28 -07:00
|
|
|
func MsgSendExec(clientCtx client.Context, from, to, amount fmt.Stringer, extraArgs ...string) ([]byte, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
clientCtx = clientCtx.WithOutput(buf)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
|
|
|
|
|
|
|
|
args := []string{from.String(), to.String(), amount.String()}
|
|
|
|
args = append(args, extraArgs...)
|
|
|
|
|
|
|
|
cmd := bankcli.NewSendTxCmd()
|
|
|
|
cmd.SetErr(buf)
|
|
|
|
cmd.SetOut(buf)
|
|
|
|
cmd.SetArgs(args)
|
|
|
|
|
|
|
|
if err := cmd.ExecuteContext(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// TODO: REMOVE ALL FUNCTIONS BELOW.
|
|
|
|
//
|
|
|
|
// REF: https://github.com/cosmos/cosmos-sdk/issues/6571
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-06-30 13:59:21 -07:00
|
|
|
|
2020-04-29 08:52:30 -07:00
|
|
|
// TxSend is simcli tx send
|
2020-05-01 13:16:17 -07:00
|
|
|
func TxSend(f *cli.Fixtures, from string, to sdk.AccAddress, amount sdk.Coin, flags ...string) (bool, string, string) {
|
2020-07-07 08:40:46 -07:00
|
|
|
cmd := fmt.Sprintf("%s tx send --keyring-backend=test %s %s %s %v", f.SimdBinary, from, to, amount, f.Flags())
|
2020-05-01 13:16:17 -07:00
|
|
|
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
|
2020-04-29 08:52:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryAccount is simcli query account
|
2020-06-17 11:42:27 -07:00
|
|
|
func QueryAccount(f *cli.Fixtures, address sdk.AccAddress, flags ...string) authtypes.BaseAccount {
|
2020-07-07 08:40:46 -07:00
|
|
|
cmd := fmt.Sprintf("%s query account %s %v", f.SimdBinary, address, f.Flags())
|
2020-04-29 08:52:30 -07:00
|
|
|
|
2020-05-01 13:16:17 -07:00
|
|
|
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
var initRes map[string]json.RawMessage
|
|
|
|
err := json.Unmarshal([]byte(out), &initRes)
|
|
|
|
require.NoError(f.T, err, "out %v, err %v", out, err)
|
|
|
|
value := initRes["value"]
|
|
|
|
|
2020-06-17 11:42:27 -07:00
|
|
|
var acc authtypes.BaseAccount
|
2020-04-29 08:52:30 -07:00
|
|
|
err = f.Cdc.UnmarshalJSON(value, &acc)
|
|
|
|
require.NoError(f.T, err, "value %v, err %v", string(value), err)
|
|
|
|
|
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryBalances executes the bank query balances command for a given address and
|
|
|
|
// flag set.
|
2020-05-01 13:16:17 -07:00
|
|
|
func QueryBalances(f *cli.Fixtures, address sdk.AccAddress, flags ...string) sdk.Coins {
|
2020-07-07 08:40:46 -07:00
|
|
|
cmd := fmt.Sprintf("%s query bank balances %s %v", f.SimdBinary, address, f.Flags())
|
2020-05-01 13:16:17 -07:00
|
|
|
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
var balances sdk.Coins
|
|
|
|
|
|
|
|
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &balances), "out %v\n", out)
|
|
|
|
|
|
|
|
return balances
|
|
|
|
}
|
2020-05-05 06:51:35 -07:00
|
|
|
|
|
|
|
// QueryTotalSupply returns the total supply of coins
|
|
|
|
func QueryTotalSupply(f *cli.Fixtures, flags ...string) (totalSupply sdk.Coins) {
|
2020-07-07 08:40:46 -07:00
|
|
|
cmd := fmt.Sprintf("%s query bank total %s", f.SimdBinary, f.Flags())
|
2020-05-05 06:51:35 -07:00
|
|
|
res, errStr := tests.ExecuteT(f.T, cmd, "")
|
|
|
|
require.Empty(f.T, errStr)
|
|
|
|
|
|
|
|
err := f.Cdc.UnmarshalJSON([]byte(res), &totalSupply)
|
|
|
|
require.NoError(f.T, err)
|
|
|
|
return totalSupply
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryTotalSupplyOf returns the total supply of a given coin denom
|
|
|
|
func QueryTotalSupplyOf(f *cli.Fixtures, denom string, flags ...string) sdk.Int {
|
2020-07-07 08:40:46 -07:00
|
|
|
cmd := fmt.Sprintf("%s query bank total %s %s", f.SimdBinary, denom, f.Flags())
|
2020-05-05 06:51:35 -07:00
|
|
|
res, errStr := tests.ExecuteT(f.T, cmd, "")
|
|
|
|
require.Empty(f.T, errStr)
|
|
|
|
|
|
|
|
var supplyOf sdk.Int
|
|
|
|
err := f.Cdc.UnmarshalJSON([]byte(res), &supplyOf)
|
|
|
|
require.NoError(f.T, err)
|
|
|
|
return supplyOf
|
|
|
|
}
|