-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathascanner_stream.py
207 lines (161 loc) · 5.99 KB
/
ascanner_stream.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
to_gwei = 1_000_000_000
from utils.providers import get_provider_from_uri
from utils import hex_to_dec
from utils.requests import (
get_request_all_pairs,
get_request_balanceof,
get_request_token0,
get_request_token1,
get_request_get_reserves,
)
import json
from const import ADDRESSES, UNISWAPV2_FACTORY_ABI, ERC20_ABI, UNISWAPV2_PAIR_ABI
import pandas as pd
from web3 import Web3
import numpy as np
pd.options.mode.chained_assignment = None
pairs = pd.read_csv('pairs.csv')
# Let us create py-web3 objects for Ethereum node DDOS
PROVIDER_URI = "https://eth.getblock.io/ee60e639-1307-4c20-8d64-f4441ea4b678/mainnet/"
BATCH_W3 = get_provider_from_uri(PROVIDER_URI, batch=True)
W3 = Web3(BATCH_W3)
block_number = W3.eth.block_number
get_reserves_request = json.dumps(
[
get_request_get_reserves(pair_address, block_number, request_id=i)
for i, pair_address in enumerate(pairs.pair_address_uni)
]
)
batch_response = BATCH_W3.make_batch_request(get_reserves_request)
batch_response
reserves_uni = balances = [
(hex_to_dec(response_item["result"][:66]), hex_to_dec(response_item["result"][66:130]))
for response_item in batch_response
]
get_reserves_request = json.dumps(
[
get_request_get_reserves(pair_address, block_number, request_id=i)
for i, pair_address in enumerate(pairs.pair_address_sushi)
]
)
batch_response = BATCH_W3.make_batch_request(get_reserves_request)
batch_response
reserves_sushi = balances = [
(hex_to_dec(response_item["result"][:66]), hex_to_dec(response_item["result"][66:130]))
for response_item in batch_response
]
res0_uni, res1_uni = zip(*reserves_uni)
res0_sushi, res1_sushi = zip(*reserves_sushi)
pairs['res0_uni'] = res0_uni
pairs['res1_uni'] = res1_uni
pairs['res0_sushi'] = res0_sushi
pairs['res1_sushi'] = res1_sushi
pairs['WETH_balance_uni'] = np.where(pairs.token0 == ADDRESSES.weth.lower(), pairs.res0_uni,
np.where(pairs.token1 == ADDRESSES.weth.lower(), pairs.res1_uni, np.nan))
pairs['WETH_balance_sushi'] = np.where(pairs.token0 == ADDRESSES.weth.lower(), pairs.res0_sushi,
np.where(pairs.token1 == ADDRESSES.weth.lower(), pairs.res1_sushi, np.nan))
pairs['other_balance_uni'] = np.where(pairs.token0 == ADDRESSES.weth.lower(), pairs.res1_uni,
np.where(pairs.token1 == ADDRESSES.weth.lower(), pairs.res0_uni, np.nan))
pairs['other_balance_sushi'] = np.where(pairs.token0 == ADDRESSES.weth.lower(), pairs.res1_sushi,
np.where(pairs.token1 == ADDRESSES.weth.lower(), pairs.res0_sushi, np.nan))
def get_param_get_reserves(address):
return {"to": address, "data": '0x313ce567'}
def generate_json_rpc(method, params, request_id=1):
return {
"jsonrpc": "2.0",
"method": method,
"params": params,
"id": request_id,
}
def get_request_get_decimals(token, block_identifier, request_id):
return generate_json_rpc(
method="eth_call",
params=[
get_param_get_reserves(token),
hex(block_identifier),
],
request_id=request_id,
)
get_reserves_decimals = json.dumps(
[
get_request_get_decimals(token, block_number, request_id=i)
for i, token in enumerate(pairs.token)
]
)
batch_response = BATCH_W3.make_batch_request(get_reserves_decimals)
batch_response
decimals = [
int(response_item["result"], 16)
for response_item in batch_response
]
pairs['decimals'] = decimals
calculate_all = pairs[['WETH_balance_uni', 'WETH_balance_sushi', 'decimals' , 'other_balance_uni', 'other_balance_sushi', 'token']]
from utils import arbitrage
calculate_all['arb_cond'] = calculate_all.apply(
lambda x: arbitrage.arbitrage_condition(
x1=int(x['WETH_balance_uni']),
y1=int(x['other_balance_uni']),
x2=int(x['WETH_balance_sushi']),
y2=int(x['other_balance_sushi']),
r1_bps=9970,
r2_bps=9970
),
axis=1
)
calculate_all['arb_act'] = calculate_all.apply(
lambda x: arbitrage.arbitrage_action(
x1=int(x['WETH_balance_uni']),
y1=int(x['other_balance_uni']),
x2=int(x['WETH_balance_sushi']),
y2=int(x['other_balance_sushi']),
r1_bps=9970,
r2_bps=9970
),
axis=1
)
calculate_all['optimal_dx'] = calculate_all.apply(
lambda x: arbitrage.get_optimal_dx(
x1=int(x['WETH_balance_uni']),
y1=int(x['other_balance_uni']),
x2=int(x['WETH_balance_sushi']),
y2=int(x['other_balance_sushi']),
r1_bps=9970,
r2_bps=9970,
dec = int(x['decimals'])
),
axis=1
)
calculate_all['profit_USD'] = calculate_all.apply(
lambda x: arbitrage.get_optimal_profit(
x1=int(x['WETH_balance_uni']),
y1=int(x['other_balance_uni']),
x2=int(x['WETH_balance_sushi']),
y2=int(x['other_balance_sushi']),
r1_bps=9970,
r2_bps=9970,
dec1 = 18,
dec2 = int(x['decimals'])
),
axis=1
)
calculate_all['optimal_dx'] = calculate_all['optimal_dx']/to_gwei
calculate_all['ROE_in_%'] = (100*calculate_all['profit_USD'])/(abs(1820*calculate_all['optimal_dx']))
what_to_buy = calculate_all[(calculate_all['arb_cond'])&(calculate_all['profit_USD']>0)]
what_to_buy = what_to_buy[['token','arb_cond', 'arb_act', 'profit_USD', 'ROE_in_%']]
name= []
for i in list(what_to_buy.token):
token_contract = W3.eth.contract(address=Web3.toChecksumAddress(i), abi=ERC20_ABI)
name += [token_contract.functions.name().call()]
what_to_buy['name'] = name
what_to_buy = what_to_buy[['name', 'token','arb_cond', 'arb_act', 'profit_USD', 'ROE_in_%']]
what_to_buy.to_csv('what_to_buy.csv', index=False)
calculate_all.to_csv('calculate_all.csv', index = False)
data = {
"Overall_arbitrage_USD_profit": [what_to_buy.profit_USD.sum()],
"ROE_in_%": [what_to_buy['ROE_in_%'].median()],
"Number_of_arbitrage_poistions":[what_to_buy.shape[0]]
}
df = pd.DataFrame(data)
stats = pd.read_csv('stats.csv')
stats = pd.concat([stats, df], ignore_index=True)
stats.to_csv('stats.csv', index = False)