forked from Matteus1904/Arbitrage-Scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_pairs.py
207 lines (169 loc) · 5.9 KB
/
generate_pairs.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
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
# 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)
# Now let us extract pair addresses for the first 100 pairs via 1 batch request
UNISWAPV2_FACTORY_CONTRACT = W3.eth.contract(
address=ADDRESSES.uniswapv2_factory, abi=UNISWAPV2_FACTORY_ABI
)
n_pairs = UNISWAPV2_FACTORY_CONTRACT.functions.allPairsLength().call()
print("TOTAL NUMBER OF PAIRS", n_pairs)
tokens0 = []
tokens1 = []
pair_addresses = []
balances = []
block_number = W3.eth.block_number
x = 23500
factory_address = ADDRESSES.uniswapv2_factory
for i in range(0, n_pairs//x + 1):
pair_ids = range(i*x, min(x*(i+1), n_pairs))
batch_request = json.dumps(
[
get_request_all_pairs(
factory_address, pair_id, block_number, request_id=pair_id
)
for pair_id in pair_ids
]
)
batch_response = BATCH_W3.make_batch_request(batch_request)
pair_address = [
"0x" + response_item["result"][-40:]
for response_item in batch_response
]
# Extracting token0, token1
token0_request = json.dumps(
[
get_request_token0(pair_address, block_number, request_id=i)
for i, pair_address in enumerate(pair_address)
]
)
batch_response = BATCH_W3.make_batch_request(token0_request)
tokens0 += [
"0x" + response_item["result"][-40:]
for response_item in batch_response
]
token1_request = json.dumps(
[
get_request_token1(pair_address, block_number, request_id=i)
for i, pair_address in enumerate(pair_address)
]
)
batch_response = BATCH_W3.make_batch_request(token1_request)
tokens1 += [
"0x" + response_item["result"][-40:]
for response_item in batch_response
]
# Well, let us calculate how many WETH tokens are located in each pair
liquidity_request = json.dumps(
[
get_request_balanceof(ADDRESSES.weth, pair_address, block_number, request_id=i)
for i, pair_address in enumerate(pair_address)
]
)
batch_response = BATCH_W3.make_batch_request(liquidity_request)
balances += [
hex_to_dec(response_item["result"])
for response_item in batch_response
]
pair_addresses += pair_address
uniswap = pd.DataFrame(
{
"pair_address": pair_addresses,
"token0": tokens0,
"token1": tokens1,
"WETH_balance": balances,
}
)
# Now let us extract pair addresses for the first 100 pairs via 1 batch request
UNISWAPV2_FACTORY_CONTRACT = W3.eth.contract(
address=ADDRESSES.sushiswapv2_factory, abi=UNISWAPV2_FACTORY_ABI
)
n_pairs = UNISWAPV2_FACTORY_CONTRACT.functions.allPairsLength().call()
print("TOTAL NUMBER OF PAIRS", n_pairs)
tokens0 = []
tokens1 = []
pair_addresses = []
balances = []
block_number = W3.eth.block_number
x = 23500
factory_address = ADDRESSES.sushiswapv2_factory
for i in range(0, n_pairs//x + 1):
pair_ids = range(i*x, min(x*(i+1), n_pairs))
batch_request = json.dumps(
[
get_request_all_pairs(
factory_address, pair_id, block_number, request_id=pair_id
)
for pair_id in pair_ids
]
)
batch_response = BATCH_W3.make_batch_request(batch_request)
pair_address = [
"0x" + response_item["result"][-40:]
for response_item in batch_response
]
# Extracting token0, token1
token0_request = json.dumps(
[
get_request_token0(pair_address, block_number, request_id=i)
for i, pair_address in enumerate(pair_address)
]
)
batch_response = BATCH_W3.make_batch_request(token0_request)
tokens0 += [
"0x" + response_item["result"][-40:]
for response_item in batch_response
]
token1_request = json.dumps(
[
get_request_token1(pair_address, block_number, request_id=i)
for i, pair_address in enumerate(pair_address)
]
)
batch_response = BATCH_W3.make_batch_request(token1_request)
tokens1 += [
"0x" + response_item["result"][-40:]
for response_item in batch_response
]
# Well, let us calculate how many WETH tokens are located in each pair
liquidity_request = json.dumps(
[
get_request_balanceof(ADDRESSES.weth, pair_address, block_number, request_id=i)
for i, pair_address in enumerate(pair_address)
]
)
batch_response = BATCH_W3.make_batch_request(liquidity_request)
balances += [
hex_to_dec(response_item["result"])
for response_item in batch_response
]
pair_addresses += pair_address
sushiswap = pd.DataFrame(
{
"pair_address": pair_addresses,
"token0": tokens0,
"token1": tokens1,
"WETH_balance": balances,
}
)
sushi = sushiswap[((sushiswap.token0 == ADDRESSES.weth.lower()) | (sushiswap.token1 == ADDRESSES.weth.lower())) & (sushiswap.WETH_balance >= 10**18)]
uni = uniswap[((uniswap.token0 == ADDRESSES.weth.lower()) | (uniswap.token1 == ADDRESSES.weth.lower())) & (uniswap.WETH_balance >= 10**18)]
pairs = pd.merge(uni, sushi, on=['token0', 'token1'], how = 'inner', suffixes = ['_uni', '_sushi'])
import numpy as np
pairs['token'] = np.where(pairs.token0 == ADDRESSES.weth.lower(), pairs.token1,
np.where(pairs.token1 == ADDRESSES.weth.lower(), pairs.token0, np.nan))
pairs = pairs[['token0', 'token1', 'token', 'pair_address_sushi', 'pair_address_uni']]
pairs.to_csv('pairs.csv')