-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.py
262 lines (211 loc) · 11.9 KB
/
app.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
#!/usr/bin/python
import _thread
import time
import math
import json
import requests
from random import randrange
from web3 import Web3
with open('config.json') as json_file:
p = json.load(json_file)
gethIPC = p['gethIPC']
myaddress = p['myaddress']
ethAmount = p['ethAmount']
gasPrice = p['gasPrice']
ethKey = p['ethKey']
my_provider = Web3.IPCProvider(gethIPC)
w3 = Web3(my_provider)
myaddress = myaddress
syms2 = {}
syms = {}
ignore = []
ignore2 = []
# Define a function for the thread
errcount = 0
def print_time2( threadName, token):
while 1:
time.sleep(randrange(thelength) / 4)
try:
r2 = requests.get('https://api.0x.org/swap/v0/quote?sellToken=WETH&buyToken=' + token['address'] + '&sellAmount=100000000000000000').json()
total = 0
time.sleep(0.5 )
r1 = requests.get('https://api.0x.org/swap/v0/quote?sellToken=WETH&buyToken=' + token['address'] + '&sellAmount=100000000000000000').json()
#print(r1['sources'])
sym = token['symbol']
#print(r1)
try:
#print('r1 and r2')
if 'price' in r1:
price2 = (1 / float(r2['price'])) * 1.001215
price = float(r1['price']) * (1.001215)
arbpotential = (price2 / price)
arb = False
if arbpotential > 1:
arb = True
if sym not in syms2:
syms2[sym] = 0
if not arb and sym in ignore2:
ignore2.remove(sym)
print('length ignore ' + str(len(ignore2)))
if arb and syms2[sym] == 0:
ignore2.append(sym)
if arb and syms2[sym] != 0 and sym not in ignore2:
print('arb!')
f1=open('./arbs.json', 'a+')
f1.write(json.dumps({'platform': '0x','symbol': sym, 'arb': -1*((1-(float(price2) / float(price))) * 100) } )+ '\n' )
f1.close()
#print(syms2[sym])
syms2[sym] = syms2[sym] + 1
except Exception as e:
#print('ex 1')
time.sleep(5)
except Exception as e:
time.sleep(randrange(70) + 5)
blacklist = []
def print_time( threadName, token):
while 1:
try:
time.sleep(randrange(thelength))
url = 'https://api.totle.com/swap'
headers = {'content-type': 'application/json',
"Authorization": "Bearer " + 'd09529fa-23b5-456b-996b-d141ce5d4640'
}
payload = {"swap":{
"sourceAsset":"ETH",
"destinationAsset":token['address'],
"sourceAmount":ethAmount * 10 ** 18,
"maxMarketSlippagePercent":"50",
"maxExecutionSlippagePercent":"10"
},
'apiKey': 'd09529fa-23b5-456b-996b-d141ce5d4640',
'partnerContract': '0x0a92bcab3019839ea1a8349fa5c940e38e9c88b9',
'address': myaddress}
if token['symbol'] not in blacklist:
r = requests.post(url, data=json.dumps(payload), headers=headers).json()
if 'success' in r:
if 'response' in r:
if 'summary' in r['response']:
total=float(r['response']['summary'][0]['destinationAmount'])
orders = r['response']['summary'][0]['trades'][0]['orders']
#print(total)
payload2 = {"swap":{
"sourceAsset":token['address'],
"destinationAsset":"ETH",
"sourceAmount": total,
"maxMarketSlippagePercent":"50",
"maxExecutionSlippagePercent":"10"
},
'apiKey': 'd09529fa-23b5-456b-996b-d141ce5d4640',
'partnerContract': '0x0a92bcab3019839ea1a8349fa5c940e38e9c88b9'}
if token['symbol'] not in blacklist:
r2 = requests.post(url, data=json.dumps(payload2), headers=headers).json()
if 'success' not in r2:
if 'Endpoint' in r2['message']:
#print(r2['message'])
time.sleep(randrange(15))
elif r2['success']:
#print(token['symbol'])
if 'response' in r2:
sym = token['symbol']
print(sym)
if sym not in syms:
syms[sym] = 0
os = (r2['response']['summary'][0]['trades'][0]['orders'])
for o in os:
fee2 = float(o['fee']['percentage'])
fee = fee2
tx1price=((float(r['response']['summary'][0]['sourceAmount'])) / 10 ** 18)
# tx1price = tx1price * 1.0025 * (1+fee/100)
tx1price2=(float(r2['response']['summary'][0]['destinationAmount'])) / 10 ** 18
#tx1price2=tx1price2* ((1.0025*(1+fee2/100)))
arbpotential = 100*((float(tx1price2) / float(tx1price)-1) )
print(arbpotential)
arb = False
if arbpotential > 0.05:
print(arbpotential)
print(tx1price2)
print(tx1price)
arb = True
if sym not in syms:
syms[sym] = 0
if not arb and sym in syms:
try:
ignore.remove(sym)
print('length ignore ' + str(len(ignore)))
except Exception as e:
abcc = 1
if arb and syms[sym] == 0:
ignore.append(sym)
print('length ignore ' + str(len(ignore)))
if arb and syms[sym] != 0 and sym not in ignore:
print('arb! ' + sym)
f1=open('./arbs.json', 'a+')
f1.write(json.dumps({'playform': 'totle', 'symbol': sym, 'arb': -1*(1-((float(tx1price) / float(tx1price2)))) * 100} ) + '\n')
f1.close()
tx1 = (r['response']['transactions'])
tx = (r2['response']['transactions'])
for t in tx1:
print(t)
transaction = {
'from': t['tx']['from'],
'to': t['tx']['to'],
'value': t['tx']['value'],
'gas': t['tx']['gas'],
'gasPrice': gasPrice,
'nonce': w3.eth.getTransactionCount(myaddress)+1,
'data': t['tx']['data'],
'chainId': 1
}
key = ethKey
signed = w3.eth.account.sign_transaction(transaction, key)
w3.eth.sendRawTransaction(signed.rawTransaction)
for t in tx:
print(t)
transaction = {
'from': t['tx']['from'],
'to': t['tx']['to'],
'value': t['tx']['value'],
'gas': t['tx']['gas'],
'gasPrice': gasPrice,
'nonce': w3.eth.getTransactionCount(myaddress)+1,
'data': t['tx']['data'],
'chainId': 1
}
key = ethKey
signed = w3.eth.account.sign_transaction(transaction, key)
w3.eth.sendRawTransaction(signed.rawTransaction)
#print(price2 / price * 100)
#print(r2['response']['summary'][0]['trades'][0]['orders'][0]['exchange'])
#print(r2['response']['summary'][0]['trades'][0]['orders'][0]['exchange'])
print(syms[sym])
syms[sym] = syms[sym] + 1
#print(syms[sym])
time.sleep(randrange(15))
else:
print('no response')
else:
if 'is not tradable' in r2['response']['message']:
blacklist.append(token['symbol'])
break
else:
print(r2)
except Exception as e:
xyz = 1
#print(e)
thelength=224
# Create two threads as follows
try:
r = requests.get('https://api.totle.com/tokens').json()
r2 = requests.get('https://api.0x.org/swap/v0/tokens').json()
tokens = []
for token in r['tokens']:
tokens.append(token)
for token in r2['records']:
tokens.append(token)
for token in tokens:
_thread.start_new_thread( print_time, (token['symbol'], token, ) )
#_thread.start_new_thread( print_time2, (token['symbol'], token, ) )
except Exception as e:
print (e)
while 1:
pass