Contracts
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()
.
In [1]:
import ctc
In [2]:
address = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
Get proxy implementation¶
In [3]:
implementation = await ctc.async_get_proxy_implementation(address)
implementation
Out[3]:
'0xa2327a938febf5fec13bacfb16ae10ecbc4cbdcf'
In [4]:
await ctc.async_get_proxy_metadata(address)
Out[4]:
{'implementation': '0xa2327a938febf5fec13bacfb16ae10ecbc4cbdcf', 'proxy_type': 'openzeppelin'}
Get contract creation block¶
In [5]:
block_number = await ctc.async_get_contract_creation_block(address)
block_number
Out[5]:
6082465
Check whether address is a contract¶
In [6]:
await ctc.async_is_contract_address(address)
Out[6]:
True
Resolve address¶
In [7]:
# for more detailed tools, see ctc.protocols.ens_utils
address = await ctc.async_resolve_address('vitalik.eth')
print(address)
address = await ctc.async_resolve_address('vitalik.eth', block=14000000)
print(address)
0xd8da6bf26964af9d7eed9e03e53415d37aa96045 0xd8da6bf26964af9d7eed9e03e53415d37aa96045
In [8]:
await ctc.async_resolve_addresses(
['vitalik.eth', 'paradigm.eth', 'tetranode.eth'],
)
Out[8]:
['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', '0xd61415f6c5d93027778ceab5f099f64c29f58fd7', '0x465e7c8cd7126dad1a905624ea5d85eb717ef41f']
In [9]:
await ctc.async_resolve_address_by_block(
'tetranode.eth',
blocks=[13000000, 14000000, 14500000, 15000000],
)
Out[9]:
['0xf7832ae2ae254326c9fb9b7ea1e1fa74d867f05c', '0xf7832ae2ae254326c9fb9b7ea1e1fa74d867f05c', '0xf7832ae2ae254326c9fb9b7ea1e1fa74d867f05c', '0x465e7c8cd7126dad1a905624ea5d85eb717ef41f']
Get address checksum¶
In [10]:
ctc.get_address_checksum(address)
Out[10]:
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
Get CREATE and CREATE2 addresses¶
In [11]:
# CREATE
ctc.get_created_address(sender=address, nonce=42)
Out[11]:
'0x1740e833ed8c289d79323054524cdbd52de500b8'
In [12]:
# CREATE2
ctc.get_created_address(
sender=address,
salt='0xdeadbeef',
init_code='0xdeadbeef',
)
Out[12]:
'0x752083dd1c2e2f0b44de3a41a42ea21ed0345575'
Reference
- async ctc.evm.async_get_contract_creation_block(contract_address, *, provider=None, use_db=True, **search_kwargs)
get block number of when contract was created
behavior is undefined for functions that have undergone SELF-DESTRUCT(S)
caches result in local database