Skip to content

Commit

Permalink
make deposit and withdraw fail with amount 0
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Oct 8, 2024
1 parent c2b86d4 commit 8ef6a05
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion precompiles/bank/method_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func unpackDepositArgs(args []interface{}) (zrc20Addr common.Address, amount *bi
}

amount, ok = args[1].(*big.Int)
if !ok || amount.Sign() < 0 || amount == nil || amount == new(big.Int) {
if !ok || amount.Sign() <= 0 || amount == nil || amount == new(big.Int) {
return common.Address{}, nil, &ptypes.ErrInvalidAmount{
Got: amount.String(),
}
Expand Down
37 changes: 29 additions & 8 deletions precompiles/bank/method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,12 @@ func Test_Methods(t *testing.T) {
ts.mockVMContract.Input = packInputArgs(
t,
methodID,
[]interface{}{ts.zrc20Address, big.NewInt(0)}...,
[]interface{}{ts.zrc20Address, big.NewInt(1000)}...,
)

success, err := ts.bankContract.Run(ts.mockEVM, ts.mockVMContract, false)
require.Error(t, err)
require.ErrorAs(
t,
ptypes.ErrInvalidCoin{
Empty: true,
},
err,
)
require.Contains(t, err.Error(), "invalid allowance, got: 0")

res, err := ts.bankABI.Methods[DepositMethodName].Outputs.Unpack(success)
require.NoError(t, err)
Expand All @@ -150,6 +144,33 @@ func Test_Methods(t *testing.T) {
require.False(t, ok)
})

t.Run("should fail when trying to deposit 0", func(t *testing.T) {
ts := setupChain(t)
caller := fungibletypes.ModuleAddressZEVM
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, caller, big.NewInt(1000))

methodID := ts.bankABI.Methods[DepositMethodName]

// Allow bank to spend 500 ZRC20 tokens.
allowBank(t, ts, big.NewInt(500))

// Set CallerAddress and evm.Origin to the caller address.
// Caller does not have any balance, and bank does not have any allowance.
ts.mockVMContract.CallerAddress = caller
ts.mockEVM.Origin = caller

// Set the input arguments for the deposit method.
ts.mockVMContract.Input = packInputArgs(
t,
methodID,
[]interface{}{ts.zrc20Address, big.NewInt(0)}...,
)

_, err := ts.bankContract.Run(ts.mockEVM, ts.mockVMContract, false)
require.Error(t, err)
require.Contains(t, err.Error(), "invalid token amount: 0")
})

t.Run("should fail when trying to deposit more than allowed to bank", func(t *testing.T) {
ts := setupChain(t)
caller := fungibletypes.ModuleAddressZEVM
Expand Down
2 changes: 1 addition & 1 deletion precompiles/bank/method_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func unpackWithdrawArgs(args []interface{}) (zrc20Addr common.Address, amount *b
}

amount, ok = args[1].(*big.Int)
if !ok || amount.Sign() < 0 || amount == nil || amount == new(big.Int) {
if !ok || amount.Sign() <= 0 || amount == nil || amount == new(big.Int) {
return common.Address{}, nil, &ptypes.ErrInvalidAmount{
Got: amount.String(),
}
Expand Down

0 comments on commit 8ef6a05

Please sign in to comment.