2323 Tag ,
2424)
2525from starknet_py .net .full_node_client import FullNodeClient
26- from starknet_py .net .models import AddressRepresentation , StarknetChainId , parse_address
26+ from starknet_py .net .models import AddressRepresentation , parse_address
27+ from starknet_py .net .models .chains import RECOGNIZED_CHAIN_IDS , Chain , parse_chain
2728from starknet_py .net .models .transaction import (
2829 AccountTransaction ,
2930 DeclareV1 ,
@@ -72,7 +73,7 @@ def __init__(
7273 client : Client ,
7374 signer : Optional [BaseSigner ] = None ,
7475 key_pair : Optional [KeyPair ] = None ,
75- chain : Optional [StarknetChainId ] = None ,
76+ chain : Optional [Chain ] = None ,
7677 ):
7778 """
7879 :param address: Address of the account contract.
@@ -81,11 +82,18 @@ def __init__(
8182 If none is provided, default
8283 :py:class:`starknet_py.net.signer.stark_curve_signer.StarkCurveSigner` is used.
8384 :param key_pair: Key pair that will be used to create a default `Signer`.
84- :param chain: ChainId of the chain used to create the default signer.
85+ :param chain: Chain ID associated with the account.
86+ This can be supplied in multiple formats:
87+
88+ - an enum :py:class:`starknet_py.net.models.StarknetChainId`
89+ - a string name (e.g. 'SN_SEPOLIA')
90+ - a hexadecimal value (e.g. '0x1')
91+ - an integer (e.g. 1)
8592 """
8693 self ._address = parse_address (address )
8794 self ._client = client
8895 self ._cairo_version = None
96+ self ._chain_id = None if chain is None else parse_chain (chain )
8997
9098 if signer is not None and key_pair is not None :
9199 raise ValueError ("Arguments signer and key_pair are mutually exclusive." )
@@ -95,14 +103,13 @@ def __init__(
95103 raise ValueError (
96104 "Either a signer or a key_pair must be provided in Account constructor."
97105 )
98- if chain is None :
106+ if self . _chain_id is None :
99107 raise ValueError ("One of chain or signer must be provided." )
100108
101109 signer = StarkCurveSigner (
102- account_address = self .address , key_pair = key_pair , chain_id = chain
110+ account_address = self .address , key_pair = key_pair , chain_id = self . _chain_id
103111 )
104112 self .signer : BaseSigner = signer
105- self ._chain_id = chain
106113
107114 @property
108115 def address (self ) -> int :
@@ -293,13 +300,18 @@ async def get_nonce(
293300 async def get_balance (
294301 self ,
295302 token_address : Optional [AddressRepresentation ] = None ,
296- chain_id : Optional [StarknetChainId ] = None ,
297303 * ,
298304 block_hash : Optional [Union [Hash , Tag ]] = None ,
299305 block_number : Optional [Union [int , Tag ]] = None ,
300306 ) -> int :
301307 if token_address is None :
302- token_address = self ._default_token_address_for_chain (chain_id )
308+ chain_id = await self ._get_chain_id ()
309+ if chain_id in RECOGNIZED_CHAIN_IDS :
310+ token_address = FEE_CONTRACT_ADDRESS
311+ else :
312+ raise ValueError (
313+ "Argument token_address must be specified when using a custom network."
314+ )
303315
304316 low , high = await self ._client .call_contract (
305317 Call (
@@ -596,7 +608,6 @@ async def deploy_account_v1(
596608 salt : int ,
597609 key_pair : KeyPair ,
598610 client : Client ,
599- chain : StarknetChainId ,
600611 constructor_calldata : Optional [List [int ]] = None ,
601612 nonce : int = 0 ,
602613 max_fee : Optional [int ] = None ,
@@ -618,7 +629,6 @@ async def deploy_account_v1(
618629 :param salt: Salt used to calculate the address.
619630 :param key_pair: KeyPair used to calculate address and sign deploy account transaction.
620631 :param client: Client instance used for deployment.
621- :param chain: Id of the Starknet chain used.
622632 :param constructor_calldata: Optional calldata to account contract constructor. If ``None`` is passed,
623633 ``[key_pair.public_key]`` will be used as calldata.
624634 :param nonce: Nonce of the transaction.
@@ -631,6 +641,8 @@ async def deploy_account_v1(
631641 else [key_pair .public_key ]
632642 )
633643
644+ chain = await client .get_chain_id ()
645+
634646 account = _prepare_account_to_deploy (
635647 address = address ,
636648 class_hash = class_hash ,
@@ -650,7 +662,7 @@ async def deploy_account_v1(
650662 auto_estimate = auto_estimate ,
651663 )
652664
653- if chain in StarknetChainId :
665+ if parse_chain ( chain ) in RECOGNIZED_CHAIN_IDS :
654666 balance = await account .get_balance ()
655667 if balance < deploy_account_tx .max_fee :
656668 raise ValueError (
@@ -671,7 +683,6 @@ async def deploy_account_v3(
671683 salt : int ,
672684 key_pair : KeyPair ,
673685 client : Client ,
674- chain : StarknetChainId ,
675686 constructor_calldata : Optional [List [int ]] = None ,
676687 nonce : int = 0 ,
677688 l1_resource_bounds : Optional [ResourceBounds ] = None ,
@@ -690,7 +701,6 @@ async def deploy_account_v3(
690701 :param salt: Salt used to calculate the address.
691702 :param key_pair: KeyPair used to calculate address and sign deploy account transaction.
692703 :param client: Client instance used for deployment.
693- :param chain: Id of the Starknet chain used.
694704 :param constructor_calldata: Optional calldata to account contract constructor. If ``None`` is passed,
695705 ``[key_pair.public_key]`` will be used as calldata.
696706 :param nonce: Nonce of the transaction.
@@ -704,6 +714,8 @@ async def deploy_account_v3(
704714 else [key_pair .public_key ]
705715 )
706716
717+ chain = await client .get_chain_id ()
718+
707719 account = _prepare_account_to_deploy (
708720 address = address ,
709721 class_hash = class_hash ,
@@ -729,19 +741,12 @@ async def deploy_account_v3(
729741 hash = result .transaction_hash , account = account , _client = account .client
730742 )
731743
732- def _default_token_address_for_chain (
733- self , chain_id : Optional [StarknetChainId ] = None
734- ) -> str :
735- if (chain_id or self ._chain_id ) not in [
736- StarknetChainId .SEPOLIA ,
737- StarknetChainId .SEPOLIA_INTEGRATION ,
738- StarknetChainId .MAINNET ,
739- ]:
740- raise ValueError (
741- "Argument token_address must be specified when using a custom network."
742- )
744+ async def _get_chain_id (self ) -> int :
745+ if self ._chain_id is None :
746+ chain = await self ._client .get_chain_id ()
747+ self ._chain_id = parse_chain (chain )
743748
744- return FEE_CONTRACT_ADDRESS
749+ return self . _chain_id
745750
746751
747752def _prepare_account_to_deploy (
@@ -750,7 +755,7 @@ def _prepare_account_to_deploy(
750755 salt : int ,
751756 key_pair : KeyPair ,
752757 client : Client ,
753- chain : StarknetChainId ,
758+ chain : Chain ,
754759 calldata : List [int ],
755760) -> Account :
756761 # pylint: disable=too-many-arguments
0 commit comments