-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_liquidity.py
68 lines (60 loc) · 2.02 KB
/
write_liquidity.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
import os
from brownie import Contract
from brownie import chain
def main():
os.chdir("data/")
addresses = getEnvVariable("STRATEGY_ADDRESSES")
for address in addresses.split(","):
fetchAndStoreLiquidityForStrategy(address.strip())
def getEnvVariable(key):
try:
return os.environ[key]
except KeyError:
print("ERROR: Please set the environment variable:", key)
exit(1)
def fetchAndStoreLiquidityForStrategy(strategyAddress):
# it would be nice to remove Contract.from_explorer and import MorphoStrategy class but brownie cannot import abstract class
strategy = Contract.from_explorer(strategyAddress)
timestamp = chain.time() # or use chain.height for block number
(
strategyBalanceOnPool,
strategyBalanceInP2P,
strategyTotalBalance,
) = strategy.getStrategySupplyBalance()
(
p2pSupplyAmount,
p2pBorrowAmount,
poolSupplyAmount,
poolBorrowAmount,
) = strategy.getCurrentMarketLiquidity()
maxP2PSupply = strategy.getMaxP2PSupply()
# create row in CSV table
row = "{},{},{},{},{},{},{},{},{}\n".format(
timestamp,
strategyTotalBalance,
strategyBalanceInP2P,
strategyBalanceOnPool,
p2pSupplyAmount,
p2pBorrowAmount,
poolSupplyAmount,
poolBorrowAmount,
maxP2PSupply,
)
fileName = "strategy_" + strategyAddress + ".csv"
print("Writing liquidity data to file:", fileName)
if os.path.isfile(fileName):
# append existing file
dataFile = open(fileName, "a")
dataFile.write(row)
dataFile.close()
else:
# create file
dataFile = open(fileName, "w+")
# add table header
dataFile.write(
"Timestamp,Strategy Total Balance,Strategy Balance in P2P,Strategy Balance"
" On Pool,Market P2P Supply,Market P2P Borrow,Market Pool Supply,Market"
" Pool Borrow,Max P2P Supply\n"
)
dataFile.write(row)
dataFile.close()