@lifi/sdk-provider-solana 4.2.2 → 4.3.0
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/CHANGELOG.md +19 -0
- package/dist/cjs/actions/getSolanaBalance.js +3 -3
- package/dist/cjs/actions/getSolanaBalance.js.map +1 -1
- package/dist/cjs/actions/sendAndConfirmBundle.d.ts +5 -0
- package/dist/cjs/actions/sendAndConfirmBundle.js +38 -3
- package/dist/cjs/actions/sendAndConfirmBundle.js.map +1 -1
- package/dist/cjs/actions/sendAndConfirmTransaction.d.ts +3 -0
- package/dist/cjs/actions/sendAndConfirmTransaction.js +56 -10
- package/dist/cjs/actions/sendAndConfirmTransaction.js.map +1 -1
- package/dist/cjs/confirmation/confirmSignature.d.ts +2 -0
- package/dist/cjs/confirmation/confirmSignature.js +2 -0
- package/dist/cjs/confirmation/confirmSignature.js.map +1 -1
- package/dist/cjs/rpc/registry.d.ts +20 -0
- package/dist/cjs/rpc/registry.js +54 -11
- package/dist/cjs/rpc/registry.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/version.js.map +1 -1
- package/dist/esm/actions/getSolanaBalance.js +3 -3
- package/dist/esm/actions/getSolanaBalance.js.map +1 -1
- package/dist/esm/actions/sendAndConfirmBundle.d.ts +5 -0
- package/dist/esm/actions/sendAndConfirmBundle.d.ts.map +1 -1
- package/dist/esm/actions/sendAndConfirmBundle.js +40 -5
- package/dist/esm/actions/sendAndConfirmBundle.js.map +1 -1
- package/dist/esm/actions/sendAndConfirmTransaction.d.ts +3 -0
- package/dist/esm/actions/sendAndConfirmTransaction.d.ts.map +1 -1
- package/dist/esm/actions/sendAndConfirmTransaction.js +58 -12
- package/dist/esm/actions/sendAndConfirmTransaction.js.map +1 -1
- package/dist/esm/confirmation/confirmSignature.d.ts +2 -0
- package/dist/esm/confirmation/confirmSignature.d.ts.map +1 -1
- package/dist/esm/confirmation/confirmSignature.js +2 -1
- package/dist/esm/confirmation/confirmSignature.js.map +1 -1
- package/dist/esm/rpc/registry.d.ts +20 -0
- package/dist/esm/rpc/registry.d.ts.map +1 -1
- package/dist/esm/rpc/registry.js +53 -12
- package/dist/esm/rpc/registry.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/version.js.map +1 -1
- package/package.json +2 -2
- package/src/actions/getSolanaBalance.ts +3 -3
- package/src/actions/sendAndConfirmBundle.ts +85 -5
- package/src/actions/sendAndConfirmTransaction.ts +108 -18
- package/src/confirmation/confirmSignature.ts +2 -1
- package/src/rpc/registry.ts +66 -21
- package/src/version.ts +1 -1
package/dist/esm/rpc/registry.js
CHANGED
|
@@ -2,8 +2,8 @@ import { createJitoRpc } from "./jito/createJitoRpc.js";
|
|
|
2
2
|
import { ChainId, LruMap, withDedupe } from "@lifi/sdk";
|
|
3
3
|
import { createSolanaRpc } from "@solana/kit";
|
|
4
4
|
//#region src/rpc/registry.ts
|
|
5
|
-
const solanaRpcs = new LruMap(
|
|
6
|
-
const jitoRpcs = new LruMap(
|
|
5
|
+
const solanaRpcs = new LruMap(64);
|
|
6
|
+
const jitoRpcs = new LruMap(64);
|
|
7
7
|
/**
|
|
8
8
|
* A well-formed but non-existent Jito bundle id used solely to probe RPC
|
|
9
9
|
* capability. It forces `getBundleStatuses` to actually execute, so we can tell
|
|
@@ -31,7 +31,7 @@ const JITO_PROBE_GATEWAY_RETRY_MS = 9e5;
|
|
|
31
31
|
* set never shrinks, and every bundle submission re-probes every non-Jito
|
|
32
32
|
* endpoint at up to `PROBE_TIMEOUT_MS` each, on the latency path before
|
|
33
33
|
* submission can start. */
|
|
34
|
-
const jitoProbes = new LruMap(
|
|
34
|
+
const jitoProbes = new LruMap(64);
|
|
35
35
|
/** One rule for every outcome: a record is fresh until its own window closes.
|
|
36
36
|
* `Infinity` covers the answers that cannot change. */
|
|
37
37
|
const isProbeFresh = (record) => record !== void 0 && Date.now() - record.at < record.retryMs;
|
|
@@ -95,20 +95,26 @@ const probeJitoRpc = async (rpcUrl) => {
|
|
|
95
95
|
*/
|
|
96
96
|
const ensureSolanaRpcs = async (client) => {
|
|
97
97
|
const rpcUrls = await client.getRpcUrlsByChainId(ChainId.SOL);
|
|
98
|
-
|
|
98
|
+
ensureSolanaRpcsFor(rpcUrls);
|
|
99
99
|
return rpcUrls;
|
|
100
100
|
};
|
|
101
|
+
/** Creates and caches a client for each URL that has none yet. */
|
|
102
|
+
const ensureSolanaRpcsFor = (rpcUrls) => {
|
|
103
|
+
for (const rpcUrl of rpcUrls) if (!solanaRpcs.has(rpcUrl)) solanaRpcs.set(rpcUrl, createSolanaRpc(rpcUrl));
|
|
104
|
+
};
|
|
105
|
+
const cachedSolanaRpcs = (rpcUrls) => rpcUrls.map((rpcUrl) => solanaRpcs.get(rpcUrl)).filter((rpc) => Boolean(rpc));
|
|
101
106
|
/**
|
|
102
|
-
*
|
|
107
|
+
* Probes each URL for Jito support and caches a client for every supported
|
|
108
|
+
* one. The configured RPCs and the write RPCs share this cache.
|
|
103
109
|
*
|
|
104
110
|
* Every probe outcome is cached, so a non-Jito endpoint is probed once rather
|
|
105
111
|
* than once per bundle submission. Each record carries its own retry window:
|
|
106
112
|
* see `JITO_PROBE_RETRY_MS` and `JITO_PROBE_GATEWAY_RETRY_MS` for the two that
|
|
107
113
|
* expire.
|
|
108
|
-
* @param
|
|
114
|
+
* @param rpcUrls - The URLs to probe.
|
|
115
|
+
* @returns - How many of the URLs are currently unreachable.
|
|
109
116
|
*/
|
|
110
|
-
const
|
|
111
|
-
const rpcUrls = await client.getRpcUrlsByChainId(ChainId.SOL);
|
|
117
|
+
const ensureJitoRpcsFor = async (rpcUrls) => {
|
|
112
118
|
const unprobed = rpcUrls.filter((rpcUrl) => !isProbeFresh(jitoProbes.get(rpcUrl)));
|
|
113
119
|
(await Promise.all(unprobed.map((rpcUrl) => withDedupe(() => probeJitoRpc(rpcUrl), { id: `jito-probe:${rpcUrl}` })))).forEach(({ outcome, retryMs }, index) => {
|
|
114
120
|
const rpcUrl = unprobed[index];
|
|
@@ -120,17 +126,37 @@ const ensureJitoRpcs = async (client) => {
|
|
|
120
126
|
if (outcome === "supported") jitoRpcs.set(rpcUrl, createJitoRpc(rpcUrl));
|
|
121
127
|
else jitoRpcs.delete(rpcUrl);
|
|
122
128
|
});
|
|
129
|
+
return rpcUrls.filter((rpcUrl) => jitoProbes.get(rpcUrl)?.outcome === "unreachable").length;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Initializes and caches Jito RPCs for every configured Solana RPC URL.
|
|
133
|
+
* @param client - The SDK client used to fetch RPC URLs.
|
|
134
|
+
*/
|
|
135
|
+
const ensureJitoRpcs = async (client) => {
|
|
136
|
+
const rpcUrls = await client.getRpcUrlsByChainId(ChainId.SOL);
|
|
123
137
|
return {
|
|
124
138
|
rpcUrls,
|
|
125
|
-
unreachable:
|
|
139
|
+
unreachable: await ensureJitoRpcsFor(rpcUrls)
|
|
126
140
|
};
|
|
127
141
|
};
|
|
142
|
+
const cachedJitoRpcs = (rpcUrls) => rpcUrls.map((rpcUrl) => jitoRpcs.get(rpcUrl)).filter((rpc) => Boolean(rpc));
|
|
128
143
|
/**
|
|
129
144
|
* Wrapper around getting the Solana RPCs
|
|
130
145
|
* @returns - Solana RPCs
|
|
131
146
|
*/
|
|
132
147
|
const getSolanaRpcs = async (client) => {
|
|
133
|
-
return (await ensureSolanaRpcs(client))
|
|
148
|
+
return cachedSolanaRpcs(await ensureSolanaRpcs(client));
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Clients for the write RPCs (`rpcUrls[ChainId.SOL].write`). They only
|
|
152
|
+
* send transactions; the one read they get is the Jito probe in
|
|
153
|
+
* `getJitoCapableRpcs`.
|
|
154
|
+
* @param rpcUrls - The write RPC URLs.
|
|
155
|
+
* @returns - Solana RPCs to send transactions through.
|
|
156
|
+
*/
|
|
157
|
+
const getSolanaWriteRpcs = (rpcUrls) => {
|
|
158
|
+
ensureSolanaRpcsFor(rpcUrls);
|
|
159
|
+
return cachedSolanaRpcs(rpcUrls);
|
|
134
160
|
};
|
|
135
161
|
/** `unreachable` counts endpoints whose probe failed without naming the
|
|
136
162
|
* method unknown. Empty `rpcs` with a non-zero count is usually an outage, but
|
|
@@ -140,11 +166,26 @@ const getSolanaRpcs = async (client) => {
|
|
|
140
166
|
const getJitoRpcs = async (client) => {
|
|
141
167
|
const { rpcUrls, unreachable } = await ensureJitoRpcs(client);
|
|
142
168
|
return {
|
|
143
|
-
rpcs: rpcUrls
|
|
169
|
+
rpcs: cachedJitoRpcs(rpcUrls),
|
|
170
|
+
unreachable
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* The given URLs that pass the Jito probe - the bundle or write RPCs of
|
|
175
|
+
* `rpcUrls[ChainId.SOL]`. They share the probe cache with the read RPCs.
|
|
176
|
+
* `unreachable` counts the URLs whose probe failed without an answer, as in
|
|
177
|
+
* `getJitoRpcs`.
|
|
178
|
+
* @param rpcUrls - The URLs to probe.
|
|
179
|
+
* @returns - Jito RPCs to submit bundles through, and the unreachable count.
|
|
180
|
+
*/
|
|
181
|
+
const getJitoCapableRpcs = async (rpcUrls) => {
|
|
182
|
+
const unreachable = await ensureJitoRpcsFor(rpcUrls);
|
|
183
|
+
return {
|
|
184
|
+
rpcs: cachedJitoRpcs(rpcUrls),
|
|
144
185
|
unreachable
|
|
145
186
|
};
|
|
146
187
|
};
|
|
147
188
|
//#endregion
|
|
148
|
-
export { JITO_PROBE_GATEWAY_RETRY_MS, JITO_PROBE_RETRY_MS, getJitoRpcs, getSolanaRpcs, probeJitoRpc };
|
|
189
|
+
export { JITO_PROBE_GATEWAY_RETRY_MS, JITO_PROBE_RETRY_MS, getJitoCapableRpcs, getJitoRpcs, getSolanaRpcs, getSolanaWriteRpcs, probeJitoRpc };
|
|
149
190
|
|
|
150
191
|
//# sourceMappingURL=registry.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.js","names":[],"sources":["../../../src/rpc/registry.ts"],"sourcesContent":["import { ChainId, LruMap, type SDKClient, withDedupe } from '@lifi/sdk'\nimport { createSolanaRpc } from '@solana/kit'\nimport { createJitoRpc } from './jito/createJitoRpc.js'\nimport type { JitoRpcType, SolanaRpcType } from './types.js'\n\nconst solanaRpcs = new LruMap<SolanaRpcType>(12)\nconst jitoRpcs = new LruMap<JitoRpcType>(12)\n\n/**\n * A well-formed but non-existent Jito bundle id used solely to probe RPC\n * capability. It forces `getBundleStatuses` to actually execute, so we can tell\n * a Jito-capable RPC (resolves with `{ value: [null] }` — bundle not found)\n * apart from a standard Solana RPC (throws \"Method not found\").\n *\n * Uses all `1`s: 64 chars, valid as both hex and base-58 (base-58 excludes\n * `0`/`O`/`I`/`l`), so a provider that validates the id in either encoding\n * still accepts it and performs the lookup instead of rejecting the probe.\n */\nconst PROBE_BUNDLE_ID =\n '1111111111111111111111111111111111111111111111111111111111111111'\n\n/** Both leave the endpoint out of the Jito list, but only `unsupported` is a\n * configuration gap the integrator can act on. */\nexport type JitoProbeOutcome = 'supported' | 'unsupported' | 'unreachable'\n\n/** An outcome plus how long it may be trusted. The classifier decides both,\n * because how long an answer is worth is a property of the evidence that\n * produced it, not of the outcome alone. */\nexport type JitoProbeResult = {\n outcome: JitoProbeOutcome\n /** `Infinity` for an answer that cannot change. */\n retryMs: number\n}\n\n/** Retry window for a probe that failed without naming the method unknown.\n * An outage heals, so a permanent entry would keep a recovered endpoint out of\n * the Jito list for the whole process. */\nexport const JITO_PROBE_RETRY_MS = 30_000\n\n/** Retry window for a bare HTTP 401/403. Longer than an outage's, shorter than\n * forever. The refusal never reached the JSON-RPC layer, so it proves nothing\n * about capability: a plan gate, a provider deploy and an allowlist entry\n * still propagating are indistinguishable here. Treating it as permanent meant\n * one transient 403 removed a working endpoint for the process lifetime;\n * treating it as a 30 s outage would re-probe a real plan gate on the\n * pre-submission latency path twice a minute, forever. */\nexport const JITO_PROBE_GATEWAY_RETRY_MS: number = 15 * 60_000\n\ntype JitoProbeRecord = JitoProbeResult & { at: number }\n\n/** Every answered probe, the negative ones included. Without them the unprobed\n * set never shrinks, and every bundle submission re-probes every non-Jito\n * endpoint at up to `PROBE_TIMEOUT_MS` each, on the latency path before\n * submission can start. */\nconst jitoProbes = new LruMap<JitoProbeRecord>(12)\n\n/** One rule for every outcome: a record is fresh until its own window closes.\n * `Infinity` covers the answers that cannot change. */\nconst isProbeFresh = (record: JitoProbeRecord | undefined): boolean =>\n record !== undefined && Date.now() - record.at < record.retryMs\n\n/** `@solana/kit` puts the JSON-RPC code on `context.__code`, not `error.code`.\n * The message is a last resort - a provider can reword it. An unrecognized\n * failure counts as `unreachable`, so a misread blames an outage. */\nconst JSON_RPC_METHOD_NOT_FOUND = -32601\n/** Non-standard; providers use it for \"your plan lacks this method\". */\nconst PROVIDER_PLAN_RESTRICTED = -32403\n\nconst CAPABILITY_CODES: number[] = [\n JSON_RPC_METHOD_NOT_FOUND,\n PROVIDER_PLAN_RESTRICTED,\n]\n\n/** A plan gate rejects before JSON-RPC, so no `-32601` arrives - Helius\n * answers with a bare 403. Unlike a JSON-RPC code this never reached the RPC\n * layer, so it is a gateway verdict rather than a capability answer, and it\n * earns a window instead of permanence. 429 and 5xx are absent: those take the\n * shorter outage window. */\nconst GATEWAY_REFUSAL_HTTP_STATUSES: number[] = [401, 403]\n\nconst readProbeFailure = (error: unknown): JitoProbeResult => {\n const candidate = error as\n | {\n code?: unknown\n context?: { __code?: unknown; statusCode?: unknown }\n }\n | undefined\n\n // kit's `__code` is 8100002 for every HTTP failure, so only a JSON-RPC code\n // that reached the body is meaningful.\n const rpcCode = candidate?.code ?? candidate?.context?.__code\n if (typeof rpcCode === 'number' && CAPABILITY_CODES.includes(rpcCode)) {\n // The server parsed the request and said it has no such method. Definitive.\n return { outcome: 'unsupported', retryMs: Number.POSITIVE_INFINITY }\n }\n\n const status = candidate?.context?.statusCode\n if (\n typeof status === 'number' &&\n GATEWAY_REFUSAL_HTTP_STATUSES.includes(status)\n ) {\n return { outcome: 'unreachable', retryMs: JITO_PROBE_GATEWAY_RETRY_MS }\n }\n\n const message = error instanceof Error ? error.message : String(error)\n return /method not found|method does not exist|-32601|method .* not supported|unsupported method|only available for business plans/i.test(\n message\n )\n ? { outcome: 'unsupported', retryMs: Number.POSITIVE_INFINITY }\n : { outcome: 'unreachable', retryMs: JITO_PROBE_RETRY_MS }\n}\n\n/**\n * Bounded because it runs upstream of `raceRpcs`, outside the confirmation\n * budget the SDK documents: an endpoint that accepts the connection and never\n * answers would otherwise stall bundle submission indefinitely.\n */\nconst PROBE_TIMEOUT_MS = 5_000\n\n/**\n * Probes with `getBundleStatuses` rather than `getTipAccounts`: it is the\n * method bundle confirmation actually uses, and providers such as Helius\n * support it without exposing `getTipAccounts`.\n */\nexport const probeJitoRpc = async (\n rpcUrl: string\n): Promise<JitoProbeResult> => {\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), PROBE_TIMEOUT_MS)\n try {\n const rpc = createJitoRpc(rpcUrl)\n await rpc\n .getBundleStatuses([PROBE_BUNDLE_ID])\n .send({ abortSignal: controller.signal })\n return { outcome: 'supported', retryMs: Number.POSITIVE_INFINITY }\n } catch (error) {\n return readProbeFailure(error)\n } finally {\n clearTimeout(timer)\n }\n}\n\n/**\n * Initializes Solana RPCs for all available RPC URLs if they haven't been cached yet.\n * @param client - The SDK client used to fetch RPC URLs.\n */\nconst ensureSolanaRpcs = async (client: SDKClient): Promise<string[]> => {\n const rpcUrls = await client.getRpcUrlsByChainId(ChainId.SOL)\n for (const rpcUrl of rpcUrls) {\n if (!solanaRpcs.has(rpcUrl)) {\n solanaRpcs.set(rpcUrl, createSolanaRpc(rpcUrl))\n }\n }\n return rpcUrls\n}\n\n/**\n * Initializes and caches Jito RPCs for every configured Solana RPC URL.\n *\n * Every probe outcome is cached, so a non-Jito endpoint is probed once rather\n * than once per bundle submission. Each record carries its own retry window:\n * see `JITO_PROBE_RETRY_MS` and `JITO_PROBE_GATEWAY_RETRY_MS` for the two that\n * expire.\n * @param client - The SDK client used to fetch RPC URLs.\n */\nconst ensureJitoRpcs = async (\n client: SDKClient\n): Promise<{ rpcUrls: string[]; unreachable: number }> => {\n const rpcUrls = await client.getRpcUrlsByChainId(ChainId.SOL)\n // Probed in parallel: serially, one slow endpoint delayed every URL behind\n // it before submission could start.\n const unprobed = rpcUrls.filter(\n (rpcUrl) => !isProbeFresh(jitoProbes.get(rpcUrl))\n )\n // Deduplicated per URL: without it two concurrent submissions each probe\n // every unprobed endpoint, and the slower probe writes last - a 5 s timeout\n // resolving `unreachable` used to overwrite a `supported` answer that landed\n // at 300 ms, evicting a verified-healthy endpoint.\n //\n // `withDedupe` drops its entry in a `.finally`, so this guarantees at most\n // one probe *in flight* per URL, not one probe per URL. The write below waits\n // for the whole batch, so a fast URL's record is unwritten while a slow\n // sibling is still running - up to `PROBE_TIMEOUT_MS` - and every caller\n // arriving in that window re-probes the fast URL. Repeats are therefore\n // bounded by arrivals, not by one.\n //\n // What this does rule out is the clobber: a second probe can only begin\n // after the first has settled, so it always carries the newer answer and\n // always writes second. Write order matches result recency, which is why no\n // timestamp comparison is needed here.\n const results = await Promise.all(\n unprobed.map((rpcUrl) =>\n withDedupe(() => probeJitoRpc(rpcUrl), { id: `jito-probe:${rpcUrl}` })\n )\n )\n\n results.forEach(({ outcome, retryMs }, index) => {\n const rpcUrl = unprobed[index]\n jitoProbes.set(rpcUrl, { outcome, retryMs, at: Date.now() })\n if (outcome === 'supported') {\n jitoRpcs.set(rpcUrl, createJitoRpc(rpcUrl))\n } else {\n // An endpoint that used to answer and now does not must leave the list,\n // or `getJitoRpcs` keeps handing out a client for it.\n jitoRpcs.delete(rpcUrl)\n }\n })\n\n // Counted over every URL, not only the freshly probed ones: a cached\n // `unreachable` still has to reach `sendAndConfirmBundle`, which uses the\n // count to choose between \"retry, likely temporary\" and \"supply a\n // Jito-capable URL\".\n const unreachable = rpcUrls.filter(\n (rpcUrl) => jitoProbes.get(rpcUrl)?.outcome === 'unreachable'\n ).length\n\n return { rpcUrls, unreachable }\n}\n\n/**\n * Wrapper around getting the Solana RPCs\n * @returns - Solana RPCs\n */\nexport const getSolanaRpcs = async (\n client: SDKClient\n): Promise<SolanaRpcType[]> => {\n const rpcUrls = await ensureSolanaRpcs(client)\n return rpcUrls\n .map((rpcUrl) => solanaRpcs.get(rpcUrl))\n .filter((rpc): rpc is SolanaRpcType => Boolean(rpc))\n}\n\n/** `unreachable` counts endpoints whose probe failed without naming the\n * method unknown. Empty `rpcs` with a non-zero count is usually an outage, but\n * not always: a bare 401/403 lands here too, and that can be a plan gate the\n * integrator has to fix. `sendAndConfirmBundle`'s message names both, because\n * this count cannot tell them apart. */\nexport const getJitoRpcs = async (\n client: SDKClient\n): Promise<{ rpcs: JitoRpcType[]; unreachable: number }> => {\n const { rpcUrls, unreachable } = await ensureJitoRpcs(client)\n return {\n rpcs: rpcUrls\n .map((rpcUrl) => jitoRpcs.get(rpcUrl))\n .filter((rpc): rpc is JitoRpcType => Boolean(rpc)),\n unreachable,\n }\n}\n"],"mappings":";;;;AAKA,MAAM,aAAa,IAAI,OAAsB,EAAE;AAC/C,MAAM,WAAW,IAAI,OAAoB,EAAE;;;;;;;;;;;AAY3C,MAAM,kBACJ;;;;AAkBF,MAAa,sBAAsB;;;;;;;;AASnC,MAAa,8BAAsC;;;;;AAQnD,MAAM,aAAa,IAAI,OAAwB,EAAE;;;AAIjD,MAAM,gBAAgB,WACpB,WAAW,KAAA,KAAa,KAAK,IAAI,IAAI,OAAO,KAAK,OAAO;AAS1D,MAAM,mBAA6B,CACjC,QACA,MACF;;;;;;AAOA,MAAM,gCAA0C,CAAC,KAAK,GAAG;AAEzD,MAAM,oBAAoB,UAAoC;CAC5D,MAAM,YAAY;CASlB,MAAM,UAAU,WAAW,QAAQ,WAAW,SAAS;CACvD,IAAI,OAAO,YAAY,YAAY,iBAAiB,SAAS,OAAO,GAElE,OAAO;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AAc,SAAS,CAAC,CACvE,CACF,EAAA,CAEQ,SAAS,EAAE,SAAS,WAAW,UAAU;EAC/C,MAAM,SAAS,SAAS;EACxB,WAAW,IAAI,QAAQ;GAAE;GAAS;GAAS,IAAI,KAAK,IAAI;EAAE,CAAC;EAC3D,IAAI,YAAY,aACd,SAAS,IAAI,QAAQ,cAAc,MAAM,CAAC;OAI1C,SAAS,OAAO,MAAM;CAE1B,CAAC;CAUD,OAAO;EAAE;EAAS,aAJE,QAAQ,QACzB,WAAW,WAAW,IAAI,MAAM,CAAC,EAAE,YAAY,aAClD,CAAC,CAAC;CAE4B;AAChC;;;;;AAMA,MAAa,gBAAgB,OAC3B,WAC6B;CAE7B,QAAO,MADe,iBAAiB,MAAM,EAAA,CAE1C,KAAK,WAAW,WAAW,IAAI,MAAM,CAAC,CAAC,CACvC,QAAQ,QAA8B,QAAQ,GAAG,CAAC;AACvD;;;;;;AAOA,MAAa,cAAc,OACzB,WAC0D;CAC1D,MAAM,EAAE,SAAS,gBAAgB,MAAM,eAAe,MAAM;CAC5D,OAAO;EACL,MAAM,QACH,KAAK,WAAW,SAAS,IAAI,MAAM,CAAC,CAAC,CACrC,QAAQ,QAA4B,QAAQ,GAAG,CAAC;EACnD;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"registry.js","names":[],"sources":["../../../src/rpc/registry.ts"],"sourcesContent":["import { ChainId, LruMap, type SDKClient, withDedupe } from '@lifi/sdk'\nimport { createSolanaRpc } from '@solana/kit'\nimport { createJitoRpc } from './jito/createJitoRpc.js'\nimport type { JitoRpcType, SolanaRpcType } from './types.js'\n\n// Read, write and bundle URLs share these caches, so they hold more than one\n// list's worth: an entry evicted mid-use is probed or built again.\nconst solanaRpcs = new LruMap<SolanaRpcType>(64)\nconst jitoRpcs = new LruMap<JitoRpcType>(64)\n\n/**\n * A well-formed but non-existent Jito bundle id used solely to probe RPC\n * capability. It forces `getBundleStatuses` to actually execute, so we can tell\n * a Jito-capable RPC (resolves with `{ value: [null] }` — bundle not found)\n * apart from a standard Solana RPC (throws \"Method not found\").\n *\n * Uses all `1`s: 64 chars, valid as both hex and base-58 (base-58 excludes\n * `0`/`O`/`I`/`l`), so a provider that validates the id in either encoding\n * still accepts it and performs the lookup instead of rejecting the probe.\n */\nconst PROBE_BUNDLE_ID =\n '1111111111111111111111111111111111111111111111111111111111111111'\n\n/** Both leave the endpoint out of the Jito list, but only `unsupported` is a\n * configuration gap the integrator can act on. */\nexport type JitoProbeOutcome = 'supported' | 'unsupported' | 'unreachable'\n\n/** An outcome plus how long it may be trusted. The classifier decides both,\n * because how long an answer is worth is a property of the evidence that\n * produced it, not of the outcome alone. */\nexport type JitoProbeResult = {\n outcome: JitoProbeOutcome\n /** `Infinity` for an answer that cannot change. */\n retryMs: number\n}\n\n/** Retry window for a probe that failed without naming the method unknown.\n * An outage heals, so a permanent entry would keep a recovered endpoint out of\n * the Jito list for the whole process. */\nexport const JITO_PROBE_RETRY_MS = 30_000\n\n/** Retry window for a bare HTTP 401/403. Longer than an outage's, shorter than\n * forever. The refusal never reached the JSON-RPC layer, so it proves nothing\n * about capability: a plan gate, a provider deploy and an allowlist entry\n * still propagating are indistinguishable here. Treating it as permanent meant\n * one transient 403 removed a working endpoint for the process lifetime;\n * treating it as a 30 s outage would re-probe a real plan gate on the\n * pre-submission latency path twice a minute, forever. */\nexport const JITO_PROBE_GATEWAY_RETRY_MS: number = 15 * 60_000\n\ntype JitoProbeRecord = JitoProbeResult & { at: number }\n\n/** Every answered probe, the negative ones included. Without them the unprobed\n * set never shrinks, and every bundle submission re-probes every non-Jito\n * endpoint at up to `PROBE_TIMEOUT_MS` each, on the latency path before\n * submission can start. */\nconst jitoProbes = new LruMap<JitoProbeRecord>(64)\n\n/** One rule for every outcome: a record is fresh until its own window closes.\n * `Infinity` covers the answers that cannot change. */\nconst isProbeFresh = (record: JitoProbeRecord | undefined): boolean =>\n record !== undefined && Date.now() - record.at < record.retryMs\n\n/** `@solana/kit` puts the JSON-RPC code on `context.__code`, not `error.code`.\n * The message is a last resort - a provider can reword it. An unrecognized\n * failure counts as `unreachable`, so a misread blames an outage. */\nconst JSON_RPC_METHOD_NOT_FOUND = -32601\n/** Non-standard; providers use it for \"your plan lacks this method\". */\nconst PROVIDER_PLAN_RESTRICTED = -32403\n\nconst CAPABILITY_CODES: number[] = [\n JSON_RPC_METHOD_NOT_FOUND,\n PROVIDER_PLAN_RESTRICTED,\n]\n\n/** A plan gate rejects before JSON-RPC, so no `-32601` arrives - Helius\n * answers with a bare 403. Unlike a JSON-RPC code this never reached the RPC\n * layer, so it is a gateway verdict rather than a capability answer, and it\n * earns a window instead of permanence. 429 and 5xx are absent: those take the\n * shorter outage window. */\nconst GATEWAY_REFUSAL_HTTP_STATUSES: number[] = [401, 403]\n\nconst readProbeFailure = (error: unknown): JitoProbeResult => {\n const candidate = error as\n | {\n code?: unknown\n context?: { __code?: unknown; statusCode?: unknown }\n }\n | undefined\n\n // kit's `__code` is 8100002 for every HTTP failure, so only a JSON-RPC code\n // that reached the body is meaningful.\n const rpcCode = candidate?.code ?? candidate?.context?.__code\n if (typeof rpcCode === 'number' && CAPABILITY_CODES.includes(rpcCode)) {\n // The server parsed the request and said it has no such method. Definitive.\n return { outcome: 'unsupported', retryMs: Number.POSITIVE_INFINITY }\n }\n\n const status = candidate?.context?.statusCode\n if (\n typeof status === 'number' &&\n GATEWAY_REFUSAL_HTTP_STATUSES.includes(status)\n ) {\n return { outcome: 'unreachable', retryMs: JITO_PROBE_GATEWAY_RETRY_MS }\n }\n\n const message = error instanceof Error ? error.message : String(error)\n return /method not found|method does not exist|-32601|method .* not supported|unsupported method|only available for business plans/i.test(\n message\n )\n ? { outcome: 'unsupported', retryMs: Number.POSITIVE_INFINITY }\n : { outcome: 'unreachable', retryMs: JITO_PROBE_RETRY_MS }\n}\n\n/**\n * Bounded because it runs upstream of `raceRpcs`, outside the confirmation\n * budget the SDK documents: an endpoint that accepts the connection and never\n * answers would otherwise stall bundle submission indefinitely.\n */\nconst PROBE_TIMEOUT_MS = 5_000\n\n/**\n * Probes with `getBundleStatuses` rather than `getTipAccounts`: it is the\n * method bundle confirmation actually uses, and providers such as Helius\n * support it without exposing `getTipAccounts`.\n */\nexport const probeJitoRpc = async (\n rpcUrl: string\n): Promise<JitoProbeResult> => {\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), PROBE_TIMEOUT_MS)\n try {\n const rpc = createJitoRpc(rpcUrl)\n await rpc\n .getBundleStatuses([PROBE_BUNDLE_ID])\n .send({ abortSignal: controller.signal })\n return { outcome: 'supported', retryMs: Number.POSITIVE_INFINITY }\n } catch (error) {\n return readProbeFailure(error)\n } finally {\n clearTimeout(timer)\n }\n}\n\n/**\n * Initializes Solana RPCs for all available RPC URLs if they haven't been cached yet.\n * @param client - The SDK client used to fetch RPC URLs.\n */\nconst ensureSolanaRpcs = async (client: SDKClient): Promise<string[]> => {\n const rpcUrls = await client.getRpcUrlsByChainId(ChainId.SOL)\n ensureSolanaRpcsFor(rpcUrls)\n return rpcUrls\n}\n\n/** Creates and caches a client for each URL that has none yet. */\nconst ensureSolanaRpcsFor = (rpcUrls: string[]): void => {\n for (const rpcUrl of rpcUrls) {\n if (!solanaRpcs.has(rpcUrl)) {\n solanaRpcs.set(rpcUrl, createSolanaRpc(rpcUrl))\n }\n }\n}\n\nconst cachedSolanaRpcs = (rpcUrls: string[]): SolanaRpcType[] =>\n rpcUrls\n .map((rpcUrl) => solanaRpcs.get(rpcUrl))\n .filter((rpc): rpc is SolanaRpcType => Boolean(rpc))\n\n/**\n * Probes each URL for Jito support and caches a client for every supported\n * one. The configured RPCs and the write RPCs share this cache.\n *\n * Every probe outcome is cached, so a non-Jito endpoint is probed once rather\n * than once per bundle submission. Each record carries its own retry window:\n * see `JITO_PROBE_RETRY_MS` and `JITO_PROBE_GATEWAY_RETRY_MS` for the two that\n * expire.\n * @param rpcUrls - The URLs to probe.\n * @returns - How many of the URLs are currently unreachable.\n */\nconst ensureJitoRpcsFor = async (rpcUrls: string[]): Promise<number> => {\n // Probed in parallel: serially, one slow endpoint delayed every URL behind\n // it before submission could start.\n const unprobed = rpcUrls.filter(\n (rpcUrl) => !isProbeFresh(jitoProbes.get(rpcUrl))\n )\n // Deduplicated per URL: without it two concurrent submissions each probe\n // every unprobed endpoint, and the slower probe writes last - a 5 s timeout\n // resolving `unreachable` used to overwrite a `supported` answer that landed\n // at 300 ms, evicting a verified-healthy endpoint.\n //\n // `withDedupe` drops its entry in a `.finally`, so this guarantees at most\n // one probe *in flight* per URL, not one probe per URL. The write below waits\n // for the whole batch, so a fast URL's record is unwritten while a slow\n // sibling is still running - up to `PROBE_TIMEOUT_MS` - and every caller\n // arriving in that window re-probes the fast URL. Repeats are therefore\n // bounded by arrivals, not by one.\n //\n // What this does rule out is the clobber: a second probe can only begin\n // after the first has settled, so it always carries the newer answer and\n // always writes second. Write order matches result recency, which is why no\n // timestamp comparison is needed here.\n const results = await Promise.all(\n unprobed.map((rpcUrl) =>\n withDedupe(() => probeJitoRpc(rpcUrl), { id: `jito-probe:${rpcUrl}` })\n )\n )\n\n results.forEach(({ outcome, retryMs }, index) => {\n const rpcUrl = unprobed[index]\n jitoProbes.set(rpcUrl, { outcome, retryMs, at: Date.now() })\n if (outcome === 'supported') {\n jitoRpcs.set(rpcUrl, createJitoRpc(rpcUrl))\n } else {\n // An endpoint that used to answer and now does not must leave the list,\n // or `getJitoRpcs` keeps handing out a client for it.\n jitoRpcs.delete(rpcUrl)\n }\n })\n\n // Counted over every URL, not only the freshly probed ones: a cached\n // `unreachable` still has to reach `sendAndConfirmBundle`, which uses the\n // count to choose between \"retry, likely temporary\" and \"supply a\n // Jito-capable URL\".\n return rpcUrls.filter(\n (rpcUrl) => jitoProbes.get(rpcUrl)?.outcome === 'unreachable'\n ).length\n}\n\n/**\n * Initializes and caches Jito RPCs for every configured Solana RPC URL.\n * @param client - The SDK client used to fetch RPC URLs.\n */\nconst ensureJitoRpcs = async (\n client: SDKClient\n): Promise<{ rpcUrls: string[]; unreachable: number }> => {\n const rpcUrls = await client.getRpcUrlsByChainId(ChainId.SOL)\n const unreachable = await ensureJitoRpcsFor(rpcUrls)\n return { rpcUrls, unreachable }\n}\n\nconst cachedJitoRpcs = (rpcUrls: string[]): JitoRpcType[] =>\n rpcUrls\n .map((rpcUrl) => jitoRpcs.get(rpcUrl))\n .filter((rpc): rpc is JitoRpcType => Boolean(rpc))\n\n/**\n * Wrapper around getting the Solana RPCs\n * @returns - Solana RPCs\n */\nexport const getSolanaRpcs = async (\n client: SDKClient\n): Promise<SolanaRpcType[]> => {\n return cachedSolanaRpcs(await ensureSolanaRpcs(client))\n}\n\n/**\n * Clients for the write RPCs (`rpcUrls[ChainId.SOL].write`). They only\n * send transactions; the one read they get is the Jito probe in\n * `getJitoCapableRpcs`.\n * @param rpcUrls - The write RPC URLs.\n * @returns - Solana RPCs to send transactions through.\n */\nexport const getSolanaWriteRpcs = (rpcUrls: string[]): SolanaRpcType[] => {\n ensureSolanaRpcsFor(rpcUrls)\n return cachedSolanaRpcs(rpcUrls)\n}\n\n/** `unreachable` counts endpoints whose probe failed without naming the\n * method unknown. Empty `rpcs` with a non-zero count is usually an outage, but\n * not always: a bare 401/403 lands here too, and that can be a plan gate the\n * integrator has to fix. `sendAndConfirmBundle`'s message names both, because\n * this count cannot tell them apart. */\nexport const getJitoRpcs = async (\n client: SDKClient\n): Promise<{ rpcs: JitoRpcType[]; unreachable: number }> => {\n const { rpcUrls, unreachable } = await ensureJitoRpcs(client)\n return { rpcs: cachedJitoRpcs(rpcUrls), unreachable }\n}\n\n/**\n * The given URLs that pass the Jito probe - the bundle or write RPCs of\n * `rpcUrls[ChainId.SOL]`. They share the probe cache with the read RPCs.\n * `unreachable` counts the URLs whose probe failed without an answer, as in\n * `getJitoRpcs`.\n * @param rpcUrls - The URLs to probe.\n * @returns - Jito RPCs to submit bundles through, and the unreachable count.\n */\nexport const getJitoCapableRpcs = async (\n rpcUrls: string[]\n): Promise<{ rpcs: JitoRpcType[]; unreachable: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,kBAAkB,OAAO;CACrB;AAChC;AAEA,MAAM,kBAAkB,YACtB,QACG,KAAK,WAAW,SAAS,IAAI,MAAM,CAAC,CAAC,CACrC,QAAQ,QAA4B,QAAQ,GAAG,CAAC;;;;;AAMrD,MAAa,gBAAgB,OAC3B,WAC6B;CAC7B,OAAO,iBAAiB,MAAM,iBAAiB,MAAM,CAAC;AACxD;;;;;;;;AASA,MAAa,sBAAsB,YAAuC;CACxE,oBAAoB,OAAO;CAC3B,OAAO,iBAAiB,OAAO;AACjC;;;;;;AAOA,MAAa,cAAc,OACzB,WAC0D;CAC1D,MAAM,EAAE,SAAS,gBAAgB,MAAM,eAAe,MAAM;CAC5D,OAAO;EAAE,MAAM,eAAe,OAAO;EAAG;CAAY;AACtD;;;;;;;;;AAUA,MAAa,qBAAqB,OAChC,YAC0D;CAC1D,MAAM,cAAc,MAAM,kBAAkB,OAAO;CACnD,OAAO;EAAE,MAAM,eAAe,OAAO;EAAG;CAAY;AACtD"}
|
package/dist/esm/version.d.ts
CHANGED
package/dist/esm/version.js
CHANGED
package/dist/esm/version.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","names":[],"sources":["../../src/version.ts"],"sourcesContent":["export const name = '@lifi/sdk-provider-solana'\nexport const version = '4.
|
|
1
|
+
{"version":3,"file":"version.js","names":[],"sources":["../../src/version.ts"],"sourcesContent":["export const name = '@lifi/sdk-provider-solana'\nexport const version = '4.3.0'\n"],"mappings":";AAAA,MAAa,OAAO;AACpB,MAAa,UAAU"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lifi/sdk-provider-solana",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"description": "LI.FI Solana SDK Provider for Any-to-Any Cross-Chain-Swap",
|
|
5
5
|
"homepage": "https://github.com/lifinance/sdk",
|
|
6
6
|
"bugs": {
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"./package.json": "./package.json"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@lifi/sdk": "4.
|
|
31
|
+
"@lifi/sdk": "4.9.0",
|
|
32
32
|
"@solana/kit": "^8.3.0",
|
|
33
33
|
"@solana/wallet-standard-features": "^1.5.0",
|
|
34
34
|
"@wallet-standard/base": "^1.1.1"
|
|
@@ -57,7 +57,7 @@ const getSolanaBalanceDefault = async (
|
|
|
57
57
|
callSolanaRpcsWithRetry(client, (rpc) =>
|
|
58
58
|
rpc.getBalance(accountAddress, { commitment: 'confirmed' }).send()
|
|
59
59
|
),
|
|
60
|
-
{ id: `${getSolanaBalanceDefault.name}.getBalance` }
|
|
60
|
+
{ id: `${getSolanaBalanceDefault.name}.getBalance.${walletAddress}` }
|
|
61
61
|
),
|
|
62
62
|
withDedupe(
|
|
63
63
|
() =>
|
|
@@ -76,7 +76,7 @@ const getSolanaBalanceDefault = async (
|
|
|
76
76
|
.send()
|
|
77
77
|
),
|
|
78
78
|
{
|
|
79
|
-
id: `${getSolanaBalanceDefault.name}.getTokenAccountsByOwner.${TokenProgramId}`,
|
|
79
|
+
id: `${getSolanaBalanceDefault.name}.getTokenAccountsByOwner.${TokenProgramId}.${walletAddress}`,
|
|
80
80
|
}
|
|
81
81
|
),
|
|
82
82
|
withDedupe(
|
|
@@ -96,7 +96,7 @@ const getSolanaBalanceDefault = async (
|
|
|
96
96
|
.send()
|
|
97
97
|
),
|
|
98
98
|
{
|
|
99
|
-
id: `${getSolanaBalanceDefault.name}.getTokenAccountsByOwner.${Token2022ProgramId}`,
|
|
99
|
+
id: `${getSolanaBalanceDefault.name}.getTokenAccountsByOwner.${Token2022ProgramId}.${walletAddress}`,
|
|
100
100
|
}
|
|
101
101
|
),
|
|
102
102
|
])
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LiFiErrorCode, RPCError, type SDKClient } from '@lifi/sdk'
|
|
1
|
+
import { ChainId, LiFiErrorCode, RPCError, type SDKClient } from '@lifi/sdk'
|
|
2
2
|
import { getBase64EncodedWireTransaction, type Transaction } from '@solana/kit'
|
|
3
3
|
import {
|
|
4
4
|
type BundleConfirmation,
|
|
@@ -6,7 +6,8 @@ import {
|
|
|
6
6
|
} from '../confirmation/confirmBundle.js'
|
|
7
7
|
import { BRANCH_TIMEOUT_MS } from '../confirmation/createConfirmationDeadline.js'
|
|
8
8
|
import { type RaceResult, raceRpcs } from '../confirmation/raceRpcs.js'
|
|
9
|
-
import { getJitoRpcs } from '../rpc/registry.js'
|
|
9
|
+
import { getJitoCapableRpcs, getJitoRpcs } from '../rpc/registry.js'
|
|
10
|
+
import type { JitoRpcType } from '../rpc/types.js'
|
|
10
11
|
import { getTransactionLifetime } from '../utils/getTransactionLifetime.js'
|
|
11
12
|
|
|
12
13
|
/**
|
|
@@ -20,6 +21,11 @@ import { getTransactionLifetime } from '../utils/getTransactionLifetime.js'
|
|
|
20
21
|
* `sendBundle` is handed to `confirmBundle` rather than awaited here, so the
|
|
21
22
|
* deadline starts on the same clock as `BRANCH_TIMEOUT_MS` instead of after
|
|
22
23
|
* the submission returns.
|
|
24
|
+
*
|
|
25
|
+
* When the client has Solana bundle or write RPCs (`rpcUrls[ChainId.SOL]`),
|
|
26
|
+
* the bundle is submitted once through those that pass the Jito probe - the
|
|
27
|
+
* bundle list first, else the write list - and the read Jito RPCs only poll.
|
|
28
|
+
* With either list set, bundles never go to the read RPCs.
|
|
23
29
|
*/
|
|
24
30
|
export async function sendAndConfirmBundle(
|
|
25
31
|
client: SDKClient,
|
|
@@ -29,7 +35,58 @@ export async function sendAndConfirmBundle(
|
|
|
29
35
|
onBroadcast?: () => void
|
|
30
36
|
}
|
|
31
37
|
): Promise<RaceResult<BundleConfirmation>> {
|
|
32
|
-
|
|
38
|
+
// With a bundle or write list set, bundles go only to those: the bundle
|
|
39
|
+
// RPCs that pass the Jito probe, else the write RPCs that do. The write list
|
|
40
|
+
// is probed only when it is needed. With neither list, each branch submits
|
|
41
|
+
// through its own read Jito RPC, as before.
|
|
42
|
+
const [bundleUrls = [], writeUrls = []] = await Promise.all([
|
|
43
|
+
client.getBundleRpcUrlsByChainId?.(ChainId.SOL),
|
|
44
|
+
client.getWriteRpcUrlsByChainId?.(ChainId.SOL),
|
|
45
|
+
])
|
|
46
|
+
const noSubmitRpcs = { rpcs: [] as JitoRpcType[], unreachable: 0 }
|
|
47
|
+
const findSubmitRpcs = async (): Promise<typeof noSubmitRpcs> => {
|
|
48
|
+
const bundle = bundleUrls.length
|
|
49
|
+
? await getJitoCapableRpcs(bundleUrls)
|
|
50
|
+
: noSubmitRpcs
|
|
51
|
+
if (bundle.rpcs.length || !writeUrls.length) {
|
|
52
|
+
return bundle
|
|
53
|
+
}
|
|
54
|
+
const write = await getJitoCapableRpcs(writeUrls)
|
|
55
|
+
return {
|
|
56
|
+
rpcs: write.rpcs,
|
|
57
|
+
unreachable: bundle.unreachable + write.unreachable,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const [
|
|
61
|
+
{ rpcs: jitoRpcs, unreachable },
|
|
62
|
+
{ rpcs: submitRpcs, unreachable: submitUnreachable },
|
|
63
|
+
] = await Promise.all([getJitoRpcs(client), findSubmitRpcs()])
|
|
64
|
+
|
|
65
|
+
if ((bundleUrls.length || writeUrls.length) && !submitRpcs.length) {
|
|
66
|
+
const lists = [
|
|
67
|
+
bundleUrls.length && '`rpcUrls[ChainId.SOL].bundle`',
|
|
68
|
+
writeUrls.length && '`rpcUrls[ChainId.SOL].write`',
|
|
69
|
+
]
|
|
70
|
+
.filter(Boolean)
|
|
71
|
+
.join(' or ')
|
|
72
|
+
throw new RPCError(
|
|
73
|
+
LiFiErrorCode.RpcUnavailable,
|
|
74
|
+
[
|
|
75
|
+
`Jito bundle required, but no URL in ${lists} passed the Jito capability probe.`,
|
|
76
|
+
submitUnreachable > 0
|
|
77
|
+
? 'The probe got no answer from some of them. This is usually temporary - retry. If it persists, the endpoint may refuse `sendBundle` for your plan.'
|
|
78
|
+
: 'They do not support `sendBundle`: add a Jito-capable URL to `rpcUrls[ChainId.SOL].bundle`.',
|
|
79
|
+
'Bundles never go to the read RPCs while `bundle` or `write` is set.',
|
|
80
|
+
// Only a definite gap: a read RPC that did not answer the probe may
|
|
81
|
+
// support bundles, and the next attempt reports that case itself.
|
|
82
|
+
jitoRpcs.length === 0 &&
|
|
83
|
+
unreachable === 0 &&
|
|
84
|
+
'No read RPC supports `getBundleStatuses` to confirm a bundle either: also add a Jito-capable URL to `rpcUrls[ChainId.SOL].read`.',
|
|
85
|
+
]
|
|
86
|
+
.filter(Boolean)
|
|
87
|
+
.join(' ')
|
|
88
|
+
)
|
|
89
|
+
}
|
|
33
90
|
|
|
34
91
|
// Named here, where the emptiness is known: racing zero RPCs would surface
|
|
35
92
|
// as a bare `rpc-unavailable`, indistinguishable from a total outage. The
|
|
@@ -46,7 +103,9 @@ export async function sendAndConfirmBundle(
|
|
|
46
103
|
LiFiErrorCode.RpcUnavailable,
|
|
47
104
|
unreachable > 0
|
|
48
105
|
? `Jito bundle required, but the capability probe failed against ${unreachable} configured Solana RPC(s). This is usually temporary - retry. If it persists, the endpoint may refuse \`sendBundle\` for your plan.`
|
|
49
|
-
:
|
|
106
|
+
: submitRpcs.length > 0
|
|
107
|
+
? 'Jito bundle required. A bundle or write RPC can submit it, but no read RPC supports `getBundleStatuses` to confirm it. Add a Jito-capable URL to `rpcUrls[ChainId.SOL].read`.'
|
|
108
|
+
: 'Jito bundle required, but no configured Solana RPC supports `sendBundle`. Supply a Jito-capable URL via the `rpcUrls` client config option.'
|
|
50
109
|
)
|
|
51
110
|
}
|
|
52
111
|
|
|
@@ -83,6 +142,23 @@ export async function sendAndConfirmBundle(
|
|
|
83
142
|
signedTransactions.map((transaction) => getTransactionLifetime(transaction))
|
|
84
143
|
)
|
|
85
144
|
|
|
145
|
+
// One submission for every polling branch: the bundle id is derived from
|
|
146
|
+
// the transactions, so each branch polls the same bundle. Started by the
|
|
147
|
+
// first branch that asks, so its deadline already runs.
|
|
148
|
+
let submission: Promise<string> | undefined
|
|
149
|
+
const submitOnce = (signal: AbortSignal): Promise<string> => {
|
|
150
|
+
submission ??= Promise.any(
|
|
151
|
+
submitRpcs.map((rpc) =>
|
|
152
|
+
rpc.sendBundle(serializedTransactions).send({ abortSignal: signal })
|
|
153
|
+
)
|
|
154
|
+
).catch((error: unknown) => {
|
|
155
|
+
// Surface a refusal the way a single configured RPC would, not as a
|
|
156
|
+
// nested AggregateError inside the race's own error list.
|
|
157
|
+
throw error instanceof AggregateError ? error.errors[0] : error
|
|
158
|
+
})
|
|
159
|
+
return submission
|
|
160
|
+
}
|
|
161
|
+
|
|
86
162
|
return raceRpcs(
|
|
87
163
|
jitoRpcs,
|
|
88
164
|
(rpc, signal) =>
|
|
@@ -91,7 +167,11 @@ export async function sendAndConfirmBundle(
|
|
|
91
167
|
signal,
|
|
92
168
|
lifetimes,
|
|
93
169
|
send: () =>
|
|
94
|
-
|
|
170
|
+
submitRpcs.length
|
|
171
|
+
? submitOnce(signal)
|
|
172
|
+
: rpc
|
|
173
|
+
.sendBundle(serializedTransactions)
|
|
174
|
+
.send({ abortSignal: signal }),
|
|
95
175
|
onBroadcast: reportBroadcast,
|
|
96
176
|
}),
|
|
97
177
|
{ timeoutMs: BRANCH_TIMEOUT_MS }
|
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { ChainId, type SDKClient } from '@lifi/sdk'
|
|
2
2
|
import {
|
|
3
3
|
type Commitment,
|
|
4
4
|
getBase64EncodedWireTransaction,
|
|
5
5
|
getSignatureFromTransaction,
|
|
6
6
|
type Transaction,
|
|
7
7
|
} from '@solana/kit'
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
confirmSignature,
|
|
10
|
+
RESEND_INTERVAL_MS,
|
|
11
|
+
} from '../confirmation/confirmSignature.js'
|
|
9
12
|
import { BRANCH_TIMEOUT_MS } from '../confirmation/createConfirmationDeadline.js'
|
|
10
13
|
import { type RaceResult, raceRpcs } from '../confirmation/raceRpcs.js'
|
|
11
14
|
import type { SignatureStatus } from '../confirmation/types.js'
|
|
12
|
-
import { getSolanaRpcs } from '../rpc/registry.js'
|
|
15
|
+
import { getSolanaRpcs, getSolanaWriteRpcs } from '../rpc/registry.js'
|
|
13
16
|
import type { SolanaRpcType } from '../rpc/types.js'
|
|
14
17
|
import { getTransactionLifetime } from '../utils/getTransactionLifetime.js'
|
|
15
18
|
|
|
@@ -17,6 +20,9 @@ import { getTransactionLifetime } from '../utils/getTransactionLifetime.js'
|
|
|
17
20
|
* Sends a Solana transaction to every configured RPC and returns as soon as
|
|
18
21
|
* one of them confirms it.
|
|
19
22
|
*
|
|
23
|
+
* When the client has Solana write RPCs (`rpcUrls[ChainId.SOL].write`), the
|
|
24
|
+
* transaction is sent only through those, and the read RPCs only confirm it.
|
|
25
|
+
*
|
|
20
26
|
* The polling horizon comes from the signed transaction's own blockhash and a
|
|
21
27
|
* wall-clock ceiling. It deliberately never comes from `getBlockHeight`: at
|
|
22
28
|
* least one endpoint in the default LI.FI set answers that call with the slot
|
|
@@ -30,7 +36,10 @@ export async function sendAndConfirmTransaction(
|
|
|
30
36
|
onBroadcast?: () => void
|
|
31
37
|
}
|
|
32
38
|
): Promise<RaceResult<SignatureStatus>> {
|
|
33
|
-
const solanaRpcs = await
|
|
39
|
+
const [solanaRpcs, writeRpcUrls = []] = await Promise.all([
|
|
40
|
+
getSolanaRpcs(client),
|
|
41
|
+
client.getWriteRpcUrlsByChainId?.(ChainId.SOL),
|
|
42
|
+
])
|
|
34
43
|
|
|
35
44
|
let broadcastReported = false
|
|
36
45
|
// Distinct from `broadcastReported`, which records whether the integrator
|
|
@@ -76,7 +85,7 @@ export async function sendAndConfirmTransaction(
|
|
|
76
85
|
encoding: 'base64' as const,
|
|
77
86
|
}
|
|
78
87
|
|
|
79
|
-
const
|
|
88
|
+
const send = async (
|
|
80
89
|
rpc: SolanaRpcType,
|
|
81
90
|
signal: AbortSignal
|
|
82
91
|
): Promise<void> => {
|
|
@@ -85,19 +94,100 @@ export async function sendAndConfirmTransaction(
|
|
|
85
94
|
.send({ abortSignal: signal })
|
|
86
95
|
}
|
|
87
96
|
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
97
|
+
const writeRpcs = writeRpcUrls.length
|
|
98
|
+
? getSolanaWriteRpcs(writeRpcUrls)
|
|
99
|
+
: undefined
|
|
100
|
+
|
|
101
|
+
// Sends to the write RPCs belong to the whole call, not to one branch:
|
|
102
|
+
// other branches wait on the same send, so the branch that started it must
|
|
103
|
+
// not cancel it by ending. They end when the race does.
|
|
104
|
+
const writes = new AbortController()
|
|
105
|
+
|
|
106
|
+
// Every confirmation branch resends about once a second. Branches share one
|
|
107
|
+
// send to the write RPCs per interval, so each write RPC sees the same rate
|
|
108
|
+
// it would as a configured RPC, however many configured RPCs are polling.
|
|
109
|
+
let lastWrite: { at: number; sent: Promise<void> } | undefined
|
|
110
|
+
// A write RPC that has not answered its last send gets no new one, so a
|
|
111
|
+
// hung endpoint holds one open request, not one per interval.
|
|
112
|
+
const openSends = new Set<SolanaRpcType>()
|
|
113
|
+
const sendToWriteRpc = async (rpc: SolanaRpcType): Promise<void> => {
|
|
114
|
+
openSends.add(rpc)
|
|
115
|
+
try {
|
|
116
|
+
await send(rpc, writes.signal)
|
|
117
|
+
} finally {
|
|
118
|
+
openSends.delete(rpc)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const sendToWriteRpcs = (rpcs: SolanaRpcType[]): Promise<void> => {
|
|
122
|
+
const now = Date.now()
|
|
123
|
+
if (lastWrite && now - lastWrite.at < RESEND_INTERVAL_MS) {
|
|
124
|
+
return lastWrite.sent
|
|
125
|
+
}
|
|
126
|
+
// With every write RPC busy this rejects at once, and the branch polls.
|
|
127
|
+
const ready = rpcs.filter((rpc) => !openSends.has(rpc))
|
|
128
|
+
// Accepted as soon as one write RPC accepts it.
|
|
129
|
+
const sent = Promise.any(ready.map(sendToWriteRpc)).then(() => undefined)
|
|
130
|
+
// Recorded here, not only by the branches: a branch stops waiting after
|
|
131
|
+
// one interval (below), and an acceptance after that must still count.
|
|
132
|
+
// Skipped once the race is over, so a late acceptance cannot regress an
|
|
133
|
+
// action status the wait task already finalized.
|
|
134
|
+
sent.then(
|
|
135
|
+
() => {
|
|
136
|
+
if (!writes.signal.aborted) {
|
|
137
|
+
reportBroadcast()
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
() => {}
|
|
141
|
+
)
|
|
142
|
+
lastWrite = { at: now, sent }
|
|
143
|
+
return sent
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// `confirmSignature` awaits its first send before it starts polling. Every
|
|
147
|
+
// branch waits on the same shared send, so a write RPC that hangs would hold
|
|
148
|
+
// up polling on all of them - even for a transaction that already landed.
|
|
149
|
+
// A branch waits at most one resend interval, then polls either way.
|
|
150
|
+
const waitForWrite = (sent: Promise<void>): Promise<void> =>
|
|
151
|
+
new Promise((resolve, reject) => {
|
|
152
|
+
const timer = setTimeout(
|
|
153
|
+
() =>
|
|
154
|
+
reject(new Error('No write RPC accepted the transaction in time.')),
|
|
155
|
+
RESEND_INTERVAL_MS
|
|
156
|
+
)
|
|
157
|
+
sent.then(
|
|
158
|
+
() => {
|
|
159
|
+
clearTimeout(timer)
|
|
160
|
+
resolve()
|
|
161
|
+
},
|
|
162
|
+
(error: unknown) => {
|
|
163
|
+
clearTimeout(timer)
|
|
164
|
+
reject(error)
|
|
165
|
+
}
|
|
166
|
+
)
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
const resend = writeRpcs
|
|
170
|
+
? (): Promise<void> => waitForWrite(sendToWriteRpcs(writeRpcs))
|
|
171
|
+
: send
|
|
172
|
+
|
|
173
|
+
let result: RaceResult<SignatureStatus>
|
|
174
|
+
try {
|
|
175
|
+
result = await raceRpcs(
|
|
176
|
+
solanaRpcs,
|
|
177
|
+
(rpc, signal) =>
|
|
178
|
+
confirmSignature({
|
|
179
|
+
rpc,
|
|
180
|
+
signal,
|
|
181
|
+
signature: txSignature,
|
|
182
|
+
lifetimes: [lifetime],
|
|
183
|
+
resend,
|
|
184
|
+
onBroadcast: reportBroadcast,
|
|
185
|
+
}),
|
|
186
|
+
{ timeoutMs: BRANCH_TIMEOUT_MS }
|
|
187
|
+
)
|
|
188
|
+
} finally {
|
|
189
|
+
writes.abort()
|
|
190
|
+
}
|
|
101
191
|
|
|
102
192
|
// Only this scope knows whether ANY branch accepted the send. A branch that
|
|
103
193
|
// polls to its deadline reports `not-confirmed` regardless - correct per
|
|
@@ -10,7 +10,8 @@ import {
|
|
|
10
10
|
type SignatureStatus,
|
|
11
11
|
} from './types.js'
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
/** How often each confirmation branch resends the transaction. */
|
|
14
|
+
export const RESEND_INTERVAL_MS = 1000
|
|
14
15
|
/**
|
|
15
16
|
* How often this poller re-reads `getSignatureStatuses`. The default Solana
|
|
16
17
|
* RPCs tolerate 2.5 req/s of status reads; the Jito bundle poller runs slower
|