2020-05-04 06:55:16 -07:00
|
|
|
package testutil
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-29 19:36:34 -07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
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-12 07:54:37 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2020-04-29 08:52:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// TxStakingCreateValidator is simcli tx staking create-validator
|
2020-05-01 13:16:17 -07:00
|
|
|
func TxStakingCreateValidator(f *cli.Fixtures, from, consPubKey string, amount sdk.Coin, flags ...string) (bool, string, string) {
|
2020-04-29 08:52:30 -07:00
|
|
|
cmd := fmt.Sprintf("%s tx staking create-validator %v --keyring-backend=test --from=%s"+
|
|
|
|
" --pubkey=%s", f.SimcliBinary, f.Flags(), from, consPubKey)
|
|
|
|
cmd += fmt.Sprintf(" --amount=%v --moniker=%v --commission-rate=%v", amount, from, "0.05")
|
|
|
|
cmd += fmt.Sprintf(" --commission-max-rate=%v --commission-max-change-rate=%v", "0.20", "0.10")
|
|
|
|
cmd += fmt.Sprintf(" --min-self-delegation=%v", "1")
|
2020-05-02 12:26:59 -07:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// TxStakingUnbond is simcli tx staking unbond
|
2020-05-01 13:16:17 -07:00
|
|
|
func TxStakingUnbond(f *cli.Fixtures, from, shares string, validator sdk.ValAddress, flags ...string) bool {
|
2020-04-29 08:52:30 -07:00
|
|
|
cmd := fmt.Sprintf("%s tx staking unbond --keyring-backend=test %s %v --from=%s %v",
|
|
|
|
f.SimcliBinary, validator, shares, from, f.Flags())
|
2020-05-01 13:16:17 -07:00
|
|
|
return cli.ExecuteWrite(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
|
2020-04-29 08:52:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryStakingValidator is simcli query staking validator
|
2020-06-12 07:54:37 -07:00
|
|
|
func QueryStakingValidator(f *cli.Fixtures, valAddr sdk.ValAddress, flags ...string) types.Validator {
|
2020-04-29 08:52:30 -07:00
|
|
|
cmd := fmt.Sprintf("%s query staking validator %s %v", f.SimcliBinary, valAddr, f.Flags())
|
2020-05-01 13:16:17 -07:00
|
|
|
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-06-12 07:54:37 -07:00
|
|
|
var validator types.Validator
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
err := f.Cdc.UnmarshalJSON([]byte(out), &validator)
|
|
|
|
require.NoError(f.T, err, "out %v\n, err %v", out, err)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-04-29 08:52:30 -07:00
|
|
|
return validator
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryStakingUnbondingDelegationsFrom is simcli query staking unbonding-delegations-from
|
2020-06-12 07:54:37 -07:00
|
|
|
func QueryStakingUnbondingDelegationsFrom(f *cli.Fixtures, valAddr sdk.ValAddress, flags ...string) []types.UnbondingDelegation {
|
2020-04-29 08:52:30 -07:00
|
|
|
cmd := fmt.Sprintf("%s query staking unbonding-delegations-from %s %v", f.SimcliBinary, valAddr, f.Flags())
|
2020-05-01 13:16:17 -07:00
|
|
|
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-06-12 07:54:37 -07:00
|
|
|
var ubds []types.UnbondingDelegation
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
err := f.Cdc.UnmarshalJSON([]byte(out), &ubds)
|
|
|
|
require.NoError(f.T, err, "out %v\n, err %v", out, err)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-04-29 08:52:30 -07:00
|
|
|
return ubds
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryStakingDelegationsTo is simcli query staking delegations-to
|
2020-06-12 07:54:37 -07:00
|
|
|
func QueryStakingDelegationsTo(f *cli.Fixtures, valAddr sdk.ValAddress, flags ...string) []types.Delegation {
|
2020-04-29 08:52:30 -07:00
|
|
|
cmd := fmt.Sprintf("%s query staking delegations-to %s %v", f.SimcliBinary, valAddr, f.Flags())
|
2020-05-01 13:16:17 -07:00
|
|
|
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-06-12 07:54:37 -07:00
|
|
|
var delegations []types.Delegation
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
err := f.Cdc.UnmarshalJSON([]byte(out), &delegations)
|
|
|
|
require.NoError(f.T, err, "out %v\n, err %v", out, err)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-04-29 08:52:30 -07:00
|
|
|
return delegations
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryStakingPool is simcli query staking pool
|
2020-06-12 07:54:37 -07:00
|
|
|
func QueryStakingPool(f *cli.Fixtures, flags ...string) types.Pool {
|
2020-04-29 08:52:30 -07:00
|
|
|
cmd := fmt.Sprintf("%s query staking pool %v", f.SimcliBinary, f.Flags())
|
2020-05-01 13:16:17 -07:00
|
|
|
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-06-12 07:54:37 -07:00
|
|
|
var pool types.Pool
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
err := f.Cdc.UnmarshalJSON([]byte(out), &pool)
|
|
|
|
require.NoError(f.T, err, "out %v\n, err %v", out, err)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-04-29 08:52:30 -07:00
|
|
|
return pool
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryStakingParameters is simcli query staking parameters
|
2020-06-12 07:54:37 -07:00
|
|
|
func QueryStakingParameters(f *cli.Fixtures, flags ...string) types.Params {
|
2020-04-29 08:52:30 -07:00
|
|
|
cmd := fmt.Sprintf("%s query staking params %v", f.SimcliBinary, f.Flags())
|
2020-05-01 13:16:17 -07:00
|
|
|
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-06-12 07:54:37 -07:00
|
|
|
var params types.Params
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
err := f.Cdc.UnmarshalJSON([]byte(out), ¶ms)
|
|
|
|
require.NoError(f.T, err, "out %v\n, err %v", out, err)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-04-29 08:52:30 -07:00
|
|
|
return params
|
|
|
|
}
|