Helper functions#
Woke testing framework provides a set of helper functions to make testing easier.
ABI encoding and decoding#
The Abi
class provides functions to encode and decode data according to the ABI specification.
Abi.encode#
Abi.encode
encodes a list of values given a list of types. It returns bytes
:
Abi.encode_packed#
Abi.encode_packed
encodes a list of values given a list of types. It returns bytes
:
Abi.encode_with_selector#
Abi.encode_with_selector
encodes a list of values and a selector given a list of types and the selector. It returns bytes
:
from woke.testing import Abi
from pytypes.contracts.Counter import Counter
Abi.encode_with_selector(Counter.setCount.selector, ['uint256'], [0xff])
Abi.encode_with_signature#
Abi.encode_with_signature
encodes a list of values and a selector given a list of types and a signature. It returns bytes
:
Warning
The signature string must conform to the ABI specification. The common mistakes are:
uint
orint
used instead ofuint256
orint256
,- return type specified,
- spaces in the signature string.
Abi.encode_call#
Abi.encode_call
encodes a list of values and a selector given a reference to a function. It returns bytes
:
from woke.testing import Abi
from pytypes.contracts.Counter import Counter
Abi.encode_call(Counter.setCount, [0xff])
Abi.decode#
Abi.decode
decodes a bytes
object given a list of types. It returns a list of values:
from woke.testing import Abi
Abi.decode(['uint8', 'address'], b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
Keccak-256#
The keccak256
function computes the Keccak-256 hash of a bytes
object:
Computing CREATE
and CREATE2
address#
In some cases, it may be useful to compute the address of a contract before it is deployed. Woke testing framework provides three functions to do so.
get_create_address#
get_create_address
computes the address of a contract deployed in a transaction or in a contract using the CREATE
opcode.
It accepts a deployer (Account
, Address
or a hex string address) and its nonce.
from woke.testing import Account, get_create_address
deployer = Account(1)
get_create_address(deployer, deployer.nonce)
get_create2_address_from_code#
get_create2_address_from_code
computes the address of a contract deployed using the CREATE2
opcode.
It accepts a deployer (Account
, Address
or a hex string address), a salt and the contract deployment code.
from woke.testing import Account, get_create2_address_from_code
from woke.testing.fuzzing import random_bytes
from pytypes.contracts.Counter import Counter
get_create2_address_from_code(
Account(1),
random_bytes(32),
Counter.deployment_code()
)
get_create2_address_from_hash#
get_create2_address_from_hash
computes the address of a contract deployed using the CREATE2
opcode.
It accepts a deployer (Account
, Address
or a hex string address), a salt and the hash of the contract deployment code.