Skip to content

Instantly share code, notes, and snippets.

@Quasimondo
Created April 2, 2021 12:03
Show Gist options
  • Save Quasimondo/9ab0ba241f356117e60e701bce60b461 to your computer and use it in GitHub Desktop.
Save Quasimondo/9ab0ba241f356117e60e701bce60b461 to your computer and use it in GitHub Desktop.
Some basic code to retrieve hic et nunc token data from better-call.dev
import os
import pickle
import requests
#download cached token data here:
#https://drive.google.com/file/d/1g_4w_Re5Y0NmcS2Y55WQzESWDeL2dey6/view?usp=sharing
#and put it into the same folder as this file
cachedTokenData = {"maxTokenID":-1,"knownTokenIds":{},"data":[]}
if os.path.exists("cached_token_data.pickle"):
cachedTokenData = pickle.load(open("cached_token_data.pickle","rb"))
url = " https://better-call.dev/v1/contract/mainnet/KT1RJ6PbjHpwc3M5rw5s2Nbmefwbuwbdxton/tokens"
keepLoading = True
offset = 0
maxTokenID = cachedTokenData["maxTokenID"]
while keepLoading:
try:
r = requests.get(url+"?offset="+str(offset))
data = r.json()
print(len(data),"tokens received, offset",offset)
except Exception as e:
print(e)
break
for i in range(len(data)):
if not data[i]["token_id"] in cachedTokenData["knownTokenIds"]:
cachedTokenData["data"].append( data[i])
cachedTokenData["knownTokenIds"][ data[i]["token_id"] ] = len(cachedTokenData["data"])-1
maxTokenID = max(data[i]["token_id"] ,maxTokenID)
if keepLoading and data[i]["token_id"]<cachedTokenData["maxTokenID"]:
print("all unknown tokens loaded","last token id:",data[i]["token_id"],"max known token id:",cachedTokenData["maxTokenID"])
keepLoading = False
offset+=len(data)
cachedTokenData["maxTokenID"] = maxTokenID
pickle.dump(cachedTokenData,open("cached_token_data.pickle","wb"))
print("total tokens:",len(cachedTokenData["data"]))
data = cachedTokenData["data"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment