Skip to content

Commit

Permalink
fix: incorrect v3 max gas amount calculation
Browse files Browse the repository at this point in the history
When using JSON-RPC v0.7.0, the fee estimate returned contains data
gas info. Naively using `gas_consumed` as max gas amount results in
insufficient fees. The correct way to do it is to calculate a pseudo gas
amount by dividing the overall fee by the gas price.
  • Loading branch information
xJonathanLEI committed May 1, 2024
1 parent 4fa0a73 commit 01f9cf1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
7 changes: 5 additions & 2 deletions starknet-accounts/src/account/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,11 @@ where
let gas = match self.gas {
Some(gas) => gas,
None => {
(((TryInto::<u64>::try_into(fee_estimate.gas_consumed)
.map_err(|_| AccountError::FeeOutOfRange)?)
(((TryInto::<u64>::try_into(
(fee_estimate.overall_fee + fee_estimate.gas_price - FieldElement::ONE)
.floor_div(fee_estimate.gas_price),
)
.map_err(|_| AccountError::FeeOutOfRange)?)
as f64)
* self.gas_estimate_multiplier) as u64
}
Expand Down
7 changes: 5 additions & 2 deletions starknet-accounts/src/account/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,11 @@ where
let gas = match self.gas {
Some(gas) => gas,
None => {
(((TryInto::<u64>::try_into(fee_estimate.gas_consumed)
.map_err(|_| AccountError::FeeOutOfRange)?)
(((TryInto::<u64>::try_into(
(fee_estimate.overall_fee + fee_estimate.gas_price - FieldElement::ONE)
.floor_div(fee_estimate.gas_price),
)
.map_err(|_| AccountError::FeeOutOfRange)?)
as f64)
* self.gas_estimate_multiplier) as u64
}
Expand Down
7 changes: 5 additions & 2 deletions starknet-accounts/src/factory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,11 @@ where
let gas = match self.gas {
Some(gas) => gas,
None => {
(((TryInto::<u64>::try_into(fee_estimate.gas_consumed)
.map_err(|_| AccountFactoryError::FeeOutOfRange)?)
(((TryInto::<u64>::try_into(
(fee_estimate.overall_fee + fee_estimate.gas_price - FieldElement::ONE)
.floor_div(fee_estimate.gas_price),
)
.map_err(|_| AccountFactoryError::FeeOutOfRange)?)
as f64)
* self.gas_estimate_multiplier) as u64
}
Expand Down

0 comments on commit 01f9cf1

Please sign in to comment.