The source files for all examples can be found in /examples.

# Liquidating a basket of tokens

This example illustrates how to use CFMMRouter.jl to liquidate a basket of tokens.

using CFMMRouter
using LinearAlgebra

# Create CFMMs
cfmms = [
ProductTwoCoin([1e3, 1e4], 0.997, [1, 2]),
ProductTwoCoin([1e3, 1e2], 0.997, [2, 3]),
ProductTwoCoin([1e3, 2e4], 0.997, [1, 3])
]

# We want to liquidate a basket of tokens 2 & 3 into token 1
Δin = [0, 1e1, 1e2]

# Build a routing problem with liquidation objective
router = Router(
cfmms,
maximum([maximum(cfmm.Ai) for cfmm in cfmms]),
)

# Optimize!
route!(router)

# Print results
Ψ = round.(Int, netflows(router))
println("Input Basket: $(round.(Int, Δin))") println("Net trade:$Ψ")
println("Amount recieved: $(Ψ[1])") Input Basket: [0, 10, 100] Net trade: [60, -10, -100] Amount recieved: 60 We can also see the list of individual trades with each CFMM: # Print individual trades for (i, (Δ, Λ)) in enumerate(zip(router.Δs, router.Λs)) tokens = router.cfmms[i].Ai println("CFMM$i:")
for (ind, δ) in enumerate(Δ)
if δ > eps()
print("\t  $(tokens[ind]):$(round(Int, δ)), ")
end
end
for (ind, λ) in enumerate(Λ)
if λ > eps()
print("\t  $(tokens[ind]):$(round(Int, λ)), ")
end
end
print("\n")
end
CFMM 1:
2: 766,
1: 71,
CFMM 2:
3: 310,
2: 756,
CFMM 3:
1: 11,
3: 210,

## Special Case: Trade Token 1 -> Token 2

Δin = [1e1, 0.0, 0.0]

# Build a routing problem with liquidation objective (output to token 2)
router = Router(
cfmms,
maximum([maximum(cfmm.Ai) for cfmm in cfmms]),
)

# Optimize!
route!(router)

# Print results
Ψ = round.(Int, netflows(router))
println("Input Basket: $(round.(Int, Δin))") println("Net trade:$Ψ")
println("Amount recieved: $(Ψ[1])") Input Basket: [10, 0, 0] Net trade: [-10, 698, 0] Amount recieved: -10 List of individual trades with each CFMM: # Print individual trades for (i, (Δ, Λ)) in enumerate(zip(router.Δs, router.Λs)) tokens = router.cfmms[i].Ai println("CFMM$i:")
for (ind, δ) in enumerate(Δ)
if δ > eps()
print("\t  $(tokens[ind]):$(round(Int, δ)), ")
end
end
for (ind, λ) in enumerate(Λ)
if λ > eps()
print("\t  $(tokens[ind]):$(round(Int, λ)), ")
end
end
print("\n")
end
CFMM 1:
2: 72,
3: 336,