PyAbi API
Provides the ability to load and parse ABI files. It's primarily used to help extract the information needed to interact with smart contracts. This is rarely used directly. See Contracts
and Utilities
.
Import
from simular import PyAbi
Static functions
You can create an instance of PyAbi
by using one of the following static methods:
load_from_json
Create an instance by loading a JSON file from a compiled Solidity contract. Expects a JSON file the includes an abi
and bytecode
entry.
def load_from_json(abi: str) -> self
Parameters:
- abi: an un-parsed json encoded file
Returns: an instance of PyAbi
Example:
with open('counter.json') as f:
raw = f.read()
abi = PyAbi.load_from_json(raw)
load_from_parts
Create an instance by loading a the json encoded abi information and contract bytecode.
def load_from_parts(abi: str, bytecode: bytes) -> self
Parameters:
- abi: an un-parsed json encoded file with just the abi information
- bytecode: the contract bytecode
Returns: an instance of PyAbi
Example:
with open('counter.abi') as f:
abi = f.read()
with open('counter.bin') as f:
bytecode = f.read()
bits = bytes.fromhex(bytecode)
abi = PyAbi.load_from_json(abi, bits)
load_from_human_readable
Create an instance from a list of contract function descriptions
def load_from_human_readable(items: typing.List[str]) -> self
Parameters:
- items: is a list of
function desciptions
Returns: an instance of PyAbi
A function description
is shorthand way of describing the function name, inputs, and outputs. The format is the form:
function NAME(ARG TYPES) (RETURN TYPES)
Where:
- NAME: is the name of the function.
- ARG TYPES: 0 or more of the require Solidity input types
- RETURN TYPES: tuple of expected Solidity return types. This is not required if the function doesn't return anything.
For example:
'function hello() (uint256)'
is a solidity function named hello
that takes no input arguments and returns an uint256
'function hello(address, uint256)'
is a solidity function named hello
that takes 2 arguments, an address
, and uint256
and returns nothing.
Example:
abi = PyAbi.load_from_human_readable([
'function hello() (uint256)',
'function hello(address, uint256)'])
Methods
has_function
Does the ABI include the function with the given name.
def has_function(self, name: str) -> bool
Parameters:
- items: is a list of
function desciptions
Returns: an instance of True | False
Example
with open('counter.json') as f:
raw = f.read()
abi = PyAbi.load_from_json(raw)
assert abi.has_function("increment")
has_fallback
Does the Contract define a fallback function
def has_fallback(self) -> bool
Returns: an instance of True | False
Example
with open('counter.json') as f:
raw = f.read()
abi = PyAbi.load_from_json(raw)
assert not abi.has_fallback()
has_receive
Does the Contract define a receive function
def has_receive(self) -> bool
Returns: an instance of True | False
Example
with open('counter.json') as f:
raw = f.read()
abi = PyAbi.load_from_json(raw)
assert not abi.has_receive()
bytecode
Return the byte from the ABI
def bytecode(self) -> bytes | None
Returns: bytes or None if the bytecode wasn't set
with open('counter.json') as f:
raw = f.read()
abi = PyAbi.load_from_json(raw)
abi.bytecode()
constructor_input_types
Return a list (if any) of the exopected constructor arguments.
def constructor_input_types(self) -> typing.List[str] | None
Returns: a list of the Solidity types expected as arguments to the constructor. Or None if the constructor doesn't take any arguments.
with open('counter.json') as f:
raw = f.read()
abi = PyAbi.load_from_json(raw)
abi.constructor_input_types()
encode_function_input
Encode the function with any given input to call on the EVM. See Function Encoding for more details.
def encode_function_input(self,
name: str,
args: typing.Tuple[Any]
) -> typing.Tuple(bytes, typing.List[str])
Parameters:
- name: of the contract method to encode
- args: 0 or more arguments to pass to the method
Returns: tuple: the encoded bytes and a list of the expected output types from calling the method
with open('counter.json') as f:
raw = f.read()
abi = PyAbi.load_from_json(raw)
abi.encode_function_input(self, "increment", (1,))