# API Reference

CFMMRouter.BasketLiquidationType
BasketLiquidation(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.BoundedProductType
BoundedProduct(k, α, β, R_1, R_2)

Creates a bounded liquidity CFMM with invariant

$$$\varphi(R) = (R_1 + \alpha)(R_2 + \beta).$$$

For more, see An Eﬃcient Algorithm for Optimal Routing Through Constant Function Market Makers.

CFMMRouter.GeometricMeanTwoCoinType
GeometricMeanTwoCoin(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.LinearNonnegativeType
LinearNonnegative(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.ProductTwoCoinType
ProductTwoCoin(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.RouterMethod
Router(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.UniV3Type
UniV3(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+1st price, where k is the number of pools, is assumed to be Inf. The ith 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 Eﬃcient Algorithm for Optimal Routing Through Constant Function Market Makers.

CFMMRouter.SwapMethod
Swap(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.fFunction
f(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!Function
find_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.route!Method
route!(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.∇ϕ!Function
∇ϕ!(x, c::CFMM)

Computes the gradient of the trading function for CFMM c. The result is stored in x.