From 17ae9d953d6dae51c0ea9af6431b842a9504db0e Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Wed, 8 Jun 2022 10:36:35 -0700 Subject: [PATCH] Calculate gross liquidity --- common/calculate-swap3.ts | 20 +++++++++++--------- web/pages/swap.tsx | 28 +++++++++++++++++++--------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/common/calculate-swap3.ts b/common/calculate-swap3.ts index ce66ea6a..a2f957a8 100644 --- a/common/calculate-swap3.ts +++ b/common/calculate-swap3.ts @@ -78,12 +78,9 @@ export function getSwap3Probability(pool: Swap3Pool) { function calculatePurchase( pool: Swap3Pool, - amount: number, + amount: number, // In M$ outcome: 'YES' | 'NO' -) { - const shares = 10 - const newPool = {} -} +) {} export function calculateLPCost( curTick: number, @@ -104,7 +101,6 @@ export function calculateLPCost( } } -// TODO: Untested // Currently, this mutates the pool. Should it return a new object instead? export function addPosition( pool: Swap3Pool, @@ -128,7 +124,6 @@ export function addPosition( } minTickState.liquidityNet += deltaL - minTickState.liquidityGross += deltaL pool.tickStates[minTick] = minTickState // And remove it as we pass through the larger one @@ -139,11 +134,18 @@ export function addPosition( } maxTickState.liquidityNet -= deltaL - maxTickState.liquidityGross -= deltaL pool.tickStates[maxTick] = maxTickState - // TODO: add deltaL to liquidityGross of tickStates between minTick and maxTick + return pool +} +// This also mutates the pool directly +export function grossLiquidity(pool: Swap3Pool) { + let liquidityGross = 0 + for (const tickState of sortedTickStates(pool)) { + liquidityGross += tickState.liquidityNet + tickState.liquidityGross = liquidityGross + } return pool } diff --git a/web/pages/swap.tsx b/web/pages/swap.tsx index 376a1601..edeb6c48 100644 --- a/web/pages/swap.tsx +++ b/web/pages/swap.tsx @@ -3,6 +3,7 @@ import { calculateLPCost, fromProb, getSwap3Probability, + grossLiquidity, noShares, sortedTickStates, Swap3Pool, @@ -14,6 +15,7 @@ import { useState } from 'react' import { LiquidityGraph } from 'web/components/contract/liquidity-graph' import { Col } from 'web/components/layout/col' import { Row } from 'web/components/layout/row' +import { addLiquidity } from 'web/lib/firebase/fn-call' const users: Record = { alice: { @@ -94,6 +96,7 @@ function PoolTable(props: { pool: Swap3Pool }) { Tick Prob Net Liquidity + Gross Liquidity {sortedTickStates(pool).map((tickState, i) => ( @@ -103,6 +106,7 @@ function PoolTable(props: { pool: Swap3Pool }) { {formatPercent(toProb(tickState.tick))} {tickState.liquidityNet} + {tickState.liquidityGross} ))} @@ -112,23 +116,28 @@ function PoolTable(props: { pool: Swap3Pool }) { function Graph(props: { pool: Swap3Pool }) { const { pool } = props - let liquidity = 100 // TODO unhardcode - const points = [{ x: 0, y: liquidity }] + const points = [] + let lastGross = 0 for (const tickState of sortedTickStates(pool)) { - points.push({ x: toProb(tickState.tick), y: liquidity }) - liquidity += tickState.liquidityNet - points.push({ x: toProb(tickState.tick), y: liquidity }) + const { tick, liquidityGross } = tickState + points.push({ x: toProb(tick), y: lastGross }) + points.push({ x: toProb(tick), y: liquidityGross }) + lastGross = liquidityGross } - points.push({ x: 1, y: liquidity }) return } export default function Swap() { - const [pool, setPool] = useState({ - liquidity: 100, + // Set up an initial pool with 100 liquidity from 0% to 100% + // TODO: Not sure why maxTick of 2**23 breaks it, but 2**20 is okay... + let INIT_POOL: Swap3Pool = { + liquidity: 0, tick: fromProb(0.3), tickStates: [], - }) + } + INIT_POOL = addPosition(INIT_POOL, -(2 ** 23), 2 ** 20, 100) + INIT_POOL = grossLiquidity(INIT_POOL) + const [pool, setPool] = useState(INIT_POOL) const [minTick, setMinTick] = useState(0) const [maxTick, setMaxTick] = useState(0) @@ -180,6 +189,7 @@ export default function Swap() { className="btn" onClick={() => { addPosition(pool, minTick, maxTick, 100) + grossLiquidity(pool) setPool({ ...pool }) }} >