Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 11 additions & 111 deletions near/contracts/bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use near_sdk::serde::{Deserialize, Serialize};
#[allow(unused_imports)]
use near_sdk::Promise;
use near_sdk::{
env, ext_contract, near_bindgen, promise_result_as_success, require, AccountId,
BorshStorageKey, Duration, PanicOnDefault, PromiseOrValue,
env, ext_contract, near_bindgen, require, AccountId, BorshStorageKey, Duration, PanicOnDefault,
PromiseOrValue,
};
use whitelist::WhitelistMode;

Expand Down Expand Up @@ -963,102 +963,18 @@ impl FastBridge {
self.decrease_balance(&sender_id, &token_id, &amount.0);
let recipient_id = recipient_id.unwrap_or_else(|| sender_id.clone());

let memo = None;
if let Some(msg) = msg {
self.call_ft_transfer_call(token_id, amount, sender_id, recipient_id, msg)
ext_token::ext(token_id)
.with_static_gas(utils::tera_gas(50))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use const for provided gas

.with_attached_deposit(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use const

.ft_transfer_call(recipient_id, amount, memo, msg)
} else {
self.call_ft_transfer(token_id, amount, sender_id, recipient_id)
}
}

fn call_ft_transfer_call(
&self,
token_id: AccountId,
amount: U128,
sender_id: AccountId,
recipient_id: AccountId,
msg: String,
) -> Promise {
let memo = None;
ext_token::ext(token_id.clone())
.with_static_gas(utils::tera_gas(50))
.with_attached_deposit(1)
.ft_transfer_call(recipient_id.clone(), amount, memo, msg)
.then(
ext_self::ext(current_account_id())
.with_static_gas(utils::tera_gas(5))
.with_attached_deposit(utils::NO_DEPOSIT)
.withdraw_callback(token_id, amount, sender_id, recipient_id),
)
}

fn call_ft_transfer(
&self,
token_id: AccountId,
amount: U128,
sender_id: AccountId,
recipient_id: AccountId,
) -> Promise {
let memo = None;
ext_token::ext(token_id.clone())
.with_static_gas(utils::tera_gas(5))
.with_attached_deposit(1)
.ft_transfer(recipient_id.clone(), amount, memo)
.then(
ext_self::ext(current_account_id())
.with_static_gas(utils::tera_gas(2))
.with_attached_deposit(utils::NO_DEPOSIT)
.withdraw_callback(token_id, amount, sender_id, recipient_id),
)
}

/// This function finalizes the execution flow of the `withdraw()` function. This private function is called after
/// the `ft_transfer` promise made in the `withdraw` function is resolved. It checks whether the promise was
/// successful or not, and emits an event if it was. If the promise was not successful, the amount is returned
/// to the user's balance. This function is only intended for internal use and should not be called directly by
/// external accounts.
///
/// # Arguments
///
/// * `token_id`: An `AccountId` representing the token being withdrawn.
/// * `amount`: A `U128` value representing the amount being withdrawn.
/// * `recipient_id`: An `AccountId` representing the account that will receive the withdrawn funds.
///
/// # Returns
///
/// * A `U128` value representing the amount that was withdrawn, or `0` if the promise was not
/// successful and the funds were returned to the user's balance.
#[private]
pub fn withdraw_callback(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function also needs to be removed from FastBridgeInterface

&mut self,
token_id: AccountId,
amount: U128,
sender_id: AccountId,
recipient_id: AccountId,
) -> U128 {
let mut transferred_amount = U128(0);

if let Some(result) = promise_result_as_success() {
transferred_amount = if result.is_empty() {
amount
} else {
near_sdk::serde_json::from_slice::<U128>(&result).unwrap()
};

Event::FastBridgeWithdrawEvent {
sender_id: Some(sender_id.clone()),
recipient_id,
token: token_id.clone(),
amount: transferred_amount,
}
.emit();
}

let refund_amount = amount.0 - transferred_amount.0;
if refund_amount > 0 {
self.increase_balance(&sender_id, &token_id, &refund_amount);
ext_token::ext(token_id)
.with_static_gas(utils::tera_gas(5))
.with_attached_deposit(1)
.ft_transfer(recipient_id, amount, memo)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also should emit FastBridgeWithdrawEvent

}

transferred_amount
}

/// Sets the prover account. `EthProver` is a contract that checks the correctness of Ethereum proofs.
Expand Down Expand Up @@ -2318,22 +2234,6 @@ mod unit_tests {
contract.withdraw(transfer_token, Some(U128(amount + 1)), None, None);
}

#[test]
#[should_panic(expected = r#"Contract expected a result on the callback"#)]
fn test_withdraw_callback() {
let context = get_context(false);
testing_env!(context);
let mut contract = get_bridge_contract(None);
let token_id: AccountId = AccountId::try_from("token_near".to_string()).unwrap();
let amount = 42;
contract.withdraw_callback(
token_id,
U128(amount),
signer_account_id(),
signer_account_id(),
);
}

#[test]
#[should_panic(expected = r#"address should be a valid hex string"#)]
fn test_set_eth_bridge_contract_address_address_invalid_address() {
Expand Down
3 changes: 0 additions & 3 deletions near/contracts/bridge/src/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,6 @@ mod integration_tests {
)
.await?;
assert!(result.is_success(), "{:?}", result);
assert_eq!(result.logs().len(), 2);
assert!(result.logs()[1]
.contains(r#"EVENT_JSON:{"data":{"amount":"10","recipient_id":"alice.test.near""#));

// Check acoount balance after withdraw call
assert_eq!(
Expand Down
Binary file modified near/res/fastbridge.wasm
Binary file not shown.
Loading