Reputation: 110
I am just starting to learn Python for some data cleanup and I am running into an issue. I am writing a lambda function to pass through transactions for an address on zkSync Rollup on Ethereum.
import json
import requests
URL = "https://api.zksync.io/api/v0.1/account/0x2990d87e823d3cca83683207dcb2d5660debf376/history/"
def lambda_handler(event, context):
# TODO implement
#r = requests.get(url = URL)
#data = r.json()
newdata = []
#dataloaded = json.loads(data)
#for key in data:
# newdata = newdata + key
x = 1001
while x < 2000:
y = x
newURL = URL + str(y) + "/" + str(100)
r = requests.get(url = newURL)
data = r.json()
Dict = {}
i = 0
while i < 100:
Dict = {'nonce': data[i]['tx']['nonce'], 'created_at': data[i]['created_at'],'serial' : i, 'orders': data[i]['tx']['orders'] }
Dict_copy = Dict.copy()
newdata.append(Dict_copy)
i +=1
x +=100
#keyValList = ['0']
#expectedResult = [d for d in newdata if d['tx']['orders'][0]['tokenbuy'] in keyValList]
return {
'statusCode': 200,
#'body': data[0]['created_at']
'body': newdata
}
The API call takes in two values after history/, first being the tx number we want to look at, and second being the number of txs to be served per API call, which is limited to 100. So I've created a short script to parse 1000 txs per call. However the query keeps crashing if I start from tx number 1000(x value) instead of 0. Please help and apologies if the question isn't clear enough.
Upvotes: 1
Views: 145
Reputation: 33
Before we tackle the question at hand lets clear up the code a bit.
Let's not override the Dict class with a variable.
Dict = {} # ❌
my_dict = {} # ⭕
# or any other name you want.
Why use a while loop and i+=1 when we can use a for?
for i in range(100):
my_dict = {'nonce': data[i]['tx']['nonce'], 'created_at': data[i]['created_at'],'serial' : i, 'orders': data[i]['tx']['orders'] }
my_dict_copy = my_dict.copy() # I don't see why you're making a copy but sure.
newdata.append(my_dict_copy)
And the outer loop:
for x in range(1001, 2000, 100): # start at 1001, end before 2000, jump 100
[...]
In the beginning of the outer loop, lets use F strings.
newURL = URL + str(y) + "/" + str(100) # ??
newURL = f"{URL}{x}/100" # no reason to do y = x
To the problem at hand, the problem occurs because some of the entries don't have ['tx']['orders'] in them. When checking this I got that in 1501, and 1901 there are errors. If we can ignore these 2 exceptions, then:
import requests
URL = "https://api.zksync.io/api/v0.1/account/0x2990d87e823d3cca83683207dcb2d5660debf376/history/"
def is_valid(data_at_index_i, i):
try:
# check that all the fields are here
_ = {'nonce': data_at_index_i['tx']['nonce'], 'created_at': data_at_index_i['created_at'],'serial': i, 'orders': data_at_index_i['tx']['orders']}
except KeyError:
# we might be interested in what these entries are,
# so lets print them.
print(data_at_index_i)
print("Skipping", i, end=" ")
return 0
return 1
def lambda_handler():
# TODO implement
newdata = []
start = 1001
for x in range(start, start+1000, 100):
newURL = f"{URL}{x}/100"
r = requests.get(url = newURL)
data = r.json()
my_dict = {}
for i in range(100):
if not is_valid(data[i], i):
print(f"{x=}")\
else:
my_dict = {'nonce': data[i]['tx']['nonce'], 'created_at': data[i]['created_at'],'serial': i, 'orders': data[i]['tx']['orders']}
newdata.append(my_dict)
return {
'statusCode': 200,
#'body': data[0]['created_at']
'body': newdata
}
Upvotes: 1