Created
February 9, 2022 14:53
-
-
Save SEJeff/a633c284fa4f3e519859da0edb3a67df to your computer and use it in GitHub Desktop.
Getting the Solana Proof of History clock time via the Clock sysvar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Uses construct and is a bit nicer code | |
import pytz | |
import pprint | |
import datetime | |
from construct import Int64ul, Int64sl, Struct | |
import requests | |
import base64 | |
def main(): | |
request_data = { | |
"jsonrpc": "2.0", | |
"id": 1, | |
"method": "getAccountInfo", | |
"params": [ | |
"SysvarC1ock11111111111111111111111111111111", | |
{"encoding": "base64"}, | |
], | |
} | |
url = "https://api.mainnet-beta.solana.com" | |
resp = requests.post( | |
url, | |
headers={ | |
'Content-Type': 'application/json', | |
}, | |
json=request_data, | |
) | |
if not resp.ok: | |
resp.raise_for_status() | |
data, encoding = resp.json()['result']['value']['data'] | |
raw_bytes = base64.b64decode(data) | |
# <QqQQq per: https://docs.rs/solana-program/1.8.3/solana_program/clock/struct.Clock.html | |
clock = Struct( | |
"slot" / Int64ul, | |
"epoch_start_timestamp" / Int64sl, | |
"epoch" / Int64ul, | |
"leader_schedule_epoch" / Int64ul, | |
"unix_timestamp" / Int64sl, | |
).parse(raw_bytes) | |
data = {x: y for x, y in clock.items() if not x.startswith("_")} | |
for key in ("epoch_start_timestamp", "unix_timestamp"): | |
data[key] = datetime.datetime.fromtimestamp(data[key], pytz.UTC) | |
return data | |
if __name__ == '__main__': | |
pprint.pprint(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Uses stdlib only | |
import pytz | |
import pprint | |
import datetime | |
import struct | |
import requests | |
import base64 | |
def main(): | |
request_data = { | |
"jsonrpc": "2.0", | |
"id": 1, | |
"method": "getAccountInfo", | |
"params": [ | |
"SysvarC1ock11111111111111111111111111111111", | |
{"encoding": "base64"}, | |
], | |
} | |
url = "https://api.mainnet-beta.solana.com" | |
resp = requests.post( | |
url, | |
headers={ | |
'Content-Type': 'application/json', | |
}, | |
json=request_data, | |
) | |
if not resp.ok: | |
resp.raise_for_status() | |
data, encoding = resp.json()['result']['value']['data'] | |
raw_bytes = base64.b64decode(data) | |
# <QqQQq per: https://docs.rs/solana-program/1.8.3/solana_program/clock/struct.Clock.html | |
slot, epoch_start_timestamp, epoch, leader_schedule_epoch, unix_timestamp = struct.unpack( | |
"<QqQQq", raw_bytes, | |
) | |
return { | |
"slot": slot, | |
"epoch_start_timestamp": datetime.datetime.fromtimestamp(epoch_start_timestamp, pytz.UTC), | |
"epoch": epoch, | |
"leader_schedule_epoch": leader_schedule_epoch, | |
"unix_timestamp": datetime.datetime.fromtimestamp(unix_timestamp, pytz.UTC), | |
} | |
if __name__ == '__main__': | |
pprint.pprint(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment