Skip to content

Commit

Permalink
Merge pull request #8 from mangrovedao/fix/maxence/orderbook
Browse files Browse the repository at this point in the history
Fix/maxence/orderbook
  • Loading branch information
maxencerb authored May 3, 2024
2 parents f81954f + 96acd97 commit 49a674d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-pants-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": patch
---

Fixed negative tick calculation
28 changes: 28 additions & 0 deletions src/actions/book.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
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'
Expand Down Expand Up @@ -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,
// },
// )
// })
})
41 changes: 40 additions & 1 deletion src/lib/offer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
})
})
2 changes: 1 addition & 1 deletion src/lib/offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 49a674d

Please sign in to comment.