From abc881976000e481bb40dab3d51da9852dce0a43 Mon Sep 17 00:00:00 2001 From: dyedm1 Date: Wed, 18 Dec 2024 23:19:14 -0500 Subject: [PATCH] fix: jsbi -> native bigint --- package-lock.json | 9 +-------- package.json | 1 - projects/panoptic/index.js | 22 ++++++++++------------ 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/package-lock.json b/package-lock.json index 46d5b02e8e..03029eeead 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,7 +28,6 @@ "graphql-request": "^4.0.0", "hi-base32": "^0.5.1", "js-sha512": "^0.8.0", - "jsbi": "^4.3.0", "limiter": "2.1.0", "miscreant": "^0.3.2", "p-limit": "^3.1.0", @@ -3348,12 +3347,6 @@ "js-yaml": "bin/js-yaml.js" } }, - "node_modules/jsbi": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/jsbi/-/jsbi-4.3.0.tgz", - "integrity": "sha512-SnZNcinB4RIcnEyZqFPdGPVgrg2AcnykiBy0sHVJQKHYeaLUvi3Exj+iaPpLnFVkDPZIV4U0yvgC9/R4uEAZ9g==", - "license": "Apache-2.0" - }, "node_modules/json-buffer": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", @@ -4172,4 +4165,4 @@ } } } -} +} \ No newline at end of file diff --git a/package.json b/package.json index 95141d08a0..16a0dbca56 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "graphql-request": "^4.0.0", "hi-base32": "^0.5.1", "js-sha512": "^0.8.0", - "jsbi": "^4.3.0", "limiter": "2.1.0", "miscreant": "^0.3.2", "p-limit": "^3.1.0", diff --git a/projects/panoptic/index.js b/projects/panoptic/index.js index 7ffbb38907..39cc138363 100644 --- a/projects/panoptic/index.js +++ b/projects/panoptic/index.js @@ -1,7 +1,5 @@ const sdk = require('@defillama/sdk') const { getLogs } = require('../helper/cache/getLogs') -const JSBI = require('jsbi'); - const FACTORY = '0x000000000000010a1DEc6c46371A28A071F8bb01' const SFPM = '0x0000000000000DEdEDdD16227aA3D836C5753194' @@ -119,8 +117,8 @@ function chainTvl(chain) { let extraTokens1 = 0n for (let [key, liquidity] of Object.entries(sfpmOwnedLiquiditiesForMarket)) { - const sqrtLower = BigInt(getSqrtRatioAtTick(Number(key.split("-")[0])).toString()) - const sqrtUpper = BigInt(getSqrtRatioAtTick(Number(key.split("-")[1])).toString()) + const sqrtLower = getSqrtRatioAtTick(Number(key.split("-")[0])) + const sqrtUpper = getSqrtRatioAtTick(Number(key.split("-")[1])) const [amount0, amount1] = getAmountsForLiquidity(BigInt(sqrtPriceX96), liquidity, sqrtLower, sqrtUpper) extraTokens0 += amount0 @@ -206,12 +204,12 @@ function getAmountsForLiquidity( } function getSqrtRatioAtTick(tick) { - const MaxUint256 = JSBI.BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'); - const Q32 = JSBI.BigInt('0x100000000'); - const ZERO = JSBI.BigInt(0); - const ONE = JSBI.BigInt(1); + const MaxUint256 = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'); + const Q32 = BigInt('0x100000000'); + const ZERO = BigInt(0); + const ONE = BigInt(1); var absTick = tick < 0 ? tick * -1 : tick; - var ratio = (absTick & 0x1) !== 0 ? JSBI.BigInt('0xfffcb933bd6fad37aa2d162d1a594001') : JSBI.BigInt('0x100000000000000000000000000000000'); + var ratio = (absTick & 0x1) !== 0 ? BigInt('0xfffcb933bd6fad37aa2d162d1a594001') : BigInt('0x100000000000000000000000000000000'); if ((absTick & 0x2) !== 0) ratio = mulShift(ratio, '0xfff97272373d413259a46990580e213a'); if ((absTick & 0x4) !== 0) ratio = mulShift(ratio, '0xfff2e50f5f656932ef12357cf3c7fdcc'); if ((absTick & 0x8) !== 0) ratio = mulShift(ratio, '0xffe5caca7e10e4e61c3624eaa0941cd0'); @@ -231,13 +229,13 @@ function getSqrtRatioAtTick(tick) { if ((absTick & 0x20000) !== 0) ratio = mulShift(ratio, '0x5d6af8dedb81196699c329225ee604'); if ((absTick & 0x40000) !== 0) ratio = mulShift(ratio, '0x2216e584f5fa1ea926041bedfe98'); if ((absTick & 0x80000) !== 0) ratio = mulShift(ratio, '0x48a170391f7dc42444e8fa2'); - if (tick > 0) ratio = JSBI.divide(MaxUint256, ratio); + if (tick > 0) ratio = MaxUint256 /ratio; // back to Q96 - return JSBI.greaterThan(JSBI.remainder(ratio, Q32), ZERO) ? JSBI.add(JSBI.divide(ratio, Q32), ONE) : JSBI.divide(ratio, Q32); + return ratio % Q32 > ZERO ? ratio / Q32 + ONE : ratio / Q32; } function mulShift(val, mulBy) { - return JSBI.signedRightShift(JSBI.multiply(val, JSBI.BigInt(mulBy)), JSBI.BigInt(128)); + return (val * BigInt(mulBy)) >> BigInt(128); } module.exports = {