|
10 | 10 | import json |
11 | 11 | from abc import ABC, abstractmethod |
12 | 12 | from dataclasses import dataclass, field |
13 | | -from typing import List, TypeVar, Union |
| 13 | +from typing import Any, Dict, List, TypeVar, Union |
14 | 14 |
|
| 15 | +import marshmallow |
15 | 16 | import marshmallow_dataclass |
16 | 17 | from marshmallow import fields |
17 | 18 |
|
18 | 19 | from starknet_py.hash.address import compute_address |
19 | 20 | from starknet_py.hash.transaction import ( |
20 | 21 | CommonTransactionV3Fields, |
21 | 22 | TransactionHashPrefix, |
| 23 | + compute_declare_transaction_hash, |
22 | 24 | compute_declare_v2_transaction_hash, |
23 | 25 | compute_declare_v3_transaction_hash, |
24 | 26 | compute_deploy_account_transaction_hash, |
|
28 | 30 | ) |
29 | 31 | from starknet_py.net.client_models import ( |
30 | 32 | DAMode, |
| 33 | + DeprecatedContractClass, |
31 | 34 | ResourceBoundsMapping, |
32 | 35 | SierraContractClass, |
33 | 36 | TransactionType, |
34 | 37 | ) |
35 | 38 | 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 | +) |
37 | 43 |
|
38 | 44 | # TODO (#1219): |
39 | 45 | # consider unifying these classes with client_models |
@@ -175,6 +181,59 @@ def calculate_hash(self, chain_id: int) -> int: |
175 | 181 | ) |
176 | 182 |
|
177 | 183 |
|
| 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 | + |
178 | 237 | @dataclass(frozen=True) |
179 | 238 | class DeployAccountV3(_AccountTransactionV3): |
180 | 239 | """ |
@@ -305,11 +364,12 @@ def calculate_hash(self, chain_id: int) -> int: |
305 | 364 | ) |
306 | 365 |
|
307 | 366 |
|
308 | | -Declare = Union[DeclareV2, DeclareV3] |
| 367 | +Declare = Union[DeclareV1, DeclareV2, DeclareV3] |
309 | 368 | DeployAccount = Union[DeployAccountV1, DeployAccountV3] |
310 | 369 | Invoke = Union[InvokeV1, InvokeV3] |
311 | 370 |
|
312 | 371 | InvokeV1Schema = marshmallow_dataclass.class_schema(InvokeV1) |
| 372 | +DeclareV1Schema = marshmallow_dataclass.class_schema(DeclareV1) |
313 | 373 | DeclareV2Schema = marshmallow_dataclass.class_schema(DeclareV2) |
314 | 374 | DeployAccountV1Schema = marshmallow_dataclass.class_schema(DeployAccountV1) |
315 | 375 |
|
|
0 commit comments