"""Utilities for handling and decoding transaction calldata in the Arbitrum SDK.This module provides functions for extracting and decoding information fromtransaction calldata, particularly for cross-chain token transfers betweenL1 (Ethereum) and L2 (Arbitrum)."""fromtypingimportAny,Optional,TypedDictfromarbitrum_py.data_entities.errorsimportArbSdkErrorfromarbitrum_py.utils.helperimportcreate_contract_instance
[docs]classTxRequest(TypedDict):"""Type definition for transaction request data."""data:strto:strvalue:int
# Other transaction parameters can be added as needed
[docs]classParentToChildTxReqAndSigner(TypedDict):""" Type definition for a parent-to-child (L1 to L2) transaction request with signer. Attributes: txRequest: The transaction request parameters signer: The signer for the transaction (optional) """txRequest:TxRequestsigner:Optional[Any]
[docs]defget_erc20_parent_address_from_parent_to_child_tx_request(tx_req:ParentToChildTxReqAndSigner,)->str:""" Extract the ERC20 parent token address from a L1->L2 transaction request. This function decodes the transaction calldata to extract the token address from either 'outboundTransfer' or 'outboundTransferCustomRefund' function calls on the L1GatewayRouter contract. Args: tx_req (ParentToChildTxReqAndSigner): The transaction request containing the calldata. Returns: str: The ERC20 token address on L1 (parent chain) Raises: ArbSdkError: If the calldata doesn't match expected function signatures. """data:str=tx_req["txRequest"]["data"]# Create the Contract instance without a providerl1_gateway_router=create_contract_instance("L1GatewayRouter")try:# Attempt to decode using 'outboundTransfer' functionfunction,arguments=l1_gateway_router.decode_function_input(data)iffunction.fn_name=="outboundTransfer":returnarguments["_token"]else:raiseArbSdkError("Decoded function name does not match 'outboundTransfer'")exceptExceptionase:# Log or handle the first decoding failure if necessarypasstry:# Attempt to decode using 'outboundTransferCustomRefund' functionfunction,arguments=l1_gateway_router.decode_function_input(data)iffunction.fn_name=="outboundTransferCustomRefund":returnarguments["_token"]else:raiseArbSdkError("Decoded function name does not match 'outboundTransferCustomRefund'")exceptExceptionase:# Log or handle the second decoding failure if necessarypass# If neither decoding attempt was successful, raise an errorraiseArbSdkError("data signature not matching deposits methods")