Contract
To create a contract instance with the given contract address, using the ABI from Foundry compiled output:
>>> from cheb3.utils import load_compiled
>>> abi, bytecode = load_compiled("Token.sol", "Cheb3Token")
>>> contract = conn.contract(
account, # specified the signer
contract_addr,
abi=abi
)
Or, you can create an undeployed contract instance and then deploy it.
Its address will be stored in its address attribute:
>>> contract = conn.contract(
account, # specified the signer
abi=abi,
bytecode=bytecode
)
>>> contract.deploy("Cheb3Token", "CT")
- class cheb3.contract.Contract(signer: Optional[Account] = None, address: Optional[str] = None, **kwargs)
Please use
cheb3.Connection.contract()interface to create a contract instance associated with the connection.- __init__(signer: Optional[Account] = None, address: Optional[str] = None, **kwargs) None
You can read states from the blockchain even if signer is not given.
The keyword arguments follow web3.py. If the contract address and ABI are provided, you can read the contract states; if a signer, contract address, and ABI are provided, you can interact with the contract; if a signer, ABI, and bytecode are provided, you can deploy the contract and interact with it.
- deploy(*constructor_args, **kwargs) None
Deploys the contract.
- Parameters:
constructor_args – Constructor arguments.
- Keyword Arguments:
value (int) – The amount to transfer, defaults to 0 (wei).
gas_price (int) – Specifies the gas price for the deployment.
max_priority_fee_per_gas (int) – Specifies the fee that goes to the miner, defaults to the value of
max_priority_fee.max_fee_per_gas (int) – Specifies the maximum amount you are willing to pay, inclusive of baseFeePerGas and maxPriorityFeePerGas. Its default value is the sum of maxPriorityFeePerGas and twice the baseFeePerGas of the latest block.
gas_limit (int) – Specifies the maximum gas the deployment can use.
proxy (bool) – A minimal proxy contract (ERC-1167) will be deployed and connected to the logic contract if set to
True, defaults toFalse.access_list (List[Dict]) – Specifies a list of addresses and storage keys that the transaction plans to access (EIP-2930). It will only be used in logic contract deployment if proxy is
True.
Calling the contract functions
Cheb3 wraps ContractFunction, enabling the use of all web3.py contract function interaction methods. And it provides two additional methods: send_transaction() and create_access_list().
send_transaction() allows signing and sending the transaction directly with the account associated with the contract instance.
>>> contract.functions.myMethod(*args).send_transaction()
create_access_list() can create an EIP-2930 type access list based on the function call data.
>>> access_list = contract.functions.myMethod(*args).create_access_list()
>>> contract.functions.myMethod(*args).send_transaction(access_list=access_list)
- class cheb3.contract.ContractFunctionWrapper(abi: Optional[ABIFunction] = None)
- create_access_list(**kwargs) AccessList
Creates an EIP-2930 type access list based on the function call data.
- Keyword Arguments:
block_identifier (str) – A string representing a block number (hexadecimal) or latest or pending, defaults is latest.
value (int) – The amount to transfer, defaults to 0 (wei).
gas_price (int) – Specifies the gas price for the LEGACY transaction.
max_priority_fee_per_gas (int) – Specifies the fee that goes to the miner, defaults to the value of
max_priority_fee.max_fee_per_gas (int) – Specifies the maximum amount you are willing to pay, inclusive of baseFeePerGas and maxPriorityFeePerGas. Its default value is the sum of maxPriorityFeePerGas and twice the baseFeePerGas of the latest block.
gas_limit (int) – Specifies the maximum gas the transaction can use.
authorization_list (List[SignedSetCodeAuthorization]) – Specifies a list of signed authorizations (EIP-7702).
- Returns:
An access list contains all storage slots and addresses read and written by the transaction, except for the sender account and the precompiles.
- Return type:
AccessList
- send_transaction(**kwargs) Union[TxReceipt, HexStr]
Signs and sends the transaction.
- Keyword Arguments:
value (int) – The amount to transfer, defaults to 0 (wei).
gas_price (int) – Specifies the gas price for the LEGACY transaction.
max_priority_fee_per_gas (int) – Specifies the fee that goes to the miner, defaults to the value of
max_priority_fee.max_fee_per_gas (int) – Specifies the maximum amount you are willing to pay, inclusive of baseFeePerGas and maxPriorityFeePerGas. Its default value is the sum of maxPriorityFeePerGas and twice the baseFeePerGas of the latest block.
gas_limit (int) – Specifies the maximum gas the transaction can use.
nonce (int) – Allows to overwrite pending transactions that use the same nonce.
access_list (List[Dict]) – Specifies a list of addresses and storage keys that the transaction plans to access (EIP-2930).
authorization_list (List[SignedSetCodeAuthorization]) – Specifies a list of signed authorizations (EIP-7702).
wait_for_receipt (bool) – Waits for the transaction receipt, defaults to
True.
- Returns:
The transaction receipt or the transaction hash if wait_for_receipt is
False.- Return type:
Union[TxReceipt, HexStr]