API Reference
CFMMRouter.BasketLiquidation
CFMMRouter.BoundedProduct
CFMMRouter.GeometricMeanTwoCoin
CFMMRouter.LinearNonnegative
CFMMRouter.ProductTwoCoin
CFMMRouter.Router
CFMMRouter.UniV3
CFMMRouter.Swap
CFMMRouter.f
CFMMRouter.find_arb!
CFMMRouter.grad!
CFMMRouter.lower_limit
CFMMRouter.route!
CFMMRouter.upper_limit
CFMMRouter.ϕ
CFMMRouter.∇ϕ!
CFMMRouter.BasketLiquidation
— TypeBasketLiquidation(i, Δin)
Liquidation objective for the routing problem,
\[ \Psi_i - \mathbf{I}(\Psi_{-i} + Δ^\mathrm{in}_{-i} = 0, ~ \Psi_i \geq 0),\]
where i
is the desired output token and Δin
is the basket of tokens to be liquidated.
CFMMRouter.BoundedProduct
— TypeBoundedProduct(k, α, β, R_1, R_2)
Creates a bounded liquidity CFMM with invariant
\[\varphi(R) = (R_1 + \alpha)(R_2 + \beta).\]
For more, see An Efficient Algorithm for Optimal Routing Through Constant Function Market Makers.
CFMMRouter.GeometricMeanTwoCoin
— TypeGeometricMeanTwoCoin(R, w, γ, idx)
Creates a two coin geometric mean CFMM with coins idx[1]
and idx[2]
, reserves R
, fee γ
, and weights w
such that w[1] + w[2] == 1.0
. Specifically, the invariant is
\[\varphi(R) = R_1^{w_1}R_2^{w_2}.\]
CFMMRouter.LinearNonnegative
— TypeLinearNonnegative(c)
Linear objective for the routing problem,
\[ U(\Psi) = c^T\Psi - \mathbf{I}(\Psi \geq 0),\]
where c
is a positive price vector.
CFMMRouter.ProductTwoCoin
— TypeProductTwoCoin(R, γ, idx)
Creates a two coin product CFMM with coins idx[1]
and idx[2]
, reserves R
, and fee γ
. Specifically, the invariant is
\[\varphi(R) = R_1R_2.\]
CFMMRouter.Router
— MethodRouter(objective, cfmms, n_tokens)
Constructs a router that finds a set of trades (router.Δs, router.Λs)
through cfmms
which maximizes objective
. The number of tokens n_tokens
must be specified.
CFMMRouter.UniV3
— TypeUniV3(current_price, lower_ticks, liquidity, γ, Ai)
Creates a two coin Uniswap v3 CFMM. This CFMM is a collection of BoundedProduct pools with disjoint price intervals. Prices refer to the amount of asset 2 per unit of asset 1. The lower_ticks
vector stores the prices in decreasing order (i.e., asset 2 gets more expensive as the index increases). The k+1
st price, where k
is the number of pools, is assumed to be Inf
. The i
th entry of the liquidity
vector stores the invariant of each pool between price p[i]
and p[i+1]
. We use the square of the invariant in the paper, defined as
\[\varphi(R) = (R_1 + \alpha)(R_2 + \beta).\]
As before γ
is the fee rate, and the Ai
vector maps local to global indices.
For more, see An Efficient Algorithm for Optimal Routing Through Constant Function Market Makers.
CFMMRouter.Swap
— MethodSwap(i, j, δ, n)
Swap objective for the routing problem with n
tokens:
\[ \Psi_i - \mathbf{I}(\Psi_{[n]\setminus\{i,j\}} = 0,\; {\Psi_j = -\delta})\]
where i
is the desired output token, j
is the input token, and δ
the amount input. Note that this is shorthand for a BasketLiquidation objective where Δin
is a one-hot vector.
CFMMRouter.f
— Functionf(obj::Objective, v)
Evaluates the conjugate of the utility function of objective
at v
. Specifically,
\[ f(\nu) = \sup_\Psi \left(U(\Psi) - \nu^T \Psi \right).\]
CFMMRouter.find_arb!
— Functionfind_arb!(Δ, Λ, cfmm, v)
Solves the arbitrage problem for cfmm
given price vector v
,
\[\begin{array}{ll} \text{minimize} & \nu^T(\Lambda - \Delta) \\ \text{subject to} & \varphi(R + \gamma\Delta - \Lambda) = \varphi(R) \\ & \Delta, \Lambda \geq 0. \end{array}\]
Overwrites the variables Δ
and Λ
.
CFMMRouter.grad!
— Functiongrad!(g, obj::Objective, v)
Computes the gradient of f(obj, v)
at v.
CFMMRouter.lower_limit
— Functionlower_limit(obj)
Componentwise lower bound on argument v
for objective f
. Returns a vector with length length(v)
(number of tokens).
CFMMRouter.route!
— Methodroute!(r::Router)
Solves the routing problem,
\[\begin{array}{ll} \text{maximize} & U(\Psi) \\ \text{subject to} & \Psi = \sum_{i=1}^m A_i(\Lambda_i - \Delta_i) \\ & \phi_i(R_i + \gamma_i\Delta_i - \Lambda_i) \geq \phi_i(R_i), \quad i = 1, \dots, m \\ &\Delta_i \geq 0, \quad \Lambda_i \geq 0, \quad i = 1, \dots, m. \end{array}\]
Overwrites r.Δs
and r.Λs
.
CFMMRouter.upper_limit
— Functionupper_limit(obj)
Componentwise upper bound on argument v
for objective f
. Returns a vector with length length(v)
(number of tokens).
CFMMRouter.ϕ
— Functionϕ(c::CFMM)
Computes the trading function for CFMM c
.
CFMMRouter.∇ϕ!
— Function∇ϕ!(x, c::CFMM)
Computes the gradient of the trading function for CFMM c
. The result is stored in x
.