2023-02-22 23:40:56 +00:00
|
|
|
package e2e_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
2023-03-07 22:37:45 +00:00
|
|
|
"strings"
|
2023-02-22 23:40:56 +00:00
|
|
|
"testing"
|
2023-03-07 22:37:45 +00:00
|
|
|
"time"
|
2023-02-22 23:40:56 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
|
2023-03-02 01:05:53 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2023-02-22 23:40:56 +00:00
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2023-03-07 22:37:45 +00:00
|
|
|
ibctypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
|
|
|
|
ibcclienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
|
2023-03-02 01:05:53 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
emtypes "github.com/tharsis/ethermint/types"
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-03-02 01:05:53 +00:00
|
|
|
"github.com/kava-labs/kava/app"
|
2023-02-22 23:40:56 +00:00
|
|
|
"github.com/kava-labs/kava/tests/e2e/testutil"
|
2023-03-02 01:05:53 +00:00
|
|
|
"github.com/kava-labs/kava/tests/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
minEvmGasPrice = big.NewInt(1e10) // akava
|
2023-02-22 23:40:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ukava(amt int64) sdk.Coin {
|
|
|
|
return sdk.NewCoin("ukava", sdk.NewInt(amt))
|
|
|
|
}
|
|
|
|
|
|
|
|
type IntegrationTestSuite struct {
|
|
|
|
testutil.E2eTestSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntegrationTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(IntegrationTestSuite))
|
|
|
|
}
|
|
|
|
|
2023-03-02 01:05:53 +00:00
|
|
|
// example test that queries kava via SDK and EVM
|
2023-02-22 23:40:56 +00:00
|
|
|
func (suite *IntegrationTestSuite) TestChainID() {
|
2023-03-17 18:30:23 +00:00
|
|
|
expectedEvmNetworkId, err := emtypes.ParseChainID(suite.Kava.ChainId)
|
2023-03-02 01:05:53 +00:00
|
|
|
suite.NoError(err)
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-03-02 01:05:53 +00:00
|
|
|
// EVM query
|
2023-03-07 22:37:45 +00:00
|
|
|
evmNetworkId, err := suite.Kava.EvmClient.NetworkID(context.Background())
|
2023-02-22 23:40:56 +00:00
|
|
|
suite.NoError(err)
|
2023-03-02 01:05:53 +00:00
|
|
|
suite.Equal(expectedEvmNetworkId, evmNetworkId)
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-03-02 01:05:53 +00:00
|
|
|
// SDK query
|
2023-03-07 22:37:45 +00:00
|
|
|
nodeInfo, err := suite.Kava.Tm.GetNodeInfo(context.Background(), &tmservice.GetNodeInfoRequest{})
|
2023-02-22 23:40:56 +00:00
|
|
|
suite.NoError(err)
|
2023-03-17 18:30:23 +00:00
|
|
|
suite.Equal(suite.Kava.ChainId, nodeInfo.DefaultNodeInfo.Network)
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// example test that funds a new account & queries its balance
|
|
|
|
func (suite *IntegrationTestSuite) TestFundedAccount() {
|
|
|
|
funds := ukava(1e7)
|
2023-03-07 22:37:45 +00:00
|
|
|
acc := suite.Kava.NewFundedAccount("example-acc", sdk.NewCoins(funds))
|
2023-03-02 01:05:53 +00:00
|
|
|
|
|
|
|
// check that the sdk & evm signers are for the same account
|
|
|
|
suite.Equal(acc.SdkAddress.String(), util.EvmToSdkAddress(acc.EvmAddress).String())
|
|
|
|
suite.Equal(acc.EvmAddress.Hex(), util.SdkToEvmAddress(acc.SdkAddress).Hex())
|
|
|
|
|
|
|
|
// check balance via SDK query
|
2023-03-07 22:37:45 +00:00
|
|
|
res, err := suite.Kava.Bank.Balance(context.Background(), banktypes.NewQueryBalanceRequest(
|
2023-03-02 01:05:53 +00:00
|
|
|
acc.SdkAddress, "ukava",
|
2023-02-22 23:40:56 +00:00
|
|
|
))
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal(funds, *res.Balance)
|
2023-03-02 01:05:53 +00:00
|
|
|
|
|
|
|
// check balance via EVM query
|
2023-03-07 22:37:45 +00:00
|
|
|
akavaBal, err := suite.Kava.EvmClient.BalanceAt(context.Background(), acc.EvmAddress, nil)
|
2023-03-02 01:05:53 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal(funds.Amount.MulRaw(1e12).BigInt(), akavaBal)
|
|
|
|
}
|
|
|
|
|
|
|
|
// example test that signs & broadcasts an EVM tx
|
|
|
|
func (suite *IntegrationTestSuite) TestTransferOverEVM() {
|
|
|
|
// fund an account that can perform the transfer
|
|
|
|
initialFunds := ukava(1e7) // 10 KAVA
|
2023-03-07 22:37:45 +00:00
|
|
|
acc := suite.Kava.NewFundedAccount("evm-test-transfer", sdk.NewCoins(initialFunds))
|
2023-03-02 01:05:53 +00:00
|
|
|
|
|
|
|
// get a rando account to send kava to
|
|
|
|
randomAddr := app.RandomAddress()
|
|
|
|
to := util.SdkToEvmAddress(randomAddr)
|
|
|
|
|
|
|
|
// example fetching of nonce (account sequence)
|
2023-03-07 22:37:45 +00:00
|
|
|
nonce, err := suite.Kava.EvmClient.PendingNonceAt(context.Background(), acc.EvmAddress)
|
2023-03-02 01:05:53 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal(uint64(0), nonce) // sanity check. the account should have no prior txs
|
|
|
|
|
|
|
|
// transfer kava over EVM
|
|
|
|
kavaToTransfer := big.NewInt(1e18) // 1 KAVA; akava has 18 decimals.
|
|
|
|
req := util.EvmTxRequest{
|
|
|
|
Tx: ethtypes.NewTransaction(nonce, to, kavaToTransfer, 1e5, minEvmGasPrice, nil),
|
|
|
|
Data: "any ol' data to track this through the system",
|
|
|
|
}
|
|
|
|
res := acc.SignAndBroadcastEvmTx(req)
|
|
|
|
suite.NoError(res.Err)
|
|
|
|
suite.Equal(ethtypes.ReceiptStatusSuccessful, res.Receipt.Status)
|
|
|
|
|
|
|
|
// evm txs refund unused gas. so to know the expected balance we need to know how much gas was used.
|
|
|
|
ukavaUsedForGas := sdk.NewIntFromBigInt(minEvmGasPrice).
|
|
|
|
Mul(sdk.NewIntFromUint64(res.Receipt.GasUsed)).
|
|
|
|
QuoRaw(1e12) // convert akava to ukava
|
|
|
|
|
|
|
|
// expect (9 - gas used) KAVA remaining in account.
|
2023-03-07 22:37:45 +00:00
|
|
|
balance := suite.Kava.QuerySdkForBalances(acc.SdkAddress)
|
2023-03-02 01:05:53 +00:00
|
|
|
suite.Equal(sdk.NewInt(9e6).Sub(ukavaUsedForGas), balance.AmountOf("ukava"))
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
2023-03-07 22:37:45 +00:00
|
|
|
|
|
|
|
// TestIbcTransfer transfers KAVA from the primary kava chain (suite.Kava) to the ibc chain (suite.Ibc).
|
|
|
|
// Note that because the IBC chain also runs kava's binary, this tests both the sending & receiving.
|
|
|
|
func (suite *IntegrationTestSuite) TestIbcTransfer() {
|
|
|
|
suite.SkipIfIbcDisabled()
|
|
|
|
|
|
|
|
// ARRANGE
|
|
|
|
// setup kava account
|
|
|
|
funds := ukava(1e7) // 10 KAVA
|
|
|
|
kavaAcc := suite.Kava.NewFundedAccount("ibc-transfer-kava-side", sdk.NewCoins(funds))
|
|
|
|
// setup ibc account
|
|
|
|
ibcAcc := suite.Ibc.NewFundedAccount("ibc-transfer-ibc-side", sdk.NewCoins())
|
|
|
|
|
|
|
|
gasLimit := int64(2e5)
|
|
|
|
fee := ukava(7500)
|
|
|
|
|
|
|
|
fundsToSend := ukava(5e6) // 5 KAVA
|
|
|
|
transferMsg := ibctypes.NewMsgTransfer(
|
|
|
|
testutil.IbcPort,
|
|
|
|
testutil.IbcChannel,
|
|
|
|
fundsToSend,
|
|
|
|
kavaAcc.SdkAddress.String(),
|
|
|
|
ibcAcc.SdkAddress.String(),
|
|
|
|
ibcclienttypes.NewHeight(0, 0), // timeout height disabled when 0
|
|
|
|
uint64(time.Now().Add(30*time.Second).UnixNano()),
|
|
|
|
)
|
|
|
|
// initial - sent - fee
|
|
|
|
expectedSrcBalance := funds.Sub(fundsToSend).Sub(fee)
|
|
|
|
|
|
|
|
// ACT
|
|
|
|
// IBC transfer from kava -> ibc
|
|
|
|
transferTo := util.KavaMsgRequest{
|
|
|
|
Msgs: []sdk.Msg{transferMsg},
|
|
|
|
GasLimit: uint64(gasLimit),
|
|
|
|
FeeAmount: sdk.NewCoins(fee),
|
|
|
|
Memo: "sent from Kava!",
|
|
|
|
}
|
|
|
|
res := kavaAcc.SignAndBroadcastKavaTx(transferTo)
|
|
|
|
|
|
|
|
// ASSERT
|
|
|
|
suite.NoError(res.Err)
|
|
|
|
|
|
|
|
// the balance should be deducted from kava account
|
|
|
|
suite.Eventually(func() bool {
|
|
|
|
balance := suite.Kava.QuerySdkForBalances(kavaAcc.SdkAddress)
|
|
|
|
return balance.AmountOf("ukava").Equal(expectedSrcBalance.Amount)
|
|
|
|
}, 10*time.Second, 1*time.Second)
|
|
|
|
|
|
|
|
// expect the balance to be transferred to the ibc chain!
|
|
|
|
suite.Eventually(func() bool {
|
|
|
|
balance := suite.Ibc.QuerySdkForBalances(ibcAcc.SdkAddress)
|
|
|
|
found := false
|
|
|
|
for _, c := range balance {
|
|
|
|
// find the ibc denom coin
|
|
|
|
if strings.HasPrefix(c.Denom, "ibc/") {
|
|
|
|
suite.Equal(fundsToSend.Amount, c.Amount)
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}, 10*time.Second, 1*time.Second)
|
|
|
|
}
|