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)