Skip to content

Commit

Permalink
feat: Use raw density in minVolume calculation and fix density calcul…
Browse files Browse the repository at this point in the history
…ation in unpackLocalConfig function.
  • Loading branch information
maxencerb committed Jun 14, 2024
1 parent 9e20167 commit 2e2366a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-ads-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": patch
---

Fix density by using the same algorithm as from the chain
17 changes: 16 additions & 1 deletion src/lib/density.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ export function formatDensity(density: number): bigint {
return BigInt((exponent << 2) | mantissa)
}

const SUBNORMAL_LIMIT = 0b111n

export function rawDensityTo96X32(rawDensity: bigint): bigint {
if (rawDensity <= SUBNORMAL_LIMIT) {
return rawDensity & 0b11n
}
const shift = (rawDensity >> 2n) - 2n
return ((rawDensity & 0b11n) | 0b100n) << shift
}

export function multiplyUpRawDensity(rawDensity: bigint, m: bigint): bigint {
const part = m * rawDensityTo96X32(rawDensity)
return (part >> 32n) + (part % (2n << 32n) === 0n ? 0n : 1n)
}

export function multiplyDensity(density: number, b: bigint, up = true): bigint {
const roundMethod = up ? Math.ceil : Math.round
return BigInt(roundMethod(Number(b) * density))
Expand All @@ -60,5 +75,5 @@ export function multiplyDensity(density: number, b: bigint, up = true): bigint {
* @returns the minimum volume required to execute the offer.
*/
export function minVolume(config: LocalConfig, gasreq: bigint): bigint {
return multiplyDensity(config.density, gasreq + config.offer_gasbase)
return multiplyUpRawDensity(config.rawDensity, gasreq + config.offer_gasbase)
}
1 change: 1 addition & 0 deletions src/lib/local.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('local', () => {
level1: 48n,
root: 2n,
offer_gasbase: 300_000n,
rawDensity: 273n,
lock: false,
last: 8698n,
}
Expand Down
4 changes: 3 additions & 1 deletion src/lib/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export function unpackLocalConfig(_config: bigint): LocalConfig {
config >>= level3_bits
const binPosInLeaf = config & mask(binPosInLeaf_bits)
config >>= binPosInLeaf_bits
const density = parseDensity(config & mask(density_bits))
const rawDensity = config & mask(density_bits)
const density = parseDensity(rawDensity)
config >>= density_bits
const fee = config & mask(fee_bits)
config >>= fee_bits
Expand All @@ -56,6 +57,7 @@ export function unpackLocalConfig(_config: bigint): LocalConfig {
active: active === 1n,
fee,
density,
rawDensity,
binPosInLeaf,
level3,
level2,
Expand Down
1 change: 1 addition & 0 deletions src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type LocalConfig = {
active: boolean
fee: bigint
density: number
rawDensity: bigint
binPosInLeaf: bigint
level3: bigint
level2: bigint
Expand Down

0 comments on commit 2e2366a

Please sign in to comment.