Skip to content

Commit fb1aeae

Browse files
committed
refactor v1 middleware test codes
1 parent 463c4eb commit fb1aeae

2 files changed

Lines changed: 244 additions & 197 deletions

File tree

tests/ibc/helper.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package ibc
2+
3+
import (
4+
"math/big"
5+
"testing"
6+
7+
"github.com/ethereum/go-ethereum/accounts/abi"
8+
"github.com/ethereum/go-ethereum/common"
9+
10+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
11+
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
12+
13+
ibctesting "github.com/cosmos/ibc-go/v10/testing"
14+
15+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
16+
17+
"github.com/cosmos/evm/contracts"
18+
"github.com/cosmos/evm/evmd"
19+
evmibctesting "github.com/cosmos/evm/ibc/testing"
20+
erc20types "github.com/cosmos/evm/x/erc20/types"
21+
)
22+
23+
// NativeErc20Info holds details about a deployed ERC20 token.
24+
type NativeErc20Info struct {
25+
Denom string
26+
ContractAbi abi.ABI
27+
ContractAddr common.Address
28+
Account common.Address // The address of the minter on the EVM chain
29+
InitialBal *big.Int
30+
}
31+
32+
// SetupNativeErc20 deploys, registers, and mints a native ERC20 token on an EVM-based chain.
33+
// Similar to what you used in your original ICS-20 tests, but extracted to a common helper.
34+
func SetupNativeErc20(t *testing.T, chain *evmibctesting.TestChain) *NativeErc20Info {
35+
t.Helper()
36+
37+
evmCtx := chain.GetContext()
38+
evmApp := chain.App.(*evmd.EVMD)
39+
40+
// Deploy new ERC20 contract with default metadata
41+
contractAddr, err := evmApp.Erc20Keeper.DeployERC20Contract(evmCtx, banktypes.Metadata{
42+
DenomUnits: []*banktypes.DenomUnit{
43+
{Denom: "example", Exponent: 18},
44+
},
45+
Name: "Example",
46+
Symbol: "Ex",
47+
})
48+
if err != nil {
49+
t.Fatalf("ERC20 deployment failed: %v", err)
50+
}
51+
chain.NextBlock()
52+
53+
// Register the contract
54+
_, err = evmApp.Erc20Keeper.RegisterERC20(evmCtx, &erc20types.MsgRegisterERC20{
55+
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
56+
Erc20Addresses: []string{contractAddr.Hex()},
57+
})
58+
if err != nil {
59+
t.Fatalf("RegisterERC20 failed: %v", err)
60+
}
61+
62+
// Mint tokens to default sender
63+
contractAbi := contracts.ERC20MinterBurnerDecimalsContract.ABI
64+
nativeDenom := erc20types.CreateDenom(contractAddr.String())
65+
sendAmt := ibctesting.DefaultCoinAmount
66+
senderAcc := chain.SenderAccount.GetAddress()
67+
68+
_, err = evmApp.EVMKeeper.CallEVM(
69+
evmCtx,
70+
contractAbi,
71+
erc20types.ModuleAddress,
72+
contractAddr,
73+
true,
74+
"mint",
75+
common.BytesToAddress(senderAcc),
76+
big.NewInt(sendAmt.Int64()),
77+
)
78+
if err != nil {
79+
t.Fatalf("mint call failed: %v", err)
80+
}
81+
82+
// Verify minted balance
83+
bal := evmApp.Erc20Keeper.BalanceOf(evmCtx, contractAbi, contractAddr, common.BytesToAddress(senderAcc))
84+
if bal.Cmp(big.NewInt(sendAmt.Int64())) != 0 {
85+
t.Fatalf("unexpected ERC20 balance; got %s, want %s", bal.String(), sendAmt.String())
86+
}
87+
88+
return &NativeErc20Info{
89+
Denom: nativeDenom,
90+
ContractAbi: contractAbi,
91+
ContractAddr: contractAddr,
92+
Account: common.BytesToAddress(senderAcc),
93+
InitialBal: big.NewInt(sendAmt.Int64()),
94+
}
95+
}

0 commit comments

Comments
 (0)