2023-03-07 22:37:45 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-06-06 16:40:17 +00:00
|
|
|
"math/big"
|
2023-03-07 22:37:45 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-06-06 21:51:42 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2024-02-06 22:54:10 +00:00
|
|
|
tmclient "github.com/cometbft/cometbft/rpc/client"
|
|
|
|
coretypes "github.com/cometbft/cometbft/rpc/core/types"
|
2023-03-07 22:37:45 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
2023-10-31 23:37:40 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2023-03-07 22:37:45 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2023-11-02 00:22:38 +00:00
|
|
|
authz "github.com/cosmos/cosmos-sdk/x/authz"
|
2023-03-07 22:37:45 +00:00
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2023-10-31 23:37:40 +00:00
|
|
|
evmhd "github.com/evmos/ethermint/crypto/hd"
|
2023-06-06 21:51:42 +00:00
|
|
|
|
2023-06-06 16:40:17 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
2023-04-03 16:58:45 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-03-07 22:37:45 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
2023-06-06 21:51:42 +00:00
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
kavaparams "github.com/kava-labs/kava/app/params"
|
2023-12-13 17:17:37 +00:00
|
|
|
"github.com/kava-labs/kava/client/grpc"
|
2023-03-07 22:37:45 +00:00
|
|
|
"github.com/kava-labs/kava/tests/e2e/runner"
|
|
|
|
"github.com/kava-labs/kava/tests/util"
|
2023-11-02 00:22:38 +00:00
|
|
|
cdptypes "github.com/kava-labs/kava/x/cdp/types"
|
|
|
|
committeetypes "github.com/kava-labs/kava/x/committee/types"
|
|
|
|
communitytypes "github.com/kava-labs/kava/x/community/types"
|
|
|
|
earntypes "github.com/kava-labs/kava/x/earn/types"
|
|
|
|
evmutiltypes "github.com/kava-labs/kava/x/evmutil/types"
|
|
|
|
incentivetypes "github.com/kava-labs/kava/x/incentive/types"
|
|
|
|
kavadisttypes "github.com/kava-labs/kava/x/kavadist/types"
|
2023-03-07 22:37:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Chain wraps query clients & accounts for a network
|
|
|
|
type Chain struct {
|
2023-04-06 17:51:13 +00:00
|
|
|
accounts map[string]*SigningAccount
|
|
|
|
t *testing.T
|
2023-03-07 22:37:45 +00:00
|
|
|
|
2023-03-17 18:30:23 +00:00
|
|
|
StakingDenom string
|
2023-10-31 23:37:40 +00:00
|
|
|
ChainID string
|
|
|
|
Keyring keyring.Keyring
|
2023-03-07 22:37:45 +00:00
|
|
|
|
2023-04-03 16:58:45 +00:00
|
|
|
EvmClient *ethclient.Client
|
|
|
|
ContractAddrs map[string]common.Address
|
2023-06-30 05:30:02 +00:00
|
|
|
erc20s map[common.Address]struct{}
|
2023-03-28 22:32:36 +00:00
|
|
|
|
2023-04-06 17:51:13 +00:00
|
|
|
EncodingConfig kavaparams.EncodingConfig
|
|
|
|
|
2023-11-02 00:22:38 +00:00
|
|
|
Auth authtypes.QueryClient
|
|
|
|
Authz authz.QueryClient
|
|
|
|
Bank banktypes.QueryClient
|
|
|
|
Cdp cdptypes.QueryClient
|
|
|
|
Committee committeetypes.QueryClient
|
|
|
|
Community communitytypes.QueryClient
|
|
|
|
Distribution distrtypes.QueryClient
|
|
|
|
Incentive incentivetypes.QueryClient
|
|
|
|
Kavadist kavadisttypes.QueryClient
|
|
|
|
Earn earntypes.QueryClient
|
|
|
|
Evm evmtypes.QueryClient
|
|
|
|
Evmutil evmutiltypes.QueryClient
|
|
|
|
Gov govv1types.QueryClient
|
|
|
|
Mint minttypes.QueryClient
|
|
|
|
Staking stakingtypes.QueryClient
|
|
|
|
Tm tmservice.ServiceClient
|
|
|
|
Tx txtypes.ServiceClient
|
|
|
|
Upgrade upgradetypes.QueryClient
|
|
|
|
|
2023-10-31 23:37:40 +00:00
|
|
|
TmSignClient tmclient.SignClient
|
2023-12-13 17:17:37 +00:00
|
|
|
|
|
|
|
Grpc *grpc.KavaGrpcClient
|
2023-03-07 22:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewChain creates the query clients & signing account management for a chain run on a set of ports.
|
|
|
|
// A signing client for the fundedAccountMnemonic is initialized. This account is referred to in the
|
|
|
|
// code as "whale" and it is used to supply funds to all new accounts.
|
|
|
|
func NewChain(t *testing.T, details *runner.ChainDetails, fundedAccountMnemonic string) (*Chain, error) {
|
2023-03-17 18:30:23 +00:00
|
|
|
chain := &Chain{
|
2023-04-03 16:58:45 +00:00
|
|
|
t: t,
|
|
|
|
StakingDenom: details.StakingDenom,
|
2023-10-31 23:37:40 +00:00
|
|
|
ChainID: details.ChainId,
|
2023-04-03 16:58:45 +00:00
|
|
|
ContractAddrs: make(map[string]common.Address),
|
2023-06-30 05:30:02 +00:00
|
|
|
erc20s: make(map[common.Address]struct{}),
|
2023-03-17 18:30:23 +00:00
|
|
|
}
|
2023-04-06 17:51:13 +00:00
|
|
|
chain.EncodingConfig = app.MakeEncodingConfig()
|
2023-03-07 22:37:45 +00:00
|
|
|
|
2023-10-31 23:37:40 +00:00
|
|
|
// setup keyring
|
|
|
|
kr, err := keyring.New(
|
|
|
|
sdk.KeyringServiceName(),
|
|
|
|
keyring.BackendTest,
|
|
|
|
util.KavaHomePath(),
|
|
|
|
nil,
|
|
|
|
chain.EncodingConfig.Marshaler,
|
|
|
|
evmhd.EthSecp256k1Option(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chain.Keyring = kr
|
|
|
|
|
2023-12-13 17:17:37 +00:00
|
|
|
client, err := grpc.NewClient(details.GrpcUrl)
|
2023-03-07 22:37:45 +00:00
|
|
|
if err != nil {
|
2023-12-13 17:17:37 +00:00
|
|
|
chain.t.Fatalf("failed to create kava grpc client: %s", err)
|
2023-03-07 22:37:45 +00:00
|
|
|
}
|
2023-12-13 17:17:37 +00:00
|
|
|
chain.Grpc = client
|
2023-03-07 22:37:45 +00:00
|
|
|
|
2023-06-20 16:29:25 +00:00
|
|
|
chain.EvmClient, err = details.EvmClient()
|
2023-03-07 22:37:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return chain, err
|
|
|
|
}
|
|
|
|
|
2023-10-31 23:37:40 +00:00
|
|
|
chain.TmSignClient, err = details.RpcConn()
|
|
|
|
if err != nil {
|
|
|
|
return chain, err
|
|
|
|
}
|
|
|
|
|
2023-11-02 00:22:38 +00:00
|
|
|
chain.Auth = authtypes.NewQueryClient(grpcConn)
|
|
|
|
chain.Authz = authz.NewQueryClient(grpcConn)
|
|
|
|
chain.Bank = banktypes.NewQueryClient(grpcConn)
|
|
|
|
chain.Cdp = cdptypes.NewQueryClient(grpcConn)
|
|
|
|
chain.Committee = committeetypes.NewQueryClient(grpcConn)
|
|
|
|
chain.Community = communitytypes.NewQueryClient(grpcConn)
|
|
|
|
chain.Distribution = distrtypes.NewQueryClient(grpcConn)
|
|
|
|
chain.Incentive = incentivetypes.NewQueryClient(grpcConn)
|
|
|
|
chain.Kavadist = kavadisttypes.NewQueryClient(grpcConn)
|
|
|
|
chain.Earn = earntypes.NewQueryClient(grpcConn)
|
|
|
|
chain.Evm = evmtypes.NewQueryClient(grpcConn)
|
|
|
|
chain.Evmutil = evmutiltypes.NewQueryClient(grpcConn)
|
|
|
|
chain.Gov = govv1types.NewQueryClient(grpcConn)
|
|
|
|
chain.Mint = minttypes.NewQueryClient(grpcConn)
|
|
|
|
chain.Staking = stakingtypes.NewQueryClient(grpcConn)
|
|
|
|
chain.Tm = tmservice.NewServiceClient(grpcConn)
|
|
|
|
chain.Tx = txtypes.NewServiceClient(grpcConn)
|
|
|
|
chain.Upgrade = upgradetypes.NewQueryClient(grpcConn)
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
// initialize accounts map
|
|
|
|
chain.accounts = make(map[string]*SigningAccount)
|
|
|
|
// setup the signing account for the initially funded account (used to fund all other accounts)
|
|
|
|
whale := chain.AddNewSigningAccount(
|
|
|
|
FundedAccountName,
|
|
|
|
hd.CreateHDPath(Bip44CoinType, 0, 0),
|
2023-10-31 23:37:40 +00:00
|
|
|
chain.ChainID,
|
2023-03-07 22:37:45 +00:00
|
|
|
fundedAccountMnemonic,
|
|
|
|
)
|
|
|
|
|
|
|
|
// check that funded account is actually funded.
|
2023-10-31 23:37:40 +00:00
|
|
|
fmt.Printf("[%s] account used for funding (%s) address: %s\n", chain.ChainID, FundedAccountName, whale.SdkAddress)
|
2023-03-07 22:37:45 +00:00
|
|
|
whaleFunds := chain.QuerySdkForBalances(whale.SdkAddress)
|
|
|
|
if whaleFunds.IsZero() {
|
|
|
|
chain.t.Fatal("funded account mnemonic is for account with no funds")
|
|
|
|
}
|
|
|
|
|
|
|
|
return chain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown performs closes all the account request channels for this chain.
|
|
|
|
func (chain *Chain) Shutdown() {
|
|
|
|
// close all account request channels
|
|
|
|
for _, a := range chain.accounts {
|
|
|
|
close(a.sdkReqChan)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-30 05:30:02 +00:00
|
|
|
// ReturnAllFunds loops through all SigningAccounts and sends all their funds back to the
|
|
|
|
// initially funded account.
|
|
|
|
func (chain *Chain) ReturnAllFunds() {
|
|
|
|
whale := chain.GetAccount(FundedAccountName)
|
|
|
|
fmt.Println(chain.erc20s)
|
|
|
|
for _, a := range chain.accounts {
|
|
|
|
if a.SdkAddress.String() != whale.SdkAddress.String() {
|
|
|
|
// NOTE: assumes all cosmos coin conversion funds have been converted back to sdk.
|
|
|
|
|
|
|
|
// return all erc20 balance
|
|
|
|
for erc20Addr := range chain.erc20s {
|
|
|
|
erc20Bal := chain.GetErc20Balance(erc20Addr, a.EvmAddress)
|
|
|
|
// if account has no balance, do nothing
|
|
|
|
if erc20Bal.Cmp(big.NewInt(0)) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err := a.TransferErc20(erc20Addr, whale.EvmAddress, erc20Bal)
|
|
|
|
if err != nil {
|
|
|
|
a.l.Printf("FAILED TO RETURN ERC20 FUNDS (contract: %s, balance: %d): %s\n",
|
|
|
|
erc20Addr, erc20Bal, err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get sdk balance of account
|
|
|
|
balance := chain.QuerySdkForBalances(a.SdkAddress)
|
|
|
|
// assumes 200,000 gas w/ min fee of .001
|
|
|
|
gas := sdk.NewInt64Coin(chain.StakingDenom, 200)
|
|
|
|
|
|
|
|
// ensure they have enough gas to return funds
|
|
|
|
if balance.AmountOf(chain.StakingDenom).LT(gas.Amount) {
|
|
|
|
a.l.Printf("ACCOUNT LACKS GAS MONEY TO RETURN FUNDS: %s\n", balance)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// send it all back (minus gas) to the whale!
|
|
|
|
res := a.BankSend(whale.SdkAddress, balance.Sub(gas))
|
|
|
|
if res.Err != nil {
|
|
|
|
a.l.Printf("failed to return funds: %s\n", res.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterErc20 is a method to record the address of erc20s on this chain.
|
|
|
|
// The full balances of each registered erc20 will be returned to the funded
|
|
|
|
// account when ReturnAllFunds is called.
|
|
|
|
func (chain *Chain) RegisterErc20(address common.Address) {
|
|
|
|
chain.erc20s[address] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
// QuerySdkForBalances gets the balance of a particular address on this Chain.
|
|
|
|
func (chain *Chain) QuerySdkForBalances(addr sdk.AccAddress) sdk.Coins {
|
2023-12-13 17:17:37 +00:00
|
|
|
res, err := chain.Grpc.Query.Bank.AllBalances(context.Background(), &banktypes.QueryAllBalancesRequest{
|
2023-03-07 22:37:45 +00:00
|
|
|
Address: addr.String(),
|
|
|
|
})
|
|
|
|
require.NoError(chain.t, err)
|
|
|
|
return res.Balances
|
|
|
|
}
|
2023-06-05 20:22:23 +00:00
|
|
|
|
2023-11-02 00:22:38 +00:00
|
|
|
// QuerySdkForBalancesAtHeight gets the balance of a particular address on this Chain, at the specified height.
|
|
|
|
func (chain *Chain) QuerySdkForBalancesAtHeight(
|
|
|
|
height int64,
|
|
|
|
addr sdk.AccAddress,
|
|
|
|
) sdk.Coins {
|
|
|
|
res, err := chain.Bank.AllBalances(
|
|
|
|
util.CtxAtHeight(height),
|
|
|
|
&banktypes.QueryAllBalancesRequest{
|
|
|
|
Address: addr.String(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(chain.t, err)
|
|
|
|
return res.Balances
|
|
|
|
}
|
|
|
|
|
2023-06-05 20:22:23 +00:00
|
|
|
// GetModuleBalances returns the balance of a requested module account
|
|
|
|
func (chain *Chain) GetModuleBalances(moduleName string) sdk.Coins {
|
|
|
|
addr := authtypes.NewModuleAddress(moduleName)
|
|
|
|
return chain.QuerySdkForBalances(addr)
|
|
|
|
}
|
2023-06-06 16:40:17 +00:00
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// GetErc20Balance fetches the ERC20 balance of `contract` for `address`.
|
2023-06-06 16:40:17 +00:00
|
|
|
func (chain *Chain) GetErc20Balance(contract, address common.Address) *big.Int {
|
|
|
|
resData, err := chain.EvmClient.CallContract(context.Background(), ethereum.CallMsg{
|
|
|
|
To: &contract,
|
|
|
|
Data: util.BuildErc20BalanceOfCallData(address),
|
|
|
|
}, nil)
|
|
|
|
require.NoError(chain.t, err)
|
|
|
|
|
|
|
|
return new(big.Int).SetBytes(resData)
|
|
|
|
}
|
2023-10-31 23:37:40 +00:00
|
|
|
|
|
|
|
func (chain *Chain) GetBeginBlockEventsFromQuery(
|
|
|
|
ctx context.Context,
|
|
|
|
query string,
|
|
|
|
) (sdk.StringEvents, int64, error) {
|
|
|
|
// 1) Block search to find auction_start event and corresponding height
|
|
|
|
// https://rpc.kava.io/block_search?query=%22auction_start.auction_id=16837%22
|
|
|
|
|
|
|
|
blocks, err := chain.QueryBlock(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(blocks) == 0 {
|
|
|
|
return nil, 0, fmt.Errorf("no blocks found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2) Block results to query events from height
|
|
|
|
// https://rpc.kava.io/block_results?height=3146803
|
|
|
|
events, err := chain.GetBeginBlockEvents(ctx, blocks[0].Block.Height)
|
|
|
|
return events, blocks[0].Block.Height, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (chain *Chain) QueryBlock(ctx context.Context, query string) ([]*coretypes.ResultBlock, error) {
|
|
|
|
page := 1
|
|
|
|
perPage := 100
|
|
|
|
|
|
|
|
res, err := chain.TmSignClient.BlockSearch(
|
|
|
|
ctx,
|
|
|
|
query,
|
|
|
|
&page,
|
|
|
|
&perPage,
|
|
|
|
"desc",
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed BlockSearch: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Blocks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (chain *Chain) GetBeginBlockEvents(ctx context.Context, height int64) (sdk.StringEvents, error) {
|
|
|
|
res, err := chain.TmSignClient.BlockResults(
|
|
|
|
ctx,
|
|
|
|
&height,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed BlockResults: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not use sdk.StringifyEvents as it flattens events which makes it
|
|
|
|
// more difficult to parse.
|
|
|
|
strEvents := make(sdk.StringEvents, 0, len(res.BeginBlockEvents))
|
|
|
|
for _, e := range res.BeginBlockEvents {
|
|
|
|
strEvents = append(strEvents, sdk.StringifyEvent(e))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strEvents, nil
|
|
|
|
}
|