forked from 0xA1176ec01045/CompEarlyUserAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompoundV1.USDinterest.py
More file actions
259 lines (243 loc) · 12 KB
/
CompoundV1.USDinterest.py
File metadata and controls
259 lines (243 loc) · 12 KB
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
import pandas as pd
import web3
import json
import requests
from datetime import date, timedelta
from sys import argv
#earlyUserFile = 'CompoundV1.EarlyUserEvents.csv'
earlyUserFile = 'CompoundV1.EarlyUserEvents.csv'
outfile = 'CompoundV1.EarlyUserUSDInterest.csv'
txData = pd.read_csv(earlyUserFile,header=None,skiprows=1,
names=['block','txhash','address','action','token','decimals',
'amount','startingBalance','newBalance'])
tokenList = ['ZRX','BAT','REP','WETH','SAI','ETH','USDC','WBTC','DAI']
# Compound V1 contract metadata
CompV1 = {
"address" : '0x3FDA67f7583380E67ef93072294a7fAc882FD7E7',
"deployBlock" : 6400278,
"abi" : 'CompoundV1.abi.json'
}
ZRX = {
"address" : '0xE41d2489571d322189246DaFA5ebDe1F4699F498',
"label" : 'ZRX',
"decimals" : 18
}
BAT = {
"address" : '0x0D8775F648430679A709E98d2b0Cb6250d2887EF',
"label" : 'BAT',
"decimals" : 18
}
REP = {
"address" : '0x1985365e9f78359a9B6AD760e32412f4a445E862',
"label" : 'REP',
"decimals" : 18
}
WETH = {
"address" : '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
"label" : 'WETH',
"decimals" : 18
}
SAI = {
"address" : '0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359',
"label" : 'SAI',
"decimals" : 18
}
V1Tokens = [ZRX,BAT,REP,WETH,SAI]
CompV1 = {
"address" : '0x3FDA67f7583380E67ef93072294a7fAc882FD7E7',
"deployBlock" : 6400278,
"abi" : 'CompoundV1.abi.json'
}
CoinGeckoLabel = {'ZRX' : '0x',
'BAT' : 'basic-attention-token',
'REP' : 'augur',
'WETH': 'ethereum', # use ethereum records for WETH price (CoinGecko weth has holes)
'SAI' : 'sai',
'ETH' : 'ethereum',
'USDC': 'usd-coin',
'WBTC': 'wrapped-bitcoin',
'DAI' : 'dai'
}
# Get a web3 object pulling data from Infura Ethereum Mainnet RPC
w3 = web3.Web3(web3.Web3.HTTPProvider('YOUR-RPC-HERE'))
with open('abis/'+CompV1["abi"]) as json_file:
CompoundV1ABI = json.load(json_file)
MoneyMarketABI = CompoundV1ABI["MoneyMarket"]
MoneyMarket = w3.eth.contract(CompV1["address"],abi=MoneyMarketABI)
CompV1deployBlock = 6400278
# Cutoff at date of COMP token announcement:
# Using first block found on Feb 26, 2020 UTC:
EarlyUserCutoffBlock = 9555731
# Initialize a new column to store interest data in USD
#txData['USDinterest'] = 0
# ...sort transaction data by address and then by block and token
txData = txData.sort_values(['address','block','token'])
# Initialize a dataframe to store addresses' interest by token
accruedInterest = txData[['address']]
accruedInterest = pd.DataFrame(accruedInterest['address'].unique(),columns=['address'])
for token in tokenList:
tokenSupplyInterest = token + 'SupplyInterest'
tokenBorrowInterest = token + 'BorrowInterest'
accruedInterest[tokenSupplyInterest] = 0
accruedInterest[tokenBorrowInterest] = 0
# Pull historical price data from CoinGecko API into a dataframe 'pricedata'
pricedata = dict()
startTimeStamp = w3.eth.getBlock(CompV1deployBlock)['timestamp']
startDate = date.fromtimestamp(startTimeStamp)
endTimeStamp = w3.eth.getBlock(EarlyUserCutoffBlock)['timestamp']
endDate = date.fromtimestamp(endTimeStamp)
daysToQuery = str((date.today()-startDate).days)
datelist = pd.date_range(startDate,endDate).tolist()
for token in tokenList:
pricedata[token] = dict()
apicall = 'https://api.coingecko.com/api/v3/coins/'+CoinGeckoLabel[token]
apicall += '/market_chart?vs_currency=usd&days='+daysToQuery+'&interval=daily'
response = requests.get(apicall)
apidata = json.loads(response.content)
i = 0
for day in datelist:
try:
pricedata[token][day.day] = apidata['prices'][i][1]
except:
pricedata[token][day.day] = 0.
i += 1
# Compound V1 interest accrual tabulation
for index, row in txData.iterrows():
if row['action'] == 'liquidate':
continue
# Extract account status from event log
newBalance = float(row['newBalance'])
startingBalance = float(row['startingBalance'])
block = int(row['block'])
address = str(row['address'])
amount = float(row['amount'])
token = str(row['token'])
tokenSupplyInterest = token + 'SupplyInterest'
tokenBorrowInterest = token + 'BorrowInterest'
# Get the dictionary associated with this token
for i in range(len(V1Tokens)):
if V1Tokens[i]['label'] == token:
thisToken = V1Tokens[i]
# Get timestamp for this block
blockInfo = w3.eth.getBlock(block)
timestamp = blockInfo['timestamp']
blockdate = date.fromtimestamp(timestamp)
# Get USD conversion factor for this token and date
convertToUSD = pricedata[token][blockdate.day]*10**(-thisToken['decimals'])
if row['action'] == 'supply':
interest = newBalance - startingBalance - amount
interest = interest*convertToUSD
accruedInterest.loc[accruedInterest['address'] == address,
tokenSupplyInterest] += interest
print("Accrued supply interest " + str(interest) + " to " + str(row['address']))
elif row['action'] == 'withdraw':
interest = newBalance - startingBalance + amount
interest = interest*convertToUSD
accruedInterest.loc[accruedInterest['address'] == address,
tokenSupplyInterest] += interest
print("Accrued supply (redeem) interest " + str(interest) + " to " + str(row['address']))
elif row['action'] == 'borrow':
interest = newBalance - startingBalance - amount
interest = interest*convertToUSD
accruedInterest.loc[accruedInterest['address'] == address,
tokenBorrowInterest] += interest
print("Accrued borrow interest " + str(interest) + " to " + str(row['address']))
elif row['action'] == 'repay':
interest = newBalance - startingBalance + amount
interest = interest*convertToUSD
accruedInterest.loc[accruedInterest['address'] == address,
tokenBorrowInterest] += interest
print("Accrued borrow (repay) interest " + str(interest) + " to " + str(row['address']))
# Finally, we need to reconcile interest associated with any
# outstanding balances at the end of the qualifying period;
# This is a bit harder because we don't have contract events to work with
# Get timestamp for early user cutoff
blockInfo = w3.eth.getBlock(EarlyUserCutoffBlock)
timestamp = blockInfo['timestamp']
blockdate = date.fromtimestamp(timestamp)
print("Accruing outstanding interest...")
for index,row in accruedInterest.iterrows():
# For each address, find any nonzero final newBalances in the txData,
# including both supply/withdraw and borrow/repay balances
thisAddressTxData = txData[txData['address']==row['address']]
for token in tokenList:
# Get the dictionary associated with this token
for i in range(len(V1Tokens)):
if V1Tokens[i]['label'] == token:
thisToken = V1Tokens[i]
# Get USD conversion factor for this token and date
convertToUSD = pricedata[token][blockdate.day]*10**(-thisToken['decimals'])
thisTokenTxData = thisAddressTxData[thisAddressTxData['token']==token]
if thisTokenTxData.empty:
continue
# Below, we inspect the tx closest to the EarlyUserCutoffBlock,
# while within the early user window, for this (address,token) pair.
# If this tx is a liquidation (no newBalance), move back to the
# next-most-recent tx until we find a non-liquidation event
txCount = 0
foundSupplyWithdraw = False
foundBorrowRepay = False
while foundSupplyWithdraw == False or foundBorrowRepay == False:
#print("Still looking for a latest supply or latest borrow...")
txCount += 1
if thisTokenTxData.empty:
break
if len(thisTokenTxData.tail(txCount)['newBalance'].values) >= txCount:
break
try:
lastNewBalance = int(thisTokenTxData.tail(txCount)['newBalance'].values[0])
except:
foundSupplyWithdraw = True
foundBorrowRepay = True
if lastNewBalance != 0:
# compute interest on outstanding balance
# from blockDelta to last supply/borrow
# and interest rate per block at EarlyUserCutoffBlock
lastTxBlock = thisTokenTxData.tail(txCount)['block'].values[0]
blockDelta = int(EarlyUserCutoffBlock - lastTxBlock)
if (blockDelta < 0):
continue
thisAction = thisTokenTxData.tail(1)['action'].values
if thisAction == 'liquidate':
continue
elif thisAction == 'supply' or 'withdraw':
FoundSupplyWithdraw = True
# Using the current V1 rate (approximation);
# no easy way to extract the rate at a specific block in the past
supplyData = MoneyMarket.functions.markets(thisToken['address']).call()
# supplyIndex, Mantissa are 5th, 4th index returned by V1 markets()
supplyIndex = float(supplyData[5])
supplyRateMantissa = float(supplyData[4])
newInterestIndex = (1.+supplyRateMantissa*10**(-thisToken['decimals'])*blockDelta)*supplyIndex
newBalance = lastNewBalance*(newInterestIndex/supplyIndex)
interest = newBalance - lastNewBalance
interest = interest*convertToUSD
print('reconciling ' + str(interest) + ' ' + thisToken['label'] + ' to ' + row['address'])
print(row['address'] + " has " + str(newBalance*10**(-thisToken['decimals'])) + " " + thisToken['label'] + " with residual supply interest of " + str(interest*10**(-thisToken['decimals'])) + " " + thisToken['label'])
accruedInterest.loc[accruedInterest['address'] == address,
tokenSupplyInterest] += interest
#print("Accrued supply interest " + str(interest) + " to " + str(row['address']))
elif thisAction == 'borrow' or 'repay':
FoundBorrowRepay = True
borrowData = MoneyMarket.functions.markets(thisToken['address']).call()
# borrowIndex, Mantissa are the 8th, 7th index returned by V1 markets()
borrowIndex = float(borrowData[8])
borrowRateMantissa = float(borrowData[7])
newInterestIndex = (1.+borrowRateMantissa*10**(-thisToken['decimals'])*blockDelta)*borrowIndex
newBalance = lastNewBalance*(newInterestIndex/supplyIndex)
interest = newBalance - lastNewBalance
interest = interest*convertToUSD
print('reconciling ' + interest + ' ' + thisToken['label'] + ' to ' + row['address'])
print(row['address'] + " has " + str(newBalance*10**(-thisToken['decimals'])) + " " + thisToken['label'] + " with residual borrow interest of " + str(interest*10**(-thisToken['decimals'])) + " " + thisToken['label'])
accruedInterest.loc[accruedInterest['address'] == address,
tokenBorrowInterest] += interest
#print("Accrued borrow (repay) interest " + str(interest) + " to " + str(row['address']))
else:
thisAction = thisTokenTxData.tail(1)['action'].values
if thisAction == 'liquidate':
continue
elif thisAction == 'supply' or 'withdraw':
foundSupplyWithdraw = True
elif thisAction == 'borrow' or 'repay':
foundBorrowRepay = True
accruedInterest.to_csv(outfile,index=False)