Implement Balancer math on Uv3

This commit is contained in:
Austin Chen 2022-06-11 09:15:07 -07:00
parent 6ac29106f2
commit 989fa1306b
2 changed files with 41 additions and 4 deletions

View File

@ -162,7 +162,7 @@ export function addPosition(
maxTick,
deltaL
)
console.log(`Deducting required N: ${requiredN} and required Y: ${requiredY}`)
// console.log(`Deducting required N: ${requiredN} and required Y: ${requiredY}`)
// Add liquidity as we pass through the smaller tick
const minTickState = pool.tickStates[minTick] || {
@ -187,6 +187,41 @@ export function addPosition(
return pool
}
export function addBalancer(pool: Swap3Pool, p: number, deltaL: number) {
// TODO: math is borked, shouldn't be returning infinity
function tickL(tick: number) {
const q = 1 - p
return deltaL * 2 * p ** q * q ** p * 1.0001 ** ((p - 0.5) * tick)
}
// See how much liquidity is provided at +/- 5pp around p
// const minTick = fromProb(p - 0.1)
// const maxTick = fromProb(p + 0.05)
const minTick = fromProb(0.000001)
const maxTick = fromProb(0.999999)
let totalN = 0
let totalY = 0
const stride = 300
for (let t = minTick; t <= maxTick; t += stride) {
// console.log('liquidity at tick ', t, toProb(t), tickL(t))
const { requiredN, requiredY } = calculateLPCost(
fromProb(p),
t,
t + stride,
tickL(t)
)
totalN += requiredN
totalY += requiredY
// Add liquidity
addPosition(pool, t, t + stride, tickL(t))
}
console.log('rough number of ticks', (maxTick - minTick) / stride)
console.log(`Total N: ${totalN} and total Y: ${totalY}`)
grossLiquidity(pool)
return pool
}
// This also mutates the pool directly
export function grossLiquidity(pool: Swap3Pool) {
let liquidityGross = 0

View File

@ -1,4 +1,5 @@
import {
addBalancer,
addPosition,
buyYes,
calculateLPCost,
@ -203,8 +204,9 @@ export default function Swap() {
tick: fromProb(0.3),
tickStates: [],
}
INIT_POOL = addPosition(INIT_POOL, -(2 ** 23), 2 ** 20, 100)
INIT_POOL = addPosition(INIT_POOL, fromProb(0.32), fromProb(0.35), 100)
INIT_POOL = addPosition(INIT_POOL, -(2 ** 23), 2 ** 20, 1)
// INIT_POOL = addPosition(INIT_POOL, fromProb(0.32), fromProb(0.35), 100)
INIT_POOL = addBalancer(INIT_POOL, 0.3, 100)
INIT_POOL = grossLiquidity(INIT_POOL)
const [pool, setPool] = useState(INIT_POOL)
@ -215,7 +217,7 @@ export default function Swap() {
return (
<Col className="mx-auto max-w-2xl gap-10 p-4">
{/* <BalanceTable /> */}
<PoolTable pool={pool} />
{/* <PoolTable pool={pool} /> */}
<Graph
pool={pool}
previewMarker={