Skip to content

Commit fcf48c3

Browse files
authored
Add cairo 0 declaration methods (#1486)
* Revert "Remove `sign_declare_v1` and `declare_v1` methods (#1451)" This reverts commit d8b2436. Conflicts: starknet_py/tests/e2e/fixtures/contracts.py Changes to be committed: modified: starknet_py/contract.py modified: starknet_py/net/account/account.py modified: starknet_py/net/account/base_account.py modified: starknet_py/net/client_models.py modified: starknet_py/net/models/__init__.py modified: starknet_py/net/models/transaction.py modified: starknet_py/net/schemas/rpc/transactions.py * Add proper docstring
1 parent 1844b0d commit fcf48c3

File tree

7 files changed

+203
-4
lines changed

7 files changed

+203
-4
lines changed

starknet_py/contract.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,43 @@ async def from_address(
810810
cairo_version=cairo_version,
811811
)
812812

813+
# pylint: disable=line-too-long
814+
@staticmethod
815+
async def declare_v1(
816+
account: BaseAccount,
817+
compiled_contract: str,
818+
*,
819+
nonce: Optional[int] = None,
820+
max_fee: Optional[int] = None,
821+
auto_estimate: bool = False,
822+
) -> DeclareResult:
823+
"""
824+
Declares a contract.
825+
This method is deprecated, not covered by tests and will be removed in the future.
826+
Please use current version of transaction signing methods.
827+
828+
Based on https://docs.starknet.io/architecture-and-concepts/network-architecture/transactions/#transaction_versioning
829+
830+
:param account: BaseAccount used to sign and send declare transaction.
831+
:param compiled_contract: String containing compiled contract.
832+
:param nonce: Nonce of the transaction.
833+
:param max_fee: Max amount of Wei to be paid when executing transaction.
834+
:param auto_estimate: Use automatic fee estimation (not recommended, as it may lead to high costs).
835+
:return: DeclareResult instance.
836+
"""
837+
838+
declare_tx = await account.sign_declare_v1(
839+
compiled_contract=compiled_contract,
840+
nonce=nonce,
841+
max_fee=max_fee,
842+
auto_estimate=auto_estimate,
843+
)
844+
845+
return await _declare_contract(
846+
declare_tx, account, compiled_contract, cairo_version=0
847+
)
848+
849+
# pylint: enable=line-too-long
813850
@staticmethod
814851
async def declare_v2(
815852
account: BaseAccount,

starknet_py/net/account/account.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import dataclasses
2+
import json
23
from collections import OrderedDict
34
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
45

5-
from starknet_py.common import create_sierra_compiled_contract
6+
from starknet_py.common import create_compiled_contract, create_sierra_compiled_contract
67
from starknet_py.constants import FEE_CONTRACT_ADDRESS, QUERY_VERSION_BASE
78
from starknet_py.hash.address import compute_address
89
from starknet_py.hash.selector import get_selector_from_name
@@ -26,6 +27,7 @@
2627
from starknet_py.net.models.chains import RECOGNIZED_CHAIN_IDS, Chain, parse_chain
2728
from starknet_py.net.models.transaction import (
2829
AccountTransaction,
30+
DeclareV1,
2931
DeclareV2,
3032
DeclareV3,
3133
DeployAccountV1,
@@ -359,6 +361,40 @@ async def sign_invoke_v3(
359361
signature = self.signer.sign_transaction(invoke_tx)
360362
return _add_signature_to_transaction(invoke_tx, signature)
361363

364+
# pylint: disable=line-too-long
365+
async def sign_declare_v1(
366+
self,
367+
compiled_contract: str,
368+
*,
369+
nonce: Optional[int] = None,
370+
max_fee: Optional[int] = None,
371+
auto_estimate: bool = False,
372+
) -> DeclareV1:
373+
"""
374+
This method is deprecated, not covered by tests and will be removed in the future.
375+
Please use current version of transaction signing methods.
376+
377+
Based on https://docs.starknet.io/architecture-and-concepts/network-architecture/transactions/#transaction_versioning
378+
379+
"""
380+
if _is_sierra_contract(json.loads(compiled_contract)):
381+
raise ValueError(
382+
"Signing sierra contracts requires using `sign_declare_v2` method."
383+
)
384+
385+
declare_tx = await self._make_declare_v1_transaction(
386+
compiled_contract, nonce=nonce
387+
)
388+
389+
max_fee = await self._get_max_fee(
390+
transaction=declare_tx, max_fee=max_fee, auto_estimate=auto_estimate
391+
)
392+
declare_tx = _add_max_fee_to_transaction(declare_tx, max_fee)
393+
signature = self.signer.sign_transaction(declare_tx)
394+
return _add_signature_to_transaction(declare_tx, signature)
395+
396+
# pylint: enable=line-too-long
397+
362398
async def sign_declare_v2(
363399
self,
364400
compiled_contract: str,
@@ -400,6 +436,24 @@ async def sign_declare_v3(
400436
signature = self.signer.sign_transaction(declare_tx)
401437
return _add_signature_to_transaction(declare_tx, signature)
402438

439+
async def _make_declare_v1_transaction(
440+
self, compiled_contract: str, *, nonce: Optional[int] = None
441+
) -> DeclareV1:
442+
contract_class = create_compiled_contract(compiled_contract=compiled_contract)
443+
444+
if nonce is None:
445+
nonce = await self.get_nonce()
446+
447+
declare_tx = DeclareV1(
448+
contract_class=contract_class.convert_to_deprecated_contract_class(),
449+
sender_address=self.address,
450+
max_fee=0,
451+
signature=[],
452+
nonce=nonce,
453+
version=1,
454+
)
455+
return declare_tx
456+
403457
async def _make_declare_v2_transaction(
404458
self,
405459
compiled_contract: str,

starknet_py/net/account/base_account.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from starknet_py.net.models import AddressRepresentation
1414
from starknet_py.net.models.transaction import (
1515
AccountTransaction,
16+
DeclareV1,
1617
DeclareV2,
1718
DeclareV3,
1819
DeployAccountV1,
@@ -157,6 +158,31 @@ async def sign_invoke_v3(
157158
:return: Invoke created from the calls.
158159
"""
159160

161+
# pylint: disable=line-too-long
162+
@abstractmethod
163+
async def sign_declare_v1(
164+
self,
165+
compiled_contract: str,
166+
*,
167+
nonce: Optional[int] = None,
168+
max_fee: Optional[int] = None,
169+
auto_estimate: bool = False,
170+
) -> DeclareV1:
171+
"""
172+
This method is deprecated, not covered by tests and will be removed in the future.
173+
Please use current version of transaction signing methods.
174+
175+
Based on https://docs.starknet.io/architecture-and-concepts/network-architecture/transactions/#transaction_versioning
176+
177+
:param compiled_contract: string containing a compiled Starknet contract. Supports old contracts.
178+
:param nonce: Nonce of the transaction.
179+
:param max_fee: Max amount of Wei to be paid when executing transaction.
180+
:param auto_estimate: Use automatic fee estimation, not recommend as it may lead to high costs.
181+
:return: Signed Declare transaction.
182+
"""
183+
184+
# pylint: enable=line-too-long
185+
160186
@abstractmethod
161187
async def sign_declare_v2(
162188
self,

starknet_py/net/client_models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,16 @@ class InvokeTransactionV3(TransactionV3):
258258
account_deployment_data: List[int]
259259

260260

261+
@dataclass
262+
class DeclareTransactionV0(DeprecatedTransaction):
263+
"""
264+
Dataclass representing declare transaction v0.
265+
"""
266+
267+
sender_address: int
268+
class_hash: int
269+
270+
261271
@dataclass
262272
class DeclareTransactionV1(DeprecatedTransaction):
263273
"""

starknet_py/net/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .chains import StarknetChainId, chain_from_network
33
from .transaction import (
44
AccountTransaction,
5+
DeclareV1,
56
DeclareV2,
67
DeclareV3,
78
DeployAccountV1,

starknet_py/net/models/transaction.py

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010
import json
1111
from abc import ABC, abstractmethod
1212
from dataclasses import dataclass, field
13-
from typing import List, TypeVar, Union
13+
from typing import Any, Dict, List, TypeVar, Union
1414

15+
import marshmallow
1516
import marshmallow_dataclass
1617
from marshmallow import fields
1718

1819
from starknet_py.hash.address import compute_address
1920
from starknet_py.hash.transaction import (
2021
CommonTransactionV3Fields,
2122
TransactionHashPrefix,
23+
compute_declare_transaction_hash,
2224
compute_declare_v2_transaction_hash,
2325
compute_declare_v3_transaction_hash,
2426
compute_deploy_account_transaction_hash,
@@ -28,12 +30,16 @@
2830
)
2931
from starknet_py.net.client_models import (
3032
DAMode,
33+
DeprecatedContractClass,
3134
ResourceBoundsMapping,
3235
SierraContractClass,
3336
TransactionType,
3437
)
3538
from starknet_py.net.schemas.common import Felt
36-
from starknet_py.net.schemas.rpc.contract import SierraContractClassSchema
39+
from starknet_py.net.schemas.rpc.contract import (
40+
ContractClassSchema,
41+
SierraContractClassSchema,
42+
)
3743

3844
# TODO (#1219):
3945
# consider unifying these classes with client_models
@@ -175,6 +181,59 @@ def calculate_hash(self, chain_id: int) -> int:
175181
)
176182

177183

184+
# pylint: disable=line-too-long
185+
@dataclass(frozen=True)
186+
class DeclareV1(_DeprecatedAccountTransaction):
187+
"""
188+
This class is deprecated, not covered by tests and will be removed in the future.
189+
Please use current version of transactions.
190+
191+
Based on https://docs.starknet.io/architecture-and-concepts/network-architecture/transactions/#transaction_versioning
192+
193+
Represents a transaction in the Starknet network that is a declaration of a Starknet contract
194+
class.
195+
"""
196+
197+
# The class to be declared, included for all methods involving execution (estimateFee, simulateTransactions)
198+
contract_class: DeprecatedContractClass = field(
199+
metadata={"marshmallow_field": fields.Nested(ContractClassSchema())}
200+
)
201+
# The address of the account contract sending the declaration transaction.
202+
sender_address: int = field(metadata={"marshmallow_field": Felt()})
203+
204+
@property
205+
def type(self) -> TransactionType:
206+
return TransactionType.DECLARE
207+
208+
@marshmallow.post_dump
209+
def post_dump(self, data: Dict[str, Any], **kwargs) -> Dict[str, Any]:
210+
# Allowing **kwargs is needed here because marshmallow is passing additional parameters here
211+
# along with data, which we don't handle.
212+
# pylint: disable=unused-argument, no-self-use
213+
return compress_program(data)
214+
215+
@marshmallow.pre_load
216+
def pre_load(self, data: Dict[str, Any], **kwargs) -> Dict[str, Any]:
217+
# pylint: disable=unused-argument, no-self-use
218+
return decompress_program(data)
219+
220+
def calculate_hash(self, chain_id: int) -> int:
221+
"""
222+
Calculates the transaction hash in the Starknet network.
223+
"""
224+
return compute_declare_transaction_hash(
225+
contract_class=self.contract_class,
226+
chain_id=chain_id,
227+
sender_address=self.sender_address,
228+
max_fee=self.max_fee,
229+
version=self.version,
230+
nonce=self.nonce,
231+
)
232+
233+
234+
# pylint: enable=line-too-long
235+
236+
178237
@dataclass(frozen=True)
179238
class DeployAccountV3(_AccountTransactionV3):
180239
"""
@@ -305,11 +364,12 @@ def calculate_hash(self, chain_id: int) -> int:
305364
)
306365

307366

308-
Declare = Union[DeclareV2, DeclareV3]
367+
Declare = Union[DeclareV1, DeclareV2, DeclareV3]
309368
DeployAccount = Union[DeployAccountV1, DeployAccountV3]
310369
Invoke = Union[InvokeV1, InvokeV3]
311370

312371
InvokeV1Schema = marshmallow_dataclass.class_schema(InvokeV1)
372+
DeclareV1Schema = marshmallow_dataclass.class_schema(DeclareV1)
313373
DeclareV2Schema = marshmallow_dataclass.class_schema(DeclareV2)
314374
DeployAccountV1Schema = marshmallow_dataclass.class_schema(DeployAccountV1)
315375

starknet_py/net/schemas/rpc/transactions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from starknet_py.net.client_models import (
55
DAMode,
66
DeclareTransactionResponse,
7+
DeclareTransactionV0,
78
DeclareTransactionV1,
89
DeclareTransactionV2,
910
DeclareTransactionV3,
@@ -174,6 +175,15 @@ def make_transaction(self, data, **kwargs) -> InvokeTransactionV3:
174175
return InvokeTransactionV3(**data)
175176

176177

178+
class DeclareTransactionV0Schema(DeprecatedTransactionSchema):
179+
sender_address = Felt(data_key="sender_address", required=True)
180+
class_hash = Felt(data_key="class_hash", required=True)
181+
182+
@post_load
183+
def make_dataclass(self, data, **kwargs) -> DeclareTransactionV0:
184+
return DeclareTransactionV0(**data)
185+
186+
177187
class DeclareTransactionV1Schema(DeprecatedTransactionSchema):
178188
sender_address = Felt(data_key="sender_address", required=True)
179189
class_hash = Felt(data_key="class_hash", required=True)
@@ -250,6 +260,7 @@ def make_dataclass(self, data, **kwargs) -> DeployAccountTransactionV3:
250260

251261
class DeclareTransactionSchema(OneOfSchema):
252262
type_schemas = {
263+
"0": DeclareTransactionV0Schema,
253264
"1": DeclareTransactionV1Schema,
254265
"2": DeclareTransactionV2Schema,
255266
"3": DeclareTransactionV3Schema,

0 commit comments

Comments
 (0)