-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessingOptimism.py
383 lines (294 loc) · 11.9 KB
/
processingOptimism.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#getting user optimism yields
def doTheThingOptimism (address):
import requests
# for web things
import json
# for json things
from graphKey import returnKey
from donateAmounts import getDonateAmounts #(userAddress, alchemyEndpoint, subgraphEndpoint)
# import time
# to run a timer
api_key = open("/home/imimim/mysite/alch/alchemy_api_key_optimism.txt", "r")
# get the alchemy_api_key
alchemy_key = api_key.read()
# read the key from the file
api_key.close()
# close the opened file
apiString = "https://opt-mainnet.g.alchemy.com/v2/" + alchemy_key
# constructs the alchemy api key
def twos_complement(hex_value):
# converts hex number to decimal, including negative hex numbers
decimal_value = int(hex_value, 16)
bit_length = len(hex_value) * 4
if decimal_value & (1 << (bit_length - 1)):
decimal_value -= 1 << bit_length
return decimal_value
tokenInfo = [
# the various yield tokens
{
"name" : "aDAI",
"contract" : "0x43a502d7e947c8a2ebbaf7627e104ddcc253abc6",
"alchemist" : "0x10294d57a419c8eb78c648372c5baa27fd1484af",
"decimals" : 18
},
{
"name" : "aUSDC",
"contract" : "0x4186eb285b1efdf372ac5896a08c346c7e373cc4",
"alchemist" : "0x10294d57a419c8eb78c648372c5baa27fd1484af",
"decimals" : 6
},
{
"name" : "aUSDT",
"contract" : "0x2680b58945a31602e4b6122c965c2849eb76dd3b",
"alchemist" : "0x10294d57a419c8eb78c648372c5baa27fd1484af",
"decimals" : 6
},
{
"name" : "ysUSDC",
"contract" : "0x059eaa296b18e0d954632c8242ddb4a271175eed",
"alchemist" : "0x10294d57a419c8eb78c648372c5baa27fd1484af",
"decimals" : 6
},
{
"name" : "ysDAI",
"contract" : "0x0a86adbf58424ee2e304b395af0697e850730ecd",
"alchemist" : "0x10294d57a419c8eb78c648372c5baa27fd1484af",
"decimals" : 18
},
{
"name" : "aWETH",
"contract" : "0x337b4b933d60f40cb57dd19ae834af103f049810",
"alchemist" : "0xe04bb5b4de60fa2fba69a93ade13a8b3b569d5b4",
"decimals" : 18
},
{
"name" : "wstETH",
"contract" : "0xe04bb5b4de60fa2fba69a93ade13a8b3b569d5b4",
"alchemist" : "0xe04bb5b4de60fa2fba69a93ade13a8b3b569d5b4",
"decimals" : 18
}
]
earnings = []
# instantiates a blank array for tracking earnings
#if len(sys.argv) > 1:
# address = sys.argv[1]
# takes the address that's passed with the script and uses that as an address to look up
def getHarvests (yieldToken):
# uses the alchemix subgraph to get all of the harvests for a given token
query = """
{
alchemistHarvestEvents(
where: {timestamp_gte: \"0\", yieldToken: \"""" + yieldToken +"""\"}
orderBy: timestamp
orderDirection: asc
first: 1000
skip: 0
) {
contract {
id
}
transaction {
id
}
yieldToken
totalHarvested
timestamp
}
}
"""
# builds the query for getting a token's harvests
data = {
"query" : query
}
# puts the query into a usable data thingy for making a web request
headers = {
"Content-Type": "application/json"
}
#api call request headers
#graphURL = "https://api.thegraph.com/subgraphs/name/alchemix-finance/alchemix_v2_optimisim"
graphApiKey = returnKey()
graphURL = 'https://gateway-arbitrum.network.thegraph.com/api/' + graphApiKey + '/subgraphs/id/GYBJ8wsQFkSwcgCqhaxnz5RU2VbgedAkWUk2qx9gTnzr'
# url for querying the graph
queryResponse = requests.post(graphURL, json=data, headers=headers)
queryData = queryResponse.json()
return queryData
def getBlockNumber (txn):
# gets block number of transaction hash
headers = {
"Content-Type": "application/json"
}
#api call request headers
payload = {
"jsonrpc": "2.0",
"id": 1,
"method": "eth_getTransactionByHash",
"params" : [
txn
]
}
# api request payload
blockResponse = requests.post(apiString, headers=headers, data=json.dumps(payload))
blockInfo = blockResponse.json()
return(blockInfo["result"]["blockNumber"])
def getUserShares (blockNumber, alchemist, yieldToken):
# gets a user's number of shares in alchemist for a given yield token at given block number
headers = {
"Content-Type": "application/json"
}
#api call request headers
dataStr = "0x4bd21445000000000000000000000000" + address[2:42] + "000000000000000000000000" + yieldToken[2:42]
# composes the string that's passed in the payload to the api
payload = {
"jsonrpc": "2.0",
"id": 1,
"method": "eth_call",
"params" : [
{
"from": "0x0000000000000000000000000000000000000000",
"data": dataStr,
"to": alchemist
},
blockNumber
],
}
# api request payload
sharesResponse = requests.post(apiString, headers=headers, data=json.dumps(payload))
# sharesInfo = twos_complement(sharesResponse.json()["result"][0:66])
sharesInfo = int(sharesResponse.json()["result"][0:66],16)
return sharesInfo
def getTotalShares (blockNumber, alchemist, yieldToken):
# gets the total number of shares of a yield token in the alchemist at a given block
headers = {
"Content-Type": "application/json"
}
#api call request headers
dataStr = "0x5a5efc8b000000000000000000000000" + yieldToken[2:42]
# composes the string that's passed in the payload to the api
#0x5a5efc8b000000000000000000000000da816459f1ab5631232fe5e97a05bbbb94970c95
payload = {
"jsonrpc": "2.0",
"id": 1,
"method": "eth_call",
"params" : [
{
"from": "0x0000000000000000000000000000000000000000",
"data": dataStr,
"to": alchemist
},
blockNumber
],
}
# this invokes get yield token parameters, which includes total share
shareResponse = requests.post(apiString, headers=headers, data=json.dumps(payload))
#shareInfo = twos_complement(shareResponse.json()["result"][514:578])
shareInfo = int(shareResponse.json()["result"][514:578],16)
# the total shares is the ninth value in the really long hex string result.
# print ("shareInfo")
# print (shareInfo)
return shareInfo
def getHarvestShares (harvestInfo, alchemist, yieldToken, decimals, tokenName):
# takes harvestInfo and gets the address's share of the harvestInfo
for z in harvestInfo["data"]["alchemistHarvestEvents"]:
# print(z)
# print(z["transaction"]["id"])
blockNumber = getBlockNumber(z["transaction"]["id"])
# get the block number for a harvest transaction
# print("block number")
# print(blockNumber)
userShares = getUserShares(blockNumber, alchemist, yieldToken)
# get number of user shares for a yield token at the given block number
# print(userShares)
totalShares = getTotalShares (blockNumber, alchemist, yieldToken)
# get the total number of shares in the alchemist at the given block number
shareRatio = userShares / totalShares
# calculate what proportion of the total shares belonged to the address in question
# print("Share Ratio")
# print(shareRatio)
harvestShare = ((float(z["totalHarvested"]) * 0.9) * shareRatio) / (10 ** decimals)
# given that 90% of the harvest gets applied to pay down user debt, that 90% is multiplied by the share ratio and divided by the number of deccimals to get the value for how much was credited
# print("Harvest share")
# print(harvestShare)
harvestInfo = {
"timestamp" : z["timestamp"],
"txn" : z["transaction"]["id"],
"yieldToken" : tokenName,
"amountUnderlyingTokenEarned" : harvestShare
}
#compiles the information for an address's earnings for a harvest into one dict
if harvestInfo["amountUnderlyingTokenEarned"] > 0:
earnings.append(harvestInfo)
# adds that dict to the array of all earnings if the address had any share of the earnings.
def pinToPinata (inputJSON):
# takes inputed JSON file and pins to pinata
api_key = open("/home/imimim/mysite/alch/pinata_api_key.txt", "r")
# get the pinata_api_key
pinata_key = api_key.read()
# read the key from the file
api_key.close()
# close the opened file
# fileName = address + ".json"
fileName = "harvestEarnings.json"
request_headers = {
"Content-Type": "application/json",
"Authorization": pinata_key
}
# pinata api call request headers
request_data = {
"pinataOptions": {
"cidVersion": 1
},
"pinataMetadata": {
"name": fileName
},
"pinataContent": inputJSON
}
# api request payload for writing debt values to pinata
pinata_json_url = "https://api.pinata.cloud/pinning/pinJSONToIPFS"
# pinata endpoint for posting json
# print("Pinning new user debt file to pinata")
pinata_post = requests.post(pinata_json_url, headers = request_headers, data=json.dumps(request_data))
# calls the pinata API for writing debt values to pinata
result = pinata_post.json()
# turns the result into a usable json
# print("Pinata post result")
# print(result)
return result["IpfsHash"]
#start = time.time()
#start the timer
# y = 0
#if len(sys.argv) > 1:
# checks to make sure a value was passed with runnign the script
for x in tokenInfo :
# goes through each yield token to get the harvests
# print(x)
# print(x["contract"])
# print(x["alchemist"])
tokenHarvests = getHarvests(x["contract"])
# gets all the harvests for a given yield token
#print (tokenHarvests)
# y = y + 1
getHarvestShares(tokenHarvests, x["alchemist"], x["contract"], x["decimals"], x["name"])
# compiles address' share of harvests
#if y == 1:
# break
graphApiKey = returnKey()
graphURL = 'https://gateway-arbitrum.network.thegraph.com/api/' + graphApiKey + '/subgraphs/id/GYBJ8wsQFkSwcgCqhaxnz5RU2VbgedAkWUk2qx9gTnzr'
userRewards = getDonateAmounts(address, apiString, graphURL)
earnings.append(userRewards)
final = json.dumps(earnings, indent=4)
# makes json out of the compiled information
finalClean = json.loads(final)
# cleans up the json file
fileHash = pinToPinata(finalClean)
#pins the clean json file to pinata
fileURL = "https://ipfs.imimim.info/ipfs/" + fileHash
return fileURL
# end = time.time()
# stop the timer
# address = input("Please enter your address: ")
# finalPinataURL = doTheThingOptimism(address)
# print(finalPinataURL)
# duration = end - start
# print("This thing took")
# print(duration)
# print("seconds to execute")