From ccf2f052cd942809b78616162b4161a874f1d357 Mon Sep 17 00:00:00 2001 From: Maxence Raballand Date: Mon, 3 Jun 2024 10:37:33 +0200 Subject: [PATCH] feat: Fix kandel division by 0. (#76) * feat: Fix kandel division by 0. * test: Fix variable name in validateKandelParams function --- .changeset/purple-pants-applaud.md | 5 +++++ src/lib/kandel/params.test.ts | 27 ++++++++++++++++++++++++++- src/lib/kandel/params.ts | 4 ++-- 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 .changeset/purple-pants-applaud.md diff --git a/.changeset/purple-pants-applaud.md b/.changeset/purple-pants-applaud.md new file mode 100644 index 0000000..2f48ef7 --- /dev/null +++ b/.changeset/purple-pants-applaud.md @@ -0,0 +1,5 @@ +--- +"@mangrovedao/mgv": patch +--- + +Fix kandel division by 0 diff --git a/src/lib/kandel/params.test.ts b/src/lib/kandel/params.test.ts index 47a3690..5493946 100644 --- a/src/lib/kandel/params.test.ts +++ b/src/lib/kandel/params.test.ts @@ -1,12 +1,17 @@ import { describe, expect, inject, it } from 'vitest' +import { getBook } from '~mgv/actions/book.js' +import { getClient } from '~test/src/client.js' import { humanPriceToRawPrice, rawPriceToHumanPrice, } from '../human-readable.js' import { priceFromTick, tickFromPrice } from '../tick.js' -import { getKandelPositionRawParams } from './params.js' +import { getKandelPositionRawParams, validateKandelParams } from './params.js' const { wethUSDC } = inject('markets') +const params = inject('mangrove') + +const client = getClient() describe('kandel params', () => { it('kandel position raw params', () => { @@ -34,4 +39,24 @@ describe('kandel params', () => { const endPrice = rawPriceToHumanPrice(priceFromTick(endTick), wethUSDC) expect(endPrice).toApproximateEqual(3500) }) + + it('validateKandelParams', async () => { + const book = await getBook(client, params, wethUSDC) + + const _test = validateKandelParams({ + minPrice: 3100, + midPrice: 3000, + maxPrice: 3500, + pricePoints: 10n, + market: wethUSDC, + baseAmount: 10n, + quoteAmount: 10n, + stepSize: 1n, + gasreq: 250_000n, + factor: 3, + asksLocalConfig: book.asksConfig, + bidsLocalConfig: book.bidsConfig, + marketConfig: book.marketConfig, + }) + }) }) diff --git a/src/lib/kandel/params.ts b/src/lib/kandel/params.ts index cb917e5..4415ab4 100644 --- a/src/lib/kandel/params.ts +++ b/src/lib/kandel/params.ts @@ -153,8 +153,8 @@ export function validateKandelParams( const { nBids, nAsks } = countBidsAndAsks(distribution) // asks gives base and bids gives quote - const askGives = params.baseAmount / nAsks - const bidGives = params.quoteAmount / nBids + const askGives = nAsks > 0 ? params.baseAmount / nAsks : 0n + const bidGives = nBids > 0 ? params.quoteAmount / nBids : 0n distribution = changeGives(distribution, bidGives, askGives)