From a0eb0a96930d877ec0b3fdd9e6f5e5b82eb393ad Mon Sep 17 00:00:00 2001 From: Maxence Raballand Date: Fri, 3 May 2024 14:50:16 +0200 Subject: [PATCH 1/2] fix(tick): negative tick calculation --- .changeset/moody-pants-clean.md | 5 ++++ src/actions/book.test.ts | 28 ++++++++++++++++++++++ src/lib/offer.test.ts | 41 ++++++++++++++++++++++++++++++++- src/lib/offer.ts | 2 +- 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 .changeset/moody-pants-clean.md diff --git a/.changeset/moody-pants-clean.md b/.changeset/moody-pants-clean.md new file mode 100644 index 0000000..f25127e --- /dev/null +++ b/.changeset/moody-pants-clean.md @@ -0,0 +1,5 @@ +--- +"@mangrovedao/mgv": patch +--- + +Fixed negative tick calculation diff --git a/src/actions/book.test.ts b/src/actions/book.test.ts index 7167cb4..2c2ffa4 100644 --- a/src/actions/book.test.ts +++ b/src/actions/book.test.ts @@ -1,6 +1,8 @@ import { describe, expect, inject, it } from 'vitest' import { getClient } from '~test/src/client.js' import { getBook } from './book.js' +import { createPublicClient, http } from 'viem' +import { blast } from 'viem/chains' const { WETH, DAI } = inject('tokens') const { mangrove, reader, order } = inject('mangrove') @@ -35,4 +37,30 @@ describe('Getting the book', () => { expect(book.bidsConfig.active).toBeTruthy() expect(book.asksConfig.active).toBeTruthy() }) + + // it('real book', async () => { + // const client = createPublicClient({ + // transport: http(), + // chain: blast + // }) + // const book = await getBook( + // client, + // { + // mgv: "0xb1a49C54192Ea59B233200eA38aB56650Dfb448C", + // mgvOrder: "0x83251E7F36a51c5238C9aa0c6Bb7cc209b32d80e", + // mgvReader: "0x26fD9643Baf1f8A44b752B28f0D90AEBd04AB3F8", + // }, + // { + // base: { + // address: "0x4300000000000000000000000000000000000004", + // decimals: 18, + // }, + // quote: { + // address: "0x4300000000000000000000000000000000000003", + // decimals: 18, + // }, + // tickSpacing: 1n, + // }, + // ) + // }) }) diff --git a/src/lib/offer.test.ts b/src/lib/offer.test.ts index 1a2ee49..b29314a 100644 --- a/src/lib/offer.test.ts +++ b/src/lib/offer.test.ts @@ -4,7 +4,7 @@ import { unpackOfferDetail } from './offer-detail.js' import { unpackOffer } from './offer.js' describe('offer unpacking', () => { - it('should unpack correctly', () => { + it('should unpack correctly (positive tick)', () => { const offersPacked = [ 20074413445151042300888515900011094147352441356857739357192192n, 233176578729722529410070503139477613292134408309508641520409614535884800n, @@ -167,4 +167,43 @@ describe('offer unpacking', () => { expect(resolvedOffers).toEqual(rpcOffers) expect(resolvedOfferDetails).toEqual(rpcOfferDetails) }) + + it('should unpack correctly (negative tick)', () => { + const offersPacked = [ + 6037607402951440078068834159367999131039187881724974465024n, + 6037610396106793331758010605330164445154812137074822479872n, + 6037637334504972614960598542085547985349238119548191768576n, + 6037643320815679122338960834772586440906678945923150643200n, + 6037646313971032376028140869540704815886303201272998658048n, + 290196450876233378097758262348194902868415046125028938377854976n, + 501320208275672184631140271928759749678999361822713279965701379033399296n, + 6037661279747798644474008873479899466573577449747358154752n, + 85437392285878783700956946440343577085766537272144231655276544n, + 501347168245457900980323016316576116850136692486404924633552497240178688n, + ] + + const rpcOffers: RpcOffer[] = [ + { prev: 0n, next: 0n, tick: -80014n, gives: 409000000000000000000n }, + { prev: 0n, next: 0n, tick: -80013n, gives: 407000000000000000000n }, + { prev: 0n, next: 0n, tick: -80004n, gives: 384628507787907540642n }, + { prev: 0n, next: 0n, tick: -80002n, gives: 915000000000000000000n }, + { prev: 0n, next: 0n, tick: -80001n, gives: 1117000000000000000000n }, + { prev: 0n, next: 46230n, tick: -80000n, gives: 887000000000000000000n }, + { prev: 18595n, next: 0n, tick: -80000n, gives: 285151932926349488305n }, + { prev: 0n, next: 0n, tick: -79996n, gives: 298352820166691182804n }, + { prev: 0n, next: 13610n, tick: -79994n, gives: 1927000000000000000000n }, + { + prev: 18596n, + next: 3683n, + tick: -79994n, + gives: 2326000000000000000000n, + }, + ] + + const resolvedOffers: RpcOffer[] = offersPacked.map((offer) => + unpackOffer(offer), + ) + + expect(resolvedOffers).toEqual(rpcOffers) + }) }) diff --git a/src/lib/offer.ts b/src/lib/offer.ts index 848be96..b0c31de 100644 --- a/src/lib/offer.ts +++ b/src/lib/offer.ts @@ -13,7 +13,7 @@ export function unpackOffer(offer: bigint): RpcOffer { let _offer = offer >> unused_bits const gives = _offer & mask(gives_bits) _offer >>= gives_bits - const tick = _offer & mask(tick_bits) + const tick = BigInt.asIntN(Number(tick_bits), _offer & mask(tick_bits)) _offer >>= tick_bits const next = _offer & mask(next_bits) _offer >>= next_bits From 96acd978b3192a2efdd1b05816502df6b7d258a9 Mon Sep 17 00:00:00 2001 From: Maxence Raballand Date: Fri, 3 May 2024 14:50:29 +0200 Subject: [PATCH 2/2] chore: format --- src/actions/book.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/actions/book.test.ts b/src/actions/book.test.ts index 2c2ffa4..0be7701 100644 --- a/src/actions/book.test.ts +++ b/src/actions/book.test.ts @@ -1,8 +1,8 @@ +import { http, createPublicClient } from 'viem' +import { blast } from 'viem/chains' import { describe, expect, inject, it } from 'vitest' import { getClient } from '~test/src/client.js' import { getBook } from './book.js' -import { createPublicClient, http } from 'viem' -import { blast } from 'viem/chains' const { WETH, DAI } = inject('tokens') const { mangrove, reader, order } = inject('mangrove')