This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
279 lines (241 loc) · 11.1 KB
/
main.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from web3 import Web3
from polysynth import Polysynth
import json
import csv
import requests
import pandas as pd
import time
from constants import *
# ----------------------------------------------------------------
# SET UP
# ----------------------------------------------------------------
# Read the configuration file
with open('config.json') as config:
CONFIG = json.load(config)
# Get ERC20 ABI contract specification (for transfers of USDC)
abi = json.loads(ERC20_ABI)
# Create a connection to RPC (infura [email protected], Polygon Mainnet)
w3 = Web3(Web3.HTTPProvider(CONFIG['HTTP_PROVIDER']))
print('Connection to HTTP provider established') if w3.isConnected()\
else print('Connection to HTTP provider not established')
quit() if not w3.isConnected() else None
w3.eth.account.enable_unaudited_hdwallet_features()
# USDC contract
usdc_contract_address = Web3.toChecksumAddress(USDC_ADDRESS)
usdc_contract_instance = w3.eth.contract(address=usdc_contract_address, abi=abi)
# Variables
previous_address = ''
previous_privatekey = ''
tx_timeout = 300 # seconds
tr_counter = 0
tr_mxcounter = 10
tr_side = 'Buy'
tr_size = 50
tr_leverage = 1
tr_slippage = 0.01
send_funds = False # Update the flag after the first iteration completed
# Get IDs of profiles which already processed (create the list of strings)
df_processed = pd.read_csv(CONFIG['PROCESSED_IDS'], names=['id'])
# List all AdsPower profiles
list_url = '{}:{}/api/v1/user/list?page_size={}'.format(CONFIG['ADSPOWER_URL'],
CONFIG['ADSPOWER_PORT'],
CONFIG['PAGE_SIZE'],)
resp_list = requests.get(list_url).json()
# Initiate Polysynth object and update it each time
poly_account = Polysynth(address=CONFIG['SAMPLE_ADDRESS'], private_key=CONFIG['SAMPLE_PK'],
provider=CONFIG['HTTP_PROVIDER'], web3=w3,
default_slippage=0.1)
# ----------------------------------------------------------------
# PROCESS EACH PROFILE IN THE LOOP
# ----------------------------------------------------------------
# Start processing each profile
for profile in resp_list['data']['list']:
# Check if the profile has been processed before
if profile['user_id'] in df_processed.values:
print('{} already processed ({})'.format(profile['user_id'], profile['name']))
# Go to next profile in the list
continue
print('{} is being processed ({})'.format(profile['user_id'], profile['name']))
# Get the seed of the current profile and restore the account (public, private key)
with open(CONFIG['FOLDER_KEYS'] + '{}.txt'.format(profile['user_id']), newline='') as f:
r = csv.reader(f)
seed = next(r)[0]
account = w3.eth.account.from_mnemonic(seed)
address = Web3.toChecksumAddress(account.address) # public key (address)
private_key = Web3.toHex(account.privateKey)
# Send funds from the previous wallet if required
if send_funds:
# Send USDC funds from previous wallet to the current one
usdc_balance = usdc_contract_instance.functions.balanceOf(previous_address).call() # Convert to decimal human readable value
if usdc_balance / 1000000 < tr_size * 1.1:
raise Exception('{} has not enough USDC'.format(profile['user_id']))
# Get nonce
nonce = w3.eth.getTransactionCount(previous_address)
# Create a transaction
tx = usdc_contract_instance.functions.transfer(address, usdc_balance).buildTransaction({
'chainId': 137, # Polygon Mainnet
'nonce': nonce,
'gas': 100000,
'maxFeePerGas': w3.toWei(35, 'gwei'),
'maxPriorityFeePerGas': w3.toWei(35, 'gwei')
})
# Sign and send the transaction
signed_tx = w3.eth.account.signTransaction(tx, previous_privatekey)
tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
# Check the transaction status
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash, tx_timeout)
if tx_receipt['status'] == 1:
print('Transaction is successful')
else:
raise Exception('Bad transaction')
# Send all MATIC funds
matic_balance = Web3.fromWei(w3.eth.get_balance(previous_address), 'ether')
if matic_balance < 0.5:
raise Exception('{} has not enough MATIC'.format(profile['user_id']))
# Get nonce
time.sleep(10)
nonce = w3.eth.getTransactionCount(previous_address)
tx = {
'chainId': 137, # Polygon Mainnet
'nonce': nonce,
'to': address,
'value': w3.toWei(matic_balance, 'ether') - 50000 * w3.toWei(35, 'gwei'),
'gas': 50000,
'maxFeePerGas': w3.toWei(35, 'gwei'),
'maxPriorityFeePerGas': w3.toWei(35, 'gwei')
}
# Sign and send the transaction
signed_tx = w3.eth.account.signTransaction(tx, previous_privatekey)
tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
# Check the transaction status
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash, tx_timeout)
if tx_receipt['status'] == 1:
print('Transaction is successful')
else:
raise Exception('Bad transaction')
# ----------------------------------------------------------------
# DEAL WITH POLYSYNTH DEX
# ----------------------------------------------------------------
# Update Polysynth object (workaround)
poly_account.address = address
poly_account.private_key = private_key
poly_account.update_nonce()
# Run open-close position loop
while tr_counter < tr_mxcounter:
# Try-catch for the whole iteration
try:
# If position left opened - close all
print('Check positions')
positions = poly_account.positions()
if positions['data'] != '':
# Consider we have 3 attempts to close all positions, otherwise - throw an exception
for attempt in range(3):
try:
# If we have no position - break
positions = poly_account.positions()
if positions['data'] == '':
break
status_close = poly_account.close_position(CONFIG['MARKET'], tr_slippage)
if status_close['error']['code'] != '' or status_close['status_code'] != 200:
raise Exception('Position not closed. Exception occurred.')
except Exception as e:
# Next attempt
time.sleep(5)
print(e.args)
print('Retry')
continue
else:
break
else:
raise Exception('Position not closed. Exception occurred.')
print('Positions checked')
# Try to open a new position
# Consider we have 3 attempts to open a new position, otherwise - throw an exception
for attempt in range(3):
try:
# If we have an opened position - break
positions = poly_account.positions()
if positions['data'] != '':
break
status_open = poly_account.open_position(CONFIG['MARKET'], tr_side, tr_size, tr_leverage, tr_slippage)
if status_open['error']['code'] != '' or status_open['status_code'] != 200:
raise Exception('Position not opened. Exception occurred.')
except Exception as e:
# Next attempt
time.sleep(5)
print(e.args)
print('Retry')
continue
else:
break
else:
raise Exception('Position not opened. Exception occurred.')
print('Position opened')
# Try to close (the same approach as in the beginning of the loop)
# Consider we have 3 attempts to close all positions, otherwise - throw an exception
for attempt in range(3):
try:
# If we have no position - break
positions = poly_account.positions()
if positions['data'] == '':
break
status_close = poly_account.close_position(CONFIG['MARKET'], tr_slippage)
if status_close['error']['code'] != '' or status_close['status_code'] != 200:
raise Exception('Position not closed. Exception occurred.')
except Exception as e:
# Next attempt
time.sleep(5)
print(e.args)
print('Retry')
continue
else:
break
else:
raise Exception('Position not closed. Exception occurred.')
print('Position closed')
tr_counter += 1
print('Iteration ' + str(tr_counter) + ' finished')
# Try-catch for the whole iteration
except:
print('Retry iteration ' + str(tr_counter))
continue
# ----------------------------------------------------------------
# FINISH
# ----------------------------------------------------------------
# Additional check of balance
time.sleep(10)
matic_balance = Web3.fromWei(w3.eth.get_balance(address), 'ether')
if matic_balance < 0.5:
raise Exception('{} has not enough MATIC'.format(profile['user_id']))
usdc_balance = usdc_contract_instance.functions.balanceOf(address).call() # Convert to decimal human readable value
if usdc_balance / 1000000 < tr_size * 1.1:
for attempt in range(3):
try:
# If we have no position - break
positions = poly_account.positions()
if positions['data'] == '':
break
status_close = poly_account.close_position(CONFIG['MARKET'], tr_slippage)
if status_close['error']['code'] != '' or status_close['status_code'] != 200:
raise Exception('Position not closed (iteration finished). Exception occurred.')
except Exception as e:
# Next attempt
time.sleep(5)
print(e.args)
print('Retry')
continue
else:
break
else:
raise Exception('Position not closed (iteration finished). Exception occurred.')
print('Position closed (iteration finished)')
# Save processed ID to the file
with open(CONFIG['PROCESSED_IDS'], 'a', newline="") as file:
writer = csv.writer(file)
writer.writerow([profile['user_id']])
# Set flag to send funds in the beginning of next iteration, clean Polysynth instance
send_funds = True
previous_address = address
previous_privatekey = private_key
tr_counter = 0
time.sleep(10)