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

Feat/adjust kandel #142

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/healthy-ads-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": patch
---

Add adjust params for kandel
37 changes: 31 additions & 6 deletions src/lib/kandel/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type RawKandelPositionParams = {
midPrice: number
pricePoints: bigint
market: MarketParams
adjust?: boolean | undefined
}

export type PositionKandelParams = {
Expand All @@ -31,22 +32,46 @@ export type PositionKandelParams = {
pricePoints: bigint
}

function getTick(price: number, tickSpacing: bigint, adjust: boolean) {
const lowerTick = tickFromPrice(price, tickSpacing, false)
if (!adjust) return lowerTick
const upperTick = tickFromPrice(price, tickSpacing, true)

if (upperTick === lowerTick) return lowerTick

const lowerPrice = priceFromTick(lowerTick)
const upperPrice = priceFromTick(upperTick)

const lowerDiff = Math.abs(price - lowerPrice)
const upperDiff = Math.abs(price - upperPrice)

return lowerDiff < upperDiff ? lowerTick : upperTick
}

export function getKandelPositionRawParams(
params: RawKandelPositionParams,
): PositionKandelParams {
const { market, pricePoints } = params
const baseQuoteTickIndex0 = tickFromPrice(
humanPriceToRawPrice(params.minPrice, market),
const minPriceRaw = humanPriceToRawPrice(params.minPrice, market)
const midPriceRaw = humanPriceToRawPrice(params.midPrice, market)
const maxPriceRaw = humanPriceToRawPrice(params.maxPrice, market)

const baseQuoteTickIndex0 = getTick(
minPriceRaw,
market.tickSpacing,
params.adjust ?? false,
)
const midTick = tickFromPrice(
humanPriceToRawPrice(params.midPrice, market),
const midTick = getTick(
midPriceRaw,
market.tickSpacing,
params.adjust ?? false,
)
const maxTick = tickFromPrice(
humanPriceToRawPrice(params.maxPrice, market),
const maxTick = getTick(
maxPriceRaw,
market.tickSpacing,
params.adjust ?? false,
)

let baseQuoteTickOffset =
((maxTick - baseQuoteTickIndex0) /
(pricePoints - 1n) /
Expand Down
7 changes: 7 additions & 0 deletions src/lib/tick.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
MAX_SAFE_VOLUME,
inboundFromOutbound,
outboundFromInbound,
tickFromPrice,
tickFromVolumes,
} from './tick.js'

Expand Down Expand Up @@ -44,6 +45,12 @@ describe('ticks', () => {
assertEq(tickFromVolumes(1000000n * 10n ** 18n, 999999n * 10n ** 18n), 0n)
})

test('tickFromPrice', () => {
assertEq(tickFromPrice(1), 0n)
assertEq(tickFromPrice(1.0001), 1n)
assertEq(tickFromPrice(0.9998, 1n, true), -2n)
})

test('outboundFromInbound and inboundFromOutbound', () => {
testPrice(1n, 1n)
testPrice(2n, 1n)
Expand Down
10 changes: 8 additions & 2 deletions src/lib/tick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ export function tickInRange(tick: bigint): boolean {
* Computes the tick value corresponding to the price of the asset.
* @param price the price of the asset
* @param tickSpacing the tick spacing of the market @default 1n
* @param roundUp round up the result @default false
* @returns A Tick instance corresponding to the price of the asset.
*/
export function tickFromPrice(price: number, tickSpacing = 1n): bigint {
const rawTick = BigInt(Math.floor(Math.log(price) / Math.log(1.0001)))
export function tickFromPrice(
price: number,
tickSpacing = 1n,
roundUp = false,
): bigint {
const roundMethod = roundUp ? Math.ceil : Math.floor
const rawTick = BigInt(roundMethod(Math.log(price) / Math.log(1.0001)))
const bin = rawTick / tickSpacing + (rawTick % tickSpacing > 0n ? 1n : 0n)
return bin * tickSpacing
}
Expand Down