0g-chain/tests/e2e/e2e_test.go

110 lines
3.6 KiB
Go
Raw Normal View History

package e2e_test
import (
"context"
"math/big"
"testing"
"github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
ethtypes "github.com/ethereum/go-ethereum/core/types"
emtypes "github.com/tharsis/ethermint/types"
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/tests/e2e/testutil"
"github.com/kava-labs/kava/tests/util"
)
var (
minEvmGasPrice = big.NewInt(1e10) // akava
)
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))
}
// example test that queries kava via SDK and EVM
func (suite *IntegrationTestSuite) TestChainID() {
// TODO: make chain agnostic, don't hardcode expected chain ids (in testutil)
expectedEvmNetworkId, err := emtypes.ParseChainID(testutil.ChainId)
suite.NoError(err)
// EVM query
evmNetworkId, err := suite.EvmClient.NetworkID(context.Background())
suite.NoError(err)
suite.Equal(expectedEvmNetworkId, evmNetworkId)
// SDK query
nodeInfo, err := suite.Tm.GetNodeInfo(context.Background(), &tmservice.GetNodeInfoRequest{})
suite.NoError(err)
suite.Equal(testutil.ChainId, nodeInfo.DefaultNodeInfo.Network)
}
// example test that funds a new account & queries its balance
func (suite *IntegrationTestSuite) TestFundedAccount() {
funds := ukava(1e7)
acc := suite.NewFundedAccount("example-acc", sdk.NewCoins(funds))
// 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
res, err := suite.Bank.Balance(context.Background(), banktypes.NewQueryBalanceRequest(
acc.SdkAddress, "ukava",
))
suite.NoError(err)
suite.Equal(funds, *res.Balance)
// check balance via EVM query
akavaBal, err := suite.EvmClient.BalanceAt(context.Background(), acc.EvmAddress, nil)
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
acc := suite.NewFundedAccount("evm-test-transfer", sdk.NewCoins(initialFunds))
// get a rando account to send kava to
randomAddr := app.RandomAddress()
to := util.SdkToEvmAddress(randomAddr)
// example fetching of nonce (account sequence)
nonce, err := suite.EvmClient.PendingNonceAt(context.Background(), acc.EvmAddress)
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.
balance := suite.QuerySdkForBalances(acc.SdkAddress)
suite.Equal(sdk.NewInt(9e6).Sub(ukavaUsedForGas), balance.AmountOf("ukava"))
}