Skip to content

Commit

Permalink
Account for gwei overflows while multiplying
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Aug 16, 2023
1 parent 3c114a9 commit 9a29d88
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions shared/src/ledger/eth_bridge/bridge_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,27 +649,41 @@ mod recommendations {
);
return None;
}
Some(rate)
Some(Uint::from(rate))
})?;

// And this the amount of gas tokens a user is willing to
// pay for the transfer
let gas_amount: Uint = pending.gas_fee.amount.into();

// Multiply both to acquire the amount of earned gwei,
// after the relay operation completes
let amt_of_earned_gwei = if let Some(gwei) =
gas_amount.checked_mul(gas_token_to_gwei_conversion_rate)
{
gwei
} else {
return Some(Err("Overflowing error while calculating \
earned gwei amount"
.into()));
};

Some(
I256::try_from(
pending.gas_fee.amount
* gas_token_to_gwei_conversion_rate,
)
.map_err(|err| err.to_string())
.and_then(|amt_of_earned_gwei| {
transfer_fee()
.checked_sub(&amt_of_earned_gwei)
.ok_or_else(|| {
"Underflowed calculating relaying cost".into()
})
})
.map(|cost| EligibleRecommendation {
cost,
pending_transfer: pending,
transfer_hash: pending_hash,
}),
I256::try_from(amt_of_earned_gwei)
.map_err(|err| err.to_string())
.and_then(|amt_of_earned_gwei| {
transfer_fee()
.checked_sub(&amt_of_earned_gwei)
.ok_or_else(|| {
"Underflowed calculating relaying cost"
.into()
})
})
.map(|cost| EligibleRecommendation {
cost,
pending_transfer: pending,
transfer_hash: pending_hash,
}),
)
})
.maybe_collect_result()
Expand Down

0 comments on commit 9a29d88

Please sign in to comment.