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

Fix BasketLiquidation objective #17

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

prism0x
Copy link

@prism0x prism0x commented Aug 22, 2022

There are certain issues with limits in the BasketLiquidation objective:

  • For some cases, the solver returns netflows where the liquidated amount is greater than the input basket amounts. This might be due to an issue with the upper limit.
  • In the initializer, we should have i <= length(Δin)) instead of i < length(Δin)

It is intended to fix these in this PR.

@prism0x
Copy link
Author

prism0x commented Aug 22, 2022

Here is a MWE to reproduce the issue

#=
# 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([14e3, 15e6], 0.99, [1, 2]),
    ProductTwoCoin([19e3, 11e6], 0.99, [1, 2]),
]

Δin = [0, 3000]

## 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])")

#=
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:")
    println("\tTendered basket:")
    for (ind, δ) in enumerate(Δ)
        if δ > eps()
            print("\t  $(tokens[ind]): $(round(Int, δ)), ")
        end
    end
    println("\n\tRecieved basket:")
    for (ind, λ) in enumerate(Λ)
        if λ > eps()
            print("\t  $(tokens[ind]): $(round(Int, λ)), ")
        end
    end
    print("\n")
end

Without the fix, this computes:

Input Basket: [0, 3000]
Net trade: [32981, -45187377944]
Amount recieved: 32981
CFMM 1:
	Tendered basket:
	  2: 22618645921,
	Recieved basket:
	  1: 13991,
CFMM 2:
	Tendered basket:
	  2: 22568732023,
	Recieved basket:
	  1: 18991,

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

Successfully merging this pull request may close these issues.

1 participant