mirror of
https://github.com/0glabs/0g-chain.git
synced 2025-04-02 14:55:17 +00:00
Merge 2a8bc0f4fe
into 559d1beb03
This commit is contained in:
commit
8a940d23f4
@ -20,45 +20,75 @@ import (
|
||||
|
||||
const initialCosmosCoinConversionDenomFunds = int64(1e4)
|
||||
|
||||
// setupConvertToCoinTest prepares a test environment for cosmos coin conversion tests.
|
||||
// It retrieves allowed cosmos denoms from parameters, sets up initial funds, and creates a funded test account.
|
||||
//
|
||||
// Parameters:
|
||||
// - suite: The test suite instance
|
||||
// - accountName: Name of the test account to be created
|
||||
//
|
||||
// Returns:
|
||||
// - denom: The cosmos denom used for testing
|
||||
// - initialFunds: Initial funds allocated to the test account
|
||||
// - user: The created and funded test account
|
||||
func setupConvertToCoinTest(
|
||||
suite *IntegrationTestSuite, accountName string,
|
||||
) (denom string, initialFunds sdk.Coins, user *testutil.SigningAccount) {
|
||||
// we expect a denom to be registered to the allowed denoms param
|
||||
// and for the funded account to have a balance for that denom
|
||||
// Query for allowed denoms from parameters
|
||||
params, err := suite.ZgChain.Grpc.Query.Evmutil.Params(
|
||||
context.Background(),
|
||||
&evmutiltypes.QueryParamsRequest{},
|
||||
)
|
||||
suite.NoError(err)
|
||||
suite.GreaterOrEqual(
|
||||
suite.Require().NoError(err, "failed to query evmutil params")
|
||||
suite.Require().GreaterOrEqual(
|
||||
len(params.Params.AllowedCosmosDenoms), 1,
|
||||
"0g-chain expected to have at least one AllowedCosmosDenom for ERC20 conversion",
|
||||
"0g-chain must have at least one AllowedCosmosDenom for ERC20 conversion",
|
||||
)
|
||||
|
||||
tokenInfo := params.Params.AllowedCosmosDenoms[0]
|
||||
denom = tokenInfo.CosmosDenom
|
||||
suite.Require().NotEmpty(denom, "cosmos denom cannot be empty")
|
||||
|
||||
// Setup initial funds with both staking denom (for gas) and conversion denom
|
||||
initialFunds = sdk.NewCoins(
|
||||
sdk.NewInt64Coin(suite.ZgChain.StakingDenom, 1e5), // gas money
|
||||
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(),
|
||||
@ -71,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
|
||||
}
|
||||
@ -413,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
|
||||
@ -439,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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user