Calculate gross liquidity

This commit is contained in:
Austin Chen 2022-06-08 10:36:35 -07:00
parent 853c45d564
commit 17ae9d953d
2 changed files with 30 additions and 18 deletions

View File

@ -78,12 +78,9 @@ export function getSwap3Probability(pool: Swap3Pool) {
function calculatePurchase( function calculatePurchase(
pool: Swap3Pool, pool: Swap3Pool,
amount: number, amount: number, // In M$
outcome: 'YES' | 'NO' outcome: 'YES' | 'NO'
) { ) {}
const shares = 10
const newPool = {}
}
export function calculateLPCost( export function calculateLPCost(
curTick: number, curTick: number,
@ -104,7 +101,6 @@ export function calculateLPCost(
} }
} }
// TODO: Untested
// Currently, this mutates the pool. Should it return a new object instead? // Currently, this mutates the pool. Should it return a new object instead?
export function addPosition( export function addPosition(
pool: Swap3Pool, pool: Swap3Pool,
@ -128,7 +124,6 @@ export function addPosition(
} }
minTickState.liquidityNet += deltaL minTickState.liquidityNet += deltaL
minTickState.liquidityGross += deltaL
pool.tickStates[minTick] = minTickState pool.tickStates[minTick] = minTickState
// And remove it as we pass through the larger one // And remove it as we pass through the larger one
@ -139,11 +134,18 @@ export function addPosition(
} }
maxTickState.liquidityNet -= deltaL maxTickState.liquidityNet -= deltaL
maxTickState.liquidityGross -= deltaL
pool.tickStates[maxTick] = maxTickState 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 return pool
} }

View File

@ -3,6 +3,7 @@ import {
calculateLPCost, calculateLPCost,
fromProb, fromProb,
getSwap3Probability, getSwap3Probability,
grossLiquidity,
noShares, noShares,
sortedTickStates, sortedTickStates,
Swap3Pool, Swap3Pool,
@ -14,6 +15,7 @@ import { useState } from 'react'
import { LiquidityGraph } from 'web/components/contract/liquidity-graph' import { LiquidityGraph } from 'web/components/contract/liquidity-graph'
import { Col } from 'web/components/layout/col' import { Col } from 'web/components/layout/col'
import { Row } from 'web/components/layout/row' import { Row } from 'web/components/layout/row'
import { addLiquidity } from 'web/lib/firebase/fn-call'
const users: Record<string, any> = { const users: Record<string, any> = {
alice: { alice: {
@ -94,6 +96,7 @@ function PoolTable(props: { pool: Swap3Pool }) {
<th className="px-4 py-2">Tick</th> <th className="px-4 py-2">Tick</th>
<th className="px-4 py-2">Prob</th> <th className="px-4 py-2">Prob</th>
<th className="px-4 py-2">Net Liquidity</th> <th className="px-4 py-2">Net Liquidity</th>
<th className="px-4 py-2">Gross Liquidity</th>
</tr> </tr>
</thead> </thead>
{sortedTickStates(pool).map((tickState, i) => ( {sortedTickStates(pool).map((tickState, i) => (
@ -103,6 +106,7 @@ function PoolTable(props: { pool: Swap3Pool }) {
{formatPercent(toProb(tickState.tick))} {formatPercent(toProb(tickState.tick))}
</td> </td>
<td className="px-4 py-2">{tickState.liquidityNet}</td> <td className="px-4 py-2">{tickState.liquidityNet}</td>
<td className="px-4 py-2">{tickState.liquidityGross}</td>
</tr> </tr>
))} ))}
</table> </table>
@ -112,23 +116,28 @@ function PoolTable(props: { pool: Swap3Pool }) {
function Graph(props: { pool: Swap3Pool }) { function Graph(props: { pool: Swap3Pool }) {
const { pool } = props const { pool } = props
let liquidity = 100 // TODO unhardcode const points = []
const points = [{ x: 0, y: liquidity }] let lastGross = 0
for (const tickState of sortedTickStates(pool)) { for (const tickState of sortedTickStates(pool)) {
points.push({ x: toProb(tickState.tick), y: liquidity }) const { tick, liquidityGross } = tickState
liquidity += tickState.liquidityNet points.push({ x: toProb(tick), y: lastGross })
points.push({ x: toProb(tickState.tick), y: liquidity }) points.push({ x: toProb(tick), y: liquidityGross })
lastGross = liquidityGross
} }
points.push({ x: 1, y: liquidity })
return <LiquidityGraph points={points} marker={toProb(pool.tick)} /> return <LiquidityGraph points={points} marker={toProb(pool.tick)} />
} }
export default function Swap() { export default function Swap() {
const [pool, setPool] = useState({ // Set up an initial pool with 100 liquidity from 0% to 100%
liquidity: 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), tick: fromProb(0.3),
tickStates: [], 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 [minTick, setMinTick] = useState(0)
const [maxTick, setMaxTick] = useState(0) const [maxTick, setMaxTick] = useState(0)
@ -180,6 +189,7 @@ export default function Swap() {
className="btn" className="btn"
onClick={() => { onClick={() => {
addPosition(pool, minTick, maxTick, 100) addPosition(pool, minTick, maxTick, 100)
grossLiquidity(pool)
setPool({ ...pool }) setPool({ ...pool })
}} }}
> >