Skip to content

Commit

Permalink
fix: remove redundant fee bounds check in forwarder contract (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksuss authored Apr 16, 2024
1 parent f173068 commit 21de33e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 21 deletions.
2 changes: 1 addition & 1 deletion factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const STORAGE_BALANCE_BOUND: NearToken = NearToken::from_yoctonear(1_250_000_000
const FORWARDER_NEW_GAS: Gas = Gas::from_tgas(2);

pub const MAX_NUM_CONTRACTS: usize = 12;
pub const INIT_BALANCE: NearToken = NearToken::from_millinear(360);
pub const INIT_BALANCE: NearToken = NearToken::from_millinear(355);

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
Expand Down
19 changes: 1 addition & 18 deletions forwarder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ mod types;
#[global_allocator]
static ALLOCATOR: NoopAllocator = NoopAllocator;

const MINIMUM_BALANCE: u128 = 360_000_000_000_000_000_000_000;
const MINIMUM_BALANCE: u128 = 355_000_000_000_000_000_000_000;
const ZERO_YOCTO: u128 = 0;
const MAX_FEE_PERCENT: u128 = 10;

const CALCULATE_FEES_GAS: u64 = 4_000_000_000_000;
const NEAR_DEPOSIT_GAS: u64 = 2_000_000_000_000;
Expand Down Expand Up @@ -155,10 +154,6 @@ pub extern "C" fn finish_forward_callback() {
_ => panic_utf8(b"FEE RESULT IS NOT READY"),
};

if !is_fee_allowed(params.amount, fee) {
panic_utf8(b"FEE IS TOO BIG");
}

let amount = params.amount.saturating_sub(fee);

let mut promise_id = unsafe {
Expand Down Expand Up @@ -266,18 +261,6 @@ fn forward_nep141_token<I: IO + Env + PromiseHandler>(mut io: I, token_id: Accou
io.promise_return(promise_id);
}

// Validate that calculated part of the fee isn't more than `MAX_FEE_PERCENT`.
fn is_fee_allowed(amount: u128, fee: u128) -> bool {
match (fee * 100)
.checked_div(amount)
.zip((fee * 100).checked_rem(amount))
{
Some((percent, _)) if percent > MAX_FEE_PERCENT => false,
Some((percent, reminder)) if percent == MAX_FEE_PERCENT && reminder > 0 => false,
_ => true,
}
}

struct NoopAllocator;

unsafe impl GlobalAlloc for NoopAllocator {
Expand Down
4 changes: 2 additions & 2 deletions tests/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ async fn test_storage_deposit_refund() {
let balance_after_create = sandbox.balance(factory.id()).await;
assert_eq!(
to_near(balance_before_create - balance_after_create),
0.363_685 // Ⓝ
0.358_680 // Ⓝ
);

let sk = SecretKey::from_str("ed25519:61TF7S52FVETjLp6KMUDp1TYBEdc1km1GnHgZc67VhWfyHTCUTMjUY6mM3qML17EAHFiutjpmF4CD9wdSGtG19tR").unwrap();
Expand All @@ -405,7 +405,7 @@ async fn test_storage_deposit_refund() {
println!("balance_after_delete: {balance_after_delete}");
assert_eq!(
to_near(balance_before_create - balance_after_delete),
0.003_722 // Ⓝ
0.003_716 // Ⓝ
);
}

Expand Down

0 comments on commit 21de33e

Please sign in to comment.