Utility functions
Functions:
Compute the sha3_256 flavor hash. |
|
Get value from nested dictionary by dotted path. |
Classes:
Implementation of web3 client. |
- matic.utils.keccak256(list_of_bytes: Iterable[bytes]) bytes [source]
Compute the sha3_256 flavor hash.
- matic.utils.resolve(obj: dict[str, Any], path: Union[str, Iterable[str]]) Any [source]
Get value from nested dictionary by dotted path.
- class matic.utils.Web3Client(provider: web3.providers.base.BaseProvider)[source]
Bases:
matic.abstracts.BaseWeb3Client
Implementation of web3 client.
Methods:
Perform a reading (non-modifying) operation.
Perform a writing (modifying) operation.
Obtain a contract from deployment address and ABI dictionary.
Estimate gas amount for transaction.
Get amount of transactions in specified block for given address.
Obtain transaction object by hash.
Get receipt for transaction.
Get block (with raw transaction data) by hash or number.
Get block (with decoded transaction data) by hash or number.
Perform arbitrary RPC request.
Encode ABI parameters according to schema.
Decode binary data to ABI parameters according to schema.
Calculate solidity keccak hash of given values (encoded as types).
Attributes:
Current gas price.
Current chain id.
- read(config: matic.json_types.ITransactionRequestConfig, return_transaction: bool = False) Any [source]
Perform a reading (non-modifying) operation.
- write(config: matic.json_types.ITransactionRequestConfig, private_key: Optional[str] = None, return_transaction: bool = False) matic.web3_client.TransactionWriteResult [source]
Perform a writing (modifying) operation.
- get_contract(address: eth_typing.evm.HexAddress, abi: dict[str, Any]) matic.web3_client.Web3Contract [source]
Obtain a contract from deployment address and ABI dictionary.
- estimate_gas(transaction: matic.json_types.ITransactionRequestConfig) int [source]
Estimate gas amount for transaction.
- get_transaction_count(address: str, block_number: int) int [source]
Get amount of transactions in specified block for given address.
- get_transaction(transaction_hash: bytes) matic.json_types.ITransactionData [source]
Obtain transaction object by hash.
- get_transaction_receipt(transaction_hash: bytes, timeout: int = 120) matic.json_types.ITransactionReceipt [source]
Get receipt for transaction.
- get_block(block_hash_or_block_number: Union[Literal['latest', 'earliest', 'pending'], eth_typing.evm.BlockNumber, eth_typing.evm.Hash32, eth_typing.encoding.HexStr, hexbytes.main.HexBytes, int]) matic.json_types.IBlock [source]
Get block (with raw transaction data) by hash or number.
- get_block_with_transaction(block_hash_or_block_number: Union[Literal['latest', 'earliest', 'pending'], eth_typing.evm.BlockNumber, eth_typing.evm.Hash32, eth_typing.encoding.HexStr, hexbytes.main.HexBytes, int]) matic.json_types.IBlockWithTransaction [source]
Get block (with decoded transaction data) by hash or number.
- send_rpc_request(method: web3.types.RPCEndpoint, params: Iterable[Any]) web3.types.RPCResponse [source]
Perform arbitrary RPC request.
- encode_parameters(params: Sequence[Any], types: Sequence[str]) bytes [source]
Encode ABI parameters according to schema.
ABI manager
Classes:
Caching manager for fetched contract ABI dicts. |
- class matic.utils.abi_manager.ABIManager(network_name: str, version: str)[source]
Bases:
object
Caching manager for fetched contract ABI dicts.
Methods:
Get option from cache item by dotted path.
Get ABI dict for contract and memoise it.
Store ABI dict in cache.
Proof generation
Functions:
Return a str with the given prefix string removed if present. |
|
Get fast Merkle proof for block. |
|
Get proof for block as single byte string. |
|
Get root hash. |
|
Get n-th recursive zero hash. |
|
Get proof for receipt. |
|
Check if transaction was performed and type is non-zero. |
|
Get block's tx hash for state-sync receipt. |
|
Get binary representation of receipt for storing in trie. |
- matic.utils.proof_utils.removeprefix(self, prefix, /)
Return a str with the given prefix string removed if present.
If the string starts with the prefix string, return string[len(prefix):]. Otherwise, return a copy of the original string.
- matic.utils.proof_utils.get_fast_merkle_proof(web3: matic.abstracts.BaseWeb3Client, block_number: int, start_block: int, end_block: int) list[bytes] [source]
Get fast Merkle proof for block.
- matic.utils.proof_utils.build_block_proof(matic_web3: matic.abstracts.BaseWeb3Client, start_block: int, end_block: int, block_number: int) bytes [source]
Get proof for block as single byte string.
- matic.utils.proof_utils.query_root_hash(client: matic.abstracts.BaseWeb3Client, start_block: int, end_block: int) bytes [source]
Get root hash.
- matic.utils.proof_utils.recursive_zero_hash(n: int, client: matic.abstracts.BaseWeb3Client) bytes [source]
Get n-th recursive zero hash.
- matic.utils.proof_utils.get_receipt_proof(receipt: matic.json_types.ITransactionReceipt, block: matic.json_types.IBlockWithTransaction, web3: matic.abstracts.BaseWeb3Client, request_concurrency: Optional[int] = None, receipts_val: Optional[Iterable[matic.json_types.ITransactionReceipt]] = None) matic.json_types.IReceiptProof [source]
Get proof for receipt.
- matic.utils.proof_utils.is_typed_receipt(receipt: matic.json_types.ITransactionReceipt) bool [source]
Check if transaction was performed and type is non-zero.
- matic.utils.proof_utils.get_state_sync_tx_hash(block: matic.json_types.IBaseBlock) bytes [source]
Get block’s tx hash for state-sync receipt.
Bor blockchain includes extra receipt/tx for state-sync logs, but it is not included in transactionRoot or receiptRoot. So, while calculating proof, we have to exclude them.
This is derived from block’s hash and int.
- matic.utils.proof_utils.get_receipt_bytes(receipt: matic.json_types.ITransactionReceipt) bytes [source]
Get binary representation of receipt for storing in trie.
Web3 side chain client
Classes:
Web3 client class for a side chain. |
- class matic.utils.web3_side_chain_client.Web3SideChainClient(config: matic.utils.web3_side_chain_client._C)[source]
Bases:
Generic
[matic.utils.web3_side_chain_client._C
]Web3 client class for a side chain.
Attributes:
Parent client instance.
Child client instance.
ABI manager instance.
Parameters of plasma contracts.
Parameters of POS contracts.
Methods:
Get ABI dictionary for given name and type.
Fetch configuration option by a dotted path.
Check if EIP-1559 (improved fee specification) is available for chain.
- parent: matic.abstracts.BaseWeb3Client
Parent client instance.
- child: matic.abstracts.BaseWeb3Client
Child client instance.
- abi_manager: matic.utils.abi_manager.ABIManager
ABI manager instance.
Root chain
Classes:
Root chain implementation. |
- class matic.utils.root_chain.RootChain(client: matic.utils.web3_side_chain_client.Web3SideChainClient[matic.utils.root_chain._C], address: eth_typing.evm.HexAddress)[source]
Bases:
matic.utils.base_token.BaseToken
[matic.utils.root_chain._C
]Root chain implementation.
This represents a connection between parent (root) and child chains. For example, Goerli testnet is a root chain for Mumbai testnet.
Attributes:
Get last block number on child chain.
Methods:
Find root block corresponding to child block of given number.
Exit data building
Classes:
Helper utility class for building and performing exit actions with POS bridge. |
- class matic.utils.exit_util.ExitUtil(client: matic.utils.web3_side_chain_client.Web3SideChainClient[matic.utils.exit_util._C], root_chain: matic.utils.root_chain.RootChain[matic.utils.exit_util._C])[source]
Bases:
Generic
[matic.utils.exit_util._C
]Helper utility class for building and performing exit actions with POS bridge.
Attributes:
Root chain address.
Configuration (same as of client).
Methods:
Obtain information about block that includes given transaction.
Check if given transaction is checkpointed.
Build exit payload for transaction hash.
Build exit payload for multiple indices.
Build exit hash for burn transaction.
- root_chain: matic.utils.root_chain.RootChain[matic.utils.exit_util._C]
Root chain address.
- config: matic.utils.exit_util._C
Configuration (same as of client).
- get_chain_block_info(burn_tx_hash: bytes) matic.utils.exit_util._IChainBlockInfo [source]
Obtain information about block that includes given transaction.
- build_payload_for_exit(burn_tx_hash: bytes, index: int, log_event_sig: bytes, is_fast: bool) bytes [source]
Build exit payload for transaction hash.
Implementation details
matic.utils.base_token
Classes:
Base class for all tokens. |
- class matic.utils.base_token.BaseToken(address: eth_typing.evm.HexAddress, is_parent: bool, name: str, client: matic.utils.web3_side_chain_client.Web3SideChainClient[matic.utils.base_token._C], bridge_type: Optional[str] = None)[source]
Bases:
Generic
[matic.utils.base_token._C
]Base class for all tokens.
Attributes:
Contract object.
Methods:
Call arbitrary JSON-RPC method.
Perform write (modifying) operation.
Sign and send the transaction.
Send read (non-modifying) transaction without RPC method.
Perform read (non-modifying) operation.
Get web3 client instance.
Fill in missing fields in transaction request.
Transfer ERC-20 token.
Transfer ERC-721 token.
Assert that method is called on parent chain instance.
Assert that method is called on child chain instance.
Transfer ERC-1155 token.
- property contract: matic.abstracts.BaseContract
Contract object.
- process_write(method: matic.abstracts.BaseContractMethod, option: Optional[matic.json_types.ITransactionOption] = None, private_key: Optional[str] = None) matic.json_types.ITransactionWriteResult [source]
Perform write (modifying) operation.
- Parameters
method – Method instance (with arguments passed on instantiation)
option – Additional parameters. May contain special key
return_transaction
(see Returns section)private_key – Sender private key (may be missing, if your provider supports implicit tx signing)
- Returns
- ITransactionWriteResult if actual write was performed;
ITransactionRequestConfig if return_transaction=True. (builds the final transaction dictionary and returns it).
- send_transaction(option: Optional[matic.json_types.ITransactionOption] = None, private_key: Optional[str] = None) matic.json_types.ITransactionWriteResult [source]
Sign and send the transaction.
- read_transaction(option: Optional[matic.json_types.ITransactionOption] = None) Any [source]
Send read (non-modifying) transaction without RPC method.
- Parameters
option – Additional parameters. May contain special key
return_transaction
(see Returns section)- Returns
- ITransactionWriteResult if actual write was performed;
ITransactionRequestConfig if return_transaction=True. (builds the final transaction dictionary and returns it).
- process_read(method: matic.abstracts.BaseContractMethod, option: Optional[matic.json_types.ITransactionOption] = None) Any [source]
Perform read (non-modifying) operation.
- Parameters
method – Method instance (with arguments passed on instantiation)
option – Additional parameters. May contain special key
return_transaction
(see Returns section)
- Returns
- ITransactionWriteResult if actual write was performed;
ITransactionRequestConfig if return_transaction=True. (builds the final transaction dictionary and returns it).
- get_client(is_parent: bool) matic.abstracts.BaseWeb3Client [source]
Get web3 client instance.
- create_transaction_config(tx_config: matic.json_types.ITransactionRequestConfig | None, method: matic.abstracts.BaseContractMethod | None, is_parent: bool, is_write: bool) matic.json_types.ITransactionRequestConfig [source]
Fill in missing fields in transaction request.
- Warning: this method may raise if your transaction cannot be executed,
pass
gas_limit
to prevent it from happening.
- transfer_erc_20(to: str, amount: int, private_key: Optional[str] = None, option: Optional[matic.json_types.ITransactionOption] = None)[source]
Transfer ERC-20 token.
matic.utils.bridge_client
Classes:
Base class for POS bridge with reusable methods. |
- class matic.utils.bridge_client.BridgeClient(config: matic.utils.bridge_client._C)[source]
Bases:
Generic
[matic.utils.bridge_client._C
]Base class for POS bridge with reusable methods.
Attributes:
Helper class for exit data building.
Actual connecting client.
Methods:
Check if transaction is checkpointed.
Check if deposit has finished after exit (StateSynced happened).
- exit_util: matic.utils.exit_util.ExitUtil[matic.utils.bridge_client._C]
Helper class for exit data building.
Should be set after instantiation to prevent cycles.
- client: matic.utils.web3_side_chain_client.Web3SideChainClient[matic.utils.bridge_client._C]
Actual connecting client.
matic.utils.merkle_tree
Classes:
Hash tree. |
- class matic.utils.merkle_tree.MerkleTree(leaves: Optional[Sequence[bytes]] = None)[source]
Bases:
object
Hash tree.
See this article.
Attributes:
Tree leaves.
Tree layers.
Tree depth.
Tree root.
Methods:
Estimate depth of a tree with given size.
Add nodes to tree.
Get proof for leaf as a sequence of nodes.
Verify given proof to match the tree.
matic.utils.polyfill
Functions:
Return a str with the given prefix string removed if present. |
|
Return a str with the given suffix string removed if present. |
- matic.utils.polyfill.removeprefix(self, prefix, /)
Return a str with the given prefix string removed if present.
If the string starts with the prefix string, return string[len(prefix):]. Otherwise, return a copy of the original string.
- matic.utils.polyfill.removesuffix(self, suffix, /)
Return a str with the given suffix string removed if present.
If the string ends with the suffix string and that suffix is not empty, return string[:-len(suffix)]. Otherwise, return a copy of the original string.