Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support RPC 0.8.0 #1510

Draft
wants to merge 21 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
931f4cd
Support `starknet_getStorageProof`
franciszekjob Oct 28, 2024
f1f5d50
Support failure reason in transaction status
franciszekjob Oct 28, 2024
3fe3460
Support `starknet_getMessagesStatus`
franciszekjob Oct 28, 2024
ecf4f06
Adapt execution resources
franciszekjob Oct 28, 2024
5d15915
Change `l1_resource_bounds` to `resource_bounds`
franciszekjob Oct 29, 2024
f0a5bfd
Rename `DataResources` to `InnerCallExecutionResources`; Update `Esti…
franciszekjob Oct 29, 2024
b8dc86a
Add todo in migration guide
franciszekjob Oct 29, 2024
ddea26e
Format
franciszekjob Oct 29, 2024
19036e4
Update resource bounds in `test_invoke`
franciszekjob Oct 29, 2024
e832c30
Make `block_id` required in `get_storage_proof`
franciszekjob Oct 30, 2024
b182eff
Update docstring in `to_resource_bounds`
franciszekjob Oct 30, 2024
e82c017
Minor fix in `StorageProofResponse`
franciszekjob Oct 30, 2024
bb2d82e
Update `rousurce_bounds` param in docs
franciszekjob Oct 30, 2024
9621f36
Add missing `make_dataclass` in schemas
franciszekjob Oct 30, 2024
e6e7c53
Refactor `MerkleNodeSchema`
franciszekjob Oct 30, 2024
3b7ffe2
Remove unused import
franciszekjob Oct 31, 2024
6e11803
Update `execution_resources` type to `InnerCallExecutionResources` in…
franciszekjob Nov 1, 2024
76e789c
Use `MAX_RESOURCE_BOUNDS`
franciszekjob Nov 1, 2024
bae1f49
Refactor `EstimatedFee.to_resource_bounds()`
franciszekjob Nov 4, 2024
bd9d693
Fix `resource_bounds` params name
franciszekjob Nov 8, 2024
b8748e0
Add default values in `get_storage_proof()`
franciszekjob Nov 8, 2024
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
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Example usage:

```python
from starknet_py.contract import Contract
from starknet_py.net.client_models import ResourceBounds
from starknet_py.net.client_models import ResourceBounds, ResourceBoundsMapping
l1_resource_bounds = ResourceBounds(
max_amount=int(1e5), max_price_per_unit=int(1e13)
)
Expand All @@ -111,12 +111,12 @@ declare_result = await Contract.declare_v3(
account,
compiled_contract=compiled_contract,
compiled_class_hash=class_hash,
l1_resource_bounds=l1_resource_bounds,
resource_bounds=resource_bounds,
)

await declare_result.wait_for_acceptance()
deploy_result = await declare_result.deploy_v3(
l1_resource_bounds=l1_resource_bounds,
resource_bounds=resource_bounds,
)
# Wait until deployment transaction is accepted
await deploy_result.wait_for_acceptance()
Expand All @@ -126,13 +126,15 @@ map_contract = deploy_result.deployed_contract
k, v = 13, 4324
# Adds a transaction to mutate the state of k-v store. The call goes through account proxy, because we've used
# Account to create the contract object
resource_bounds = ResourceBoundsMapping(
l1_gas = ResourceBounds(max_amount=int(1e5), max_price_per_unit=int(1e13)),
l2_gas = ResourceBounds.init_with_zeros()
)
await (
await map_contract.functions["put"].invoke_v3(
k,
v,
l1_resource_bounds=ResourceBounds(
max_amount=int(1e5), max_price_per_unit=int(1e13)
),
resource_bounds=resource_bounds,
)
).wait_for_acceptance()

