This repository was archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
140 lines (112 loc) · 4.77 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
import json
from web3 import Web3
import pandas as pd
from time import sleep, strftime, localtime
from inputs import settings
from decouple import config
POOL_FEE = 3000
SWAP_TOKEN_AMOUNT = 1
SWAP_TOKEN_DECIMALS = 6
# LET'S IMAGINE WE HAVE 10 USDC (BLOCKCHAIN FEES ARE NOT CONSIDERED)
SWAP_AMOUNT_IN_WEI = SWAP_TOKEN_AMOUNT * 1000 ** SWAP_TOKEN_DECIMALS
provider_url = f"https://mainnet.infura.io/v3/{config('INFURA_PROJECT_ID')}"
web3 = Web3(Web3.HTTPProvider(provider_url))
def get_max_for_input_token_uniswap_v2(input_amount,
input_token_address,
output_token_address,
output_token_decimals):
try:
amount_in_wei = uniswapv2_router2_contract.functions.getAmountsOut(
input_amount,
[input_token_address,
output_token_address]
).call()[1]
return amount_in_wei / (10 ** output_token_decimals)
except Exception as e:
return str(e)
def get_max_for_input_token_uniswap_v3(input_amount,
input_token_address,
output_token_address,
output_token_decimals):
try:
amount_in_wei = uniswapv3_quoter3_contract.functions.quoteExactInputSingle(
input_token_address,
output_token_address,
POOL_FEE,
input_amount,
0
).call()
return amount_in_wei / (10 ** output_token_decimals)
except Exception as e:
return str(e)
# TODO: add def retrieve decimals of the token
# Sources for prices (DEXes smart contracts)
# Uniswap V2
uniswapv2_router2_contract = web3.eth.contract(
address=settings.uniswapv2_router2_address,
abi=json.loads(settings.uniswapv2_router2_abi)
)
# Uniswap V3
uniswapv3_quoter3_contract = web3.eth.contract(
address=settings.uniswapv3_quoter3_address,
abi=json.loads(settings.uniswapv3_quoter3_abi)
)
# TODO: Sushiswap
# TODO: Balancer
# Import tokens names and addresses
header = ['token_name',
'token_address',
'token_decimals']
df_tokens = pd.read_csv('inputs//tokens_list.csv', names=header)
df_tokens['token_address'] = df_tokens['token_address'].apply(Web3.toChecksumAddress)
while 1:
# BUYING
df_tokens['amount_for_input_token_uni_v2'] = df_tokens.apply(
lambda df: get_max_for_input_token_uniswap_v2(
input_amount=SWAP_AMOUNT_IN_WEI,
input_token_address=settings.usdc_address,
output_token_address=df['token_address'],
output_token_decimals=df['token_decimals']
),
axis=1
)
df_tokens['amount_for_input_token_uni_v3'] = df_tokens.apply(
lambda df: get_max_for_input_token_uniswap_v3(
input_amount=SWAP_AMOUNT_IN_WEI,
input_token_address=settings.usdc_address,
output_token_address=df['token_address'],
output_token_decimals=df['token_decimals']
),
axis=1
)
df_tokens['max_amount_for_input_token'] = df_tokens[['amount_for_input_token_uni_v2',
'amount_for_input_token_uni_v3']].max(axis=1)
df_tokens['max_amount_for_input_token_wei_int'] = (df_tokens['max_amount_for_input_token'] *
10 ** df_tokens['token_decimals']).astype(int)
# SELLING
df_tokens['amount_for_quote_token_uni_v2'] = df_tokens.apply(
lambda df: get_max_for_input_token_uniswap_v2(
input_amount=df['max_amount_for_input_token_wei_int'],
input_token_address=df['token_address'],
output_token_address=settings.usdc_address,
output_token_decimals=SWAP_TOKEN_DECIMALS
),
axis=1
)
df_tokens['amount_for_quote_token_uni_v3'] = df_tokens.apply(
lambda df: get_max_for_input_token_uniswap_v3(
input_amount=df['max_amount_for_input_token_wei_int'],
input_token_address=df['token_address'],
output_token_address=settings.usdc_address,
output_token_decimals=SWAP_TOKEN_DECIMALS
),
axis=1
)
df_tokens['max_amount_for_token'] = df_tokens[['amount_for_quote_token_uni_v2',
'amount_for_quote_token_uni_v3']].max(axis=1)
df_tokens['arbitrage_result'] = round((df_tokens['max_amount_for_token'] - SWAP_TOKEN_AMOUNT) / \
df_tokens['max_amount_for_input_token'], 4)
print(strftime("%d/%m/%Y %H:%M:%S (LOCAL)", localtime()))
print(df_tokens[['token_name', 'arbitrage_result']].to_string(index=False, header=['Token', 'Arb result']))
df_tokens.to_csv('outputs//arbitrage_table.csv')
sleep(60)