@net-protocol/score 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1805,7 +1805,59 @@ var multi_version_uniswap_pool_info_retriever_default = [
1805
1805
  type: "function",
1806
1806
  name: "V4_POOL_MANAGER",
1807
1807
  inputs: [],
1808
- outputs: [{ name: "", type: "address", internalType: "address" }],
1808
+ outputs: [
1809
+ {
1810
+ name: "",
1811
+ type: "address",
1812
+ internalType: "address"
1813
+ }
1814
+ ],
1815
+ stateMutability: "view"
1816
+ },
1817
+ {
1818
+ type: "function",
1819
+ name: "_getV4Liquidity",
1820
+ inputs: [
1821
+ {
1822
+ name: "poolKey",
1823
+ type: "tuple",
1824
+ internalType: "struct PoolKey",
1825
+ components: [
1826
+ {
1827
+ name: "currency0",
1828
+ type: "address",
1829
+ internalType: "Currency"
1830
+ },
1831
+ {
1832
+ name: "currency1",
1833
+ type: "address",
1834
+ internalType: "Currency"
1835
+ },
1836
+ {
1837
+ name: "fee",
1838
+ type: "uint24",
1839
+ internalType: "uint24"
1840
+ },
1841
+ {
1842
+ name: "tickSpacing",
1843
+ type: "int24",
1844
+ internalType: "int24"
1845
+ },
1846
+ {
1847
+ name: "hooks",
1848
+ type: "address",
1849
+ internalType: "contract IHooks"
1850
+ }
1851
+ ]
1852
+ }
1853
+ ],
1854
+ outputs: [
1855
+ {
1856
+ name: "liquidity",
1857
+ type: "uint128",
1858
+ internalType: "uint128"
1859
+ }
1860
+ ],
1809
1861
  stateMutability: "view"
1810
1862
  },
1811
1863
  {
@@ -1827,8 +1879,16 @@ var multi_version_uniswap_pool_info_retriever_default = [
1827
1879
  type: "address",
1828
1880
  internalType: "Currency"
1829
1881
  },
1830
- { name: "fee", type: "uint24", internalType: "uint24" },
1831
- { name: "tickSpacing", type: "int24", internalType: "int24" },
1882
+ {
1883
+ name: "fee",
1884
+ type: "uint24",
1885
+ internalType: "uint24"
1886
+ },
1887
+ {
1888
+ name: "tickSpacing",
1889
+ type: "int24",
1890
+ internalType: "int24"
1891
+ },
1832
1892
  {
1833
1893
  name: "hooks",
1834
1894
  type: "address",
@@ -1838,7 +1898,11 @@ var multi_version_uniswap_pool_info_retriever_default = [
1838
1898
  }
1839
1899
  ],
1840
1900
  outputs: [
1841
- { name: "sqrtPriceX96", type: "uint160", internalType: "uint160" }
1901
+ {
1902
+ name: "sqrtPriceX96",
1903
+ type: "uint160",
1904
+ internalType: "uint160"
1905
+ }
1842
1906
  ],
1843
1907
  stateMutability: "view"
1844
1908
  },
@@ -1871,8 +1935,16 @@ var multi_version_uniswap_pool_info_retriever_default = [
1871
1935
  type: "address",
1872
1936
  internalType: "Currency"
1873
1937
  },
1874
- { name: "fee", type: "uint24", internalType: "uint24" },
1875
- { name: "tickSpacing", type: "int24", internalType: "int24" },
1938
+ {
1939
+ name: "fee",
1940
+ type: "uint24",
1941
+ internalType: "uint24"
1942
+ },
1943
+ {
1944
+ name: "tickSpacing",
1945
+ type: "int24",
1946
+ internalType: "int24"
1947
+ },
1876
1948
  {
1877
1949
  name: "hooks",
1878
1950
  type: "address",
@@ -1880,7 +1952,11 @@ var multi_version_uniswap_pool_info_retriever_default = [
1880
1952
  }
1881
1953
  ]
1882
1954
  },
1883
- { name: "baseToken", type: "address", internalType: "address" }
1955
+ {
1956
+ name: "baseToken",
1957
+ type: "address",
1958
+ internalType: "address"
1959
+ }
1884
1960
  ],
