ABIs
Examples
from ctc import evm
from ctc import binary
from ctc import rpc
address = '0x956f47f50a910163d8bf957cf5846d573e7f87ca'
Get contract ABI¶
contract_abi = await evm.async_get_contract_abi(contract_address=address)
print(len(contract_abi), 'items')
print()
print('last item:', contract_abi[-1])
38 items last item: {'inputs': [], 'name': 'unpause', 'outputs': [], 'stateMutability': 'nonpayable', 'type': 'function'}
Get function ABI by name¶
await evm.async_get_function_abi(
contract_address=address,
function_name='totalSupply',
)
{'inputs': [], 'name': 'totalSupply', 'outputs': [{'internalType': 'uint256', 'name': '', 'type': 'uint256'}], 'stateMutability': 'view', 'type': 'function'}
Get function ABI by selector¶
function_abi = await evm.async_get_function_abi(
contract_address=address,
function_selector='0x18160ddd',
)
function_abi
{'inputs': [], 'name': 'totalSupply', 'outputs': [{'internalType': 'uint256', 'name': '', 'type': 'uint256'}], 'stateMutability': 'view', 'type': 'function'}
Get event ABI by name¶
event_abi = await evm.async_get_event_abi(
contract_address=address,
event_name='Transfer',
)
event_abi
{'anonymous': False, 'inputs': [{'indexed': True, 'internalType': 'address', 'name': 'from', 'type': 'address'}, {'indexed': True, 'internalType': 'address', 'name': 'to', 'type': 'address'}, {'indexed': False, 'internalType': 'uint256', 'name': 'value', 'type': 'uint256'}], 'name': 'Transfer', 'type': 'event'}
Get event ABI by hash¶
event_hash = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
event_abi = await evm.async_get_event_abi(
contract_address=address,
event_hash=event_hash,
)
event_abi
{'anonymous': False, 'inputs': [{'indexed': True, 'internalType': 'address', 'name': 'from', 'type': 'address'}, {'indexed': True, 'internalType': 'address', 'name': 'to', 'type': 'address'}, {'indexed': False, 'internalType': 'uint256', 'name': 'value', 'type': 'uint256'}], 'name': 'Transfer', 'type': 'event'}
Parse a function ABI¶
function_signature = binary.get_function_signature(function_abi)
function_selector = binary.get_function_selector(function_abi)
parameter_names = binary.get_function_parameter_names(function_abi)
parameter_types = binary.get_function_parameter_types(function_abi)
output_names = binary.get_function_output_names(function_abi)
output_types = binary.get_function_output_types(function_abi)
print('function_signature:', function_signature)
print('function_selector:', function_selector)
print('parameter_names:', parameter_names)
print('parameter_types:', parameter_types)
print('output_names:', output_names)
print('output_types:', output_types)
function_signature: totalSupply() function_selector: 18160ddd parameter_names: [] parameter_types: [] output_names: [''] output_types: ['uint256']
Parse an event ABI¶
event_hash = binary.get_event_hash(event_abi)
event_signature = binary.get_event_signature(event_abi)
event_indexed_names = binary.get_event_indexed_names(event_abi)
event_indexed_types = binary.get_event_indexed_types(event_abi)
event_unindexed_names = binary.get_event_unindexed_names(event_abi)
event_unindexed_types = binary.get_event_unindexed_types(event_abi)
print('event_hash:', event_hash)
print('event_signature:', event_signature)
print('event_indexed_names:', event_indexed_names)
print('event_indexed_types:', event_indexed_types)
print('event_unindexed_names:', event_unindexed_names)
print('event_unindexed_types:', event_unindexed_types)
event_hash: 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef event_signature: Transfer(address,address,uint256) event_indexed_names: ['from', 'to'] event_indexed_types: ['address', 'address'] event_unindexed_names: ['value'] event_unindexed_types: ['uint256']
Decompile function ABIs from bytecode¶
bytecode = await rpc.async_eth_get_code(address)
function_abis = await evm.async_decompile_function_abis(bytecode)
for function_abi in function_abis:
print(function_abi['hex_signature'], function_abi['text_signature'])
0xbc701e75 incentiveContract(address) 0xd505accf permit(address,address,uint256,uint256,uint8,bytes32,bytes32) 0xdd62ed3e allowance(address,address) 0xdd62ed3e remove_good(uint256[],bytes8,bool) 0xdd62ed3e _func_5437782296(address,address) 0xf2f4eb26 core() 0xa9059cbb transfer(address,uint256) 0xa9059cbb many_msg_babbage(bytes1) 0xa9059cbb transfer(bytes4[9],bytes5[6],int48[11]) 0xa9059cbb func_2093253501(bytes) 0xb4905897 feiBalance() 0xb6232c99 setIncentiveContract(address,address) 0xb86677fe tribe() 0x8456cb59 pause() 0x95d89b41 symbol() 0x95d89b41 link_classic_internal(uint64,int64) 0x9a9ba4da fei() 0xa457c2d7 decreaseAllowance(address,uint256) 0x79cc6790 burnFrom(address,uint256) 0x7ecebe00 nonces(address) 0x80009630 setCore(address) 0x42966c68 burn(uint256) 0x42966c68 collate_propagate_storage(bytes16) 0x5c975abb paused() 0x6b6dff0a tribeBalance() 0x70a08231 balanceOf(address) 0x70a08231 branch_passphrase_public(uint256,bytes8) 0x70a08231 passphrase_calculate_transfer(uint64,address) 0x39509351 increaseAllowance(address,uint256) 0x3f4ba83a unpause() 0x40c10f19 mint(address,uint256) 0x23b872dd transferFrom(address,address,uint256) 0x23b872dd gasprice_bit_ether(int128) 0x30adf81f PERMIT_TYPEHASH() 0x313ce567 decimals() 0x313ce567 available_assert_time(uint16,uint64) 0x3644e515 DOMAIN_SEPARATOR() 0x06fdde03 name() 0x06fdde03 message_hour(uint256,int8,uint16,bytes32) 0x095ea7b3 approve(address,uint256) 0x095ea7b3 sign_szabo_bytecode(bytes16,uint128) 0x18160ddd totalSupply() 0x18160ddd voting_var(address,uint256,int128,int128)
Reference
- ctc.binary.get_event_hash(event_abi)
compute event hash from event’s abi
- Parameters
event_abi (<class 'EventABI'>) –
- Return type
<class ‘str’>
- ctc.binary.get_event_indexed_names(event_abi)
get list of indexed names in signature of event
- ctc.binary.get_event_indexed_types(event_abi)
get list of indexed types in signature of event
- ctc.binary.get_event_signature(event_abi)
- Parameters
event_abi (<class 'EventABI'>) –
- Return type
<class ‘str’>
- ctc.binary.get_event_unindexed_names(event_abi)
get list of data names in signature of event
- ctc.binary.get_event_unindexed_types(event_abi)
get list of data types in signature of event
- ctc.binary.get_function_output_names(function_abi, human_readable=False)
- ctc.binary.get_function_output_types(function_abi)
- ctc.binary.get_function_parameter_types(function_abi=None, function_signature=None)
- Parameters
function_abi (FunctionABI | None) –
function_signature (Optional[str]) –
- Return type
list[ABIDatumType]
- ctc.binary.get_function_selector(function_abi=None, function_signature=None)
- Parameters
function_abi (Union[FunctionABI, NoneType]) –
function_signature (Union[str, NoneType]) –
- Return type
<class ‘str’>
- ctc.binary.get_function_signature(function_abi=None, parameter_types=None, function_name=None, include_names=False)
- Parameters
function_abi (FunctionABI | None) –
parameter_types (Optional[list[str]]) –
function_name (Optional[str]) –
include_names (bool) –
- Return type
str
- async ctc.evm.async_decompile_function_abis(bytecode, sort=None)
- Parameters
bytecode (str) –
sort (str | None) –
- Return type
Sequence[Mapping]
- async ctc.evm.async_get_contract_abi(**query)
- Parameters
query (Any) –
- Return type
List[Union[FunctionABI, EventABI, ErrorABI]]
- async ctc.evm.async_get_event_abi(contract_abi=None, contract_address=None, event_name=None, event_hash=None, event_abi=None, network=None)
- Parameters
contract_abi (Union[List[Union[FunctionABI, EventABI, ErrorABI]], NoneType]) –
contract_address (Union[str, NoneType]) –
event_name (Union[str, NoneType]) –
event_hash (Union[str, NoneType]) –
event_abi (Union[EventABI, NoneType]) –
network (Union[int, str, NoneType]) –
- Return type
<class ‘EventABI’>
- async ctc.evm.async_get_function_abi(function_name=None, contract_abi=None, contract_address=None, n_parameters=None, parameter_types=None, function_selector=None, network=None)