diff --git a/e2e/e2etests/test_precompiles_bank.go b/e2e/e2etests/test_precompiles_bank.go index 0b2cb0e99c..4da23223cf 100644 --- a/e2e/e2etests/test_precompiles_bank.go +++ b/e2e/e2etests/test_precompiles_bank.go @@ -3,6 +3,7 @@ package e2etests import ( "math/big" + "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/stretchr/testify/require" "github.com/zeta-chain/node/e2e/runner" @@ -13,28 +14,49 @@ import ( func TestPrecompilesBank(r *runner.E2ERunner, args []string) { require.Len(r, args, 0, "No arguments expected") + owner, spender := r.ZEVMAuth.From, bank.ContractAddress + bankContract, err := bank.NewIBank(bank.ContractAddress, r.ZEVMClient) require.NoError(r, err, "Failed to create bank contract caller") - // Get the balance of the user_precompile in coins "zevm/0x12345" + // Get the initial balance of the owner in ERC20ZRC20 tokens. + ownerERC20InitialBalance, err := r.ERC20ZRC20.BalanceOf(&bind.CallOpts{}, owner) + require.NoError(r, err, "Error retrieving initial owner balance") + + // Get the balance of the owner in coins "zevm/0x12345". // BalanceOf will convert the ZRC20 address to a Cosmos denom formatted as "zevm/0x12345". - retBalanceOf, err := bankContract.BalanceOf(nil, r.ERC20ZRC20Addr, r.ZEVMAuth.From) - require.NoError(r, err, "Error calling balanceOf") + retBalanceOf, err := bankContract.BalanceOf(nil, r.ERC20ZRC20Addr, owner) + require.NoError(r, err, "Error calling bank.balanceOf") require.EqualValues(r, uint64(0), retBalanceOf.Uint64(), "balanceOf result has to be 0") - // Allow the bank contract to spend 100 coins - tx, err := r.ERC20ZRC20.Approve(r.ZEVMAuth, bank.ContractAddress, big.NewInt(100)) - require.NoError(r, err, "Error approving bank contract") + // Allow the bank contract to spend 100 ERC20ZRC20 tokens. + tx, err := r.ERC20ZRC20.Approve(r.ZEVMAuth, spender, big.NewInt(100)) + require.NoError(r, err, "Error approving allowance for bank contract") receipt := utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout) - require.EqualValues(r, 1, receipt.Status, "Error approving allowance for bank contract") + require.EqualValues(r, 1, receipt.Status, "Error in the approve allowance transaction") - // Call deposit with 100 coins - _, err = bankContract.Deposit(r.ZEVMAuth, r.ERC20ZRC20Addr, big.NewInt(100)) - require.NoError(r, err, "Error calling deposit") + // Check the allowance of the bank in ERC20ZRC20 tokens. Should be 100. + balance, err := r.ERC20ZRC20.Allowance(&bind.CallOpts{}, owner, spender) + require.NoError(r, err, "Error retrieving bank allowance") + require.EqualValues(r, uint64(100), balance.Uint64(), "Error allowance for bank contract") - // Check the balance of the user_precompile in coins "zevm/0x12345" - - // Check the balance of the user_precompile in r.ERC20ZRC20Addr + // Call deposit with 100 coins. + _, err = bankContract.Deposit(r.ZEVMAuth, r.ERC20ZRC20Addr, big.NewInt(100)) + require.NoError(r, err, "Error calling bank.deposit") + // Check the balance of the owner in coins "zevm/0x12345". + retBalanceOf, err = bankContract.BalanceOf(nil, r.ERC20ZRC20Addr, owner) + require.NoError(r, err, "Error calling balanceOf") + require.EqualValues(r, uint64(100), retBalanceOf.Uint64(), "balanceOf result has to be 100") + + // Check the balance of the owner in r.ERC20ZRC20Addr, should be 100 less. + ownerERC20FinalBalance, err := r.ERC20ZRC20.BalanceOf(&bind.CallOpts{}, owner) + require.NoError(r, err, "Error retrieving final owner balance") + require.EqualValues( + r, + ownerERC20InitialBalance.Uint64()-100, // expected + ownerERC20FinalBalance.Uint64(), // actual + "Final balance should be initial - 100", + ) } diff --git a/precompiles/bank/call_contract.go b/precompiles/bank/call_contract.go index 9b7e825491..8277f96b8a 100644 --- a/precompiles/bank/call_contract.go +++ b/precompiles/bank/call_contract.go @@ -19,25 +19,17 @@ func (c *Contract) CallContract( method string, args []interface{}, ) ([]interface{}, error) { - input, err := abi.Methods[method].Inputs.Pack(args) - if err != nil { - return nil, &ptypes.ErrUnexpected{ - When: "Pack " + method, - Got: err.Error(), - } - } - res, err := c.fungibleKeeper.CallEVM( - ctx, - *abi, - ContractAddress, - dst, - big.NewInt(0), - nil, - true, - false, - method, - input, + ctx, // ctx + *abi, // abi + ContractAddress, // from + dst, // to + big.NewInt(0), // value + nil, // gasLimit + true, // commit + false, // noEthereumTxEvent + method, // method + args..., // args ) if err != nil { return nil, &ptypes.ErrUnexpected{