Skip to content

Commit 2631b1a

Browse files
Split deprecated schemas to their respective classes based on versions (#1259)
* issue 1249: split deprecated schemas to their respective classes based on versions * Issue-1249: remove InvokeTransaction object from tests
1 parent b685182 commit 2631b1a

File tree

8 files changed

+138
-89
lines changed

8 files changed

+138
-89
lines changed

starknet_py/net/client_models.py

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,25 @@ def __post_init__(self):
191191

192192

193193
@dataclass
194-
class InvokeTransaction(DeprecatedTransaction):
194+
class InvokeTransactionV0(DeprecatedTransaction):
195195
"""
196-
Dataclass representing invoke transaction.
196+
Dataclass representing invoke transaction v0.
197197
"""
198198

199-
sender_address: int
200199
calldata: List[int]
201-
# This field is always None for transactions with version = 1
202-
entry_point_selector: Optional[int] = None
203-
nonce: Optional[int] = None
200+
contract_address: int
201+
entry_point_selector: int
202+
203+
204+
@dataclass
205+
class InvokeTransactionV1(DeprecatedTransaction):
206+
"""
207+
Dataclass representing invoke transaction v1.
208+
"""
209+
210+
calldata: List[int]
211+
sender_address: int
212+
nonce: int
204213

205214

206215
@dataclass
@@ -209,22 +218,43 @@ class InvokeTransactionV3(TransactionV3):
209218
Dataclass representing invoke transaction v3.
210219
"""
211220

212-
sender_address: int
213221
calldata: List[int]
222+
sender_address: int
223+
nonce: int
214224
account_deployment_data: List[int]
225+
226+
227+
@dataclass
228+
class DeclareTransactionV0(DeprecatedTransaction):
229+
"""
230+
Dataclass representing declare transaction v0.
231+
"""
232+
233+
sender_address: int
234+
class_hash: int
235+
236+
237+
@dataclass
238+
class DeclareTransactionV1(DeprecatedTransaction):
239+
"""
240+
Dataclass representing declare transaction v1.
241+
"""
242+
243+
sender_address: int
244+
class_hash: int
215245
nonce: int
216246

217247

218248
@dataclass
219-
class DeclareTransaction(DeprecatedTransaction):
249+
class DeclareTransactionV2(DeprecatedTransaction):
220250
"""
221-
Dataclass representing declare transaction.
251+
Dataclass representing declare transaction v2.
222252
"""
223253

224-
class_hash: int # Responses to getBlock and getTransaction include the class hash
225254
sender_address: int
226-
compiled_class_hash: Optional[int] = None # only in DeclareV2, hence Optional
227-
nonce: Optional[int] = None
255+
class_hash: int
256+
compiled_class_hash: int
257+
nonce: int
228258

229259

230260
@dataclass
@@ -233,10 +263,10 @@ class DeclareTransactionV3(TransactionV3):
233263
Dataclass representing declare transaction v3.
234264
"""
235265

266+
sender_address: int
236267
class_hash: int
237268
compiled_class_hash: int
238269
nonce: int
239-
sender_address: int
240270
account_deployment_data: List[int]
241271

242272

@@ -252,15 +282,15 @@ class DeployTransaction(Transaction):
252282

253283

254284
@dataclass
255-
class DeployAccountTransaction(DeprecatedTransaction):
285+
class DeployAccountTransactionV1(DeprecatedTransaction):
256286
"""
257-
Dataclass representing deploy account transaction.
287+
Dataclass representing deploy account transaction v1.
258288
"""
259289

290+
nonce: int
260291
contract_address_salt: int
261-
class_hash: int
262292
constructor_calldata: List[int]
263-
nonce: int
293+
class_hash: int
264294

265295

266296
@dataclass
@@ -269,10 +299,10 @@ class DeployAccountTransactionV3(TransactionV3):
269299
Dataclass representing deploy account transaction v3.
270300
"""
271301

302+
nonce: int
272303
contract_address_salt: int
273-
class_hash: int
274304
constructor_calldata: List[int]
275-
nonce: int
305+
class_hash: int
276306

277307

278308
@dataclass

starknet_py/net/schemas/rpc.py

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
ContractsNonce,
1111
DAMode,
1212
DeclaredContractHash,
13-
DeclareTransaction,
1413
DeclareTransactionResponse,
1514
DeclareTransactionTrace,
15+
DeclareTransactionV0,
16+
DeclareTransactionV1,
17+
DeclareTransactionV2,
1618
DeclareTransactionV3,
17-
DeployAccountTransaction,
1819
DeployAccountTransactionResponse,
1920
DeployAccountTransactionTrace,
21+
DeployAccountTransactionV1,
2022
DeployAccountTransactionV3,
2123
DeployedContract,
2224
DeployTransaction,
@@ -28,8 +30,9 @@
2830
ExecutionResources,
2931
FeePayment,
3032
FunctionInvocation,
31-
InvokeTransaction,
3233
InvokeTransactionTrace,
34+
InvokeTransactionV0,
35+
InvokeTransactionV1,
3336
InvokeTransactionV3,
3437
L1HandlerTransaction,
3538
L1HandlerTransactionTrace,
@@ -74,10 +77,7 @@
7477
Uint64,
7578
Uint128,
7679
)
77-
from starknet_py.net.schemas.utils import (
78-
_extract_tx_version,
79-
_replace_invoke_contract_address_with_sender_address,
80-
)
80+
from starknet_py.net.schemas.utils import _extract_tx_version
8181

8282
# pylint: disable=unused-argument, no-self-use
8383

@@ -253,50 +253,77 @@ class TransactionV3Schema(TransactionSchema):
253253
)
254254

255255

256-
class DeprecatedInvokeTransactionSchema(DeprecatedTransactionSchema):
257-
contract_address = Felt(data_key="contract_address", load_default=None)
258-
sender_address = Felt(data_key="sender_address", load_default=None)
259-
entry_point_selector = Felt(data_key="entry_point_selector", load_default=None)
256+
class InvokeTransactionV0Schema(DeprecatedTransactionSchema):
260257
calldata = fields.List(Felt(), data_key="calldata", required=True)
261-
nonce = Felt(data_key="nonce", load_default=None)
258+
contract_address = Felt(data_key="contract_address", required=True)
259+
entry_point_selector = Felt(data_key="entry_point_selector", required=True)
262260

263261
@post_load
264-
def make_transaction(self, data, **kwargs) -> InvokeTransaction:
265-
_replace_invoke_contract_address_with_sender_address(data)
266-
return InvokeTransaction(**data)
262+
def make_transaction(self, data, **kwargs) -> InvokeTransactionV0:
263+
return InvokeTransactionV0(**data)
267264

268265

269-
class InvokeTransactionV3Schema(TransactionV3Schema):
266+
class InvokeTransactionV1Schema(DeprecatedTransactionSchema):
267+
calldata = fields.List(Felt(), data_key="calldata", required=True)
270268
sender_address = Felt(data_key="sender_address", required=True)
269+
nonce = Felt(data_key="nonce", required=True)
270+
271+
@post_load
272+
def make_transaction(self, data, **kwargs) -> InvokeTransactionV1:
273+
return InvokeTransactionV1(**data)
274+
275+
276+
class InvokeTransactionV3Schema(TransactionV3Schema):
271277
calldata = fields.List(Felt(), data_key="calldata", required=True)
278+
sender_address = Felt(data_key="sender_address", required=True)
279+
nonce = Felt(data_key="nonce", required=True)
272280
account_deployment_data = fields.List(
273-
Felt(), data_key="account_deployment_data", load_default=[]
281+
Felt(), data_key="account_deployment_data", required=True
274282
)
275-
nonce = Felt(data_key="nonce", required=True)
276283

277284
@post_load
278285
def make_transaction(self, data, **kwargs) -> InvokeTransactionV3:
279286
return InvokeTransactionV3(**data)
280287

281288

282-
class DeprecatedDeclareTransactionSchema(DeprecatedTransactionSchema):
289+
class DeclareTransactionV0Schema(DeprecatedTransactionSchema):
290+
sender_address = Felt(data_key="sender_address", required=True)
283291
class_hash = Felt(data_key="class_hash", required=True)
292+
293+
@post_load
294+
def make_dataclass(self, data, **kwargs) -> DeclareTransactionV0:
295+
return DeclareTransactionV0(**data)
296+
297+
298+
class DeclareTransactionV1Schema(DeprecatedTransactionSchema):
284299
sender_address = Felt(data_key="sender_address", required=True)
285-
nonce = Felt(data_key="nonce", load_default=None)
286-
compiled_class_hash = Felt(data_key="compiled_class_hash", load_default=None)
300+
class_hash = Felt(data_key="class_hash", required=True)
301+
nonce = Felt(data_key="nonce", required=True)
287302

288303
@post_load
289-
def make_dataclass(self, data, **kwargs) -> DeclareTransaction:
290-
return DeclareTransaction(**data)
304+
def make_dataclass(self, data, **kwargs) -> DeclareTransactionV1:
305+
return DeclareTransactionV1(**data)
291306

292307

293-
class DeclareTransactionV3Schema(TransactionV3Schema):
308+
class DeclareTransactionV2Schema(DeprecatedTransactionSchema):
309+
sender_address = Felt(data_key="sender_address", required=True)
294310
class_hash = Felt(data_key="class_hash", required=True)
295-
compiled_class_hash = Felt(data_key="compiled_class_hash", load_default=None)
311+
compiled_class_hash = Felt(data_key="compiled_class_hash", required=True)
296312
nonce = Felt(data_key="nonce", required=True)
313+
314+
@post_load
315+
def make_dataclass(self, data, **kwargs) -> DeclareTransactionV2:
316+
return DeclareTransactionV2(**data)
317+
318+
319+
class DeclareTransactionV3Schema(TransactionV3Schema):
297320
sender_address = Felt(data_key="sender_address", required=True)
321+
class_hash = Felt(data_key="class_hash", required=True)
322+
323+
compiled_class_hash = Felt(data_key="compiled_class_hash", required=True)
324+
nonce = Felt(data_key="nonce", required=True)
298325
account_deployment_data = fields.List(
299-
Felt(), data_key="account_deployment_data", load_default=[]
326+
Felt(), data_key="account_deployment_data", required=True
300327
)
301328

302329
@post_load
@@ -316,26 +343,26 @@ def make_dataclass(self, data, **kwargs) -> DeployTransaction:
316343
return DeployTransaction(**data)
317344

318345

319-
class DeprecatedDeployAccountTransactionSchema(DeprecatedTransactionSchema):
346+
class DeployAccountTransactionV1Schema(DeprecatedTransactionSchema):
347+
nonce = Felt(data_key="nonce", required=True)
320348
contract_address_salt = Felt(data_key="contract_address_salt", required=True)
321349
constructor_calldata = fields.List(
322350
Felt(), data_key="constructor_calldata", required=True
323351
)
324352
class_hash = Felt(data_key="class_hash", required=True)
325-
nonce = Felt(data_key="nonce", load_default=None)
326353

327354
@post_load
328-
def make_dataclass(self, data, **kwargs) -> DeployAccountTransaction:
329-
return DeployAccountTransaction(**data)
355+
def make_dataclass(self, data, **kwargs) -> DeployAccountTransactionV1:
356+
return DeployAccountTransactionV1(**data)
330357

331358

332359
class DeployAccountTransactionV3Schema(TransactionV3Schema):
360+
nonce = Felt(data_key="nonce", required=True)
333361
contract_address_salt = Felt(data_key="contract_address_salt", required=True)
334362
constructor_calldata = fields.List(
335363
Felt(), data_key="constructor_calldata", required=True
336364
)
337365
class_hash = Felt(data_key="class_hash", required=True)
338-
nonce = Felt(data_key="nonce", required=True)
339366

340367
@post_load
341368
def make_dataclass(self, data, **kwargs) -> DeployAccountTransactionV3:
@@ -344,9 +371,9 @@ def make_dataclass(self, data, **kwargs) -> DeployAccountTransactionV3:
344371

345372
class DeclareTransactionSchema(OneOfSchema):
346373
type_schemas = {
347-
0: DeprecatedDeclareTransactionSchema,
348-
1: DeprecatedDeclareTransactionSchema,
349-
2: DeprecatedDeclareTransactionSchema,
374+
0: DeclareTransactionV0Schema,
375+
1: DeclareTransactionV1Schema,
376+
2: DeclareTransactionV2Schema,
350377
3: DeclareTransactionV3Schema,
351378
}
352379

@@ -356,8 +383,8 @@ def get_data_type(self, data):
356383

357384
class InvokeTransactionSchema(OneOfSchema):
358385
type_schemas = {
359-
0: DeprecatedInvokeTransactionSchema,
360-
1: DeprecatedInvokeTransactionSchema,
386+
0: InvokeTransactionV0Schema,
387+
1: InvokeTransactionV1Schema,
361388
3: InvokeTransactionV3Schema,
362389
}
363390

@@ -367,7 +394,7 @@ def get_data_type(self, data):
367394

368395
class DeployAccountTransactionSchema(OneOfSchema):
369396
type_schemas = {
370-
1: DeprecatedDeployAccountTransactionSchema,
397+
1: DeployAccountTransactionV1Schema,
371398
3: DeployAccountTransactionV3Schema,
372399
}
373400

starknet_py/net/schemas/utils.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@
33
from starknet_py.constants import QUERY_VERSION_BASE
44

55

6-
def _replace_invoke_contract_address_with_sender_address(data: dict):
7-
data["sender_address"] = data.get("sender_address") or data.get("contract_address")
8-
9-
if data["sender_address"] is None:
10-
raise ValueError(
11-
"Missing field `contract_address` or `sender_address` for InvokeTransactionSchema."
12-
)
13-
14-
del data["contract_address"]
15-
16-
176
def _extract_tx_version(version: Union[int, str]):
187
if isinstance(version, str):
198
version = int(version, 16)

starknet_py/tests/e2e/account/account_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from starknet_py.net.client_errors import ClientError
1212
from starknet_py.net.client_models import (
1313
Call,
14-
DeployAccountTransaction,
1514
DeployAccountTransactionResponse,
15+
DeployAccountTransactionV1,
1616
DeployAccountTransactionV3,
1717
EstimatedFee,
1818
InvokeTransactionV3,
@@ -466,7 +466,7 @@ async def test_deploy_account_v1(client, deploy_account_details_factory, map_con
466466
assert account.address == address
467467

468468
transaction = await client.get_transaction(tx_hash=deploy_result.hash)
469-
assert isinstance(transaction, DeployAccountTransaction)
469+
assert isinstance(transaction, DeployAccountTransactionV1)
470470
assert transaction.constructor_calldata == [key_pair.public_key]
471471

472472
res = await account.execute_v1(
@@ -615,7 +615,7 @@ async def test_deploy_account_uses_custom_calldata(
615615
)
616616

617617
tx = await client.get_transaction(deploy_result.hash)
618-
assert isinstance(tx, DeployAccountTransaction)
618+
assert isinstance(tx, DeployAccountTransactionV1)
619619
assert tx.constructor_calldata == calldata
620620

621621

0 commit comments

Comments
 (0)