Skip to content

Commit

Permalink
Hopefully remove division by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavBreadless committed Dec 6, 2021
1 parent b149360 commit ef8c557
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions core/bin/zksync_api/src/fee_ticker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,24 @@ impl<API: FeeTickerAPI, INFO: FeeTickerInfo, WATCHER: TokenWatcher> FeeTicker<AP
.get_token_price(TokenLike::Id(token.id), TokenPriceRequestType::USDForOneWei)
.await?;
let token_price = big_decimal_to_ratio(&token_price).unwrap();
let subsidized_gas_amount = &self.config.subsidy_cpk_price_usd
/ (&wei_price_usd * &scale_gas_price * &token_usd_risk * &token_price);

let denom_part = &wei_price_usd * &scale_gas_price * &token_usd_risk * &token_price;

let subsidized_gas_amount = if token_price.is_zero() {
// If the price of the token is zero, than it is not possible to calculate the fee in this token
// Actually we should never get into this clause, since we divive by the token's price in the calculation of token_usd_risk

return Err(anyhow::Error::msg("The token is not acceptable for fee"));
} else if denom_part.is_zero() {
// If the denom_part is zero, it means that either of wei_price_usd, scale_gas_price, token_usd_risk are equal to 0
// The total_gas_fee is multiplied by all of these multiples in the final calculation of the fee, so it is safe to return 0 here,
// since the gas cost would be zero anyway.

// This would mean that the final subsidized fee is zero. However, this is a very rare ocasion
Ratio::from(BigUint::zero())
} else {
&self.config.subsidy_cpk_price_usd / denom_part
};

for (tx_type, recipient) in txs {
let (output_fee_type, gas_tx_amount, op_chunks) =
Expand Down

0 comments on commit ef8c557

Please sign in to comment.