Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions tests/ibc/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package ibc

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"

banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

ibctesting "github.com/cosmos/ibc-go/v10/testing"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/cosmos/evm/contracts"
"github.com/cosmos/evm/evmd"
evmibctesting "github.com/cosmos/evm/ibc/testing"
erc20types "github.com/cosmos/evm/x/erc20/types"
)

// NativeErc20Info holds details about a deployed ERC20 token.
type NativeErc20Info struct {
Denom string
ContractAbi abi.ABI
ContractAddr common.Address
Account common.Address // The address of the minter on the EVM chain
InitialBal *big.Int
}

// SetupNativeErc20 deploys, registers, and mints a native ERC20 token on an EVM-based chain.
// Similar to what you used in your original ICS-20 tests, but extracted to a common helper.
func SetupNativeErc20(t *testing.T, chain *evmibctesting.TestChain) *NativeErc20Info {
t.Helper()

evmCtx := chain.GetContext()
evmApp := chain.App.(*evmd.EVMD)

// Deploy new ERC20 contract with default metadata
contractAddr, err := evmApp.Erc20Keeper.DeployERC20Contract(evmCtx, banktypes.Metadata{
DenomUnits: []*banktypes.DenomUnit{
{Denom: "example", Exponent: 18},
},
Name: "Example",
Symbol: "Ex",
})
if err != nil {
t.Fatalf("ERC20 deployment failed: %v", err)
}
chain.NextBlock()

// Register the contract
_, err = evmApp.Erc20Keeper.RegisterERC20(evmCtx, &erc20types.MsgRegisterERC20{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Erc20Addresses: []string{contractAddr.Hex()},
})
if err != nil {
t.Fatalf("RegisterERC20 failed: %v", err)
}

// Mint tokens to default sender
contractAbi := contracts.ERC20MinterBurnerDecimalsContract.ABI
nativeDenom := erc20types.CreateDenom(contractAddr.String())
sendAmt := ibctesting.DefaultCoinAmount
senderAcc := chain.SenderAccount.GetAddress()

_, err = evmApp.EVMKeeper.CallEVM(
evmCtx,
contractAbi,
erc20types.ModuleAddress,
contractAddr,
true,
"mint",
common.BytesToAddress(senderAcc),
big.NewInt(sendAmt.Int64()),
)
if err != nil {
t.Fatalf("mint call failed: %v", err)
}

// Verify minted balance
bal := evmApp.Erc20Keeper.BalanceOf(evmCtx, contractAbi, contractAddr, common.BytesToAddress(senderAcc))
if bal.Cmp(big.NewInt(sendAmt.Int64())) != 0 {
t.Fatalf("unexpected ERC20 balance; got %s, want %s", bal.String(), sendAmt.String())
}

return &NativeErc20Info{
Denom: nativeDenom,
ContractAbi: contractAbi,
ContractAddr: contractAddr,
Account: common.BytesToAddress(senderAcc),
InitialBal: big.NewInt(sendAmt.Int64()),
}
}
Loading
Loading