Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Liquidaiton blows up when input amount is much bigger than reserves #12

Open
fulminmaxi opened this issue Jun 4, 2022 · 1 comment
Open

Comments

@fulminmaxi
Copy link

Hey,
I've noticed that a liquidation tends to blow up when reserves are much smaller than the input amount.
I don't think this would ever happen in real situation, but I thought the bug was interesting regardless.
I've set up a repo with an example here https://github.com/fulminmaxi/CFMMRouterExample/tree/main

An example of liquidations blowing up

Running this code:

#=
# 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, 1e9, 0]

## Build a routing problem with liquidation objective
router = Router(
    BasketLiquidation(1, Δin),
    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])")

Prints:

Input Basket: [0, 1000000000, 0]
Net trade: [1977, -9790664250826, -869499]
Amount recieved: 1977

which I think violates constraints

@tjdiamandis
Copy link
Collaborator

We could check constraints after the solve. Unfortunately, I don't think there's an easy way to get this information from the LBFGSB.jl wrapper (ideally will do a Julia native implementation at some point).

But we should allow LBFGSB options to be passed into the route! method

_, v = optimizer(fn, g!, r.v, bounds, m=m, factr=1e1, pgtol=1e-5, iprint=verbose ? 1 : -1, maxfun=15000, maxiter=15000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants