-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdex_liquidity.py
250 lines (210 loc) · 11.6 KB
/
dex_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
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
import os
import logging
import pandas as pd
import requests
from datetime import datetime
from web3 import Web3
from web3.exceptions import BadFunctionCallOutput, ContractLogicError
from multicall import Call, Multicall
from google.oauth2 import service_account
import pandas_gbq
abi="""[
{"name":"factory",
"inputs":[],"constant":true, "outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view",
"type":"function"}
,{"name":"getPool",
"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"},{"internalType":"uint24","name":"","type":"uint24"}],"outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view",
"type":"function"}
,{"name":"getPair",
"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","constant":true,
"type":"function"}
,{"name":"balanceOf",
"inputs":[{"name":"","type":"address"}],
"outputs":[{"name":"","type":"uint256"}],"constant":true, "payable":false,"stateMutability":"view",
"type":"function"}
,{"name":"decimals",
"inputs":[],"outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view",
"type":"function"}
]
"""
# The dafault RPC from ethersjs, change it if it doesn't work: https://infura.io/docs
RPC_Endpoint = 'https://mainnet.infura.io/v3/84842078b09946638c03157f83405213'
UniV2Router = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'
UniV3Router = '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45'
UniV3Fee = [100, 500, 3000, 10000]
SushiRouter = '0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F'
CURVE_GITHUB_POOLS = "https://api.github.com/repos/curvefi/curve-contract/contents/contracts/pools"
CURVE_GITHUB_POOLDATA = "https://raw.githubusercontent.com/curvefi/curve-contract/master/contracts/pools/{}/pooldata.json"
token_base = {'WBTC':'0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599',
'USDC':'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
'DAI':'0x6B175474E89094C44Da98b954EedeAC495271d0F',
'WETH':'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
}
token_dict = {'WBTC':'0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599',
'USDC':'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
'DAI':'0x6B175474E89094C44Da98b954EedeAC495271d0F',
'1INCH':'0x111111111117dC0aa78b770fA6A738034120C302',
'AAVE':'0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9',
'COMP':'0xc00e94Cb662C3520282E6f5717214004A7f26888',
'DPI':'0x1494CA1F11D487c2bBe4543E90080AeBa4BA3C2b',
'FEI':'0x956F47F50A910163D8BF957Cf5846D573E7f87CA',
'LINK':'0x514910771AF9Ca656af840dff83E8264EcF986CA',
'SNX':'0xC011a73ee8576Fb46F5E1c5751cA3B9Fe0af2a6F',
'UNI':'0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984',
'YFI':'0x0bc529c00C6401aEF6D220BE8C6Ea1667F6Ad93e',
'LUNA':'0xd2877702675e6cEb975b4A1dFf9fb7BAF4C91ea9',
'FTM':'0x4E15361FD6b4BB609Fa63C81A2be19d873717870',
'SUSHI':'0x6B3595068778DD592e39A122f4f5a5cF09C90fE2',
'CRV':'0xD533a949740bb3306d119CC777fa900bA034cd52',
'CVX':'0x4e3FBD56CD56c3e72c1403e103b45Db9da5B9D2B',
'LDO':'0x5A98FcBEA516Cf06857215779Fd812CA3beF1B32',
'STETH':'0xDFe66B14D37C77F4E9b180cEb433d1b164f0281D',
'FRAX':'0x853d955aCEf822Db058eb8505911ED77F175b99e',
'LUSD':'0x5f98805A4E8be255a32880FDeC7F6728C6568bA0',
'SUSD':'0x57Ab1ec28D129707052df4dF418D58a2D46d5f51',
'GUSD':'0x056Fd409E1d7A124BD7017459dFEa2F387b6d5Cd',
'USDT':'0xdAC17F958D2ee523a2206206994597C13D831ec7',
'FXS' :'0x3432B6A60D23Ca0dFCa7761B7ab56459D9C964D0',
'SPELL':'0x090185f2135308BaD17527004364eBcC2D37e5F6',
'LQTY' :'0x6DEA81C8171D0bA574754EF6F8b412F2Ed88c54D',
'WAVAX':'0x85f138bfEE4ef8e540890CFb48F620571d67Eda3',
'MIM':'0x99D8a9C45b2ecA8864373A26D1459e3Dff1e17F3',
}
token_decimals = {}
curve_pool_data = {}
def get_curve_pools(tokenA, tokenB):
if tokenA == tokenB:
return []
curve_pools = []
if not curve_pool_data:
pool_names = [i["name"] for i in requests.get(CURVE_GITHUB_POOLS).json() if i["type"] == "dir"]
for name in pool_names:
data = requests.get(CURVE_GITHUB_POOLDATA.format(name)).json()
if "swap_address" not in data:
print(f"Cannot add {name} - no deployment address!")
continue
curve_pool_data[name] = data
for pool in curve_pool_data:
for coin in curve_pool_data[pool]['coins']:
if 'underlying_address' in coin and coin['underlying_address'].lower() == tokenA.lower():
for coin in curve_pool_data[pool]['coins']:
if ('underlying_address' in coin and coin['underlying_address'].lower() == tokenB.lower()):
curve_pools.append(curve_pool_data[pool]['swap_address'])
return curve_pools
def get_factory(w3, abi, Router):
UniRouter = Web3.toChecksumAddress(Router)
Factory = w3.eth.contract(address=UniRouter, abi=abi).functions.factory().call()
return Factory
def get_token_decimals (w3, abi, token_address, symbol=None):
token_address = Web3.toChecksumAddress(token_address)
if token_address not in token_decimals:
decimals = w3.eth.contract(address=token_address, abi=abi).functions.decimals().call()
token_decimals[token_address] = decimals
if symbol:
token_decimals[symbol] = decimals
return token_decimals[token_address]
def main(w3):
pools = {}
UniFactory2 = get_factory(w3, abi, UniV2Router)
logging.info(f'UniFactory2: {UniFactory2}')
UniFactory3 = get_factory(w3,abi, UniV3Router)
logging.info(f'UniFactory3: {UniFactory3}')
SushiFactory = get_factory(w3,abi, SushiRouter)
logging.info(f'SushiRouter: {SushiRouter}')
for symbol0 in token_base:
token0 = Web3.toChecksumAddress(token_base[symbol0])
base_decimals = get_token_decimals(w3, abi, token0, symbol0)
logging.info(f'pools count= {len(pools)}..')
logging.debug(f'base = {symbol0}')
for symbol1 in token_dict:
logging.debug(f'token= {symbol1}')
token1 = Web3.toChecksumAddress(token_dict[symbol1])
token_decimals = get_token_decimals(w3, abi, token1, symbol1)
pool_uni2 = w3.eth.contract(address=UniFactory2, abi=abi).functions.getPair(token0,token1).call()
pools[symbol1+'_'+symbol0+'_'+pool_uni2] = \
{'pool' :pool_uni2,
'token' :symbol1,
'token_decimals':token_decimals,
'base_token' :symbol0,
'base_decimals' :base_decimals,
'version' :'Uniswap V2',
'fee' : None,}
pool_sushi = w3.eth.contract(address=SushiFactory, abi=abi).functions.getPair(token0,token1).call()
pools[symbol1+'_'+symbol0+'_'+pool_sushi] = \
{'pool' :pool_sushi,
'token' :symbol1,
'token_decimals':token_decimals,
'base_token' :symbol0,
'base_decimals' :base_decimals,
'version' :'Sushiswap',
'fee' : None,}
multi_getPool = Multicall([
Call(UniFactory3, ['getPool(address,address,uint24)(address)', token0,token1,x], [[x, Web3.toChecksumAddress]]) for x in UniV3Fee
]
,_w3 = w3)
multi_getPool = multi_getPool()
pools.update({ symbol1+'_'+symbol0+'_'+multi_getPool[fee]:
{'pool' :multi_getPool[fee],
'token' :symbol1,
'token_decimals':token_decimals,
'base_token' :symbol0,
'base_decimals' :base_decimals,
'version' :'Uniswap V3',
'fee' : fee/10**6}
for fee in multi_getPool})
curve_pools = get_curve_pools(token1, token0)
pools.update({ symbol1+'_'+symbol0+'_'+x:
{'pool' :x,
'token' :symbol1,
'token_decimals':token_decimals,
'base_token' :symbol0,
'base_decimals' :base_decimals,
'version' :'Curve',
'fee' : None}
for x in curve_pools})
logging.info(f'pools count= {len(pools)}')
for x in list(pools.keys()):
if pools[x]['pool']=='0x0000000000000000000000000000000000000000':
pools.pop(x)
multi_balanceOwn = Multicall([Call(token_dict[pools[x]['token']],
['balanceOf(address)(uint256)', pools[x]['pool']],
[[x, None]]
) for x in pools
]
,_w3 = w3)
multi_balanceOwn = multi_balanceOwn()
multi_balanceBase = Multicall([Call(token_base[pools[x]['base_token']],
['balanceOf(address)(uint256)', pools[x]['pool']],
[[x, None]]
) for x in pools
]
,_w3 = w3)
multi_balanceBase = multi_balanceBase()
{pools[x].update({ 'OwnBalance' : multi_balanceOwn[x]*10**(-pools[x]['token_decimals']),
pools[x]['base_token'] : multi_balanceBase[x]*10**(-pools[x]['base_decimals']),
})
for x in multi_balanceBase}
logging.info(f'pools final count= {len(pools)}')
return pools
if __name__ == '__main__':
logging.basicConfig(
format='%(asctime)s %(funcName)s %(levelname)s %(message)s',
level=os.environ.get('LOGLEVEL', 'INFO').upper(),
datefmt='%Y-%m-%d %H:%M:a%S',
)
w3_eth = Web3(Web3.HTTPProvider(RPC_Endpoint, request_kwargs={'timeout': 30}))
logging.info (f'Ethereum connected: {w3_eth.isConnected()}')
ret = main(w3_eth)
df = pd.DataFrame.from_dict(ret, orient = 'index').reset_index()
df['batchtime'] = datetime.utcnow()
logging.info(df)
credentials = service_account.Credentials.from_service_account_file(
'gearbox-336415-5ed144668529.json',
)
gcp_project_id = 'gearbox-336415'
pandas_gbq.to_gbq(df,
'gearbox.dex_liquidity',
project_id=gcp_project_id,
if_exists = 'append',
progress_bar = False)
logging.info('gearbox.dex_liquidity, insert done')