@guru-fund/sdk 0.2.9 → 0.2.10

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.
@@ -7,6 +7,7 @@ import getUniswapV4Route, { connectV4Quoter, toAdapterPathKeys, } from './getUni
7
7
  import getVeloraRoute from './getVeloraRoute.js';
8
8
  import { stablecoinAddresses } from './helpers.js';
9
9
  import PoolHelper from './poolHelper.js';
10
+ import { V4_ZERO_ADDRESS } from './constants.js';
10
11
  import { discoverV4Paths } from './v4PoolDiscovery.js';
11
12
  const AERO_V3_FACTORY_ABI = [
12
13
  'function getPool(address tokenA, address tokenB, int24 tickSpacing) view returns (address pool)',
@@ -342,6 +343,11 @@ async function _priceViaV4Pools(token, oneToken, getWethUsd, ctx) {
342
343
  }
343
344
  return best > 0n ? best : null;
344
345
  };
346
+ const inNative = await quoteVia(V4_ZERO_ADDRESS);
347
+ if (inNative !== null) {
348
+ const wethUsd = await getWethUsd();
349
+ return (inNative * wethUsd) / WETH_UNIT;
350
+ }
345
351
  const inUsdc = await quoteVia(addresses.tokens.USDG ?? addresses.tokens.USDC);
346
352
  if (inUsdc !== null) {
347
353
  return inUsdc * 10n ** 12n;
@@ -23,6 +23,7 @@ interface DexscreenerPair {
23
23
  liquidity?: {
24
24
  usd?: number;
25
25
  };
26
+ pairCreatedAt?: number;
26
27
  }
27
28
  export declare function selectV4PairPoolIds(pairs: DexscreenerPair[], tokenIn: string, tokenOut: string): string[];
28
29
  export declare function stitchNativeBridgeV4Paths(firstLegs: V4Path[], secondLegs: V4Path[]): V4Path[];
@@ -66,18 +66,63 @@ export function stitchNativeBridgeV4Paths(firstLegs, secondLegs) {
66
66
  return paths;
67
67
  }
68
68
  const poolKeyCache = new Map();
69
- async function resolvePoolKey(chainId, poolId, provider) {
69
+ async function resolvePoolKey(chainId, poolId, provider, pairCreatedAt) {
70
70
  const cacheKey = `${chainId}:${poolId}`;
71
71
  const cached = poolKeyCache.get(cacheKey);
72
72
  if (cached)
73
73
  return cached;
74
74
  const config = V4_CHAIN_CONFIG[chainId];
75
- const logs = await provider.getLogs({
75
+ const filter = {
76
76
  address: config.poolManager,
77
77
  topics: [POOL_INITIALIZE_TOPIC, poolId],
78
78
  fromBlock: config.deployBlock,
79
79
  toBlock: 'latest',
80
- });
80
+ };
81
+ let logs;
82
+ try {
83
+ logs = await provider.getLogs(filter);
84
+ }
85
+ catch {
86
+ logs = [];
87
+ const latestBlock = await provider.getBlockNumber();
88
+ const chunkSize = 9_999;
89
+ if (pairCreatedAt) {
90
+ let low = config.deployBlock;
91
+ let high = latestBlock;
92
+ const targetTimestamp = Math.floor(pairCreatedAt / 1_000);
93
+ while (low < high) {
94
+ const middle = Math.floor((low + high) / 2);
95
+ const block = await provider.getBlock(middle);
96
+ if (!block)
97
+ break;
98
+ if (block.timestamp < targetTimestamp)
99
+ low = middle + 1;
100
+ else
101
+ high = middle;
102
+ }
103
+ const fromBlock = Math.max(config.deployBlock, low - 5_000);
104
+ const toBlock = Math.min(latestBlock, fromBlock + chunkSize);
105
+ logs = await provider.getLogs({
106
+ ...filter,
107
+ fromBlock,
108
+ toBlock,
109
+ });
110
+ }
111
+ if (logs.length === 0) {
112
+ for (let toBlock = latestBlock; toBlock >= config.deployBlock; toBlock -= chunkSize + 1) {
113
+ const fromBlock = Math.max(config.deployBlock, toBlock - chunkSize);
114
+ const chunk = await provider.getLogs({
115
+ ...filter,
116
+ fromBlock,
117
+ toBlock,
118
+ });
119
+ if (chunk.length > 0) {
120
+ logs = chunk;
121
+ break;
122
+ }
123
+ }
124
+ }
125
+ }
81
126
  const log = logs[0];
82
127
  if (!log)
83
128
  return null;
@@ -142,6 +187,7 @@ async function discoverDirectV4Paths(chainId, tokenIn, tokenOut, provider, endpo
142
187
  const isMajor = (token) => majors.some((major) => compareAddresses(major, token));
143
188
  const queryOrder = isMajor(tokenIn) ? [tokenOut, tokenIn] : [tokenIn, tokenOut];
144
189
  let poolIds = [];
190
+ const pairCreatedAtByPoolId = new Map();
145
191
  for (const queryToken of queryOrder) {
146
192
  const response = await fetch(`${endpoint}/${config.dexscreenerSlug}/${queryToken}`);
147
193
  if (!response.ok) {
@@ -149,10 +195,16 @@ async function discoverDirectV4Paths(chainId, tokenIn, tokenOut, provider, endpo
149
195
  }
150
196
  const pairs = (await response.json());
151
197
  poolIds = selectV4PairPoolIds(pairs, tokenIn, tokenOut);
152
- if (poolIds.length > 0)
198
+ if (poolIds.length > 0) {
199
+ for (const pair of pairs) {
200
+ if (!pair.pairAddress || !pair.pairCreatedAt)
201
+ continue;
202
+ pairCreatedAtByPoolId.set(pair.pairAddress.toLowerCase(), pair.pairCreatedAt);
203
+ }
153
204
  break;
205
+ }
154
206
  }
155
- const keys = await Promise.all(poolIds.map((poolId) => resolvePoolKey(chainId, poolId, provider)));
207
+ const keys = await Promise.all(poolIds.map((poolId) => resolvePoolKey(chainId, poolId, provider, pairCreatedAtByPoolId.get(poolId))));
156
208
  const resolvedKeys = keys.filter((key) => key !== null && isSafeDiscoveredV4PoolKey(key));
157
209
  const onchainKeys = resolvedKeys.length > 0
158
210
  ? []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guru-fund/sdk",
3
- "version": "0.2.9",
3
+ "version": "0.2.10",
4
4
  "description": "Guru Protocol SDK — quote + transaction builders for on-chain managed funds.",
5
5
  "license": "MIT",
6
6
  "type": "module",