Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Allow fee estimation for version 0 and 1 #268

Merged
merged 2 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
8 changes: 7 additions & 1 deletion starknet_devnet/blueprints/feeder_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
AccountTransaction,
InvokeFunction,
)
from starkware.starknet.services.api.gateway.transaction import Transaction
from starkware.starknet.services.api.feeder_gateway.request_objects import CallFunction
from starkware.starknet.services.api.feeder_gateway.response_objects import (
TransactionSimulationInfo,
Expand Down Expand Up @@ -268,7 +269,12 @@ def get_state_update():
@feeder_gateway.route("/estimate_fee", methods=["POST"])
async def estimate_fee():
"""Returns the estimated fee for a transaction."""
transaction = validate_request(request.data, InvokeFunction)

try:
transaction = validate_request(request.data, Transaction) # version 1
except StarknetDevnetException:
transaction = validate_request(request.data, InvokeFunction) # version 0

_, fee_response = await state.starknet_wrapper.calculate_actual_fee(transaction)
return jsonify(FeeEstimationInfo.load(fee_response))

Expand Down
12 changes: 9 additions & 3 deletions starknet_devnet/starknet_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from .origin import NullOrigin, Origin
from .util import (
DummyExecutionInfo,
StarknetDevnetException,
Uint256,
enable_pickling,
get_storage_diffs,
Expand Down Expand Up @@ -476,9 +477,14 @@ async def calculate_actual_fee(self, external_tx: InvokeFunction):
"""Calculates actual fee"""
state = self.get_state()

internal_tx = InternalInvokeFunctionForSimulate.from_external(
external_tx, state.general_config
)
try:
internal_tx = InternalInvokeFunctionForSimulate.from_external(
external_tx, state.general_config
)
except AssertionError as error:
raise StarknetDevnetException(
status_code=400, message="Invalid format of fee estimation request"
) from error

execution_info = await internal_tx.apply_state_updates(
# pylint: disable=protected-access
Expand Down
14 changes: 11 additions & 3 deletions test/test_estimate_fee.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,19 @@ def test_estimate_fee_with_invalid_data():

json_error_message = resp.json()["message"]
assert resp.status_code == 400
assert "Invalid InvokeFunction" in json_error_message
assert "Invalid format of fee estimation request" in json_error_message


@pytest.mark.estimate_fee
@pytest.mark.parametrize(
"request_kwargs",
[
{}, # tx version 0
{"type": "INVOKE_FUNCTION"}, # tx version 1
],
)
@devnet_in_background("--gas-price", str(DEFAULT_GAS_PRICE))
def test_estimate_fee_with_complete_request_data():
def test_estimate_fee_with_complete_request_data(request_kwargs):
"""Estimate fee with complete request data"""

deploy_info = deploy(CONTRACT_PATH, ["0"])
Expand All @@ -104,10 +111,11 @@ def test_estimate_fee_with_complete_request_data():
"calldata": ["10", "20"],
"max_fee": "0x0",
"entry_point_selector": "0x362398bec32bc0ebb411203221a35a0301193a96f317ebe5e40be9f60d15320",
**request_kwargs,
}
)

assert response.status_code == 200
assert response.status_code == 200, f"Request not OK: {response.json()}"
common_estimate_response(response.json())


Expand Down