Blocks
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
Get block¶
In [2]:
block = await ctc.async_get_block(14000000)
block
Out[2]:
{'base_fee_per_gas': 139541559304, 'difficulty': 12316581093827601, 'extra_data': '0x6175737472616c69612d736f75746865617374312d31', 'gas_limit': 30058561, 'gas_used': 8119826, 'hash': '0x9bff49171de27924fa958faf7b7ce605c1ff0fdee86f4c0c74239e6ae20d9446', 'logs_bloom': '0x083815041300081040f485088003de10046040491c3c0164189830ca3c2c0d1121040493008ba600924849820c8901130b0084038802610706000b6101b220d01210000400020b08480850880d0226a04024e107005114513410021080080451126063400a1084021c62bc08290008a0430081540c892545360c401410194021426103228249162800ac016601100d0520b96203432080086a00424d8214010b63180471318634d0ae00108041a1184488a10841a2610e0401e9001a504a4010098182020808155027009c02008e56070c28728114480490400801cac010290ae291a00ca0408b60204c128012601c100a24cc01e100897242832a825000c1e1', 'miner': '0xea674fdde714fd979de3edf0f56aa9716b898ec8', 'mix_hash': '0xa832679fcf3e71f0b29bd5913955151be5bf1fc59e135e3c7eb6e30f8442a5df', 'nonce': '0x31e7e99df18e0424', 'number': 14000000, 'parent_hash': '0x0c9ef41f038aa58a4aa2810fda03d9d82aac9082c80283230fd74cb1cceb4b00', 'receipts_root': '0xf7b9f8e92a4b420a27302ab91084e5499d3d711183a021f4956ff32de246e14e', 'sha3_uncles': '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', 'size': 58470, 'state_root': '0xfb3df73e7a41d500a374ee236dd613bb24f1a5fa5b80615a7953ea75c26c6a6d', 'timestamp': 1642114795, 'total_difficulty': '38896129597749836221010', 'transactions': ['0x3dac2080b4c423029fcc9c916bc430cde441badfe736fc6d1fe9325348af80fd', '0x82a6fdc276916a97a4723bb15ffc39b4e8efd2cd2044e7957b46a93c00e95b7c', '0x1dc98e66e46d8bbb0936b5d57ef9685caf2be3a561ae232fe35cc91d2f1f49d3', '0x28c466d01156df471dca4655c7199706afbcb4a849cc808e8640a7e7de89f7b2', '0x3b9c1f0a4a2e9eee54dea8c217711a3addb558d64fa2b7c70578fa2502edb0c3', '0x296cc22df5c8b951f297dc4e2cfd53562f67a774e80a089f479571b25670e66b', '0xb48403ca12261877e572c52d580dd978c275ebae9211e7a672ed857c8d5500f6', '0x627d63666c75192aec83ea65dc442df369decf696cf8144f68dbf7f187094039', '0x7781f35dc378a967e43dce46cd8607646a918be3bec904a8b7d2ef549e363417', '0x21ebb965dfcdaba5acd5be2b36edc33320f075dc39be2040e0927667f4457fb6', '0xa70fdd89ad3b69973359c2644fb2bf85d8c7e17f6e102eeb6477b19c38aa98bb', '0xdbd52ee92b76edd9db38da42856a51107433edd86cc77f85732f9fc34452fcdc', '0x5b8f2f0357dbdb278abd5d55b6f8fa138eb3c5bcb5eee853f359e3f12bda2838', '0x45113eb6abd490372fb22b9212adaa8dbbc882b4781fcee6cbb7643f7a338091', '0x9f6e14474230f412c1dd5034a2209372763539b6ee76acf7288836efc03bfd84', '0xda19521fd5f0ad64b33fe39c70f9a313bf432826aa1f37959a88381af34efa59', '0x26256210c64300cdef6bcd4663206676a918e0b7c4c2bef2058b839fe1bf5ade', '0xc26aac94c691040a0f126a0582c7342795af39034e79b54943c34ac99329c822', '0x8ab0f56e9cc352314368a20fcd73a7de622692983fd19e53087081578e889d57', '0x57d755615684d53415cf2d47004f46d78478fd81df886d86e0f6f2066f222b2f', '0x28aea3e818f854744bb6bb4a4f05cc5612026e5fb53eeffe750373bd6268c1b5', '0xfb81b965f0b55327d06b63e597cb80768d83227045e3065fd214982e3aae7ead', '0x58c03330f067e408fcb251bec67a18920bcf13b53f559fd7c99d90abb84c5751', '0xfe7efe42fb99036825c456c890f2da08ed58f22ef7c6b38d27d866eb69e31782', '0x3dfcf735a0acb9955a4457c5dfff589f16efc59926b7cfc4de74098fd17f4c79', '0xd49caad5d0fc7171266516ebb091da5662b36981138de47386e59677c6b73b2e', '0x12e72642adbfac3c0f2144e3f83a18c8a51aababea1940dfb458474aadd7d6fb', '0xb365be2030527975486f1a39427d846b8d33e052af1c0557eeec6aa902fee985', '0x7dc548e2551ae5b40170006c7a1f1c9d00006d43623f21fa77d9a9621da1960b', '0x814f18814e74e4e39393355ab9a21858e6894cd1bb74113f7f44f33c4441a310', '0xc9deea622c8901db67edd7c991d613c35b73fa2fca4fbd2bc59548cf34b42d88', '0xf55f98b1be7dabad23cff853ee0de0175b95ea3fdedc35ef323c126cc7df3ad9', '0x67b282062ff5ca96eb3d587b6ae4771bda0d170aec2e40a1ea93d23973443821', '0x60085d9f157a8426b3d2795de6cebbd4b50d541445be08e23f2b458d4bd0e60e', '0x6d7b7c7b43aeebece34dfb17c46a0bf83237693e57742d1c6166f1a74217125f', '0xb9768a9acddde75d8b80d4b9d661024086674341626837e31a3ac12c20ad7298', '0xf41ffd9f8a232dd5383328178999a3d859a8947b152ceb25650dbb4a0a9ff445', '0x7ad3c1541183fccb969fd4652e7a856b23026cec1e5153899c641579147df3e9', '0x73e5371291a6df1535f03573030bfca55c8185b65a939d6c59304778cbb59523', '0xc997cd28446ea82a660957535c8e6604885323d9fbd6c3189b1cc6e4bfc2ca32', '0x479806bdca2826848153f90caa7437e8b635639aab93f4cfabcc2bd914883780', '0xe4724b44a49cbf94d8f9657e619051fcf4ea4a5ea1bf98187b5a19b9623d910f', '0x37b618552833545cc294f42061ba5c43df47c71423ef58ea0fc71f7bc69cdfe8', '0x95d0cf5306b464b272445d80d5fcf264a4fba8ba908352e364aac0fb569d13b2', '0x71a5827a67459b7e7a1a7a84459174b8816441977b8b5be885cb2f05f2535319', '0x426e9c522f36c2bc8637616d7a125afd5dbac627d7e73e45f0477a282544f732', '0x1b8433d194a29b981d1a94a73b4e50de4de6e27cb3dd0f3a76e5919655fad73a', '0x937a105002120c54542d050270d81d618c70d702761630664d6a73d33085f993', '0xd6b6c532c065cd49a0485aa1af9bb067df5ad36704d30c799b248bd08eaf39ff', '0x2f002236f8f0d85cb411b7d5c958c266921ce2787ab14472afb8515cd3b1bcbd', '0x6851c2514310a597460ddffea524e1dd5b321124355dd83bd6b236b7c61ea1e9', '0x57b28d954dc03e9fbfb674fb100e96f721c27e9f1eaab9aaefb44729b2fd7ef3', '0xc303b7a2f54e89643b73c7ff11f165b2ba979878028f106686085da1b98302e4', '0xeaf6671ddf866fe0632612b80136032f50c096162198905ab6fd7544385ff64c', '0xd3607f690abf48d29c4d37ee117b0e9061dcf0e59919871dc2d4e0e25384ef4e', '0x108d74bf50d4cdfd5f778426f39facb32bd0773d53f5ce39167334824af6f376', '0xcac1e7878c91064571f7bd6c48c814b8c3bc39459f4e9ae7b034709b0f8c961e', '0x77ae0fe18ac9b3147e2e7542f4d352039c37c7ff3751a31e2bc69e27f5d72f55', '0xb5eb72b80b23698cf88dd445688109ee407cd98dc58f92e0617093fee529ba2a', '0xeb6b97eeb9e23275392757cb59140c05b67887c1d63d70b9d2e352ecc6927cb8', '0x077ad2efa19c7b4caeba6f175c2899b82382dff6c80cfe08571aad0189bd04a4', '0x56442cc66fc031cacf528aee196904d9665fe6d6da26d4de993e3ae6353d3ec5', '0x998340271bf5500ff7b4d732fd6d6cbc3c9d4fb9d8c367dec1a40fb9a130056e', '0x7a38f49483bf8270c05c0d168612b59e65b44acc6d0c465c447a90b356589b53', '0xeae63c92f6083e41292445909e5713e3227edc96bc7d84f85f43c739dba07de0', '0xc2ee70de2133d625f8033a4b4b08ea34b27dd0ab0da69dc62f010be2324bde3e', '0x028a22ee71202d985ab3c349aa5fd152bda1ca648a15392a9da1a9a9f8dd92dd', '0x6234a2669da8e91fcbe6c81458a0b89616a6d9ee81380b9e88d3d1b905a3a42f', '0x8a875ed36fa0116a9ce71bb289dddc24d84229a9e4b5b4329ad14c39234bc54d', '0xdfa69e312ba84abf6da37799b15171a5e553ec805aa23141f729da78fa00bfbe', '0xdb33606e300cc18a0f7d1d2ef8afc54f587117d75ae328d2cc1d21a281783b7f', '0x2cc9e8a5061bad4513ced54f4b14da3f49ffbb4fc662f5233a6cafe74e5e561f', '0x54f23a1418ee9ae058eafee2bd923087266fe0e4b92f477e9041444500cbe469', '0xaddf3bf9142cd7c2bface37492517e9eee2b4b29eb348f938ff7a4de16829150', '0xab0f76a492da23fdaf61a02de4cf4a9248c745262d16d9f3e3841a3d24f5ff9b', '0x031f831c69b9ada875b89de687114f6bdb58bad7db866e805b2a78326a392dc3', '0x4775c998bf82e2eea995deeae091df3e73f417ab27a2f5544348b7816af92127', '0xefddceb649e8b0d1b091fd9f7d768d13f2b085d50ae87becbc087c952879beaa', '0xc6c79ffb86f0f2efd16f49271224823d078a30a7892c465ba3cba20e87c0a36e', '0xccdeaf56bd85047cfd2d7c565341e862c55c139b81550bf35e2add633ddf3a47', '0xab8b149b54d17e86be6e817950be1ca7eeb92961189b5c378787eb6623f481d9', '0x62ddc128f28f1290b131a81fb1343fe865968d848fcb25ed6fc528a098e7e5f2', '0xdb3d3f5a611d7c7eba5aa9d262773d61a9b17053bec3b5b80ca32d693734ab9d', '0xb97a697052ae86df2bc654d0efaac8c5e8e6a8dd2b5e3f85b3405ea5aa8f3885', '0x8c776afd6a7788e60ba0f6a4cc481bd3aa83103c8c17181cf1258d22256d6c38', '0x8cf7be01b6b673636db8fcda3dfff311560c84d3415a7c0c1696f126955f02e9', '0x6b1672db798a679c676731595c2ed0a097e6f1289c0cbdd04b8a6294901017d5', '0x980458bce7ac5cd9298085fb9ad28d78ae05a32ef5957f34589bda023d28489a', '0x316614b6ff95f4dff67126c3823515b724b0dbb7b64b33d0c38fc3713e734c58', '0xf658c0e7cd445d33c51ab898a392e9be0b8993c9b488f2230e10053f2e06e464', '0xca36ca009002bfed0aab34e2f1956715ea87a6a2b9789510d7dedcb70d6caf93', '0x1f34dfdf44ba37e291a789a2f2ef2f50fd1c164f877bda42589011d78939fb03', '0xef3fbc478323963fddc3897fc87c2d5fde44723788bfdea8aee81d0112e4e9bf', '0x8fdda8443257b354f735edf389a2c21ec8b99622d4b1b7bfdc1dbd1da8794f0e', '0xe0c368234454e27f66d26b2d6fb56c3e69a5de50e0ac13ba8b551650a0a4889d', '0xb4fbcd7d03598433a5f62bf6c5b2b3439679ebc20ef70456f781c3b93de7f412', '0xef2a8b39cb701fb31de04f9012be4cf2afc926199712aa2ee84a9565900d9645', '0x3ee5d86e13de20224e294bde4e3ca05988b993c6c072da5dbfdbc91fea83a43c', '0x0d0022a1153b603ab7c366e53ed98519f42cdced8c4f6fa82ff91d5df3669e1e', '0x0c6bf6056445de030b248368fdab25b023775d8b83c29a42ccd9103d4f37400e', '0x9ea837318f53828f5cf1c41a9d37fafc84d9045823bf5c5f6a44d9cc36f2a796', '0x673c56108c17f9c3b72424212879e7a1e8af5250d7b5f902dcd640f9a0c9ab1a', '0xe2d6ec4a37beedad1b22c23086edbce75285eeb54fd84b6b38573d9e22553827', '0xb58330d5f922894800112c781831802d32b413eff2ee3b7656f3747ab3f8178a', '0x45552aea90d9a8ce86291a9b786d2b6106e625ccac44bb1984b9346ae84e50dc', '0x4db4831ae73afe2dba5079b4fbe50566f77bea28947078f46ef85d05b6beab41', '0xae264a00b982b439e21fea8a99845d3c3d1143b05d15b990ce5453579cd7ebc9', '0x5a80c76aa726b3bd0da69d1f3eaf4ab5d98ee9f95f337cc07799d2e5fd8f201a', '0x97abc3053b0e74381d52bfd3b278a135afdae0b7e1ee21a30dfbbc08dc20b422', '0x989240bc99820609573e19fbfa3b69780b06c6255163cb9059ac60dd0bec766c', '0x2f89ebd51377291f39b315a5feac7cbef0c8377f512bba32b128d59b086f6abe', '0x146063226f2bc60ab02fff825393555672ff505afb352ff11b820355422ba31e'], 'transactions_root': '0x16b6ff83df3ef14f614c70ac29e8a05d102c6bed0e5882c284abf0120b89529c', 'uncles': []}
Get blocks¶
In [3]:
blocks = await ctc.async_get_blocks(range(14000000, 14000100))
len(blocks)
Out[3]:
100
latest
is a valid block number reference¶
In [4]:
block = await ctc.async_get_block('latest')
block['number']
Out[4]:
15613480
In [5]:
await ctc.async_get_latest_block_number()
Out[5]:
15613480
Get block number of timestamp¶
In [6]:
block_number = await ctc.async_get_block_of_timestamp(1650946000)
block_number
Out[6]:
14657968
Get block numbers of timestamps¶
In [7]:
timestamps = range(1650746000, 1650946000, 86400)
block_numbers = await ctc.async_get_blocks_of_timestamps(timestamps)
block_numbers
Out[7]:
[14643250, 14649567, 14655946]
Gas Statistics¶
In [8]:
await ctc.async_get_block_gas_stats(14000000)
Out[8]:
{'base_fee': 139.541559304, 'min_gas_price': 140.0, 'median_gas_price': 142.203313325, 'mean_gas_price': 152.89720574486594, 'max_gas_price': 230.0, 'gas_used': 8119826, 'gas_limit': 30058561, 'n_transactions': 112}
Predicting future blocks¶
In [9]:
import tooltime
In [10]:
# predict timestamp of future block
block_number = 20000000
timestamp = await ctc.async_predict_block_timestamp(block_number)
print('timestamp:', timestamp)
print('timestamp pretty:', tooltime.timestamp_to_iso_pretty(timestamp))
timestamp: 1718757106 timestamp pretty: 2024-06-19 00:31:46Z
In [11]:
# predict block of future timesatmp
block_number = await ctc.async_predict_timestamp_block('2024-01-01')
block_number
Out[11]:
18735802
Reference
- async ctc.evm.async_get_block(block, *, include_full_transactions=False, provider=None, use_db=True)
get block from local database or from RPC node
- async ctc.evm.async_get_block_of_timestamp(timestamp, *, nary=None, cache=None, block_timestamps=None, block_timestamp_array=None, block_number_array=None, verbose=False, provider=None, use_db=True, use_db_assist=True, mode='>=')
search for the block that corresponds to a given timestamp
- async ctc.evm.async_get_blocks(blocks, *, include_full_transactions=False, chunk_size=500, provider=None, use_db=True, latest_block_number=None)
get blocks from local database or from RPC node
- async ctc.evm.async_get_blocks_of_timestamps(timestamps, *, block_timestamps=None, block_number_array=None, block_timestamp_array=None, nary=None, cache=None, provider=None, use_db=True, mode='>=')
search for blocks corresponding to list of timestamps