Skip to content

Commit

Permalink
Add the example code for the fetch timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypan authored and Jay Pan committed Dec 17, 2024
1 parent 0482c0d commit 9f84c64
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tools/example/fetch_timestamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sys
sys.path.append('./')

from substrateinterface import SubstrateInterface
import datetime


if __name__ == '__main__':
URL = 'ws://127.0.0.1:10044'
substrate = SubstrateInterface(url=URL)

now_block_num = substrate.get_block_number(None)
block_hash = substrate.get_block_hash(now_block_num)
block_info = substrate.get_block(block_hash, include_author=True)
for extrinsic in block_info['extrinsics']:
if extrinsic.value['call']['call_module'] != 'Timestamp':
continue
if extrinsic.value['call']['call_function'] != 'set':
continue
timestamp = extrinsic.value['call']['call_args'][0]['value']
# Convert time stamp to human readable format
human_readable_time = datetime.datetime.fromtimestamp(timestamp / 1000).strftime('%Y-%m-%d %H:%M:%S')
print(human_readable_time)

0 comments on commit 9f84c64

Please sign in to comment.