cheb3.utils

TBD

cheb3.utils.calc_create2_address(sender: HexStr, salt: int, initcode: HexStr) HexStr

Calculate the address of the contract created by the given sender using the CREATE2 opcode with the given salt and contract bytecode.

Parameters:
  • sender (HexStr) – The address of the sender.

  • salt (int) – The salt.

  • initcode (HexStr) – The contract bytecode.

Returns:

The address of the contract.

Return type:

HexStr

cheb3.utils.calc_create_address(sender: HexStr, nonce: int) HexStr

Calculate the address of the contract created by the given sender using the CREATE opcode with the given nonce.

Parameters:
  • sender (HexStr) – The address of the sender.

  • nonce (int) – The transaction count of the sender before the creation.

Returns:

The address of the contract.

Return type:

HexStr

cheb3.utils.compile_file(contract_file: str, contract_names: Optional[Union[str, List[str]]] = None, solc_version: Optional[str] = None, base_path: Optional[str] = None) Dict[str, Tuple[Dict, str]]

Compile the Solidity source in the given file.

Check compile_sol() for more details.

cheb3.utils.compile_sol(contract_source: str, contract_names: Optional[Union[str, List[str]]] = None, solc_version: Optional[str] = None, base_path: Optional[str] = None) Dict[str, Tuple[Dict, str]]

Compile the Solidity source and return the ABI and bytecode of the specific contracts.

Parameters:
  • contract_source (str) – The Solidity source code.

  • contract_name (str | List[str]) – A target contract name or a list of target contract names, defaults to None. If not given, it will return all contracts in the source file.

  • solc_version (str) – solc version to use, defaults to None. If not given, the currently active version is used. If the specified version is not installed, it will be installed automatically.

  • base_path (str) – Use the given path as the root of the source tree to include other dependence contracts, e.g. the path to openzeppelin contracts. Defaults to None.

Returns:

A dict, mapping the contract name to a tuple of the ABI and bytecode.

Return type:

Dict[str, Tuple[Dict, str]]

cheb3.utils.decode_data(encoded_data: Union[HexBytes, HexStr], types: Iterable[str]) Union[Any, Tuple[Any]]

The same as abi.decode in Solidity.

Examples

>>> # decode a single value
>>> decode_data(b'\xd0!\xb0\xc3\x07\xaf\x00\x9b?\xbe\x03\x99M\xb4\xfa\x9fy\x17 \x84'.rjust(32, b'\0'), ['address'])
'0xd021b0c307af009b3fbe03994db4fa9f79172084'
>>> # decode multiple values
>>> decode_data('000000000000000000000000000000000000000000000000058d15e17628\
00000000000000000000000000000000000000000000000000000000000000000064000000000\
00000000000000000000000000000000000000000000000645f7142',\
('uint112', 'uint112', 'uint32'))
(400000000000000000, 100, 1683976514)
Parameters:
  • encoded_data (Union[HexBytes, HexStr]) – The encoded data.

  • types (Iterable[str]) – A list or tuple of string representations of the ABI types that will be used for decoding.

Returns:

The decoded data.

Return type:

Union[Any, Tuple[Any]]

cheb3.utils.encode_with_signature(signature: str, *args) HexStr

The same as abi.encodeWithSignature in Solidity except that it can handle type alias.

Examples

>>> encode_with_signature("transfer(address,uint)", "0x617F2E2fD72FD9D5503197092aC168c91465E7f2", 100)
'0xa9059cbb000000000000000000000000617f2e2fd72fd9d5503197092ac168c91465e7f20000000000000000000000000000000000000000000000000000000000000064'
Parameters:
  • signature (str) – The function signature.

  • *args – The parameters to be encoded.

Returns:

The encoded data.

Return type:

HexStr

cheb3.utils.load_compiled(contract_file: str, contract_name: Optional[str] = None, base_path: str = 'out/') Tuple[Dict, str]

Loads compiled contracts from the project.

Parameters:
  • contract_file (str) – The name of the contract file.

  • contract_name (str) – Specifies the contract to be loaded. Defaults to the contract filename without suffix.

  • base_path (str) – Use the given path as the root of the source tree to load the compiled output. Defaults to “out/”.

Returns:

ABI and bytecode of the required contract.

Return type:

Tuple[Dict, str]