ABIs
Examples
Note
These examples are crafted as a Jupyter notebook. You can download the original notebook file here.
Also note that inside Jupyter notebooks, await
can be used freely outside of asyncio.run()
.
import ctc
Get contract ABI¶
# example contract: LUSD
contract_address = '0x5f98805a4e8be255a32880fdec7f6728c6568ba0'
contract_abi = await ctc.async_get_contract_abi(contract_address)
ctc.summarize_contract_abi(contract_abi)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [3], in <cell line: 3>() 1 contract_abi = await ctc.async_get_contract_abi(contract_address) ----> 3 ctc.summarize_contract_abi(contract_abi) AttributeError: module 'ctc' has no attribute 'summarize_contract_abi'
Get function ABI by name¶
function_abi = await ctc.async_get_function_abi(
contract_address=contract_address,
function_name='balanceOf',
)
function_abi
{'inputs': [{'internalType': 'address', 'name': 'account', 'type': 'address'}], 'name': 'balanceOf', 'outputs': [{'internalType': 'uint256', 'name': '', 'type': 'uint256'}], 'stateMutability': 'view', 'type': 'function'}
Get function ABI by selector¶
function_abi = await ctc.async_get_function_abi(
contract_address=contract_address,
function_selector='0x70a08231',
)
function_abi
{'inputs': [{'internalType': 'address', 'name': 'account', 'type': 'address'}], 'name': 'balanceOf', 'outputs': [{'internalType': 'uint256', 'name': '', 'type': 'uint256'}], 'stateMutability': 'view', 'type': 'function'}
Get event ABI by name¶
event_abi = await ctc.async_get_event_abi(
contract_address=contract_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 ctc.async_get_event_abi(
contract_address=contract_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 = ctc.get_function_signature(function_abi)
function_selector = ctc.get_function_selector(function_abi)
parameter_names = ctc.get_function_parameter_names(function_abi)
parameter_types = ctc.get_function_parameter_types(function_abi)
output_names = ctc.get_function_output_names(function_abi)
output_types = ctc.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: balanceOf(address) function_selector: 70a08231 parameter_names: ['account'] parameter_types: ['address'] output_names: [''] output_types: ['uint256']
Parse an event ABI¶
event_hash = ctc.get_event_hash(event_abi)
event_signature = ctc.get_event_signature(event_abi)
event_indexed_names = ctc.get_event_indexed_names(event_abi)
event_indexed_types = ctc.get_event_indexed_types(event_abi)
event_unindexed_names = ctc.get_event_unindexed_names(event_abi)
event_unindexed_types = ctc.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¶
import ctc.rpc
bytecode = await ctc.rpc.async_eth_get_code(contract_address)
function_abis = await ctc.async_decompile_function_abis(bytecode)
for function_abi in function_abis:
print(function_abi['hex_signature'], function_abi['text_signature'])
0xa9059cbb transfer(address,uint256) 0xa9059cbb many_msg_babbage(bytes1) 0xa9059cbb transfer(bytes4[9],bytes5[6],int48[11]) 0xa9059cbb func_2093253501(bytes) 0xb7f8cf9b borrowerOperationsAddress() 0xbb997bac sendToPool(address,address,uint256) 0xd505accf permit(address,address,uint256,uint256,uint8,bytes32,bytes32) 0xdd62ed3e allowance(address,address) 0xdd62ed3e remove_good(uint256[],bytes8,bool) 0xdd62ed3e _func_5437782296(address,address) 0xf698da25 domainSeparator() 0x5a4d28bb troveManagerAddress() 0x70a08231 balanceOf(address) 0x70a08231 branch_passphrase_public(uint256,bytes8) 0x70a08231 passphrase_calculate_transfer(uint64,address) 0x7ecebe00 nonces(address) 0x95d89b41 symbol() 0x95d89b41 link_classic_internal(uint64,int64) 0x9dc29fac burn(address,uint256) 0xa457c2d7 decreaseAllowance(address,uint256) 0x20c582be returnFromPool(address,address,uint256) 0x23b872dd transferFrom(address,address,uint256) 0x23b872dd gasprice_bit_ether(int128) 0x313ce567 decimals() 0x313ce567 available_assert_time(uint16,uint64) 0x39509351 increaseAllowance(address,uint256) 0x40c10f19 mint(address,uint256) 0x54fd4d50 version() 0x06fdde03 name() 0x06fdde03 message_hour(uint256,int8,uint16,bytes32) 0x095ea7b3 approve(address,uint256) 0x095ea7b3 sign_szabo_bytecode(bytes16,uint128) 0x0b622ab2 stabilityPoolAddress() 0x10ce43bd permitTypeHash() 0x18160ddd totalSupply() 0x18160ddd voting_var(address,uint256,int128,int128)
Reference
- async ctc.evm.async_decompile_function_abis(bytecode, sort=None)
decompile solidity-style function ABI’s from contract bytecode
- Return type
Sequence[Mapping[str, Any]]
- async ctc.evm.async_get_contract_abi(contract_address, *, network=None, provider=None, use_db=True, db_query=None, db_intake=None, block=None, proxy_implementation=None, verbose=True)
retrieve abi of contract either from local database or block explorer
for addresses that change ABI’s over time, use db_query=False to skip cache
- async ctc.evm.async_get_event_abi(*, contract_abi=None, contract_address=None, event_name=None, event_hash=None, event_abi=None, network=None)
get event ABI from local database or block explorer
- 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)
get function ABI from local database or block explorer
ctc.binary.get_event_hash ctc.binary.get_event_indexed_names ctc.binary.get_event_indexed_types ctc.binary.get_event_signature ctc.binary.get_event_unindexed_names ctc.binary.get_event_unindexed_types ctc.binary.get_function_output_names ctc.binary.get_function_output_types ctc.binary.get_function_parameter_names ctc.binary.get_function_parameter_types ctc.binary.get_function_selector ctc.binary.get_function_signature