Skip to content

Commit 44b226d

Browse files
author
sam bacha
authored
draft v0.0.2
1 parent 291b776 commit 44b226d

File tree

1 file changed

+348
-0
lines changed

1 file changed

+348
-0
lines changed

paper.tex

Lines changed: 348 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,348 @@
1+
\documentclass{article}
2+
3+
\usepackage{arxiv}
4+
5+
6+
\usepackage[utf8]{inputenc} % allow utf-8 input
7+
\usepackage[T1]{fontenc} % use 8-bit T1 fonts
8+
\usepackage{hyperref} % hyperlinks
9+
\usepackage{url} % simple URL typesetting
10+
\usepackage{amsmath}
11+
\usepackage{amsthm}
12+
\usepackage{amssymb}
13+
\usepackage{mathtools, nccmath}
14+
\usepackage{wrapfig}
15+
\usepackage{comment}
16+
\usepackage{booktabs} % professional-quality tables
17+
\usepackage{amsfonts} % blackboard math symbols
18+
\usepackage{nicefrac} % compact symbols for 1/2, etc.
19+
\usepackage{microtype} % microtypography
20+
\usepackage{cleveref} % smart cross-referencing
21+
\usepackage{lipsum} % Can be removed after putting your text content
22+
\usepackage{graphicx}
23+
\usepackage{natbib}
24+
\usepackage{doi}
25+
26+
27+
28+
\usepackage[dvipsnames]{xcolor}
29+
\usepackage{tikz}
30+
\usetikzlibrary{backgrounds}
31+
\usetikzlibrary{arrows,shapes}
32+
\usetikzlibrary{tikzmark}
33+
\usetikzlibrary{calc}
34+
35+
36+
37+
38+
% Here you can change the date presented in the paper title
39+
%\date{September 9, 1985}
40+
% Or remove it
41+
%\date{}
42+
43+
\author{%
44+
\textsc{Alexander Bradley} \\[1ex] % Your name
45+
\normalsize Manifold \\ % Your institution
46+
\normalsize \href{mailto:[email protected]}{[email protected]} % Your email address
47+
%\and % Uncomment if 2 authors are required, duplicate these 4 lines if more
48+
%\textsc{Jane Smith}\thanks{Corresponding author} \\[1ex] % Second author's name
49+
%\normalsize University of Utah \\ % Second author's institution
50+
%\normalsize \href{mailto:[email protected]}{[email protected]} % Second author's email address
51+
}
52+
53+
% Uncomment to override the `A preprint' in the header
54+
\renewcommand{\headeright}{Technical Report}
55+
\renewcommand{\undertitle}{Technical Report}
56+
\renewcommand{\shorttitle}{\textit{Manifold Finance} Preprint}
57+
58+
%%% Add PDF metadata to help others organize their library
59+
%%% Once the PDF is generated, you can check the metadata with
60+
%%% $ pdfinfo template.pdf
61+
\begin{document}
62+
63+
\hypersetup{
64+
pdftitle={procedural approaches towards Maximal Extracted Value for UniswapV02/SushiswapV01},
65+
pdfsubject={q-bio.NC, q-bio.QM},
66+
pdfauthor={Alexander Bradley, Sam Bacha},
67+
pdfkeywords={MEV, Ethereum, Uniswap, Sushiswap, High Frequency Trading, Decentralised Finance},
68+
}
69+
70+
71+
\title{Procedural approaches towards Maximal Extracted Value for UniswapV02/SushiswapV01} % Article title
72+
73+
74+
%----------------------------------------------------------------------------------------
75+
76+
% Print the title
77+
\maketitle
78+
79+
%----------------------------------------------------------------------------------------
80+
% ARTICLE CONTENTS
81+
%----------------------------------------------------------------------------------------
82+
83+
\begin{abstract}
84+
Presented herein, a derivation of optimal arbitrage between 2 Constant Product Automated Market Maker (CPAAM), Decentralised Exchanges (DEXs) for usage in procedural processes that enable value extraction (i.e. MEV).
85+
This math is applied in the new Sushiswap router for at source Miner Extractable Value (MEV) post user swap atomically.
86+
Profits are distributed to liquidity providers, in turn giving users better rates. Extracting MEV at source protects user trades from front-run attacks inherently and helps prevent fee spikes from attackers.
87+
\end{abstract}
88+
89+
\newpage
90+
91+
\section{Introduction}
92+
93+
TODO: Overview paragraph
94+
95+
96+
The following sections describe the derivation of the optimal sizes for post user swap arbitrage between Uniswap V2 style exchanges.
97+
In particular direct (single hop) swaps. Since this subset of opportunities is concise and known, it is favoured for inclusion into the router contract. Clearly there is room for advancement to include tri-arbs and notable exchange protocols such as Curve and Uniswap V3, however these calculations are currently too expensive in gas to include on-chain.
98+
99+
For Sushiswap, this subset is huge as they operate on several chains with Uniswap V2 forks. As an example, on Avalanche there is TraderJoe, Fantom Spookyswap and Polygon Quickswap. This is therefore a cross chain solution.
100+
101+
102+
103+
104+
%------------------------------------------------
105+
106+
\section{Constant Product Automated Market Maker}
107+
Constant Product Automated Market Makers (CPAMMs) are smart contracts for token liquidity pairs. Uniswap V2 was the first to adopt this protocol. Sushiswap, Bancor, Kyber, Honeyswap, PancakeSwap and Quickswap all use the same protocol. They are all governed by the constant product formula given in equation \ref{eqn:constProduct}.
108+
109+
\begin{eqnarray}
110+
k &=& R_{\alpha} R_{\beta} \label{eqn:constProduct}
111+
\end{eqnarray}
112+
113+
Where \(R_{\alpha}\) corresponds to the Reserves of token \(\alpha\), \(R_{\beta}\) to the Reserves of token \(\beta\) within the pair contract and \( k \) the constant invariant.
114+
115+
A swap trading \( \Delta\beta\) tokens for \( \Delta\alpha \) must satisfy equation \ref{eqn:swap}.
116+
117+
\begin{eqnarray}
118+
k &=& (R_{\alpha} - \Delta\alpha) (R_{\beta} + \gamma\Delta\beta ) \label{eqn:swap}\\
119+
\gamma &=& 1 - fee \label{eqn:gamma}
120+
\end{eqnarray}
121+
122+
$$
123+
\begin{aligned}
124+
&k=\left(R_{\alpha}-\Delta \alpha\right)\left(R_{\beta}+\gamma \Delta \beta\right) \\
125+
&\gamma=1-f e e
126+
\end{aligned}
127+
$$
128+
129+
Where the fee on Uniswap V2 and Sushiswap is 0.3\%. For big integer math, equation \ref{eqn:gamma} can be written in the form of equation \ref{eqn:gammaBig}.
130+
131+
\begin{eqnarray}
132+
\gamma &=& \frac{997}{1000} \label{eqn:gammaBig}
133+
\end{eqnarray}
134+
135+
From equations \ref{eqn:constProduct} and \ref{eqn:swap} we can derive an equations for the expected amounts out and in, given in equations \ref{eqn:amountOut} and \ref{eqn:amountIn}.
136+
137+
\begin{eqnarray}
138+
amountOut: \Delta\alpha &=& \frac{997 R_{\alpha} \Delta\beta }{1000 R_{\beta} + 997 \Delta\beta} \label{eqn:amountOut}\\
139+
amountIn: \Delta\beta &=& \frac{1000 R_{\beta} \Delta\alpha }{997 (R_{\alpha} - \Delta\alpha)} \label{eqn:amountIn}
140+
\end{eqnarray}
141+
142+
Post swap, the new liquidity reserves are modified as shown in equations \ref{eqn:reserveA} and \ref{eqn:reserveB}.
143+
144+
\begin{eqnarray}
145+
R_{\alpha}{new} &=& R_{\alpha}{old} - \Delta\alpha \label{eqn:reserveA}\\
146+
R_{\beta}{new} &=& R_{\beta}{old} + \Delta\beta \label{eqn:reserveB}
147+
\end{eqnarray}
148+
149+
Therefore sequential swaps can be simulated off-chain in a deterministic way, given the current liquidity state.
150+
151+
\section{Minimal Procedural DEX Arbitrage}
152+
Establishing a minimal swap for DEX arbitrage consists of a single swap on one DEX followed by the reverse swap on another.
153+
154+
Token amount swap path:
155+
\begin{eqnarray}
156+
DEX0: \: \: \Delta\beta_{0} \Rightarrow \Delta\alpha_{0}\\
157+
DEX1: \: \: \Delta\alpha_{0} \Rightarrow \Delta\beta_{1}
158+
\end{eqnarray}
159+
160+
\section{Optimal simple DEX arbitrage size}
161+
From equation \ref{eqn:amountOut}, the definition of a simple DEX arbitrage for CPAMMs can be written in the form of equations \ref{eqn:dex1} and \ref{eqn:dex2}.
162+
163+
\begin{eqnarray}
164+
\Delta\alpha_{0} &=& \frac{997 R_{\alpha 0} \Delta\beta_{0} }{1000 R_{\beta 0} + 997 \Delta\beta_{0}} \label{eqn:dex1}\\
165+
\Delta\beta_{1} &=& \frac{997 R_{\beta 1} \Delta\alpha_{0} }{1000 R_{\alpha 1} + 997 \Delta\alpha_{0}} \label{eqn:dex2}
166+
\end{eqnarray}
167+
168+
Profit of the arbitrage is simply the amount out of the second trade minus the amount in of the first, shown by equation \ref{eqn:profit}.
169+
170+
\begin{eqnarray}
171+
profit: y &=& \Delta\beta_{1} - \Delta\beta_{0} \label{eqn:profit}
172+
\end{eqnarray}
173+
174+
175+
Substituting equation \ref{eqn:dex1} into equation \ref{eqn:dex2}, we get equation \ref{eqn:suby}.
176+
177+
\begin{eqnarray}
178+
\Delta\beta_{1} &=& \frac{997 R_{\beta 1} \frac{997 R_{\alpha 0} \Delta\beta_{0} }{1000 R_{\beta 0} + 997 \Delta\beta_{0}} }{1000 R_{\alpha 1} + 997 \frac{997 R_{\alpha 0} \Delta\beta_{0} }{1000 R_{\beta 0} + 997 \Delta\beta_{0}}} \label{eqn:suby}\\
179+
&=& \frac{997^2 R_{\beta 1} R_{\alpha 0} \Delta\beta_{0} }{(1000 R_{\beta 0} + 997 \Delta\beta_{0}) 1000 R_{\alpha 1} + 997^2 R_{\alpha 0} \Delta\beta_{0} } \label{eqn:subyMore}
180+
\end{eqnarray}
181+
182+
Since we are looking for the optimal amount In ( \(\Delta\beta_{0}\) ), we can make the following simplifications.
183+
184+
\begin{eqnarray}
185+
let \: x &=& \Delta\beta_{0}\\
186+
let \: C_{A} &=& 997^2 R_{\beta 1} R_{\alpha 0}\\
187+
let \: C_{B} &=& 1000^2 R_{\beta 0} R_{\alpha 1}\\
188+
let \: C_{C} &=& 997000 R_{\alpha 1}\\
189+
let \: C_{D} &=& 997^2 R_{\alpha 0}
190+
\end{eqnarray}
191+
192+
Thus equation \ref{eqn:subyMore} can be reduced to equation \ref{eqn:reduceOut}.
193+
194+
\begin{eqnarray}
195+
\Delta\beta_{1} &=& \frac{C_{A} x}{C_{B} + x(C_{C} + C_{D})} \label{eqn:reduceOut}
196+
\end{eqnarray}
197+
198+
Therefore the profit (y), from equation \ref{eqn:profit} can be expressed in terms of the amount In (x), shown in equation \ref{eqn:profit2}.
199+
200+
\begin{eqnarray}
201+
y &=& \frac{C_{A} x}{C_{B} + x(C_{C} + C_{D})} - x \label{eqn:profit2}\\
202+
&=& \frac{C_{A} x - x(C_{B} + x(C_{C} + C_{D}))}{C_{B} + x(C_{C} + C_{D})} \label{eqn:profit3}\\
203+
&=& \frac{x(C_{A} - C_{B}) - x^2(C_{C} + C_{D})}{C_{B} + x(C_{C} + C_{D})} \label{eqn:profit4}\\
204+
&=& \frac{x C_{F} - x^2 C_{G} }{C_{B} + x C_{G}} \label{eqn:profit5}
205+
\end{eqnarray}
206+
207+
Where:
208+
\begin{eqnarray}
209+
C_{F} &=& C_{A} - C_{B}\\
210+
C_{G} &=& C_{C} + C_{D}
211+
\end{eqnarray}
212+
213+
Maximum profit occurs at a turning point i.e. where the gradient or differential is zero, shown in equation \ref{eqn:turn}.
214+
215+
\begin{eqnarray}
216+
\frac{dy}{dx} = 0 \label{eqn:turn}
217+
\end{eqnarray}
218+
219+
Since we have a complex equation for differentiating, we can use the quotient rule from equation \ref{eqn:quotient}. Numerator and denominator differentials are shown in equations \ref{eqn:fdash} and \ref{eqn:gdash}.
220+
221+
\begin{eqnarray}
222+
\frac{dy}{dx} &=& \frac{d \frac{ f(x)}{g(x)}}{dx} \label{eqn:turn2}\\
223+
f(x) &=& x C_{F} - x^2 C_{G} \label{eqn:f}\\
224+
g(x) &=& C_{B} + x C_{G} \label{eqn:g}\\
225+
\frac{f(x)}{dx} &=& C_{F} - 2 x C_{G} \label{eqn:fdash}\\
226+
\frac{g(x)}{dx} &=& C_{G} \label{eqn:gdash}
227+
\end{eqnarray}
228+
229+
Combining the quotient rule with equation \ref{eqn:turn}, we get equation \ref{eqn:quoSolDex}, which expands to equation \ref{eqn:quoSol2}.
230+
231+
\begin{eqnarray}
232+
f'g &=& g'f \label{eqn:quoSolDex}\\
233+
(C_{F} - 2 x C_{G})(C_{B} + x C_{G}) &=& C_{G} ( x C_{F} - x^2 C_{G}) \label{eqn:quoSol2}
234+
\end{eqnarray}
235+
236+
Equation \ref{eqn:quoSol2} can be re-arranged to form a generic quadratic equation \ref{eqn:quadRefDex} and so the parameters can be defined for the quadratic solution in equation \ref{eqn:sol}.
237+
238+
\begin{eqnarray}
239+
x^2 C_{G}^2 + x(2 C_{B} C_{G}) - C_{B} C_{F} &=& 0 \label{eqn:quadRefDex}
240+
\end{eqnarray}
241+
242+
Solution to the optimal simple DEX arbitrage size for a given swap is shown in equation \ref{eqn:sol}.
243+
244+
\begin{eqnarray}
245+
x^* &=& \frac{-(2 C_{B} C_{G}) \pm \sqrt{(2 C_{B} C_{G})^2 - 4(C_{G}^2)(- C_{B} C_{F})}}{2 C_{G}^2} \label{eqn:sol}
246+
\end{eqnarray}
247+
248+
For positive roots only, this can be reduced to:
249+
250+
\begin{eqnarray}
251+
x^* &=& \frac{- C_{B} + \sqrt{C_{B} ^2 + C_{B} C_{F}}}{C_{G}} \label{eqn:sol2}
252+
\end{eqnarray}
253+
254+
%----------------------------------------------------------------------------------------
255+
256+
\subsection{Tables}
257+
See Trade Size Table~\ref{tab:table}.
258+
259+
This work was done for \verb+booktabs+ (`Manifold Finance'), more information can be found at:
260+
\begin{center}
261+
\url{https://www.manifoldfinance.com}
262+
\end{center}
263+
264+
265+
\begin{equation}
266+
\vspace{\baselineskip}
267+
\label{eq:epsilon}
268+
\pr[\tikzmarknode{x}{\highlight{red}{$\mathcal{X}(\cdot)$}}\in \tikzmarknode{s}{\highlight{blue}{$\mathcal{S}$}}] \leq e^\epsilon \cdot \pr[\mathcal{X}(\cdot)\in \mathcal{S}]
269+
\end{equation}
270+
\begin{tikzpicture}[overlay,remember picture,>=stealth,nodes={align=left,inner ysep=1pt},<-]
271+
% For "X"
272+
\path (x.north) ++ (0,2em) node[anchor=south east,color=red!67] (scalep){\textbf{system state}};
273+
\draw [color=red!87](x.north) |- ([xshift=-0.3ex,color=red]scalep.south west);
274+
% For "S"
275+
\path (s.south) ++ (0,-1.5em) node[anchor=north west,color=blue!67] (mean){\textbf{$\mathcal{S} \subseteq \mathrm{Range}(\mathcal{X})$}};
276+
\draw [color=blue!57](s.south) |- ([xshift=-0.3ex,color=blue]mean.south east);
277+
\end{tikzpicture}
278+
279+
280+
281+
\begin{table}
282+
\caption{Trade Size Efficiency}
283+
\centering
284+
\begin{tabular}{lll}
285+
\toprule
286+
\multicolumn{2}{c}{Part} \\
287+
\cmidrule(r){1-2}
288+
Name & Description & Size ($\mu$m) \\
289+
\midrule
290+
10,000 & ETH & $\sim$100 \\
291+
50,000 & ExactToken & $\sim$10 \\
292+
100,000 & Token & up to $10^6$ \\
293+
\bottomrule
294+
\end{tabular}
295+
\label{tab:table}
296+
\end{table}
297+
298+
\subsection{Analysis}
299+
\begin{itemize}
300+
\item Benchmarking
301+
\item Fuzzing
302+
\item Invariant Testing
303+
\end{itemize}
304+
305+
306+
307+
\bibliographystyle{unsrtnat}
308+
\bibliography{references} %%% Uncomment this line and comment out the ``thebibliography'' section below to use the external .bib file (using bibtex) .
309+
\begin{thebibliography}{1}
310+
\bibitem{Angeris2020}
311+
Guillermo Angeris, Tarun Chitra, Alex Evans, Stephen Boyd
312+
\newblock Guillermo Angeris et al. Optimal Routing for Constant Function Market Makers. arXiv:2204.05238
313+
\newblock In {\em arXiv 1911.03380}, 26 Jul 2021
314+
Optimization and Control (math.OC); Trading and Market Microstructure (q-fin.TR)
315+
316+
317+
\bibitem{Angeris2019}
318+
Guillermo Angeris et al. An analysis of Uniswap markets. 2019. arXiv: 1911.03380
319+
\newblock An analysis of Uniswap markets.
320+
Mathematical Finance (q-fin.MF); Optimization and Control; Trading and Market Microstructure (q-fin.TR)
321+
\newblock {\em arXiv: 1911.03380},
322+
323+
\bibitem{Angeris2019}
324+
Guillermo Angeris, Alex Evans, Tarun Chitra
325+
\newblock Replicating Market Makers
326+
Mathematical Finance (q-fin.MF); Optimization and Control; Trading and Market Microstructure (q-fin.TR)
327+
\newblock {\em arXiv:2103.14769}, 26 Mar 2021.
328+
329+
330+
\bibitem{Tarun2021}
331+
Guillermo Angeris, Alex Evans, Tarun Chitra
332+
\newblock Constant Function Market Makers: Multi-Asset Trades via Convex Optimization
333+
Mathematical Finance (q-fin.MF); Optimization and Control; Trading and Market Microstructure (q-fin.TR)
334+
\newblock {\em arXiv:2107.12484 }, 26 Jul 2021]
335+
https://doi.org/10.48550/arXiv.2107.12484
336+
337+
338+
\bibitem{Tarun2022}
339+
Guillermo Angeris, Tarun Chitra, Alex Evans, Stephen Boyd
340+
\newblock Optimal Routing for Constant Function Market Makers
341+
Optimization and Control (math.OC); Trading and Market Microstructure (q-fin.TR)
342+
\newblock {\em arXiv:2204.05238}, 11 Apr 2022]
343+
https://arxiv.org/abs/2204.05238v1
344+
345+
\end{thebibliography}
346+
347+
348+
\end{document}

0 commit comments

Comments
 (0)