Skip to content

Commit e55937e

Browse files
authored
tests: add ibc test cases (#63)
* add basic test cases for ibc middleware v1, v2 more test cases will be added. * add test cases for ibc middleware v2 * add test cases for ibc middleware v1 and post state check * add OnAcknowledgementPacket tc for v1 ibc middleware * add OnTimeoutPacket tc for v1 ibc middleware * chore: unify variable names * use internal testing pkg and add TestOnRecvPacketNativeErc20 tc * add v1 tcs for handling erc20 native coin OnTimeoutPacket, OnAcknowledgementPacket * refactor v1 middleware test codes * apply gci * fix ci: receiver name should be same * fix ci: unify receiver name and also update comments and variable name * fix ci: run gofumpt and remove tc copy * test: update TestOnRecvPacket make sure whether it is registered as dynamic precompiled contract or not * chore: test suite name convention
1 parent 77e425e commit e55937e

File tree

4 files changed

+1416
-0
lines changed

4 files changed

+1416
-0
lines changed

Diff for: tests/ibc/helper.go

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

0 commit comments

Comments
 (0)