2023-04-03 16:58:45 +00:00
|
|
|
package testutil
|
|
|
|
|
2023-04-06 17:51:13 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/tests/e2e/contracts/greeter"
|
|
|
|
evmutiltypes "github.com/0glabs/0g-chain/x/evmutil/types"
|
2023-04-06 17:51:13 +00:00
|
|
|
)
|
2023-04-03 16:58:45 +00:00
|
|
|
|
|
|
|
// InitKavaEvmData is run after the chain is running, but before the tests are run.
|
|
|
|
// It is used to initialize some EVM state, such as deploying contracts.
|
|
|
|
func (suite *E2eTestSuite) InitKavaEvmData() {
|
|
|
|
whale := suite.Kava.GetAccount(FundedAccountName)
|
|
|
|
|
2023-04-06 17:51:13 +00:00
|
|
|
// ensure funded account has nonzero erc20 balance
|
2023-06-26 22:03:51 +00:00
|
|
|
balance := suite.Kava.GetErc20Balance(suite.DeployedErc20.Address, whale.EvmAddress)
|
2023-04-06 17:51:13 +00:00
|
|
|
if balance.Cmp(big.NewInt(0)) != 1 {
|
2023-10-31 23:37:40 +00:00
|
|
|
panic(fmt.Sprintf(
|
|
|
|
"expected funded account (%s) to have erc20 balance of token %s",
|
|
|
|
whale.EvmAddress.Hex(),
|
|
|
|
suite.DeployedErc20.Address.Hex(),
|
|
|
|
))
|
2023-04-06 17:51:13 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// expect the erc20 to be enabled for conversion to sdk.Coin
|
|
|
|
params, err := suite.Kava.Evmutil.Params(context.Background(), &evmutiltypes.QueryParamsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to fetch evmutil params during init: %s", err))
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
erc20Addr := suite.DeployedErc20.Address.Hex()
|
|
|
|
for _, p := range params.Params.EnabledConversionPairs {
|
2024-05-01 03:56:00 +00:00
|
|
|
if common.BytesToAddress(p.ZgChainERC20Address).Hex() == erc20Addr {
|
2023-06-26 22:03:51 +00:00
|
|
|
found = true
|
|
|
|
suite.DeployedErc20.CosmosDenom = p.Denom
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("erc20 %s must be enabled for conversion to cosmos coin", erc20Addr))
|
|
|
|
}
|
2023-06-30 05:30:02 +00:00
|
|
|
suite.Kava.RegisterErc20(suite.DeployedErc20.Address)
|
2023-06-26 22:03:51 +00:00
|
|
|
|
2023-04-03 16:58:45 +00:00
|
|
|
// deploy an example contract
|
|
|
|
greeterAddr, _, _, err := greeter.DeployGreeter(
|
|
|
|
whale.evmSigner.Auth,
|
|
|
|
whale.evmSigner.EvmClient,
|
|
|
|
"what's up!",
|
|
|
|
)
|
2023-04-04 21:22:18 +00:00
|
|
|
suite.NoError(err, "failed to deploy a contract to the EVM")
|
2023-04-03 16:58:45 +00:00
|
|
|
suite.Kava.ContractAddrs["greeter"] = greeterAddr
|
|
|
|
}
|
2023-04-06 17:51:13 +00:00
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// FundKavaErc20Balance sends the pre-deployed ERC20 token to the `toAddress`.
|
2023-04-06 17:51:13 +00:00
|
|
|
func (suite *E2eTestSuite) FundKavaErc20Balance(toAddress common.Address, amount *big.Int) EvmTxResponse {
|
|
|
|
// funded account should have erc20 balance
|
|
|
|
whale := suite.Kava.GetAccount(FundedAccountName)
|
2023-06-30 05:30:02 +00:00
|
|
|
res, err := whale.TransferErc20(suite.DeployedErc20.Address, toAddress, amount)
|
2023-04-06 17:51:13 +00:00
|
|
|
suite.NoError(err)
|
2023-06-30 05:30:02 +00:00
|
|
|
return res
|
2023-04-06 17:51:13 +00:00
|
|
|
}
|