Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure sender_id has a balance is greater than amount #708

Merged
merged 12 commits into from
Mar 31, 2023
40 changes: 40 additions & 0 deletions engine-tests/src/tests/eth_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,46 @@ fn test_ft_transfer_call_without_message() {
let balance = get_eth_on_near_balance(&master_account, CONTRACT_ACC, CONTRACT_ACC);
assert_eq!(balance, DEPOSITED_FEE);

// should revert with `not enough balance` error when sending arbitray amount while sender_id = receiver_id
0x3bfc marked this conversation as resolved.
Show resolved Hide resolved
let transfer_amount = 1000000000;
let res = recipient_account.call(
CONTRACT_ACC.parse().unwrap(),
"ft_transfer_call",
json!({
"receiver_id": recipient_account.signer.account_id.to_string(),
"amount": transfer_amount.to_string(),
"msg": "",
})
.to_string()
.as_bytes(),
DEFAULT_GAS,
1,
);

assert_execution_status_failure(
res.outcome().clone().status,
"ExecutionError(\"Smart contract panicked: ERR_NOT_ENOUGH_BALANCE\")",
"Expected failure in `ft_transfer_call` call, but call succeeded",
);

// should not revert with `not enough balance` error when sending arbitray amount while sender_id = receiver_id with amount < balance
0x3bfc marked this conversation as resolved.
Show resolved Hide resolved
let transfer_amount = 1;
let res = recipient_account.call(
CONTRACT_ACC.parse().unwrap(),
"ft_transfer_call",
json!({
"receiver_id": recipient_account.signer.account_id.to_string(),
"amount": transfer_amount.to_string(),
"msg": "",
})
.to_string()
.as_bytes(),
DEFAULT_GAS,
1,
);

res.assert_success();

// Sending to random account should not change balances
let transfer_amount = 22;
let res = recipient_account.call(
Expand Down
7 changes: 7 additions & 0 deletions engine/src/fungible_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ impl<I: IO + Copy> FungibleTokenOps<I> {
current_account_id: AccountId,
prepaid_gas: NearGas,
) -> Result<PromiseWithCallbackArgs, error::TransferError> {
// check balance to prevent setting an arbitrary value for `amount` for (receiver_id == receiver_id).
let balance = self
.get_account_eth_balance(&sender_id)
.unwrap_or(ZERO_NEP141_WEI);
if amount > balance {
return Err(error::TransferError::InsufficientFunds);
}
// Special case for Aurora transfer itself - we shouldn't transfer
if sender_id != receiver_id {
self.internal_transfer_eth_on_near(&sender_id, &receiver_id, amount, memo)?;
Expand Down