Skip to content

Commit 9a27c8a

Browse files
authored
Add method to convert EstimatedFee to ResourceBoundsMapping (#1347)
1 parent f0d7440 commit 9a27c8a

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

starknet_py/net/account/account.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,9 @@ async def _get_resource_bounds(
170170
estimated_fee = await self.estimate_fee(transaction)
171171
assert isinstance(estimated_fee, EstimatedFee)
172172

173-
l1_resource_bounds = ResourceBounds(
174-
max_amount=int(
175-
(estimated_fee.overall_fee / estimated_fee.gas_price)
176-
* Account.ESTIMATED_AMOUNT_MULTIPLIER
177-
if estimated_fee.gas_price != 0
178-
else 0
179-
),
180-
max_price_per_unit=int(
181-
estimated_fee.gas_price * Account.ESTIMATED_UNIT_PRICE_MULTIPLIER
182-
),
173+
return estimated_fee.to_resource_bounds(
174+
Account.ESTIMATED_AMOUNT_MULTIPLIER,
175+
Account.ESTIMATED_UNIT_PRICE_MULTIPLIER,
183176
)
184177

185178
if l1_resource_bounds is None:

starknet_py/net/client_models.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,40 @@ class EstimatedFee:
629629
overall_fee: int
630630
unit: PriceUnit
631631

632+
def to_resource_bounds(
633+
self, amount_multiplier=1.5, unit_price_multiplier=1.5
634+
) -> ResourceBoundsMapping:
635+
"""
636+
Converts estimated fee to resource bounds with applied multipliers.
637+
638+
Calculates max amount as `max_amount` = `overall_fee` / `gas_price`, unless `gas_price` is 0,
639+
then `max_amount` is 0. Calculates max price per unit as `max_price_per_unit` = `gas_price`.
640+
641+
Then multiplies `max_amount` by `amount_multiplier` and `max_price_per_unit` by `unit_price_multiplier`.
642+
643+
:param amount_multiplier: Multiplier for max amount, defaults to 1.5.
644+
:param unit_price_multiplier: Multiplier for max price per unit, defaults to 1.5.
645+
:return: Resource bounds with applied multipliers.
646+
"""
647+
648+
if amount_multiplier <= 0 or unit_price_multiplier <= 0:
649+
raise ValueError(
650+
"Values of 'amount_multiplier' and 'unit_price_multiplier' must be greater than 0"
651+
)
652+
653+
l1_resource_bounds = ResourceBounds(
654+
max_amount=int(
655+
(self.overall_fee / self.gas_price) * amount_multiplier
656+
if self.gas_price != 0
657+
else 0
658+
),
659+
max_price_per_unit=int(self.gas_price * unit_price_multiplier),
660+
)
661+
662+
return ResourceBoundsMapping(
663+
l1_gas=l1_resource_bounds, l2_gas=ResourceBounds.init_with_zeros()
664+
)
665+
632666

633667
@dataclass
634668
class DeployedContract:

0 commit comments

Comments
 (0)