Utilities

Provides several helper functions to simplify working with accounts and Contract

Functions

ether_to_wei(value: int) int

Convert Ether to Wei

Params value:

the given value in Ether to convert

Returns:

the amount in Wei

generate_random_address() str

Randomly generate an Ethereum address

Returns:

(str) the hex-encoded address

create_account(evm: PyEvm, address: str = None, value: int = 0) str

Create a new account in the Evm

Parameters:
  • evm – an instance of PyEvm

  • address – (optional) a valid, hex-encoded Ethereum address

  • balance – (optional) balance in wei to set for the account.

Returns:

the hex-encoded address

Note

  • If no address is provided, a random address will be created

  • If no value is provided, the account balance will be set to 0

create_many_accounts(evm: PyEvm, num: int, value: int = 0) [str]

Just like create_account except it can create many accounts at once.

Parameters:
  • evm – an instance of PyEvm

  • num – the number of accounts to create

  • value – (optional) the initial balance in wei for each account

Returns:

a list of account addresses

contract_from_raw_abi(evm: PyEvm, raw_abi: str) Contract

Create an instance of Contract given the full ABI. A full ABI should include the abi and bytecode. This is usually a single json file from a compiled Solidity contract.

Parameters:
  • evm – an instance of PyEvm

  • raw_abi – the full abi contents

Returns:

an instance of Contract based on the ABI and contract creation bytecode

Note

Don’t forget to deploy the contract to make it available in the EVM

contract_from_abi_bytecode(evm: PyEvm, raw_abi: str, bytecode: bytes = None) Contract

Create an instance of Contract from the ABI and (optionally) the contract creation bytecode. This is often used when you have the ABI and bytecode are not in the same file OR when you just want to create a Contract using just the ABI to interact with an already deployed contract.

param evm:

an instance of PyEvm

param raw_abi:

the full abi contents

param bytecode:

(optional) contract creation code

return:

an instance of Contract

Note

Don’t forget to deploy the contract to make it available in the EVM

contract_from_inline_abi(evm: PyEvm, abi: List[str]) Contract

Create an instance of Contract from a user-friendly form of the ABI This is used to interact with an already deployed contract. See Human-Friendly ABI

Parameters:
  • evm – an instance of PyEvm

  • abi – a list of (str) describing the contract’s functions

  • bytecode

    (optional) contract creation code

    return:

    an instance of Contract

Warning

The contract must already be deployed. You will need to use Contract.at() to set the address of the contract.

Example:

>>> evm = PyEvm()

# specifies a single contract function 'hello'
# that takes no arguments and returns a number
>>> abi = ["function hello()(uint256)"]

>>> contract = contract_from_inline_abi(abi)
>>> contract.at('deployed contracts address')

# call it
>>> value = contract.hello.call()