1885
1961
  outputs: [
1886
1962
  {
@@ -1893,8 +1969,16 @@ var multi_version_uniswap_pool_info_retriever_default = [
1893
1969
  type: "address",
1894
1970
  internalType: "address"
1895
1971
  },
1896
- { name: "token0", type: "address", internalType: "address" },
1897
- { name: "token1", type: "address", internalType: "address" },
1972
+ {
1973
+ name: "token0",
1974
+ type: "address",
1975
+ internalType: "address"
1976
+ },
1977
+ {
1978
+ name: "token1",
1979
+ type: "address",
1980
+ internalType: "address"
1981
+ },
1898
1982
  {
1899
1983
  name: "token0Decimals",
1900
1984
  type: "uint8",
@@ -1924,6 +2008,11 @@ var multi_version_uniswap_pool_info_retriever_default = [
1924
2008
  name: "token1Balance",
1925
2009
  type: "uint256",
1926
2010
  internalType: "uint256"
2011
+ },
2012
+ {
2013
+ name: "liquidity",
2014
+ type: "uint128",
2015
+ internalType: "uint128"
1927
2016
  }
1928
2017
  ]
1929
2018
  }
@@ -2431,7 +2520,7 @@ var MULTI_VERSION_UNISWAP_BULK_POOL_FINDER = {
2431
2520
  abi: multi_version_uniswap_bulk_pool_finder_default
2432
2521
  };
2433
2522
  var MULTI_VERSION_UNISWAP_POOL_INFO_RETRIEVER = {
2434
- address: "0x7A9EF0AC6F6a254cd570B05D62D094D3aa5067f1",
2523
+ address: "0x00000002986Ca76897216a8A3A7Db10FcB0d29Cf",
2435
2524
  abi: multi_version_uniswap_pool_info_retriever_default
2436
2525
  };
2437
2526
  var WETH_BY_CHAIN = {
@@ -3165,8 +3254,24 @@ var V4_HOOKS = [
3165
3254
  "0xbB7784A4d481184283Ed89619A3e3ed143e1Adc0",
3166
3255
  "0xBDF938149ac6a781F94FAa0ed45E6A0e984c6544"
3167
3256
  ];
3168
- var LIQUIDITY_THRESHOLD_TO_PREFER_V2_V3_POOLS = 0.1 * 1e18;
3169
- var LIQUIDITY_THRESHOLD_TO_CONSIDER_V2_V3_POOLS = 0.01 * 1e18;
3257
+ var Q96 = BigInt(2) ** BigInt(96);
3258
+ var ZERO_BI = BigInt(0);
3259
+ function deriveV4WethDepthWei(liquidity, sqrtPriceX96, wethIsCurrency1) {
3260
+ if (sqrtPriceX96 === ZERO_BI || liquidity === ZERO_BI) return ZERO_BI;
3261
+ if (wethIsCurrency1) {
3262
+ return liquidity * sqrtPriceX96 / Q96;
3263
+ }
3264
+ return liquidity * Q96 / sqrtPriceX96;
3265
+ }
3266
+ function deriveV2V3WethDepthWei(info, wethLower) {
3267
+ if ((info.token0 ?? "").toLowerCase() === wethLower) {
3268
+ return BigInt(info.token0Balance ?? 0);
3269
+ }
3270
+ if ((info.token1 ?? "").toLowerCase() === wethLower) {
3271
+ return BigInt(info.token1Balance ?? 0);
3272
+ }
3273
+ return BigInt(info.baseTokenBalance ?? 0);
3274
+ }
3170
3275
  function normalizeTokenPairs(pairs, wethAddress) {
3171
3276
  return pairs.map((pair) => {
3172
3277
  const tokenA = pair.tokenAddress;
@@ -3373,42 +3478,7 @@ function constructPoolKey(info, pair, version, v4PoolKey) {
3373
3478
  return void 0;
3374
3479
  }
3375
3480
  }
3376
- function getWethBalanceWei(pool, wethAddress) {
3377
- if (pool.token0?.toLowerCase() === wethAddress.toLowerCase()) {
3378
- return Number(pool.token0Balance);
3379
- } else if (pool.token1?.toLowerCase() === wethAddress.toLowerCase()) {
3380
- return Number(pool.token1Balance);
3381
- } else {
3382
- return Number(pool.baseTokenBalance);
3383
- }
3384
- }
3385
- function filterV2V3PoolsByLiquidity(pools, threshold, wethAddress) {
3386
- return pools.filter((pool) => {
3387
- if (!pool.poolAddress) return false;
3388
- const wethBalanceWei = getWethBalanceWei(pool, wethAddress);
3389
- return wethBalanceWei >= threshold;
3390
- });
3391
- }
3392
- function selectBestV2V3Pool(pools) {
3393
- return pools.reduce((a, b) => {
3394
- const aIsV3 = a.fee > 0;
3395
- const bIsV3 = b.fee > 0;
3396
- if (aIsV3 && !bIsV3) return a;
3397
- if (!aIsV3 && bIsV3) return b;
3398
- return Number(a.baseTokenBalance) > Number(b.baseTokenBalance) ? a : b;
3399
- });
3400
- }
3401
- function selectBestV4Pool(pools) {
3402
- return pools.reduce((a, b) => {
3403
- return a.fee < b.fee ? a : b;
3404
- });
3405
- }
3406
- function selectBestV2V3PoolByFee(pools) {
3407
- return pools.reduce((a, b) => {
3408
- return a.fee < b.fee ? a : b;
3409
- });
3410
- }
3411
- function selectBestPoolPerPair(allPools, wethAddress) {
3481
+ function selectBestPoolPerPair(allPools) {
3412
3482
  const poolsByPair = {};
3413
3483
  for (const pool of allPools) {
3414
3484
  const key = pool.tokenAddress.toLowerCase() + "_" + pool.baseTokenAddress.toLowerCase();
@@ -3416,37 +3486,13 @@ function selectBestPoolPerPair(allPools, wethAddress) {
3416
3486
  poolsByPair[key].push(pool);
3417
3487
  }
3418
3488
  const bestPools = [];
3419
- for (const [, group] of Object.entries(poolsByPair)) {
3420
- let best;
3421
- if (group.length === 1) {
3422
- best = group[0];
3423
- } else {
3424
- const v2v3PoolsWithPreferredLiquidity = filterV2V3PoolsByLiquidity(
3425
- group,
3426
- LIQUIDITY_THRESHOLD_TO_PREFER_V2_V3_POOLS,
3427
- wethAddress
3428
- );
3429
- if (v2v3PoolsWithPreferredLiquidity.length > 0) {
3430
- best = selectBestV2V3Pool(v2v3PoolsWithPreferredLiquidity);
3431
- } else {
3432
- const v4Pools = group.filter((pool) => !pool.poolAddress);
3433
- if (v4Pools.length > 0) {
3434
- best = selectBestV4Pool(v4Pools);
3435
- } else {
3436
- const v2v3PoolsWithAcceptableLiquidity = filterV2V3PoolsByLiquidity(
3437
- group,
3438
- LIQUIDITY_THRESHOLD_TO_CONSIDER_V2_V3_POOLS,
3439
- wethAddress
3440
- );
3441
- if (v2v3PoolsWithAcceptableLiquidity.length > 0) {
3442
- best = selectBestV2V3PoolByFee(v2v3PoolsWithAcceptableLiquidity);
3443
- }
3444
- }
3445
- }
3446
- }
3447
- if (best) {
3448
- bestPools.push(best);
3449
- }
3489
+ for (const group of Object.values(poolsByPair)) {
3490
+ const withDepth = group.filter((p) => p.wethDepthWei > ZERO_BI);
3491
+ if (withDepth.length === 0) continue;
3492
+ const best = withDepth.reduce(
3493
+ (a, b) => a.wethDepthWei > b.wethDepthWei ? a : b
3494
+ );
3495
+ bestPools.push(best);
3450
3496
  }
3451
3497
  return bestPools;
3452
3498
  }
@@ -3480,13 +3526,27 @@ async function discoverPools({
3480
3526
  ]
3481
3527
  });
3482
3528
  if (!Array.isArray(poolInfos) || poolInfos.length === 0) return [];
3529
+ const wethLower = wethAddress.toLowerCase();
3483
3530
  const allPools = poolInfos.map((info, index) => {
3484
3531
  const poolData = determinePoolVersion(discoveries, index);
3485
3532
  if (!poolData || !poolData.pair) return null;
3533
+ const isV4 = poolData.version === 4;
3534
+ const liquidity = BigInt(info.liquidity ?? 0);
3535
+ let wethDepthWei;
3536
+ if (isV4) {
3537
+ const wethIsCurrency1 = poolData.v4PoolKey.currency1.toLowerCase() === wethLower;
3538
+ wethDepthWei = deriveV4WethDepthWei(
3539
+ liquidity,
3540
+ BigInt(info.sqrtPriceX96 ?? 0),
3541
+ wethIsCurrency1
3542
+ );
3543
+ } else {
3544
+ wethDepthWei = deriveV2V3WethDepthWei(info, wethLower);
3545
+ }
3486
3546
  return {
3487
3547
  tokenAddress: poolData.pair.tokenAddress,
3488
3548
  baseTokenAddress: poolData.pair.baseTokenAddress || wethAddress,
3489
- poolAddress: poolData.version === 4 ? null : info.poolAddress,
3549
+ poolAddress: isV4 ? null : info.poolAddress,
3490
3550
  price: calculatePoolPrice(info, poolData.pair, poolData.version),
3491
3551
  baseTokenBalance: String(info.baseTokenBalance || 0),
3492
3552
  token0: info.token0,
@@ -3499,10 +3559,12 @@ async function discoverPools({
3499
3559
  poolData.pair,
3500
3560
  poolData.version,
3501
3561
  poolData.v4PoolKey
3502
- )
3562
+ ),
3563
+ liquidity: String(liquidity),
3564
+ wethDepthWei
3503
3565
  };
3504
3566
  }).filter((p) => p !== null);
3505
- const bestPools = selectBestPoolPerPair(allPools, wethAddress);
3567
+ const bestPools = selectBestPoolPerPair(allPools);
3506
3568
  return bestPools.map(
3507
3569
  (pool) => ({
3508
3570
  tokenAddress: pool.tokenAddress,
@@ -3511,6 +3573,7 @@ async function discoverPools({
3511
3573
  price: pool.price,
3512
3574
  fee: pool.fee,
3513
3575
  poolKey: pool.poolKey,
3576
+ liquidity: pool.liquidity,
3514
3577
  balances: {
3515
3578
  baseTokenBalance: pool.baseTokenBalance,
3516
3579
  token0Balance: pool.token0Balance,