Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Aug 27, 2024
1 parent 742c8d4 commit 503c6ff
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions x/evm/keeper/erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,42 @@ func transferERC20(t *testing.T, ctx sdk.Context, input TestKeepers, caller, rec

}

func approveERC20(t *testing.T, ctx sdk.Context, input TestKeepers, caller, spender common.Address, amount sdk.Coin, expectErr bool) {
abi, err := erc20.Erc20MetaData.GetAbi()
require.NoError(t, err)

inputBz, err := abi.Pack("approve", spender, amount.Amount.BigInt())
require.NoError(t, err)

erc20ContractAddr, err := types.DenomToContractAddr(ctx, &input.EVMKeeper, amount.Denom)
require.NoError(t, err)

_, _, err = input.EVMKeeper.EVMCall(ctx, caller, erc20ContractAddr, inputBz, nil)
if expectErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
}

func transferFromERC20(t *testing.T, ctx sdk.Context, input TestKeepers, caller, from, to common.Address, amount sdk.Coin, expectErr bool) {
abi, err := erc20.Erc20MetaData.GetAbi()
require.NoError(t, err)

inputBz, err := abi.Pack("transferFrom", from, to, amount.Amount.BigInt())
require.NoError(t, err)

erc20ContractAddr, err := types.DenomToContractAddr(ctx, &input.EVMKeeper, amount.Denom)
require.NoError(t, err)

_, _, err = input.EVMKeeper.EVMCall(ctx, caller, erc20ContractAddr, inputBz, nil)
if expectErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
}

func Test_BalanceOf(t *testing.T) {
ctx, input := createDefaultTestInput(t)

Expand Down Expand Up @@ -424,3 +460,40 @@ func Test_IterateAccountBalances(t *testing.T) {
return false, nil
})
}

func Test_Approf(t *testing.T) {
ctx, input := createDefaultTestInput(t)
_, _, addr := keyPubAddr()
_, _, addr2 := keyPubAddr()

evmAddr := common.BytesToAddress(addr.Bytes())
evmAddr2 := common.BytesToAddress(addr2.Bytes())

erc20Keeper, err := keeper.NewERC20Keeper(&input.EVMKeeper)
require.NoError(t, err)

// deploy erc20 contract
fooContractAddr := deployERC20(t, ctx, input, evmAddr, "foo")
fooDenom, err := types.ContractAddrToDenom(ctx, &input.EVMKeeper, fooContractAddr)
require.NoError(t, err)
require.Equal(t, "evm/"+fooContractAddr.Hex()[2:], fooDenom)

// mint erc20
mintERC20(t, ctx, input, evmAddr, evmAddr, sdk.NewCoin(fooDenom, math.NewInt(100)), false)

// approve erc20
approveERC20(t, ctx, input, evmAddr, evmAddr2, sdk.NewCoin(fooDenom, math.NewInt(50)), false)

// transferFrom erc20
transferFromERC20(t, ctx, input, evmAddr2, evmAddr, evmAddr2, sdk.NewCoin(fooDenom, math.NewInt(50)), false)

amount, err := erc20Keeper.GetBalance(ctx, addr, fooDenom)
require.NoError(t, err)
require.Equal(t, math.NewInt(50), amount)
amount, err = erc20Keeper.GetBalance(ctx, addr2, fooDenom)
require.NoError(t, err)
require.Equal(t, math.NewInt(50), amount)

// should fail to transferFrom more than approved
transferFromERC20(t, ctx, input, evmAddr2, evmAddr, evmAddr2, sdk.NewCoin(fooDenom, math.NewInt(50)), true)
}

0 comments on commit 503c6ff

Please sign in to comment.