-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compound Pool Economics - The Graph.py
384 lines (254 loc) · 8.14 KB
/
Compound Pool Economics - The Graph.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
#!/usr/bin/env python
# coding: utf-8
# # Compound Pool Economics - The Graph
# In[1]:
# cadCAD standard dependencies
# cadCAD configuration modules
from cadCAD.configuration.utils import config_sim
from cadCAD.configuration import Experiment
# cadCAD simulation engine modules
from cadCAD.engine import ExecutionMode, ExecutionContext
from cadCAD.engine import Executor
# cadCAD global simulation configuration list
from cadCAD import configs
# In[2]:
# Additional dependencies
# For parsing the data from the API
import json
# For downloading data from API
import requests as req
# For generating random numbers
import math
# For analytics
import pandas as pd
# For visualization
import plotly.express as px
import numpy as np
import datetime
# # Setup / Preparatory Steps
#
# ## Query the Balancer subgraph for a UNI-BAL pool
# In[3]:
# You can explore the subgraph at https://thegraph.com/hosted-service/subgraph/graphprotocol/compound-v2
API_URI = 'https://api.thegraph.com/subgraphs/name/graphprotocol/compound-v2'
# Query for retrieving the history of swaps on a BAL <> UNI 50-50 pool
GRAPH_QUERY = '''
{
markets{
borrowRate
supplyRate
totalBorrows
totalSupply
exchangeRate
}
}
'''
'''
borrowRate
cash
collateralFactor
exchangeRate
interestRateModelAddress
name
reserves
supplyRate
symbol
id
totalBorrows
totalSupply
underlyingAddress
underlyingName
underlyingPrice
underlyingSymbol
reserveFactor
underlyingPriceUSD
'''
# Retrieve data from query
JSON = {'query': GRAPH_QUERY}
r = req.post(API_URI, json=JSON)
graph_data = json.loads(r.content)['data']
print("Print first 200 characters of the response")
print(r.text[:200])
# ## Data Wrangle the Data
# In[4]:
raw_df = pd.DataFrame(graph_data['markets'])
raw_df.head(5)
# In[5]:
# Clean the data:
# 1. convert the raw timestamps to Python DateTime objects
# 2. make the token flow values numerical
# 3. order by time
df = (raw_df.assign(totalBorrows=lambda df: pd.to_numeric(df.totalBorrows))
.assign(totalSupply=lambda df: pd.to_numeric(df.totalSupply))
.assign(borrowRate=lambda df: pd.to_numeric(df.borrowRate))
.assign(supplyRate=lambda df: pd.to_numeric(df.supplyRate))
.assign(exchangeRate=lambda df: pd.to_numeric(df.exchangeRate))
#.assign(blockTimestamp=lambda df: (pd.to_datetime(df.blockTimestamp, unit='s')))
.reset_index()
)
df.head(5)
# # Modelling
# ## 1. State Variables
# In[6]:
initial_state = {
'lender_APY': 0.0,
'borrower_rate': 0.0,
'utilization_rate': 0.0,
'exchange_rate': 0.0
#'block_time_stamp': None
}
initial_state
# ## 2. System Parameters
# In[7]:
# Transform the swap history data frame into a {timestep: data} dictionary
# Turning the df into a dictionary form
df_dict = df.to_dict(orient='index')
system_params = {
'new_df': [df_dict]
# Transaction fees being applied to the input token
#'exchangeRate': df['exchangeRate']
#'block_time_stamp': ['blockTimestamp']
}
# Element for timestep = 3
# In[ ]:
# ## 3. Policy Functions
# In[8]:
def p_rates(params, substep, state_history, previous_state):
"""
Calculate cumulative transaction fees & swaps
from a swap event
"""
t = previous_state['timestep']
# Data for this timestep
ts_data = params['new_df'][t]
lender_APY = ts_data['supplyRate']
borrower_rate = ts_data['borrowRate']
exchange_rate = ts_data['exchangeRate']
#block_time_stamp = ts_data['blockTimestamp']
total_borrowed = ts_data['totalBorrows']
TVL = ts_data['totalSupply']
#utilization_rate = total_borrowed / TVL * 100
try:
utilization_rate = pd.to_numeric(total_borrowed) / pd.to_numeric(TVL) * 100
except ZeroDivisionError:
utilization_rate = 0
#exchange_rate1 = swap_in * params['exchange_rate']
return {'lender_APY': lender_APY,
'borrower_rate': borrower_rate,
'exchange_rate': exchange_rate,
'utilization_rate': utilization_rate}
#'block_time_stamp': block_time_stamp
# In[9]:
print(df.dtypes)
# ## 4. State Update Functions
# In[10]:
def s_lender_APY(params,
substep,
state_history,
previous_state,
policy_input):
value = policy_input['lender_APY']
return ('lender_APY', value)
def s_borrower_APY(params,
substep,
state_history,
previous_state,
policy_input):
value = policy_input['borrower_rate']
#fee = policy_input['fee_UNI']
#value = previous_state['cumulative_fee_UNI'] + fee
return ('borrower_rate', value)
def s_utilization_rate(params,
substep,
state_history,
previous_state,
policy_input):
value = policy_input['utilization_rate']
#fee = policy_input['fee_BAL']
#value = previous_state['cumulative_fee_BAL'] + fee
return ('utilization_rate', value)
def s_exchange_rate(params,
substep,
state_history,
previous_state,
policy_input):
value = policy_input['exchange_rate']
#fee = policy_input['fee_BAL']
#value = previous_state['cumulative_fee_BAL'] + fee
return ('exchange_rate', value)
'''def s_block_time_stamp(params,
substep,
state_history,
previous_state,
policy_input):
value = policy_input['block_time_stamp']
#fee = policy_input['fee_BAL']
#value = previous_state['cumulative_fee_BAL'] + fee
return ('block_time_stamp', value)'''
# ## 5. Partial State Update Blocks
# In[11]:
partial_state_update_blocks = [
{
'policies': {
'policy_rates': p_rates
},
'variables': {
's_lender_APY': s_lender_APY,
's_borrower_rate': s_borrower_APY,
's_exchange_rate': s_exchange_rate,
's_utilization_rate': s_utilization_rate
#'s_block_time_stamp': s_block_time_stamp
}
}
]
# # Simulation
# ## 6. Configuration
# In[12]:
sim_config = config_sim({
"N": 1, # the number of times we'll run the simulation ("Monte Carlo runs")
"T": range(len(df)), # the number of timesteps the simulation will run for
"M": system_params # the parameters of the system
})
# In[13]:
del configs[:] # Clear any prior configs
# In[14]:
experiment = Experiment()
experiment.append_configs(
initial_state = initial_state,
partial_state_update_blocks = partial_state_update_blocks,
sim_configs = sim_config
)
# ## 7. Execution
# In[15]:
exec_context = ExecutionContext()
simulation = Executor(exec_context=exec_context, configs=configs)
raw_result, tensor_field, sessions = simulation.execute()
# ## 8. Output Preparation
# In[16]:
simulation_result = pd.DataFrame(raw_result)
simulation_result.head(5)
# ## 9. Analysis
# In[17]:
# Visualize how much transaction fees were paid over time on each token
print("High supply of lenders → Low utilization rate → Lower lender APY")
print("High demand for borrowing → High utilization rate → Higher borrower rates")
graph = px.line(simulation_result,
x='timestep',
y=['borrower_rate','lender_APY','exchange_rate'],
#],
#, 'utilization_rate'],
title='Compound Pool Economics',
facet_row='subset')
graph.update_layout(yaxis=dict(tickformat="%", hoverformat="%.2f%"))
graph1 = graph.update_yaxes(hoverformat=".2%")
graph1
# In[18]:
ur_graph = px.line(simulation_result,
x='timestep',
y=['utilization_rate'],
facet_row='subset')
ur_graph = ur_graph.update_layout(yaxis=dict(tickformat="%", hoverformat="%.2f%"))
ur_graph1 = ur_graph.update_yaxes(hoverformat=".2%")
ur_graph1
# In[ ]:
# In[ ]: