forked from Matteus1904/Arbitrage-Scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arbitrage.py
127 lines (99 loc) · 4.3 KB
/
arbitrage.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
from const import UNIT_IN_BPS
from math import sqrt
GAS_USAGE = 350000
to_gwei = 1_000_000_000
from utils.providers import get_provider_from_uri
from web3 import Web3
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)
gas_fee = (GAS_USAGE*W3.eth.gasPrice)//to_gwei
def arbitrage_action(
x1: int, y1: int, x2: int, y2: int, r1_bps: int, r2_bps: int
) -> tuple[bool, str]:
"""
Check if arbitrage condition is satisfied for 2 Uniswapv2 type DEXes
Args:
x1 (int): reserve of x token on the 1st DEX
y1 (int): reserve of y token on the 1st DEX
x2 (int): reserve of x token on the 2nd DEX
y2 (int): reserve of y token on the 2nd DEX
r1_bps (int): 1 - phi1 in basis points, where phi1 is the fee for 1st DEX
r2_bps (int): 1 - phi2 in basis points, where phi2 is the fee for 2nd DEX
Returns:
bool: Returns Action if arbitrage opportunity exists and - otherwise
"""
if x2*y1*r1_bps*r2_bps > 100_000_000*x1*y2:
return 'Sell Eth at Uni'
if x1*y2*r1_bps*r2_bps > 100_000_000*x2*y1:
return 'Sell Eth at Sushi'
return '-'
def arbitrage_condition(
x1: int, y1: int, x2: int, y2: int, r1_bps: int, r2_bps: int
) -> tuple[bool, str]:
"""
Check if arbitrage condition is satisfied for 2 Uniswapv2 type DEXes
Args:
x1 (int): reserve of x token on the 1st DEX
y1 (int): reserve of y token on the 1st DEX
x2 (int): reserve of x token on the 2nd DEX
y2 (int): reserve of y token on the 2nd DEX
r1_bps (int): 1 - phi1 in basis points, where phi1 is the fee for 1st DEX
r2_bps (int): 1 - phi2 in basis points, where phi2 is the fee for 2nd DEX
Returns:
bool: Returns True if arbitrage opportunity exists and False otherwise
"""
if x2*y1*r1_bps*r2_bps > 100_000_000*x1*y2:
return True
if x1*y2*r1_bps*r2_bps > 100_000_000*x2*y1:
return True
return False
def get_optimal_dx(
x1: int, y1: int, x2: int, y2: int, r1_bps: int, r2_bps: int, dec: int
) -> int:
"""
Calculate optimal dx for executing the arbitrage opportunity
Args:
x1 (int): reserve of x token on the 1st DEX
y1 (int): reserve of y token on the 1st DEX
x2 (int): reserve of x token on the 2nd DEX
y2 (int): reserve of y token on the 2nd DEX
r1_bps (int): 1 - phi1 in basis points, where phi1 is the fee for 1st DEX
r2_bps (int): 1 - phi2 in basis points, where phi2 is the fee for 2nd DEX
dec (int): decimals of x token
Returns:
int: Arbitrage optimal dx for swapping dx -> dy -> dx'
"""
num = 10_000*int(sqrt(x1*y1*x2*y2*r1_bps*r2_bps)) - 100_000_000*x1*y2
den = (10_000*r1_bps*y2 + r1_bps*r2_bps*y1) * (10**dec)
return (to_gwei*num) // den
def get_optimal_profit(
x1: int, y1: int, x2: int, y2: int, r1_bps: int, r2_bps: int, dec1: int, dec2: int
) -> int:
"""
Returns maximized profit from executing of the arbitrage opportunity
Args:
x1 (int): reserve of x token on the 1st DEX
y1 (int): reserve of y token on the 1st DEX
x2 (int): reserve of x token on the 2nd DEX
y2 (int): reserve of y token on the 2nd DEX
r1_bps (int): 1 - phi1 in basis points, where phi1 is the fee for 1st DEX
r2_bps (int): 1 - phi2 in basis points, where phi2 is the fee for 2nd DEX
dec1 (int): decimals of x token
dec2 (int): decimals of y token
Returns:
int: Maximized profit from executing the arbitrage opportunity
"""
def calc_profit(x1, y1, x2, y2, r1_bps, r2_bps, dec):
dx = get_optimal_dx(x1, y1, x2, y2, r1_bps, r2_bps, dec)
dy = (to_gwei*y1 * r1_bps * dx) // (to_gwei*10_000 * x1 + r1_bps * dx)
dx_new = (to_gwei*x2 * r2_bps * dy) // (to_gwei*10_000 * y2 + r2_bps * dy)
return (1820*(dx_new - dx - gas_fee)) //to_gwei
arb_cond = arbitrage_condition(x1, y1, x2, y2, r1_bps, r2_bps)
instr = arbitrage_action(x1, y1, x2, y2, r1_bps, r2_bps)
if not arb_cond:
return 0
if instr == 'Sell Eth at Uni':
return calc_profit(x1, y1, x2, y2, r1_bps, r2_bps, dec1)
if instr == 'Sell Eth at Sushi':
return calc_profit(x2, y2, x1, y1, r2_bps, r1_bps, dec2)