Expand All @@ -150,7 +152,7 @@ calls = [
# Executes only one transaction with prepared calls
transaction_response = await account.execute_v3(
calls=calls,
l1_resource_bounds=l1_resource_bounds,
resource_bounds=resource_bounds,
)
await account.client.wait_for_tx(transaction_response.transaction_hash)
```
Expand Down
2 changes: 1 addition & 1 deletion docs/account_creation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ Here is step by step example:
If you are experiencing transaction failures with ``FEE_TRANSFER_FAILURE``
make sure that the address you are trying to deploy is prefunded with enough
tokens, and verify that ``max_fee`` argument in :meth:`~starknet_py.net.account.account.Account.sign_deploy_account_v1`
or ``l1_resource_bounds`` argument in :meth:`~starknet_py.net.account.account.Account.sign_deploy_account_v3` is set
or ``resource_bounds`` argument in :meth:`~starknet_py.net.account.account.Account.sign_deploy_account_v3` is set
to a high enough value.
2 changes: 1 addition & 1 deletion docs/guide/account_and_client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Transaction Fee

All methods within the :ref:`Account` that involve on-chain modifications require either specifying a maximum transaction fee or using auto estimation.
In the case of V1 and V2 transactions, the transaction fee, denoted in Wei, is configured by the ``max_fee`` parameter.
For V3 transactions, however, the fee is expressed in Fri and is determined by the ``l1_resource_bounds`` parameter.
For V3 transactions, however, the fee is expressed in Fri and is determined by the ``resource_bounds`` parameter.
To enable auto estimation, set the ``auto_estimate`` parameter to ``True``.

.. code-block:: python
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/using_existing_contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Alternatively, you can estimate fee automatically, as described in the :ref:`aut
await contract.functions["put"].invoke_v1(k, v, max_fee=5000)

The ``max_fee`` argument can be also defined in :meth:`~ContractFunction.prepare_invoke_v1`. Subsequently, the :meth:`~PreparedFunctionInvokeV1.invoke` method on a prepared call can be used either with ``max_fee`` omitted or with its value overridden.
The same behavior applies to :meth:`~ContractFunction.prepare_invoke_v3` and ``l1_resource_bounds``.
The same behavior applies to :meth:`~ContractFunction.prepare_invoke_v3` and ``resource_bounds``.

.. code-block:: python

Expand All @@ -60,7 +60,7 @@ The same behavior applies to :meth:`~ContractFunction.prepare_invoke_v3` and ``l
.. warning::

For V1 transactions if ``max_fee`` is not specified at any step it will default to ``None``,
and will raise an exception when invoking a transaction, unless `auto_estimate` is specified and is set to `True`. The same applies to ``l1_resource_bounds`` and V3 transactions.
and will raise an exception when invoking a transaction, unless `auto_estimate` is specified and is set to `True`. The same applies to ``resource_bounds`` and V3 transactions.

Please note you will need to have enough Wei (for V1 transaction) or Fri (for V3 transaction) in your Starknet account otherwise
transaction will be rejected.
Expand Down
16 changes: 15 additions & 1 deletion docs/migration_guide.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
Migration guide
===============

******************************
0.25.0 Migration guide
******************************

Version 0.25.0 of **starknet.py** comes with support for RPC 0.8.0!

0.25.0 Targeted versions
------------------------

- Starknet - `0.13.3 <https://docs.starknet.io/documentation/starknet_versions/version_notes/#version0.13.3>`_
- RPC - `0.8.0 <https://github.com/starkware-libs/starknet-specs/releases/tag/v0.8.0>`_

TODO (#1498): List changes
Copy link
Collaborator Author

@franciszekjob franciszekjob Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Migration guide will be updated once we confirm that all introduced changes are correct.


******************************
0.24.2 Migration guide
******************************
Expand Down Expand Up @@ -186,7 +200,7 @@ Changes in the :class:`~starknet_py.net.account.account.Account`:
- :meth:`sign_declare_transaction`, :meth:`sign_declare_v2_transaction`, :meth:`sign_deploy_account_transaction` and :meth:`sign_invoke_transaction` have been renamed to :meth:`~Account.sign_declare_v1`, :meth:`~Account.sign_declare_v2`, :meth:`~Account.sign_deploy_account_v1` and :meth:`~Account.sign_invoke_v1` respectively

All new functions with ``v3`` in their name operate similarly to their ``v1`` and ``v2`` counterparts.
Unlike their ``v1`` counterparts however, ``v3`` transaction fees are paid in Fri (10^-18 STRK). Therefore, ``max_fee`` parameter, which is typically set in Wei, is not applicable for ``v3`` functions. Instead, ``l1_resource_bounds`` parameter is utilized to limit the Fri amount used.
Unlike their ``v1`` counterparts however, ``v3`` transaction fees are paid in Fri (10^-18 STRK). Therefore, ``max_fee`` parameter, which is typically set in Wei, is not applicable for ``v3`` functions. Instead, ``resource_bounds`` parameter is utilized to limit the Fri amount used.
The same applies to the new ``v3`` methods in the :class:`~starknet_py.contract.Contract` class.

Changes in the :class:`~starknet_py.net.full_node_client.FullNodeClient`:
Expand Down
50 changes: 29 additions & 21 deletions starknet_py/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
from starknet_py.hash.selector import get_selector_from_name
from starknet_py.net.account.base_account import BaseAccount
from starknet_py.net.client import Client
from starknet_py.net.client_models import Call, EstimatedFee, Hash, ResourceBounds, Tag
from starknet_py.net.client_models import (
Call,
EstimatedFee,
Hash,
ResourceBoundsMapping,
Tag,
)
from starknet_py.net.models import AddressRepresentation, parse_address
from starknet_py.net.models.transaction import Declare, Invoke
from starknet_py.net.udc_deployer.deployer import Deployer
Expand Down Expand Up @@ -231,7 +237,7 @@ async def deploy_v3(
unique: bool = True,
constructor_args: Optional[Union[List, Dict]] = None,
nonce: Optional[int] = None,
l1_resource_bounds: Optional[ResourceBounds] = None,
resource_bounds: Optional[ResourceBoundsMapping] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe having 2 parameters l1_resource_bounds and l2_resource_bounds would be more convenient?

Copy link
Collaborator Author

@franciszekjob franciszekjob Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh I don't think it's good to pass all fields of data class separately.
Maybe we can set l1_gas and l2_gas as optional in ResourceBoundsMapping (if not passed they will be zero)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave a parameter only for ResourceBoundsMapping as it is now, but please add a method in ResourceBoundsMapping that allows to specify only l1_gas and sets the rest of the fields to 0

auto_estimate: bool = False,
) -> "DeployResult":
"""
Expand All @@ -244,7 +250,7 @@ async def deploy_v3(
:param unique: Determines if the contract should be salted with the account address.
:param constructor_args: a ``list`` or ``dict`` of arguments for the constructor.
:param nonce: Nonce of the transaction with call to deployer.
:param l1_resource_bounds: Max amount and max price per unit of L1 gas (in Fri) used when executing
:param resource_bounds: Max amount and max price per unit of L1 and L2 gas (in Fri) used when executing
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description needs to be updated in all places (perhaps in the docs as well)

this transaction.
:param auto_estimate: Use automatic fee estimation (not recommended, as it may lead to high costs).
:return: DeployResult instance.
Expand All @@ -260,7 +266,7 @@ async def deploy_v3(
deployer_address=deployer_address,
cairo_version=self._cairo_version,
nonce=nonce,
l1_resource_bounds=l1_resource_bounds,
resource_bounds=resource_bounds,
auto_estimate=auto_estimate,
salt=salt,
unique=unique,
Expand Down Expand Up @@ -462,19 +468,19 @@ class PreparedFunctionInvokeV3(PreparedFunctionInvoke):
Prepared date to send an InvokeV3 transaction.
"""

l1_resource_bounds: Optional[ResourceBounds]
resource_bounds: Optional[ResourceBoundsMapping]

async def invoke(
self,
l1_resource_bounds: Optional[ResourceBounds] = None,
resource_bounds: Optional[ResourceBoundsMapping] = None,
auto_estimate: bool = False,
*,
nonce: Optional[int] = None,
) -> InvokeResult:
"""
Send an Invoke transaction version 3 for the prepared data.

:param l1_resource_bounds: Max amount and max price per unit of L1 gas (in Fri) used when executing
:param resource_bounds: Max amount and max price per unit of L1 and L2 gas (in Fri) used when executing
this transaction.
:param auto_estimate: Use automatic fee estimation (not recommended, as it may lead to high costs).
:param nonce: Nonce of the transaction.
Expand All @@ -484,7 +490,7 @@ async def invoke(
transaction = await self.get_account.sign_invoke_v3(
calls=self,
nonce=nonce,
l1_resource_bounds=l1_resource_bounds or self.l1_resource_bounds,
resource_bounds=resource_bounds or self.resource_bounds,
auto_estimate=auto_estimate,
)

Expand All @@ -498,7 +504,9 @@ async def estimate_fee(
nonce: Optional[int] = None,
) -> EstimatedFee:
tx = await self.get_account.sign_invoke_v3(
calls=self, nonce=nonce, l1_resource_bounds=ResourceBounds.init_with_zeros()
calls=self,
nonce=nonce,
resource_bounds=ResourceBoundsMapping.init_with_zeros(),
)
estimate_tx = await self.get_account.sign_for_fee_estimate(transaction=tx)

Expand Down Expand Up @@ -652,15 +660,15 @@ async def invoke_v1(
def prepare_invoke_v3(
self,
*args,
l1_resource_bounds: Optional[ResourceBounds] = None,
resource_bounds: Optional[ResourceBoundsMapping] = None,
**kwargs,
) -> PreparedFunctionInvokeV3:
"""
``*args`` and ``**kwargs`` are translated into Cairo calldata.
Creates a ``PreparedFunctionInvokeV3`` instance which exposes calldata for every argument
and adds more arguments when calling methods.

:param l1_resource_bounds: Max amount and max price per unit of L1 gas (in Fri) used when executing
:param resource_bounds: Max amount and max price per unit of L1 and L2 gas (in Fri) used when executing
this transaction.
:return: PreparedFunctionInvokeV3.
"""
Expand All @@ -670,7 +678,7 @@ def prepare_invoke_v3(
to_addr=self.contract_data.address,
calldata=calldata,
selector=self.get_selector(self.name),
l1_resource_bounds=l1_resource_bounds,
resource_bounds=resource_bounds,
_contract_data=self.contract_data,
_client=self.client,
_account=self.account,
Expand All @@ -680,7 +688,7 @@ def prepare_invoke_v3(
async def invoke_v3(
self,
*args,
l1_resource_bounds: Optional[ResourceBounds] = None,
resource_bounds: Optional[ResourceBoundsMapping] = None,
auto_estimate: bool = False,
nonce: Optional[int] = None,
**kwargs,
Expand All @@ -689,15 +697,15 @@ async def invoke_v3(
Invoke contract's function. ``*args`` and ``**kwargs`` are translated into Cairo calldata.
Equivalent of ``.prepare_invoke_v3(*args, **kwargs).invoke()``.

:param l1_resource_bounds: Max amount and max price per unit of L1 gas (in Fri) used when executing
:param resource_bounds: Max amount and max price per unit of L1 and L2 gas (in Fri) used when executing
this transaction.
:param auto_estimate: Use automatic fee estimation (not recommended, as it may lead to high costs).
:param nonce: Nonce of the transaction.
:return: InvokeResult.
"""
prepared_invoke = self.prepare_invoke_v3(*args, **kwargs)
return await prepared_invoke.invoke(
l1_resource_bounds=l1_resource_bounds,
resource_bounds=resource_bounds,
nonce=nonce,
auto_estimate=auto_estimate,
)
Expand Down Expand Up @@ -896,7 +904,7 @@ async def declare_v3(
compiled_contract_casm: Optional[str] = None,
compiled_class_hash: Optional[int] = None,
nonce: Optional[int] = None,
l1_resource_bounds: Optional[ResourceBounds] = None,
resource_bounds: Optional[ResourceBoundsMapping] = None,
auto_estimate: bool = False,
) -> DeclareResult:
# pylint: disable=too-many-arguments
Expand All @@ -909,7 +917,7 @@ async def declare_v3(
:param compiled_contract_casm: String containing the content of the starknet-sierra-compile (.casm file).
:param compiled_class_hash: Hash of the compiled_contract_casm.
:param nonce: Nonce of the transaction.
:param l1_resource_bounds: Max amount and max price per unit of L1 gas (in Fri) used when executing
:param resource_bounds: Max amount and max price per unit of L1 and L2 gas (in Fri) used when executing
this transaction.
:param auto_estimate: Use automatic fee estimation (not recommended, as it may lead to high costs).
:return: DeclareResult instance.
Expand All @@ -923,7 +931,7 @@ async def declare_v3(
compiled_contract=compiled_contract,
compiled_class_hash=compiled_class_hash,
nonce=nonce,
l1_resource_bounds=l1_resource_bounds,
resource_bounds=resource_bounds,
auto_estimate=auto_estimate,
)

Expand Down Expand Up @@ -1006,7 +1014,7 @@ async def deploy_contract_v3(
deployer_address: AddressRepresentation = DEFAULT_DEPLOYER_ADDRESS,
cairo_version: int = 1,
nonce: Optional[int] = None,
l1_resource_bounds: Optional[ResourceBounds] = None,
resource_bounds: Optional[ResourceBoundsMapping] = None,
auto_estimate: bool = False,
salt: Optional[int] = None,
unique: bool = True,
Expand All @@ -1024,7 +1032,7 @@ async def deploy_contract_v3(
:param cairo_version: Version of the Cairo in which contract is written.
By default, it is set to 1.
:param nonce: Nonce of the transaction.
:param l1_resource_bounds: Max amount and max price per unit of L1 gas (in Fri) used when executing
:param resource_bounds: Max amount and max price per unit of L1 and L2 gas (in Fri) used when executing
this transaction.
:param auto_estimate: Use automatic fee estimation (not recommended, as it may lead to high costs).
:param salt: Optional salt. Random value is selected if it is not provided.
Expand All @@ -1047,7 +1055,7 @@ async def deploy_contract_v3(
res = await account.execute_v3(
calls=deploy_call,
nonce=nonce,
l1_resource_bounds=l1_resource_bounds,
resource_bounds=resource_bounds,
auto_estimate=auto_estimate,
)

Expand Down
Loading
Loading