-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdonateAmounts.py
194 lines (152 loc) · 5.85 KB
/
donateAmounts.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
def getDonateAmounts(userAddress, alchemyEndpoint, subgraphEndpoint):
import requests
import json
def getEvents():
query = '''
{
alchemistDonateEvents(orderBy: timestamp, orderDirection: asc, first: 1000) {
amount
contract {
id
}
timestamp
yieldToken
transaction {
id
block {
number
}
}
}
}
'''
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
#print('Getting events')
queryResponse = requests.post(subgraphEndpoint, json=data, headers=headers)
queryResponse = queryResponse.json()
return(queryResponse['data']['alchemistDonateEvents'])
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(alchemyEndpoint, 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 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" + userAddress[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(alchemyEndpoint, 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 getYieldTokenSymbol (yieldToken):
query = '''
{
yieldTokens(where: {id: "''' + yieldToken + '''"}) {
id
name
symbol
}
}
'''
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
#print('Getting yield token symbol')
queryResponse = requests.post(subgraphEndpoint, json=data, headers=headers)
queryResponse = queryResponse.json()
symbol = queryResponse['data']['yieldTokens'][0]['symbol']
return symbol
events = getEvents()
userRewardsData = []
for event in events:
#print('--------------------------')
blockNumber = hex(int(event['transaction']['block']['number']))
#print('Block number: ')
#print(event['transaction']['block']['number'])
#print(blockNumber)
totalshares = getTotalShares (blockNumber, event['contract']['id'], event['yieldToken'])
#print('Total shares')
#print(totalshares)
usershares = getUserShares (blockNumber, event['contract']['id'], event['yieldToken'])
#print('User shares')
#print(usershares)
if usershares > 0:
userPercent = usershares/totalshares
#print('User percentage of take')
#print(userPercent)
userReward = userPercent * int(event['amount'])
userReward = userReward / 1e18
#print('User Reward')
#print(userReward)
yieldTokenSymbol = getYieldTokenSymbol(event['yieldToken'])
#print('Yield Token Symbol')
#print(yieldTokenSymbol)
if yieldTokenSymbol[-3:] == 'ETH':
rewardToken = 'alETH'
else:
rewardToken = 'alUSD'
#print('Reward token')
#print(rewardToken)
rewardsData = {
'timestamp' : event['timestamp'],
'txn' : event['transaction']['id'],
'yieldToken' : rewardToken,
'amountUnderlyingTokenEarned' : userReward
}
userRewardsData.append(rewardsData)
return userRewardsData