Web3 client

Main implementation

This is an embedded implementation of maticjs_web3 library.

Classes:

TransactionWriteResult

Result of any writing call.

EthMethod

Wrapper around web3 contract method (web3.method.Method).

Web3Contract

A wrapper around web3 contract (web3.contract.Contract).

Web3Client

Implementation of web3 client.

class matic.web3_client.TransactionWriteResult(tx_hash: bytes | None, tx_params: matic.json_types.ITransactionRequestConfig, client: matic.web3_client.Web3Client)[source]

Bases: matic.json_types.ITransactionWriteResult

Result of any writing call.

Methods:

get_receipt

Get transaction receipt.

Attributes:

transaction_hash

Hash of performed transaction.

transaction_config

Hash of performed transaction.

get_receipt(timeout: int = 120) matic.json_types.ITransactionReceipt[source]

Get transaction receipt.

Parameters

timeout – max seconds to wait.

Raises

AttributeError – if called on transaction that was not performed.

property transaction_hash: bytes

Hash of performed transaction.

Raises

AttributeError – if called on transaction that was not performed.

property transaction_config: matic.json_types.ITransactionRequestConfig

Hash of performed transaction.

class matic.web3_client.EthMethod(address: eth_typing.evm.HexAddress, method: web3.method.Method[Any], client: matic.web3_client.Web3Client)[source]

Bases: matic.abstracts.BaseContractMethod

Wrapper around web3 contract method (web3.method.Method).

Methods:

read

Perform a read operation.

write

Perform a write operation.

estimate_gas

Estimate gas for given transaction.

encode_abi

Encode args according to method ABI and prepend the selector.

read(tx: Optional[matic.json_types.ITransactionRequestConfig] = None, return_transaction: bool = False) Any[source]

Perform a read operation.

This does not sign a transaction and does not affect the chain.

write(tx: matic.json_types.ITransactionRequestConfig, private_key: Optional[str] = None, return_transaction: bool = False) matic.web3_client.TransactionWriteResult[source]

Perform a write operation.

Transaction is signed (with given PK), affects the chain.

estimate_gas(tx: matic.json_types.ITransactionRequestConfig) int[source]

Estimate gas for given transaction.

Warning

This method may fail if your transaction is invalid or cannot be executed.

encode_abi() bytes[source]

Encode args according to method ABI and prepend the selector.

class matic.web3_client.Web3Contract(address: eth_typing.evm.HexAddress, contract: web3.contract.Contract, client: matic.web3_client.Web3Client)[source]

Bases: matic.abstracts.BaseContract

A wrapper around web3 contract (web3.contract.Contract).

Methods:

method

Obtain a method object by name and call arguments.

method(method_name: str, *args: Any) matic.web3_client.EthMethod[source]

Obtain a method object by name and call arguments.

class matic.web3_client.Web3Client(provider: web3.providers.base.BaseProvider)[source]

Bases: matic.abstracts.BaseWeb3Client

Implementation of web3 client.

Methods:

read

Perform a reading (non-modifying) operation.

write

Perform a writing (modifying) operation.

get_contract

Obtain a contract from deployment address and ABI dictionary.

estimate_gas

Estimate gas amount for transaction.

get_transaction_count

Get amount of transactions in specified block for given address.

get_transaction

Obtain transaction object by hash.

get_transaction_receipt

Get receipt for transaction.

get_block

Get block (with raw transaction data) by hash or number.

get_block_with_transaction

Get block (with decoded transaction data) by hash or number.

send_rpc_request

Perform arbitrary RPC request.

encode_parameters

Encode ABI parameters according to schema.

decode_parameters

Decode binary data to ABI parameters according to schema.

etherium_sha3

Calculate solidity keccak hash of given values (encoded as types).

Attributes:

gas_price

Current gas price.

chain_id

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.

property gas_price: int

Current gas price.

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.

property chain_id: int

Current chain id.

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.

decode_parameters(binary_data: bytes, types: Sequence[Any]) tuple[Any, ...][source]

Decode binary data to ABI parameters according to schema.

etherium_sha3(types: Iterable[str], values: Iterable[Any]) bytes[source]

Calculate solidity keccak hash of given values (encoded as types).

Web3 <–> matic data conversion

Conversion utils: web3 structures to matic.

Functions:

matic_tx_request_config_to_web3

Transaction request: matic to web3.

web3_tx_request_config_to_matic

Transaction request: web3 to matic.

web3_log_to_matic_log

Log: web3 to matic.

web3_receipt_to_matic_receipt

Transaction receipt: web3 to matic.

web3_tx_to_matic_tx

Transaction: web3 to matic.

matic.web3_client.utils.matic_tx_request_config_to_web3(config: Optional[matic.json_types.ITransactionRequestConfig] = None) web3.types.TxParams[source]

Transaction request: matic to web3.

matic.web3_client.utils.web3_tx_request_config_to_matic(data: web3.types.TxParams) matic.json_types.ITransactionRequestConfig[source]

Transaction request: web3 to matic.

matic.web3_client.utils.web3_log_to_matic_log(log: web3.types.LogReceipt) matic.json_types.ILog[source]

Log: web3 to matic.

matic.web3_client.utils.web3_receipt_to_matic_receipt(receipt: web3.types.TxReceipt) matic.json_types.ITransactionReceipt[source]

Transaction receipt: web3 to matic.

matic.web3_client.utils.web3_tx_to_matic_tx(tx: web3.types.TxData) matic.json_types.ITransactionData[source]

Transaction: web3 to matic.