@net-protocol/score 0.1.2 → 0.1.3

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.d.mts CHANGED
@@ -164,20 +164,24 @@ declare function calculatePriceFromSqrtPriceX96(sqrtPriceX96: bigint, token0Deci
164
164
  * Discover the best Uniswap pools for multiple token pairs.
165
165
  * Makes two on-chain calls: pool discovery + pool info retrieval.
166
166
  */
167
- declare function discoverPools({ publicClient, pairs, }: {
167
+ declare function discoverPools({ publicClient, pairs, chainId, }: {
168
168
  publicClient: PublicClient;
169
169
  pairs: {
170
170
  tokenAddress: string;
171
171
  baseTokenAddress?: string;
172
172
  }[];
173
+ /** Chain ID used to resolve the WETH address. Defaults to Base (8453). */
174
+ chainId?: number;
173
175
  }): Promise<PoolDiscoveryResult[]>;
174
176
  /**
175
177
  * Discover the best WETH pool for a single token.
176
178
  * Convenience wrapper around discoverPools.
177
179
  */
178
- declare function discoverTokenPool({ publicClient, tokenAddress, }: {
180
+ declare function discoverTokenPool({ publicClient, tokenAddress, chainId, }: {
179
181
  publicClient: PublicClient;
180
182
  tokenAddress: string;
183
+ /** Chain ID used to resolve the WETH address. Defaults to Base (8453). */
184
+ chainId?: number;
181
185
  }): Promise<PoolDiscoveryResult | null>;
182
186
 
183
187
  declare const SCORE_CONTRACT: {
@@ -216,8 +220,11 @@ declare const MULTI_VERSION_UNISWAP_POOL_INFO_RETRIEVER: {
216
220
  readonly address: Address;
217
221
  readonly abi: Abi;
218
222
  };
219
- declare const WETH_ADDRESS: Address;
223
+ /**
224
+ * Get the WETH address for a given chain.
225
+ */
226
+ declare function getWethAddress(chainId: number): Address;
220
227
  declare const NULL_ADDRESS: Address;
221
228
  declare const UPVOTE_PRICE_ETH = 0.000025;
222
229
 
223
- export { ALL_STRATEGY_ADDRESSES, DYNAMIC_SPLIT_STRATEGY, DecodedStrategyMetadata, GetAppKeyScoresOptions, GetStrategyKeyScoresOptions, GetUpvotesForItemsOptions, GetUpvotesOptions, LEGACY_UPVOTE_V1_ADDRESS, LEGACY_UPVOTE_V2_ADDRESS, MULTI_VERSION_UNISWAP_BULK_POOL_FINDER, MULTI_VERSION_UNISWAP_POOL_INFO_RETRIEVER, NULL_ADDRESS, PURE_ALPHA_STRATEGY, PoolDiscoveryResult, PoolKey, SCORE_CONTRACT, SUPPORTED_SCORE_CHAINS, ScoreClient, ScoreClientOptions, UNIV234_POOLS_STRATEGY, UPVOTE_APP, UPVOTE_PRICE_ETH, UPVOTE_STORAGE_APP, WETH_ADDRESS, calculatePriceFromSqrtPriceX96, decodeStrategyMetadata, decodeUpvoteMessage, decodeUpvoteStorageBlob, discoverPools, discoverTokenPool, encodePoolKey, encodeUpvoteKey, extractStrategyAddress, isDynamicSplitStrategy, isPureAlphaStrategy, isStrategyMessage, isUniv234PoolsStrategy, isUserUpvoteMessage, selectStrategy, tokenAddressToUpvoteKeyString };
230
+ export { ALL_STRATEGY_ADDRESSES, DYNAMIC_SPLIT_STRATEGY, DecodedStrategyMetadata, GetAppKeyScoresOptions, GetStrategyKeyScoresOptions, GetUpvotesForItemsOptions, GetUpvotesOptions, LEGACY_UPVOTE_V1_ADDRESS, LEGACY_UPVOTE_V2_ADDRESS, MULTI_VERSION_UNISWAP_BULK_POOL_FINDER, MULTI_VERSION_UNISWAP_POOL_INFO_RETRIEVER, NULL_ADDRESS, PURE_ALPHA_STRATEGY, PoolDiscoveryResult, PoolKey, SCORE_CONTRACT, SUPPORTED_SCORE_CHAINS, ScoreClient, ScoreClientOptions, UNIV234_POOLS_STRATEGY, UPVOTE_APP, UPVOTE_PRICE_ETH, UPVOTE_STORAGE_APP, calculatePriceFromSqrtPriceX96, decodeStrategyMetadata, decodeUpvoteMessage, decodeUpvoteStorageBlob, discoverPools, discoverTokenPool, encodePoolKey, encodeUpvoteKey, extractStrategyAddress, getWethAddress, isDynamicSplitStrategy, isPureAlphaStrategy, isStrategyMessage, isUniv234PoolsStrategy, isUserUpvoteMessage, selectStrategy, tokenAddressToUpvoteKeyString };
package/dist/index.d.ts CHANGED
@@ -164,20 +164,24 @@ declare function calculatePriceFromSqrtPriceX96(sqrtPriceX96: bigint, token0Deci
164
164
  * Discover the best Uniswap pools for multiple token pairs.
165
165
  * Makes two on-chain calls: pool discovery + pool info retrieval.
166
166
  */
167
- declare function discoverPools({ publicClient, pairs, }: {
167
+ declare function discoverPools({ publicClient, pairs, chainId, }: {
168
168
  publicClient: PublicClient;
169
169
  pairs: {
170
170
  tokenAddress: string;
171
171
  baseTokenAddress?: string;
172
172
  }[];
173
+ /** Chain ID used to resolve the WETH address. Defaults to Base (8453). */
174
+ chainId?: number;
173
175
  }): Promise<PoolDiscoveryResult[]>;
174
176
  /**
175
177
  * Discover the best WETH pool for a single token.
176
178
  * Convenience wrapper around discoverPools.
177
179
  */
178
- declare function discoverTokenPool({ publicClient, tokenAddress, }: {
180
+ declare function discoverTokenPool({ publicClient, tokenAddress, chainId, }: {
179
181
  publicClient: PublicClient;
180
182
  tokenAddress: string;
183
+ /** Chain ID used to resolve the WETH address. Defaults to Base (8453). */
184
+ chainId?: number;
181
185
  }): Promise<PoolDiscoveryResult | null>;
182
186
 
183
187
  declare const SCORE_CONTRACT: {
@@ -216,8 +220,11 @@ declare const MULTI_VERSION_UNISWAP_POOL_INFO_RETRIEVER: {
216
220
  readonly address: Address;
217
221
  readonly abi: Abi;
218
222
  };
219
- declare const WETH_ADDRESS: Address;
223
+ /**
224
+ * Get the WETH address for a given chain.
225
+ */
226
+ declare function getWethAddress(chainId: number): Address;
220
227
  declare const NULL_ADDRESS: Address;
221
228
  declare const UPVOTE_PRICE_ETH = 0.000025;
222
229
 
223
- export { ALL_STRATEGY_ADDRESSES, DYNAMIC_SPLIT_STRATEGY, DecodedStrategyMetadata, GetAppKeyScoresOptions, GetStrategyKeyScoresOptions, GetUpvotesForItemsOptions, GetUpvotesOptions, LEGACY_UPVOTE_V1_ADDRESS, LEGACY_UPVOTE_V2_ADDRESS, MULTI_VERSION_UNISWAP_BULK_POOL_FINDER, MULTI_VERSION_UNISWAP_POOL_INFO_RETRIEVER, NULL_ADDRESS, PURE_ALPHA_STRATEGY, PoolDiscoveryResult, PoolKey, SCORE_CONTRACT, SUPPORTED_SCORE_CHAINS, ScoreClient, ScoreClientOptions, UNIV234_POOLS_STRATEGY, UPVOTE_APP, UPVOTE_PRICE_ETH, UPVOTE_STORAGE_APP, WETH_ADDRESS, calculatePriceFromSqrtPriceX96, decodeStrategyMetadata, decodeUpvoteMessage, decodeUpvoteStorageBlob, discoverPools, discoverTokenPool, encodePoolKey, encodeUpvoteKey, extractStrategyAddress, isDynamicSplitStrategy, isPureAlphaStrategy, isStrategyMessage, isUniv234PoolsStrategy, isUserUpvoteMessage, selectStrategy, tokenAddressToUpvoteKeyString };
230
+ export { ALL_STRATEGY_ADDRESSES, DYNAMIC_SPLIT_STRATEGY, DecodedStrategyMetadata, GetAppKeyScoresOptions, GetStrategyKeyScoresOptions, GetUpvotesForItemsOptions, GetUpvotesOptions, LEGACY_UPVOTE_V1_ADDRESS, LEGACY_UPVOTE_V2_ADDRESS, MULTI_VERSION_UNISWAP_BULK_POOL_FINDER, MULTI_VERSION_UNISWAP_POOL_INFO_RETRIEVER, NULL_ADDRESS, PURE_ALPHA_STRATEGY, PoolDiscoveryResult, PoolKey, SCORE_CONTRACT, SUPPORTED_SCORE_CHAINS, ScoreClient, ScoreClientOptions, UNIV234_POOLS_STRATEGY, UPVOTE_APP, UPVOTE_PRICE_ETH, UPVOTE_STORAGE_APP, calculatePriceFromSqrtPriceX96, decodeStrategyMetadata, decodeUpvoteMessage, decodeUpvoteStorageBlob, discoverPools, discoverTokenPool, encodePoolKey, encodeUpvoteKey, extractStrategyAddress, getWethAddress, isDynamicSplitStrategy, isPureAlphaStrategy, isStrategyMessage, isUniv234PoolsStrategy, isUserUpvoteMessage, selectStrategy, tokenAddressToUpvoteKeyString };
package/dist/index.js CHANGED
@@ -1975,7 +1975,19 @@ var MULTI_VERSION_UNISWAP_POOL_INFO_RETRIEVER = {
1975
1975
  address: "0x7A9EF0AC6F6a254cd570B05D62D094D3aa5067f1",
1976
1976
  abi: multi_version_uniswap_pool_info_retriever_default
1977
1977
  };
1978
- var WETH_ADDRESS = "0x4200000000000000000000000000000000000006";
1978
+ var WETH_BY_CHAIN = {
1979
+ 8453: "0x4200000000000000000000000000000000000006",
1980
+ // Base (L2 predeploy)
1981
+ 1: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
1982
+ // Ethereum mainnet
1983
+ };
1984
+ function getWethAddress(chainId) {
1985
+ const addr = WETH_BY_CHAIN[chainId];
1986
+ if (!addr) {
1987
+ throw new Error(`Score: No WETH address for chain ${chainId}`);
1988
+ }
1989
+ return addr;
1990
+ }
1979
1991
  var NULL_ADDRESS = "0x0000000000000000000000000000000000000000";
1980
1992
  var UPVOTE_PRICE_ETH = 25e-6;
1981
1993
  var encodeUpvoteKey = (tokenAddress) => {
@@ -2417,10 +2429,10 @@ var V4_HOOKS = [
2417
2429
  ];
2418
2430
  var LIQUIDITY_THRESHOLD_TO_PREFER_V2_V3_POOLS = 0.1 * 1e18;
2419
2431
  var LIQUIDITY_THRESHOLD_TO_CONSIDER_V2_V3_POOLS = 0.01 * 1e18;
2420
- function normalizeTokenPairs(pairs) {
2432
+ function normalizeTokenPairs(pairs, wethAddress) {
2421
2433
  return pairs.map((pair) => {
2422
2434
  const tokenA = pair.tokenAddress;
2423
- const tokenB = pair.baseTokenAddress || WETH_ADDRESS;
2435
+ const tokenB = pair.baseTokenAddress || wethAddress;
2424
2436
  return {
2425
2437
  originalPair: pair,
2426
2438
  tokenA: tokenA.toLowerCase() < tokenB.toLowerCase() ? tokenA : tokenB,
@@ -2437,7 +2449,7 @@ function buildDiscoveryArgs(normalizedPairs) {
2437
2449
  v4Hooks: V4_HOOKS
2438
2450
  };
2439
2451
  }
2440
- function parsePoolDiscoveries(poolResults, pairs) {
2452
+ function parsePoolDiscoveries(poolResults, pairs, wethAddress) {
2441
2453
  if (!poolResults) {
2442
2454
  return {
2443
2455
  v2PoolAddresses: [],
@@ -2474,7 +2486,7 @@ function parsePoolDiscoveries(poolResults, pairs) {
2474
2486
  v2PoolAddresses.push(poolAddressValue);
2475
2487
  v2PoolAddressToPair[poolAddress] = {
2476
2488
  tokenAddress: pair.tokenAddress,
2477
- baseTokenAddress: pair.baseTokenAddress || WETH_ADDRESS,
2489
+ baseTokenAddress: pair.baseTokenAddress || wethAddress,
2478
2490
  fee: feeNum
2479
2491
  };
2480
2492
  }
@@ -2485,7 +2497,7 @@ function parsePoolDiscoveries(poolResults, pairs) {
2485
2497
  v3PoolAddresses.push(poolAddressValue);
2486
2498
  v3PoolAddressToPair[poolAddress] = {
2487
2499
  tokenAddress: pair.tokenAddress,
2488
- baseTokenAddress: pair.baseTokenAddress || WETH_ADDRESS,
2500
+ baseTokenAddress: pair.baseTokenAddress || wethAddress,
2489
2501
  fee: feeNum
2490
2502
  };
2491
2503
  }
@@ -2496,7 +2508,7 @@ function parsePoolDiscoveries(poolResults, pairs) {
2496
2508
  v4PoolKeys.push(poolKey);
2497
2509
  v4PoolKeyToPair[poolKeyString] = {
2498
2510
  tokenAddress: pair.tokenAddress,
2499
- baseTokenAddress: pair.baseTokenAddress || WETH_ADDRESS,
2511
+ baseTokenAddress: pair.baseTokenAddress || wethAddress,
2500
2512
  fee: feeNum
2501
2513
  };
2502
2514
  }
@@ -2623,19 +2635,19 @@ function constructPoolKey(info, pair, version, v4PoolKey) {
2623
2635
  return void 0;
2624
2636
  }
2625
2637
  }
2626
- function getWethBalanceWei(pool) {
2627
- if (pool.token0?.toLowerCase() === WETH_ADDRESS.toLowerCase()) {
2638
+ function getWethBalanceWei(pool, wethAddress) {
2639
+ if (pool.token0?.toLowerCase() === wethAddress.toLowerCase()) {
2628
2640
  return Number(pool.token0Balance);
2629
- } else if (pool.token1?.toLowerCase() === WETH_ADDRESS.toLowerCase()) {
2641
+ } else if (pool.token1?.toLowerCase() === wethAddress.toLowerCase()) {
2630
2642
  return Number(pool.token1Balance);
2631
2643
  } else {
2632
2644
  return Number(pool.baseTokenBalance);
2633
2645
  }
2634
2646
  }
2635
- function filterV2V3PoolsByLiquidity(pools, threshold) {
2647
+ function filterV2V3PoolsByLiquidity(pools, threshold, wethAddress) {
2636
2648
  return pools.filter((pool) => {
2637
2649
  if (!pool.poolAddress) return false;
2638
- const wethBalanceWei = getWethBalanceWei(pool);
2650
+ const wethBalanceWei = getWethBalanceWei(pool, wethAddress);
2639
2651
  return wethBalanceWei >= threshold;
2640
2652
  });
2641
2653
  }
@@ -2658,7 +2670,7 @@ function selectBestV2V3PoolByFee(pools) {
2658
2670
  return a.fee < b.fee ? a : b;
2659
2671
  });
2660
2672
  }
2661
- function selectBestPoolPerPair(allPools) {
2673
+ function selectBestPoolPerPair(allPools, wethAddress) {
2662
2674
  const poolsByPair = {};
2663
2675
  for (const pool of allPools) {
2664
2676
  const key = pool.tokenAddress.toLowerCase() + "_" + pool.baseTokenAddress.toLowerCase();
@@ -2673,7 +2685,8 @@ function selectBestPoolPerPair(allPools) {
2673
2685
  } else {
2674
2686
  const v2v3PoolsWithPreferredLiquidity = filterV2V3PoolsByLiquidity(
2675
2687
  group,
2676
- LIQUIDITY_THRESHOLD_TO_PREFER_V2_V3_POOLS
2688
+ LIQUIDITY_THRESHOLD_TO_PREFER_V2_V3_POOLS,
2689
+ wethAddress
2677
2690
  );
2678
2691
  if (v2v3PoolsWithPreferredLiquidity.length > 0) {
2679
2692
  best = selectBestV2V3Pool(v2v3PoolsWithPreferredLiquidity);
@@ -2684,7 +2697,8 @@ function selectBestPoolPerPair(allPools) {
2684
2697
  } else {
2685
2698
  const v2v3PoolsWithAcceptableLiquidity = filterV2V3PoolsByLiquidity(
2686
2699
  group,
2687
- LIQUIDITY_THRESHOLD_TO_CONSIDER_V2_V3_POOLS
2700
+ LIQUIDITY_THRESHOLD_TO_CONSIDER_V2_V3_POOLS,
2701
+ wethAddress
2688
2702
  );
2689
2703
  if (v2v3PoolsWithAcceptableLiquidity.length > 0) {
2690
2704
  best = selectBestV2V3PoolByFee(v2v3PoolsWithAcceptableLiquidity);
@@ -2700,10 +2714,12 @@ function selectBestPoolPerPair(allPools) {
2700
2714
  }
2701
2715
  async function discoverPools({
2702
2716
  publicClient,
2703
- pairs
2717
+ pairs,
2718
+ chainId = 8453
2704
2719
  }) {
2705
2720
  if (pairs.length === 0) return [];
2706
- const normalizedPairs = normalizeTokenPairs(pairs);
2721
+ const wethAddress = getWethAddress(chainId);
2722
+ const normalizedPairs = normalizeTokenPairs(pairs, wethAddress);
2707
2723
  const discoveryArgs = buildDiscoveryArgs(normalizedPairs);
2708
2724
  const poolResults = await actions.readContract(publicClient, {
2709
2725
  address: MULTI_VERSION_UNISWAP_BULK_POOL_FINDER.address,
@@ -2711,7 +2727,7 @@ async function discoverPools({
2711
2727
  functionName: "getPoolsMultiVersion",
2712
2728
  args: [discoveryArgs]
2713
2729
  });
2714
- const discoveries = parsePoolDiscoveries(poolResults, pairs);
2730
+ const discoveries = parsePoolDiscoveries(poolResults, pairs, wethAddress);
2715
2731
  const totalPools = discoveries.v2PoolAddresses.length + discoveries.v3PoolAddresses.length + discoveries.v4PoolKeys.length;
2716
2732
  if (totalPools === 0) return [];
2717
2733
  const poolInfos = await actions.readContract(publicClient, {
@@ -2722,7 +2738,7 @@ async function discoverPools({
2722
2738
  discoveries.v2PoolAddresses,
2723
2739
  discoveries.v3PoolAddresses,
2724
2740
  discoveries.v4PoolKeys,
2725
- WETH_ADDRESS
2741
+ wethAddress
2726
2742
  ]
2727
2743
  });
2728
2744
  if (!Array.isArray(poolInfos) || poolInfos.length === 0) return [];
@@ -2731,7 +2747,7 @@ async function discoverPools({
2731
2747
  if (!poolData || !poolData.pair) return null;
2732
2748
  return {
2733
2749
  tokenAddress: poolData.pair.tokenAddress,
2734
- baseTokenAddress: poolData.pair.baseTokenAddress || WETH_ADDRESS,
2750
+ baseTokenAddress: poolData.pair.baseTokenAddress || wethAddress,
2735
2751
  poolAddress: poolData.version === 4 ? null : info.poolAddress,
2736
2752
  price: calculatePoolPrice(info, poolData.pair, poolData.version),
2737
2753
  baseTokenBalance: String(info.baseTokenBalance || 0),
@@ -2748,7 +2764,7 @@ async function discoverPools({
2748
2764
  )
2749
2765
  };
2750
2766
  }).filter((p) => p !== null);
2751
- const bestPools = selectBestPoolPerPair(allPools);
2767
+ const bestPools = selectBestPoolPerPair(allPools, wethAddress);
2752
2768
  return bestPools.map(
2753
2769
  (pool) => ({
2754
2770
  tokenAddress: pool.tokenAddress,
@@ -2767,11 +2783,13 @@ async function discoverPools({
2767
2783
  }
2768
2784
  async function discoverTokenPool({
2769
2785
  publicClient,
2770
- tokenAddress
2786
+ tokenAddress,
2787
+ chainId = 8453
2771
2788
  }) {
2772
2789
  const results = await discoverPools({
2773
2790
  publicClient,
2774
- pairs: [{ tokenAddress }]
2791
+ pairs: [{ tokenAddress }],
2792
+ chainId
2775
2793
  });
2776
2794
  return results[0] ?? null;
2777
2795
  }
@@ -2791,7 +2809,6 @@ exports.UNIV234_POOLS_STRATEGY = UNIV234_POOLS_STRATEGY;
2791
2809
  exports.UPVOTE_APP = UPVOTE_APP;
2792
2810
  exports.UPVOTE_PRICE_ETH = UPVOTE_PRICE_ETH;
2793
2811
  exports.UPVOTE_STORAGE_APP = UPVOTE_STORAGE_APP;
2794
- exports.WETH_ADDRESS = WETH_ADDRESS;
2795
2812
  exports.calculatePriceFromSqrtPriceX96 = calculatePriceFromSqrtPriceX96;
2796
2813
  exports.decodeStrategyMetadata = decodeStrategyMetadata;
2797
2814
  exports.decodeUpvoteMessage = decodeUpvoteMessage;
@@ -2807,6 +2824,7 @@ exports.getScoreKey = getScoreKey;
2807
2824
  exports.getStorageScoreKey = getStorageScoreKey;
2808
2825
  exports.getStorageUpvoteContext = getStorageUpvoteContext;
2809
2826
  exports.getTokenScoreKey = getTokenScoreKey;
2827
+ exports.getWethAddress = getWethAddress;
2810
2828
  exports.isDynamicSplitStrategy = isDynamicSplitStrategy;
2811
2829
  exports.isPureAlphaStrategy = isPureAlphaStrategy;
2812
2830
  exports.isStrategyMessage = isStrategyMessage;