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"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2023-02-22 23:40:56 +00:00
|
|
|
"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"
|
2024-02-06 22:54:10 +00:00
|
|
|
ibctypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
|
|
|
|
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
|
2023-03-02 01:05:53 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2023-04-04 00:08:45 +00:00
|
|
|
emtypes "github.com/evmos/ethermint/types"
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2024-08-03 09:51:56 +00:00
|
|
|
"github.com/0glabs/0g-chain/app"
|
2024-08-03 13:44:37 +00:00
|
|
|
"github.com/0glabs/0g-chain/chaincfg"
|
2024-08-03 09:51:56 +00:00
|
|
|
"github.com/0glabs/0g-chain/tests/e2e/testutil"
|
|
|
|
"github.com/0glabs/0g-chain/tests/util"
|
2023-03-02 01:05:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-08-03 13:44:37 +00:00
|
|
|
minEvmGasPrice = big.NewInt(1e10) // neuron
|
2023-02-22 23:40:56 +00:00
|
|
|
)
|
|
|
|
|
2024-08-03 13:44:37 +00:00
|
|
|
func a0gi(amt *big.Int) sdk.Coin {
|
|
|
|
return sdk.NewCoin(chaincfg.DisplayDenom, sdkmath.NewIntFromBigInt(amt))
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IntegrationTestSuite struct {
|
|
|
|
testutil.E2eTestSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntegrationTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(IntegrationTestSuite))
|
|
|
|
}
|
|
|
|
|
2024-08-03 13:44:37 +00:00
|
|
|
// example test that queries 0gchain via SDK and EVM
|
2023-02-22 23:40:56 +00:00
|
|
|
func (suite *IntegrationTestSuite) TestChainID() {
|
2024-08-03 13:44:37 +00:00
|
|
|
expectedEvmNetworkId, err := emtypes.ParseChainID(suite.ZgChain.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
|
2024-08-03 13:44:37 +00:00
|
|
|
evmNetworkId, err := suite.ZgChain.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
|
2024-08-03 13:44:37 +00:00
|
|
|
nodeInfo, err := suite.ZgChain.Grpc.Query.Tm.GetNodeInfo(context.Background(), &tmservice.GetNodeInfoRequest{})
|
2023-02-22 23:40:56 +00:00
|
|
|
suite.NoError(err)
|
2024-08-03 13:44:37 +00:00
|
|
|
suite.Equal(suite.ZgChain.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() {
|
2024-08-03 13:44:37 +00:00
|
|
|
funds := a0gi(big.NewInt(1e3))
|
|
|
|
acc := suite.ZgChain.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
|
2024-08-03 13:44:37 +00:00
|
|
|
res, err := suite.ZgChain.Grpc.Query.Bank.Balance(context.Background(), banktypes.NewQueryBalanceRequest(
|
|
|
|
acc.SdkAddress, chaincfg.DisplayDenom,
|
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
|
2024-08-03 13:44:37 +00:00
|
|
|
neuronBal, err := suite.ZgChain.EvmClient.BalanceAt(context.Background(), acc.EvmAddress, nil)
|
2023-03-02 01:05:53 +00:00
|
|
|
suite.NoError(err)
|
2024-08-03 13:44:37 +00:00
|
|
|
suite.Equal(funds.Amount.MulRaw(1e12).BigInt(), neuronBal)
|
2023-03-02 01:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// example test that signs & broadcasts an EVM tx
|
|
|
|
func (suite *IntegrationTestSuite) TestTransferOverEVM() {
|
|
|
|
// fund an account that can perform the transfer
|
2024-08-03 13:44:37 +00:00
|
|
|
initialFunds := a0gi(big.NewInt(1e6)) // 1 A0GI
|
|
|
|
acc := suite.ZgChain.NewFundedAccount("evm-test-transfer", sdk.NewCoins(initialFunds))
|
2023-03-02 01:05:53 +00:00
|
|
|
|
2024-08-03 13:44:37 +00:00
|
|
|
// get a rando account to send 0gchain to
|
2023-03-02 01:05:53 +00:00
|
|
|
randomAddr := app.RandomAddress()
|
|
|
|
to := util.SdkToEvmAddress(randomAddr)
|
|
|
|
|
|
|
|
// example fetching of nonce (account sequence)
|
2024-08-03 13:44:37 +00:00
|
|
|
nonce, err := suite.ZgChain.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
|
|
|
|
|
2024-08-03 13:44:37 +00:00
|
|
|
// transfer a0gi over EVM
|
|
|
|
a0giToTransfer := big.NewInt(1e17) // .1 A0GI; neuron has 18 decimals.
|
2023-03-02 01:05:53 +00:00
|
|
|
req := util.EvmTxRequest{
|
2024-08-03 13:44:37 +00:00
|
|
|
Tx: ethtypes.NewTransaction(nonce, to, a0giToTransfer, 1e5, minEvmGasPrice, nil),
|
2023-03-02 01:05:53 +00:00
|
|
|
Data: "any ol' data to track this through the system",
|
|
|
|
}
|
|
|
|
res := acc.SignAndBroadcastEvmTx(req)
|
2023-10-31 23:37:40 +00:00
|
|
|
suite.Require().NoError(res.Err)
|
2023-03-02 01:05:53 +00:00
|
|
|
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.
|
2024-08-03 13:44:37 +00:00
|
|
|
a0giUsedForGas := sdkmath.NewIntFromBigInt(minEvmGasPrice).
|
2023-04-05 23:21:59 +00:00
|
|
|
Mul(sdkmath.NewIntFromUint64(res.Receipt.GasUsed)).
|
2024-08-03 13:44:37 +00:00
|
|
|
QuoRaw(1e12) // convert neuron to a0gi
|
2023-03-02 01:05:53 +00:00
|
|
|
|
2024-08-03 13:44:37 +00:00
|
|
|
// expect (9 - gas used) A0GI remaining in account.
|
|
|
|
balance := suite.ZgChain.QuerySdkForBalances(acc.SdkAddress)
|
|
|
|
suite.Equal(sdkmath.NewInt(9e5).Sub(a0giUsedForGas), balance.AmountOf(chaincfg.DisplayDenom))
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
2023-03-07 22:37:45 +00:00
|
|
|
|
2024-08-03 13:44:37 +00:00
|
|
|
// TestIbcTransfer transfers A0GI from the primary 0g-chain (suite.ZgChain) to the ibc chain (suite.Ibc).
|
|
|
|
// Note that because the IBC chain also runs 0g-chain's binary, this tests both the sending & receiving.
|
2023-03-07 22:37:45 +00:00
|
|
|
func (suite *IntegrationTestSuite) TestIbcTransfer() {
|
|
|
|
suite.SkipIfIbcDisabled()
|
|
|
|
|
|
|
|
// ARRANGE
|
2024-08-03 13:44:37 +00:00
|
|
|
// setup 0g-chain account
|
|
|
|
funds := a0gi(big.NewInt(1e5)) // .1 A0GI
|
|
|
|
zgChainAcc := suite.ZgChain.NewFundedAccount("ibc-transfer-0g-side", sdk.NewCoins(funds))
|
2023-03-07 22:37:45 +00:00
|
|
|
// setup ibc account
|
|
|
|
ibcAcc := suite.Ibc.NewFundedAccount("ibc-transfer-ibc-side", sdk.NewCoins())
|
|
|
|
|
|
|
|
gasLimit := int64(2e5)
|
2024-08-03 13:44:37 +00:00
|
|
|
fee := a0gi(big.NewInt(200))
|
2023-03-07 22:37:45 +00:00
|
|
|
|
2024-08-03 13:44:37 +00:00
|
|
|
fundsToSend := a0gi(big.NewInt(5e4)) // .005 A0GI
|
2023-03-07 22:37:45 +00:00
|
|
|
transferMsg := ibctypes.NewMsgTransfer(
|
|
|
|
testutil.IbcPort,
|
|
|
|
testutil.IbcChannel,
|
|
|
|
fundsToSend,
|
2024-08-03 13:44:37 +00:00
|
|
|
zgChainAcc.SdkAddress.String(),
|
2023-03-07 22:37:45 +00:00
|
|
|
ibcAcc.SdkAddress.String(),
|
|
|
|
ibcclienttypes.NewHeight(0, 0), // timeout height disabled when 0
|
|
|
|
uint64(time.Now().Add(30*time.Second).UnixNano()),
|
2023-04-04 00:08:45 +00:00
|
|
|
"",
|
2023-03-07 22:37:45 +00:00
|
|
|
)
|
|
|
|
// initial - sent - fee
|
|
|
|
expectedSrcBalance := funds.Sub(fundsToSend).Sub(fee)
|
|
|
|
|
|
|
|
// ACT
|
2024-08-03 13:44:37 +00:00
|
|
|
// IBC transfer from 0g-chain -> ibc
|
|
|
|
transferTo := util.ZgChainMsgRequest{
|
2023-03-07 22:37:45 +00:00
|
|
|
Msgs: []sdk.Msg{transferMsg},
|
|
|
|
GasLimit: uint64(gasLimit),
|
|
|
|
FeeAmount: sdk.NewCoins(fee),
|
2024-08-03 13:44:37 +00:00
|
|
|
Memo: "sent from ZgChain!",
|
2023-03-07 22:37:45 +00:00
|
|
|
}
|
2024-08-03 13:44:37 +00:00
|
|
|
res := zgChainAcc.SignAndBroadcastZgChainTx(transferTo)
|
2023-03-07 22:37:45 +00:00
|
|
|
|
|
|
|
// ASSERT
|
|
|
|
suite.NoError(res.Err)
|
|
|
|
|
2024-08-03 13:44:37 +00:00
|
|
|
// the balance should be deducted from 0g-chain account
|
2023-03-07 22:37:45 +00:00
|
|
|
suite.Eventually(func() bool {
|
2024-08-03 13:44:37 +00:00
|
|
|
balance := suite.ZgChain.QuerySdkForBalances(zgChainAcc.SdkAddress)
|
|
|
|
return balance.AmountOf(chaincfg.DisplayDenom).Equal(expectedSrcBalance.Amount)
|
2023-03-07 22:37:45 +00:00
|
|
|
}, 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
|
2023-03-31 17:30:37 +00:00
|
|
|
}, 15*time.Second, 1*time.Second)
|
2023-03-07 22:37:45 +00:00
|
|
|
}
|