Update e2e_convert_cosmos_coins_test.go

This commit is contained in:
Volodymyr Buhai 2025-01-03 20:05:19 +02:00 committed by GitHub
parent 1f9de5eaa9
commit 2a8bc0f4fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -55,23 +55,40 @@ func setupConvertToCoinTest(
sdk.NewInt64Coin(denom, initialCosmosCoinConversionDenomFunds), // conversion-enabled cosmos coin
)
// Create and fund the test account
user = suite.ZgChain.NewFundedAccount(accountName, initialFunds)
suite.Require().NotNil(user, "failed to create funded test account")
return denom, initialFunds, user
}
// amount must be less than initial funds (initialCosmosCoinConversionDenomFunds)
// setupAccountWithCosmosCoinERC20Balance creates a test account with both cosmos coins and ERC20 tokens.
// It first sets up an account with cosmos coins and then converts a specified amount to ERC20 tokens.
//
// Parameters:
// - accountName: Name of the test account to be created
// - amount: Amount of coins to convert to ERC20 (must be less than initialCosmosCoinConversionDenomFunds)
//
// Returns:
// - user: The created and funded test account
// - contractAddress: The deployed ERC20 contract address
// - denom: The cosmos denom used for testing
// - sdkBalance: The remaining SDK coin balance after conversion
func (suite *IntegrationTestSuite) setupAccountWithCosmosCoinERC20Balance(
accountName string, amount int64,
) (user *testutil.SigningAccount, contractAddress *evmutiltypes.InternalEVMAddress, denom string, sdkBalance sdk.Coins) {
if amount > initialCosmosCoinConversionDenomFunds {
panic(fmt.Sprintf("test erc20 amount must be less than %d", initialCosmosCoinConversionDenomFunds))
suite.FailNowf("Invalid test setup",
"test erc20 amount (%d) must be less than %d",
amount,
initialCosmosCoinConversionDenomFunds,
)
}
denom, sdkBalance, user = setupConvertToCoinTest(suite, accountName)
convertAmount := sdk.NewInt64Coin(denom, amount)
// setup user to have erc20 balance
// Convert cosmos coins to ERC20
msg := evmutiltypes.NewMsgConvertCosmosCoinToERC20(
user.SdkAddress.String(),
user.EvmAddress.Hex(),
@ -84,20 +101,22 @@ func (suite *IntegrationTestSuite) setupAccountWithCosmosCoinERC20Balance(
Data: "converting sdk coin to erc20",
}
res := user.SignAndBroadcastZgChainTx(tx)
suite.NoError(res.Err)
suite.Require().NoError(res.Err, "failed to convert cosmos coins to ERC20")
// adjust sdk balance
// Adjust SDK balance after conversion
sdkBalance = sdkBalance.Sub(convertAmount)
// query for the deployed contract
// Query for the deployed contract
deployedContracts, err := suite.ZgChain.Grpc.Query.Evmutil.DeployedCosmosCoinContracts(
context.Background(),
&evmutiltypes.QueryDeployedCosmosCoinContractsRequest{CosmosDenoms: []string{denom}},
)
suite.NoError(err)
suite.Len(deployedContracts.DeployedCosmosCoinContracts, 1)
suite.Require().NoError(err, "failed to query deployed contracts")
suite.Require().Len(deployedContracts.DeployedCosmosCoinContracts, 1,
"expected exactly one deployed contract for denom %s", denom)
contractAddress = deployedContracts.DeployedCosmosCoinContracts[0].Address
suite.Require().NotNil(contractAddress, "contract address cannot be nil")
return user, contractAddress, denom, sdkBalance
}
@ -426,13 +445,13 @@ func (suite *IntegrationTestSuite) TestConvertCosmosCoins_ERC20Magic() {
bob.SdkAddress.String(),
sdk.NewInt64Coin(denom, amount.Int64()),
)
convertTx := util.ZgChainMsgRequest{
finalConvertTx := util.ZgChainMsgRequest{
Msgs: []sdk.Msg{&convertMsg},
GasLimit: 2e5,
FeeAmount: sdk.NewCoins(chaincfg.MakeCoinForGasDenom(200)),
Data: "bob converts his new erc20 to an sdk.Coin",
}
convertRes := bob.SignAndBroadcastZgChainTx(convertTx)
convertRes := bob.SignAndBroadcastZgChainTx(finalConvertTx)
suite.NoError(convertRes.Err)
// bob should have no more erc20 balance
@ -452,6 +471,18 @@ func (suite *IntegrationTestSuite) TestConvertCosmosCoins_ERC20Magic() {
alice.SdkAddress.String(),
sdk.NewInt64Coin(denom, amount.Int64()),
)
convertRes = alice.SignAndBroadcastZgChainTx(convertTx)
finalConvertTx = util.ZgChainMsgRequest{
Msgs: []sdk.Msg{&convertMsg},
GasLimit: 2e5,
FeeAmount: sdk.NewCoins(chaincfg.MakeCoinForGasDenom(200)),
Data: "alice converts remaining erc20 back to sdk.Coin",
}
convertRes = alice.SignAndBroadcastZgChainTx(finalConvertTx)
suite.NoError(convertRes.Err)
// verify final balances
erc20Balance = suite.ZgChain.GetErc20Balance(contractAddress.Address, alice.EvmAddress)
suite.BigIntsEqual(big.NewInt(0), erc20Balance, "expected alice to have no remaining erc20 balance")
balance = suite.ZgChain.QuerySdkForBalances(alice.SdkAddress).AmountOf(denom)
suite.Equal(sdk.NewIntFromBigInt(amount), balance)
}