Skip to content

Commit

Permalink
fix(py): find correct gas_consumed
Browse files Browse the repository at this point in the history
this should be fixed in cairo-lang 0.9.1, there should be a triple
returning api.
  • Loading branch information
Joonas Koivunen committed Jun 30, 2022
1 parent d362d7c commit a0d281f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
25 changes: 20 additions & 5 deletions py/src/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,19 +641,34 @@ async def do_call(


def estimate_fee_after_call(general_config, call_info, carried_state):
from starkware.starknet.business_logic.transaction_fee import calculate_tx_fee
import math
from starkware.starknet.business_logic.utils import get_invoke_tx_total_resources

(l1_gas_used, _cairo_resources_used) = get_invoke_tx_total_resources(
# FIXME: this is now completly inlined calculate_tx_fee, fix with cairo-lang 0.9.1
# it has been adapted to variable names from 167b28bcd940fd25ea3816204fa882a0b0a49603
(l1_gas_usage, cairo_resource_usage) = get_invoke_tx_total_resources(
carried_state, call_info
)

overall_fee = calculate_tx_fee(carried_state, call_info, general_config)
cairo_resource_fee_weights = general_config.cairo_resource_fee_weights
cairo_resource_names = set(cairo_resource_usage.keys())
assert cairo_resource_names.issubset(
cairo_resource_fee_weights.keys()
), "Cairo resource names must be contained in fee weights dict."

# Convert Cairo usage to L1 gas usage.
cairo_l1_gas_usage = max(
cairo_resource_fee_weights[key] * cairo_resource_usage.get(key, 0)
for key in cairo_resource_fee_weights
)

total_l1_gas_usage = cairo_l1_gas_usage + l1_gas_usage
overall_fee = math.ceil(total_l1_gas_usage * carried_state.block_info.gas_price)

return {
"gas_consumed": l1_gas_used,
"gas_consumed": int(total_l1_gas_usage),
"gas_price": carried_state.block_info.gas_price,
"overall_fee": overall_fee,
"overall_fee": int(overall_fee),
}


Expand Down
8 changes: 4 additions & 4 deletions py/src/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def test_fee_estimate_on_positive_directly():
(verb, output, _timings) = loop_inner(con, command)

assert output == {
"gas_consumed": 0,
"gas_consumed": 3,
"gas_price": 0,
"overall_fee": 0,
}
Expand All @@ -441,7 +441,7 @@ def test_fee_estimate_on_positive():
assert first == {
"status": "ok",
"output": {
"gas_consumed": "0x" + (0).to_bytes(32, "big").hex(),
"gas_consumed": "0x" + (3).to_bytes(32, "big").hex(),
"gas_price": "0x" + (0).to_bytes(32, "big").hex(),
"overall_fee": "0x" + (0).to_bytes(32, "big").hex(),
},
Expand All @@ -450,7 +450,7 @@ def test_fee_estimate_on_positive():
assert second == {
"status": "ok",
"output": {
"gas_consumed": "0x" + (0).to_bytes(32, "big").hex(),
"gas_consumed": "0x" + (3).to_bytes(32, "big").hex(),
"gas_price": "0x" + (10).to_bytes(32, "big").hex(),
"overall_fee": "0x" + (35).to_bytes(32, "big").hex(),
},
Expand Down Expand Up @@ -509,7 +509,7 @@ def test_failing_mainnet_tx2():

# this is correct
assert output == {
"gas_consumed": 8568,
"gas_consumed": 8732,
"gas_price": 21367239423,
"overall_fee": 186590486623319,
}
Expand Down

0 comments on commit a0d281f

Please sign in to comment.