diff --git a/common/calculate-swap3.ts b/common/calculate-swap3.ts index 1b902647..ce66ea6a 100644 --- a/common/calculate-swap3.ts +++ b/common/calculate-swap3.ts @@ -105,7 +105,8 @@ export function calculateLPCost( } // TODO: Untested -function addPosition( +// Currently, this mutates the pool. Should it return a new object instead? +export function addPosition( pool: Swap3Pool, minTick: number, maxTick: number, @@ -119,34 +120,42 @@ function addPosition( ) console.log(`Deducting required N: ${requiredN} and required Y: ${requiredY}`) - // Add liquidity as we pass through the larger tick - const maxTickState = pool.tickStates[maxTick] || { - tick: maxTick, - liquidityNet: 0, - liquidityGross: 0, - } - - maxTickState.liquidityNet += deltaL - maxTickState.liquidityGross += deltaL - - // And remove it as we pass through the lower one + // Add liquidity as we pass through the smaller tick const minTickState = pool.tickStates[minTick] || { tick: minTick, liquidityNet: 0, liquidityGross: 0, } - minTickState.liquidityNet -= deltaL - minTickState.liquidityGross -= deltaL + minTickState.liquidityNet += deltaL + minTickState.liquidityGross += deltaL + pool.tickStates[minTick] = minTickState + + // And remove it as we pass through the larger one + const maxTickState = pool.tickStates[maxTick] || { + tick: maxTick, + liquidityNet: 0, + liquidityGross: 0, + } + + maxTickState.liquidityNet -= deltaL + maxTickState.liquidityGross -= deltaL + pool.tickStates[maxTick] = maxTickState // TODO: add deltaL to liquidityGross of tickStates between minTick and maxTick + + return pool +} + +export function sortedTickStates(pool: Swap3Pool) { + return Object.values(pool.tickStates).sort((a, b) => a.tick - b.tick) } function toRatio(tick: number) { return 1.0001 ** tick } -function toProb(tick: number) { +export function toProb(tick: number) { const ratio = toRatio(tick) return ratio / (ratio + 1) } diff --git a/web/pages/swap.tsx b/web/pages/swap.tsx index 32de6055..a188122c 100644 --- a/web/pages/swap.tsx +++ b/web/pages/swap.tsx @@ -1,9 +1,12 @@ import { + addPosition, calculateLPCost, fromProb, getSwap3Probability, noShares, + sortedTickStates, Swap3Pool, + toProb, yesShares, } from 'common/calculate-swap3' import { formatPercent } from 'common/util/format' @@ -60,40 +63,63 @@ function BalanceTable() { function PoolTable(props: { pool: Swap3Pool }) { const { pool } = props return ( - -
- - {pool.liquidity} -
-
- - {pool.tick} -
-
- - {yesShares(pool).toFixed(2)} -
-
- - {noShares(pool).toFixed(2)} -
-
- - {formatPercent(getSwap3Probability(pool))} -
-
+ <> + +
+ + {formatPercent(getSwap3Probability(pool))} +
+ +
+ + {pool.liquidity} +
+
+ + {pool.tick} +
+
+ + {yesShares(pool).toFixed(2)} +
+
+ + {noShares(pool).toFixed(2)} +
+
+ {/* Render each tickState as another row in a table */} + + + + + + + + + {sortedTickStates(pool).map((tickState, i) => ( + + + + + + ))} +
TickProbNet Liquidity
{tickState.tick} + {formatPercent(toProb(tickState.tick))} + {tickState.liquidityNet}
+ ) } function Graph(props: { pool: Swap3Pool }) { - const points = [ - { x: 0, y: 100 }, - { x: 0.2, y: 100 }, - { x: 0.2, y: 200 }, - { x: 0.33, y: 200 }, - { x: 0.33, y: 100 }, - { x: 1, y: 100 }, - ] + const { pool } = props + let liquidity = 100 // TODO unhardcode + const points = [{ x: 0, y: liquidity }] + 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 }) + } + points.push({ x: 1, y: liquidity }) return } @@ -115,7 +141,7 @@ export default function Swap() { ) return ( - + {/* */} @@ -123,17 +149,15 @@ export default function Swap() { className="input" placeholder="Current%" type="number" - onChange={(e) => - setPool((p) => ({ - ...p, - tick: inputPercentToTick(e), - })) - } + onChange={(e) => { + pool.tick = inputPercentToTick(e) + setPool({ ...pool }) + }} /> Alice: Add liquidity - + {/* */} Y required: {requiredY}
N required: {requiredN}
{' '} - +