@lifi/sdk-provider-solana 4.0.6 → 4.1.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.
Files changed (145) hide show
  1. package/CHANGELOG.md +116 -0
  2. package/dist/cjs/actions/sendAndConfirmBundle.d.ts +18 -25
  3. package/dist/cjs/actions/sendAndConfirmBundle.js +33 -54
  4. package/dist/cjs/actions/sendAndConfirmBundle.js.map +1 -1
  5. package/dist/cjs/actions/sendAndConfirmTransaction.d.ts +14 -22
  6. package/dist/cjs/actions/sendAndConfirmTransaction.js +38 -55
  7. package/dist/cjs/actions/sendAndConfirmTransaction.js.map +1 -1
  8. package/dist/cjs/confirmation/abortableSleep.d.ts +19 -0
  9. package/dist/cjs/confirmation/abortableSleep.js +32 -0
  10. package/dist/cjs/confirmation/abortableSleep.js.map +1 -0
  11. package/dist/cjs/confirmation/confirmBundle.d.ts +43 -0
  12. package/dist/cjs/confirmation/confirmBundle.js +57 -0
  13. package/dist/cjs/confirmation/confirmBundle.js.map +1 -0
  14. package/dist/cjs/confirmation/confirmSignature.d.ts +29 -0
  15. package/dist/cjs/confirmation/confirmSignature.js +66 -0
  16. package/dist/cjs/confirmation/confirmSignature.js.map +1 -0
  17. package/dist/cjs/confirmation/createConfirmationDeadline.d.ts +51 -0
  18. package/dist/cjs/confirmation/createConfirmationDeadline.js +101 -0
  19. package/dist/cjs/confirmation/createConfirmationDeadline.js.map +1 -0
  20. package/dist/cjs/confirmation/pollUntilDeadline.d.ts +58 -0
  21. package/dist/cjs/confirmation/pollUntilDeadline.js +97 -0
  22. package/dist/cjs/confirmation/pollUntilDeadline.js.map +1 -0
  23. package/dist/cjs/confirmation/raceRpcs.d.ts +26 -0
  24. package/dist/cjs/confirmation/raceRpcs.js +93 -0
  25. package/dist/cjs/confirmation/raceRpcs.js.map +1 -0
  26. package/dist/cjs/confirmation/types.d.ts +18 -0
  27. package/dist/cjs/confirmation/types.js +9 -0
  28. package/dist/cjs/confirmation/types.js.map +1 -0
  29. package/dist/cjs/core/tasks/SolanaJitoWaitForTransactionTask.js +30 -9
  30. package/dist/cjs/core/tasks/SolanaJitoWaitForTransactionTask.js.map +1 -1
  31. package/dist/cjs/core/tasks/SolanaSignAndExecuteTask.js +5 -1
  32. package/dist/cjs/core/tasks/SolanaSignAndExecuteTask.js.map +1 -1
  33. package/dist/cjs/core/tasks/SolanaStandardWaitForTransactionTask.js +20 -7
  34. package/dist/cjs/core/tasks/SolanaStandardWaitForTransactionTask.js.map +1 -1
  35. package/dist/cjs/core/tasks/readSignature.d.ts +17 -0
  36. package/dist/cjs/core/tasks/readSignature.js +26 -0
  37. package/dist/cjs/core/tasks/readSignature.js.map +1 -0
  38. package/dist/cjs/core/tasks/unwrapConfirmation.d.ts +25 -0
  39. package/dist/cjs/core/tasks/unwrapConfirmation.js +24 -0
  40. package/dist/cjs/core/tasks/unwrapConfirmation.js.map +1 -0
  41. package/dist/cjs/rpc/jito/api/getBundleStatuses.d.ts +4 -2
  42. package/dist/cjs/rpc/registry.d.ts +37 -11
  43. package/dist/cjs/rpc/registry.js +103 -18
  44. package/dist/cjs/rpc/registry.js.map +1 -1
  45. package/dist/cjs/rpc/utils.js +1 -1
  46. package/dist/cjs/rpc/utils.js.map +1 -1
  47. package/dist/cjs/utils/KeypairWalletAdapter.js +10 -4
  48. package/dist/cjs/utils/KeypairWalletAdapter.js.map +1 -1
  49. package/dist/cjs/utils/getTransactionLifetime.d.ts +21 -0
  50. package/dist/cjs/utils/getTransactionLifetime.js +28 -0
  51. package/dist/cjs/utils/getTransactionLifetime.js.map +1 -0
  52. package/dist/cjs/version.d.ts +1 -1
  53. package/dist/cjs/version.js +1 -1
  54. package/dist/cjs/version.js.map +1 -1
  55. package/dist/esm/actions/sendAndConfirmBundle.d.ts +18 -25
  56. package/dist/esm/actions/sendAndConfirmBundle.d.ts.map +1 -1
  57. package/dist/esm/actions/sendAndConfirmBundle.js +34 -55
  58. package/dist/esm/actions/sendAndConfirmBundle.js.map +1 -1
  59. package/dist/esm/actions/sendAndConfirmTransaction.d.ts +14 -22
  60. package/dist/esm/actions/sendAndConfirmTransaction.d.ts.map +1 -1
  61. package/dist/esm/actions/sendAndConfirmTransaction.js +38 -55
  62. package/dist/esm/actions/sendAndConfirmTransaction.js.map +1 -1
  63. package/dist/esm/confirmation/abortableSleep.d.ts +19 -0
  64. package/dist/esm/confirmation/abortableSleep.d.ts.map +1 -0
  65. package/dist/esm/confirmation/abortableSleep.js +31 -0
  66. package/dist/esm/confirmation/abortableSleep.js.map +1 -0
  67. package/dist/esm/confirmation/confirmBundle.d.ts +43 -0
  68. package/dist/esm/confirmation/confirmBundle.d.ts.map +1 -0
  69. package/dist/esm/confirmation/confirmBundle.js +55 -0
  70. package/dist/esm/confirmation/confirmBundle.js.map +1 -0
  71. package/dist/esm/confirmation/confirmSignature.d.ts +29 -0
  72. package/dist/esm/confirmation/confirmSignature.d.ts.map +1 -0
  73. package/dist/esm/confirmation/confirmSignature.js +64 -0
  74. package/dist/esm/confirmation/confirmSignature.js.map +1 -0
  75. package/dist/esm/confirmation/createConfirmationDeadline.d.ts +51 -0
  76. package/dist/esm/confirmation/createConfirmationDeadline.d.ts.map +1 -0
  77. package/dist/esm/confirmation/createConfirmationDeadline.js +93 -0
  78. package/dist/esm/confirmation/createConfirmationDeadline.js.map +1 -0
  79. package/dist/esm/confirmation/pollUntilDeadline.d.ts +58 -0
  80. package/dist/esm/confirmation/pollUntilDeadline.d.ts.map +1 -0
  81. package/dist/esm/confirmation/pollUntilDeadline.js +93 -0
  82. package/dist/esm/confirmation/pollUntilDeadline.js.map +1 -0
  83. package/dist/esm/confirmation/raceRpcs.d.ts +26 -0
  84. package/dist/esm/confirmation/raceRpcs.d.ts.map +1 -0
  85. package/dist/esm/confirmation/raceRpcs.js +92 -0
  86. package/dist/esm/confirmation/raceRpcs.js.map +1 -0
  87. package/dist/esm/confirmation/types.d.ts +18 -0
  88. package/dist/esm/confirmation/types.d.ts.map +1 -0
  89. package/dist/esm/confirmation/types.js +8 -0
  90. package/dist/esm/confirmation/types.js.map +1 -0
  91. package/dist/esm/core/tasks/SolanaJitoWaitForTransactionTask.d.ts.map +1 -1
  92. package/dist/esm/core/tasks/SolanaJitoWaitForTransactionTask.js +30 -9
  93. package/dist/esm/core/tasks/SolanaJitoWaitForTransactionTask.js.map +1 -1
  94. package/dist/esm/core/tasks/SolanaSignAndExecuteTask.js +5 -1
  95. package/dist/esm/core/tasks/SolanaSignAndExecuteTask.js.map +1 -1
  96. package/dist/esm/core/tasks/SolanaStandardWaitForTransactionTask.d.ts.map +1 -1
  97. package/dist/esm/core/tasks/SolanaStandardWaitForTransactionTask.js +20 -7
  98. package/dist/esm/core/tasks/SolanaStandardWaitForTransactionTask.js.map +1 -1
  99. package/dist/esm/core/tasks/readSignature.d.ts +17 -0
  100. package/dist/esm/core/tasks/readSignature.d.ts.map +1 -0
  101. package/dist/esm/core/tasks/readSignature.js +25 -0
  102. package/dist/esm/core/tasks/readSignature.js.map +1 -0
  103. package/dist/esm/core/tasks/unwrapConfirmation.d.ts +25 -0
  104. package/dist/esm/core/tasks/unwrapConfirmation.d.ts.map +1 -0
  105. package/dist/esm/core/tasks/unwrapConfirmation.js +23 -0
  106. package/dist/esm/core/tasks/unwrapConfirmation.js.map +1 -0
  107. package/dist/esm/rpc/jito/api/getBundleStatuses.d.ts +4 -2
  108. package/dist/esm/rpc/jito/api/getBundleStatuses.d.ts.map +1 -1
  109. package/dist/esm/rpc/registry.d.ts +37 -11
  110. package/dist/esm/rpc/registry.d.ts.map +1 -1
  111. package/dist/esm/rpc/registry.js +102 -19
  112. package/dist/esm/rpc/registry.js.map +1 -1
  113. package/dist/esm/rpc/utils.js +1 -1
  114. package/dist/esm/rpc/utils.js.map +1 -1
  115. package/dist/esm/utils/KeypairWalletAdapter.d.ts.map +1 -1
  116. package/dist/esm/utils/KeypairWalletAdapter.js +11 -5
  117. package/dist/esm/utils/KeypairWalletAdapter.js.map +1 -1
  118. package/dist/esm/utils/getTransactionLifetime.d.ts +21 -0
  119. package/dist/esm/utils/getTransactionLifetime.d.ts.map +1 -0
  120. package/dist/esm/utils/getTransactionLifetime.js +27 -0
  121. package/dist/esm/utils/getTransactionLifetime.js.map +1 -0
  122. package/dist/esm/version.d.ts +1 -1
  123. package/dist/esm/version.js +1 -1
  124. package/dist/esm/version.js.map +1 -1
  125. package/package.json +4 -4
  126. package/src/actions/sendAndConfirmBundle.ts +82 -142
  127. package/src/actions/sendAndConfirmTransaction.ts +82 -134
  128. package/src/confirmation/abortableSleep.ts +30 -0
  129. package/src/confirmation/confirmBundle.ts +121 -0
  130. package/src/confirmation/confirmSignature.ts +113 -0
  131. package/src/confirmation/createConfirmationDeadline.ts +174 -0
  132. package/src/confirmation/pollUntilDeadline.ts +182 -0
  133. package/src/confirmation/raceRpcs.ts +135 -0
  134. package/src/confirmation/types.ts +23 -0
  135. package/src/core/tasks/SolanaJitoWaitForTransactionTask.ts +49 -16
  136. package/src/core/tasks/SolanaSignAndExecuteTask.ts +15 -0
  137. package/src/core/tasks/SolanaStandardWaitForTransactionTask.ts +28 -17
  138. package/src/core/tasks/readSignature.ts +29 -0
  139. package/src/core/tasks/unwrapConfirmation.ts +59 -0
  140. package/src/rpc/jito/api/getBundleStatuses.ts +4 -2
  141. package/src/rpc/registry.ts +183 -25
  142. package/src/rpc/utils.ts +5 -2
  143. package/src/utils/KeypairWalletAdapter.ts +34 -9
  144. package/src/utils/getTransactionLifetime.ts +38 -0
  145. package/src/version.ts +1 -1
@@ -0,0 +1,27 @@
1
+ import { getCompiledTransactionMessageDecoder, getTransactionLifetimeConstraintFromCompiledTransactionMessage } from "@solana/kit";
2
+ //#region src/utils/getTransactionLifetime.ts
3
+ const decoder = getCompiledTransactionMessageDecoder();
4
+ /**
5
+ * Decoding yields `{ messageBytes, signatures }` alone, so the lifetime is
6
+ * recovered from the compiled message.
7
+ *
8
+ * Never throws: anything undecodable yields `unknown`, degrading confirmation
9
+ * to the wall-clock ceiling rather than aborting the send.
10
+ */
11
+ async function getTransactionLifetime(transaction) {
12
+ try {
13
+ const compiledMessage = decoder.decode(transaction.messageBytes);
14
+ const constraint = await getTransactionLifetimeConstraintFromCompiledTransactionMessage(compiledMessage);
15
+ if ("blockhash" in constraint) return {
16
+ kind: "blockhash",
17
+ blockhash: constraint.blockhash
18
+ };
19
+ return { kind: "nonce" };
20
+ } catch (_) {
21
+ return { kind: "unknown" };
22
+ }
23
+ }
24
+ //#endregion
25
+ export { getTransactionLifetime };
26
+
27
+ //# sourceMappingURL=getTransactionLifetime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getTransactionLifetime.js","names":[],"sources":["../../../src/utils/getTransactionLifetime.ts"],"sourcesContent":["import {\n type Blockhash,\n getCompiledTransactionMessageDecoder,\n getTransactionLifetimeConstraintFromCompiledTransactionMessage,\n type Transaction,\n} from '@solana/kit'\n\nexport type TransactionLifetime =\n | { kind: 'blockhash'; blockhash: Blockhash }\n | { kind: 'nonce' }\n | { kind: 'unknown' }\n\nconst decoder = getCompiledTransactionMessageDecoder()\n\n/**\n * Decoding yields `{ messageBytes, signatures }` alone, so the lifetime is\n * recovered from the compiled message.\n *\n * Never throws: anything undecodable yields `unknown`, degrading confirmation\n * to the wall-clock ceiling rather than aborting the send.\n */\nexport async function getTransactionLifetime(\n transaction: Transaction\n): Promise<TransactionLifetime> {\n try {\n const compiledMessage = decoder.decode(transaction.messageBytes)\n const constraint =\n await getTransactionLifetimeConstraintFromCompiledTransactionMessage(\n compiledMessage\n )\n if ('blockhash' in constraint) {\n return { kind: 'blockhash', blockhash: constraint.blockhash }\n }\n return { kind: 'nonce' }\n } catch (_) {\n return { kind: 'unknown' }\n }\n}\n"],"mappings":";;AAYA,MAAM,UAAU,qCAAqC;;;;;;;;AASrD,eAAsB,uBACpB,aAC8B;CAC9B,IAAI;EACF,MAAM,kBAAkB,QAAQ,OAAO,YAAY,YAAY;EAC/D,MAAM,aACJ,MAAM,+DACJ,eACF;EACF,IAAI,eAAe,YACjB,OAAO;GAAE,MAAM;GAAa,WAAW,WAAW;EAAU;EAE9D,OAAO,EAAE,MAAM,QAAQ;CACzB,SAAS,GAAG;EACV,OAAO,EAAE,MAAM,UAAU;CAC3B;AACF"}
@@ -1,6 +1,6 @@
1
1
  //#region src/version.d.ts
2
2
  declare const name = "@lifi/sdk-provider-solana";
3
- declare const version = "4.0.6";
3
+ declare const version = "4.1.0";
4
4
  //#endregion
5
5
  export { name, version };
6
6
  //# sourceMappingURL=version.d.ts.map
@@ -1,6 +1,6 @@
1
1
  //#region src/version.ts
2
2
  const name = "@lifi/sdk-provider-solana";
3
- const version = "4.0.6";
3
+ const version = "4.1.0";
4
4
  //#endregion
5
5
  export { name, version };
6
6
 
@@ -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.0.6'\n"],"mappings":";AAAA,MAAa,OAAO;AACpB,MAAa,UAAU"}
1
+ {"version":3,"file":"version.js","names":[],"sources":["../../src/version.ts"],"sourcesContent":["export const name = '@lifi/sdk-provider-solana'\nexport const version = '4.1.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.0.6",
3
+ "version": "4.1.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,10 +28,10 @@
28
28
  "./package.json": "./package.json"
29
29
  },
30
30
  "dependencies": {
31
- "@solana/kit": "^7.0.0",
31
+ "@lifi/sdk": "4.5.0",
32
+ "@solana/kit": "^8.0.0",
32
33
  "@solana/wallet-standard-features": "^1.4.0",
33
- "@wallet-standard/base": "^1.1.1",
34
- "@lifi/sdk": "4.4.0"
34
+ "@wallet-standard/base": "^1.1.1"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"
@@ -1,159 +1,99 @@
1
- import { type SDKClient, sleep } from '@lifi/sdk'
1
+ import { LiFiErrorCode, RPCError, type SDKClient } from '@lifi/sdk'
2
+ import { getBase64EncodedWireTransaction, type Transaction } from '@solana/kit'
2
3
  import {
3
- type Commitment,
4
- getBase64EncodedWireTransaction,
5
- type Signature,
6
- type Transaction,
7
- type TransactionError,
8
- } from '@solana/kit'
9
-
4
+ type BundleConfirmation,
5
+ confirmBundle,
6
+ } from '../confirmation/confirmBundle.js'
7
+ import { BRANCH_TIMEOUT_MS } from '../confirmation/createConfirmationDeadline.js'
8
+ import { type RaceResult, raceRpcs } from '../confirmation/raceRpcs.js'
10
9
  import { getJitoRpcs } from '../rpc/registry.js'
11
-
12
- type SignatureStatus = {
13
- slot: bigint
14
- confirmations: bigint | null
15
- err: TransactionError | null
16
- confirmationStatus: Commitment | null
17
- status: Readonly<{ Err: TransactionError }> | Readonly<{ Ok: null }>
18
- }
19
-
20
- export type BundleResult = {
21
- bundleId: string
22
- txSignatures: Signature[]
23
- signatureResults: (SignatureStatus | null)[]
24
- }
25
-
26
- const NULL_BUNDLE_RESULT = new Error('Bundle was not confirmed by this RPC')
10
+ import { getTransactionLifetime } from '../utils/getTransactionLifetime.js'
27
11
 
28
12
  /**
29
- * Send and confirm a bundle of transactions using Jito.
30
- * Automatically selects a Jito-enabled RPC connection and polls for confirmation
31
- * across multiple Jito RPCs in parallel.
32
- * @param client - The SDK client.
33
- * @param signedTransactions - Array of signed transactions to bundle.
34
- * @returns BundleResult containing Bundle ID, transaction signatures, and confirmation results.
13
+ * Sends a Jito bundle to every Jito-capable RPC and returns as soon as one of
14
+ * them confirms it.
15
+ *
16
+ * The deadline receives the lifetime of *every* signed transaction, not just
17
+ * the first: a bundle is an array of independently built backend transactions
18
+ * and they may not share a blockhash.
19
+ *
20
+ * `sendBundle` is handed to `confirmBundle` rather than awaited here, so the
21
+ * deadline starts on the same clock as `BRANCH_TIMEOUT_MS` instead of after
22
+ * the submission returns.
35
23
  */
36
24
  export async function sendAndConfirmBundle(
37
25
  client: SDKClient,
38
- signedTransactions: Transaction[]
39
- ): Promise<BundleResult> {
40
- const jitoRpcs = await getJitoRpcs(client)
41
-
26
+ signedTransactions: Transaction[],
27
+ options?: {
28
+ /** Runs once, when the first Jito RPC accepts the submission. */
29
+ onBroadcast?: () => void
30
+ }
31
+ ): Promise<RaceResult<BundleConfirmation>> {
32
+ const { rpcs: jitoRpcs, unreachable } = await getJitoRpcs(client)
33
+
34
+ // Named here, where the emptiness is known: racing zero RPCs would surface
35
+ // as a bare `rpc-unavailable`, indistinguishable from a total outage. The
36
+ // two causes get different messages - a configuration gap the integrator can
37
+ // close, or endpoints that never answered.
38
+ //
39
+ // The `unreachable` message names a plan gate as well as an outage: a bare
40
+ // HTTP 401/403 never reaches the JSON-RPC layer, so `readProbeFailure`
41
+ // cannot tell a gated endpoint from a provider mid-deploy and classifies
42
+ // both as `unreachable`. "Retry" alone would loop an integrator forever on a
43
+ // gate no retry can clear.
42
44
  if (jitoRpcs.length === 0) {
43
- throw new Error(
44
- 'No Jito-enabled RPC connection available for bundle submission'
45
+ throw new RPCError(
46
+ LiFiErrorCode.RpcUnavailable,
47
+ unreachable > 0
48
+ ? `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
+ : 'Jito bundle required, but no configured Solana RPC supports `sendBundle`. Supply a Jito-capable URL via the `rpcUrls` client config option.'
45
50
  )
46
51
  }
47
52
 
48
- // Serialize transactions to base64
49
- const serializedTransactions = signedTransactions.map((tx) =>
50
- getBase64EncodedWireTransaction(tx)
51
- )
52
-
53
- const abortController = new AbortController()
54
-
55
- const confirmPromises = jitoRpcs.map(async (jitoRpc) => {
53
+ let broadcastReported = false
54
+ // No `sendAccepted` twin here: a bundle submits once, and a failed `send()`
55
+ // throws out of `confirmBundle` before polling starts, so this action has no
56
+ // not-confirmed-but-never-sent case to disambiguate. See
57
+ // `sendAndConfirmTransaction` for the signature path's version.
58
+ const reportBroadcast = (): void => {
59
+ if (broadcastReported) {
60
+ return
61
+ }
56
62
  try {
57
- // Send bundle to Jito
58
- let bundleId: string
59
- try {
60
- bundleId = await jitoRpc.sendBundle(serializedTransactions).send()
61
- } catch (_) {
62
- return null
63
- }
64
-
65
- const [{ value: blockhashResult }, initialBlockHeight] =
66
- await Promise.all([
67
- jitoRpc
68
- .getLatestBlockhash({
69
- commitment: 'confirmed',
70
- })
71
- .send(),
72
- jitoRpc
73
- .getBlockHeight({
74
- commitment: 'confirmed',
75
- })
76
- .send(),
77
- ])
78
-
79
- let currentBlockHeight = initialBlockHeight
80
-
81
- while (
82
- currentBlockHeight < blockhashResult.lastValidBlockHeight &&
83
- !abortController.signal.aborted
84
- ) {
85
- const statusResponse = await jitoRpc
86
- .getBundleStatuses([bundleId])
87
- .send()
88
-
89
- const bundleStatus = statusResponse.value[0]
90
-
91
- // Check if bundle is confirmed or finalized
92
- if (
93
- bundleStatus &&
94
- (bundleStatus.confirmation_status === 'confirmed' ||
95
- bundleStatus.confirmation_status === 'finalized')
96
- ) {
97
- // Bundle confirmed! Extract transaction signatures from bundle status
98
- const txSignatures = bundleStatus.transactions
99
-
100
- // Fetch individual signature results from Jito RPC
101
- const sigResponse = await jitoRpc
102
- .getSignatureStatuses(txSignatures)
103
- .send()
104
-
105
- if (!sigResponse?.value || !Array.isArray(sigResponse.value)) {
106
- // Keep polling if can't find signature results
107
- await sleep(400)
108
- continue
109
- }
110
-
111
- // Immediately abort all other connections when we find a result
112
- abortController.abort()
113
- return {
114
- bundleId,
115
- txSignatures,
116
- signatureResults: sigResponse.value,
117
- }
118
- }
119
-
120
- await sleep(400)
121
- if (!abortController.signal.aborted) {
122
- currentBlockHeight = await jitoRpc
123
- .getBlockHeight({
124
- commitment: 'confirmed',
125
- })
126
- .send()
127
- }
128
- }
129
-
130
- return null
131
- } catch (error) {
132
- if (abortController.signal.aborted) {
133
- return null // Don't treat abortion as an error
134
- }
135
- throw error
63
+ options?.onBroadcast?.()
64
+ // Latched only after the callback returned. A callback that threw wrote
65
+ // nothing, so the next successful send must be allowed to try again -
66
+ // latching first made one failed `txLink` write permanent.
67
+ broadcastReported = true
68
+ } catch (_) {
69
+ // This runs integrator code: the callback reaches `updateRouteHook` via
70
+ // `StatusManager.updateAction`. Its failure must never reject the branch
71
+ // that called it - the send has already been accepted by the network at
72
+ // this point, so a throw here would report a landed transaction as an
73
+ // RPC outage. Swallowed rather than surfaced because there is no verdict
74
+ // it could honestly change.
136
75
  }
137
- })
138
-
139
- // Wait for first successful confirmation
140
- const result = await Promise.any(
141
- confirmPromises.map(async (promise) => {
142
- const bundleResult = await promise
143
- if (!bundleResult) {
144
- throw NULL_BUNDLE_RESULT
145
- }
146
- return bundleResult
147
- })
148
- ).catch(() => null)
149
-
150
- if (!abortController.signal.aborted) {
151
- abortController.abort()
152
76
  }
153
77
 
154
- if (!result) {
155
- throw new Error('Failed to send and confirm bundle')
156
- }
78
+ const serializedTransactions = signedTransactions.map((transaction) =>
79
+ getBase64EncodedWireTransaction(transaction)
80
+ )
157
81
 
158
- return result
82
+ const lifetimes = await Promise.all(
83
+ signedTransactions.map((transaction) => getTransactionLifetime(transaction))
84
+ )
85
+
86
+ return raceRpcs(
87
+ jitoRpcs,
88
+ (rpc, signal) =>
89
+ confirmBundle({
90
+ rpc,
91
+ signal,
92
+ lifetimes,
93
+ send: () =>
94
+ rpc.sendBundle(serializedTransactions).send({ abortSignal: signal }),
95
+ onBroadcast: reportBroadcast,
96
+ }),
97
+ { timeoutMs: BRANCH_TIMEOUT_MS }
98
+ )
159
99
  }
@@ -1,50 +1,69 @@
1
- import { type SDKClient, sleep } from '@lifi/sdk'
1
+ import type { SDKClient } from '@lifi/sdk'
2
2
  import {
3
3
  type Commitment,
4
4
  getBase64EncodedWireTransaction,
5
5
  getSignatureFromTransaction,
6
6
  type Transaction,
7
- type TransactionError,
8
7
  } from '@solana/kit'
8
+ import { confirmSignature } from '../confirmation/confirmSignature.js'
9
+ import { BRANCH_TIMEOUT_MS } from '../confirmation/createConfirmationDeadline.js'
10
+ import { type RaceResult, raceRpcs } from '../confirmation/raceRpcs.js'
11
+ import type { SignatureStatus } from '../confirmation/types.js'
9
12
  import { getSolanaRpcs } from '../rpc/registry.js'
10
-
11
- type SignatureStatus = {
12
- slot: bigint
13
- confirmations: bigint | null
14
- err: TransactionError | null
15
- confirmationStatus: Commitment | null
16
- status: Readonly<{ Err: TransactionError }> | Readonly<{ Ok: null }>
17
- }
18
-
19
- type ConfirmedTransactionResult = {
20
- signatureResult: SignatureStatus | null
21
- txSignature: string
22
- }
23
-
24
- const NULL_CONFIRMATION_RESULT = new Error(
25
- 'Transaction was not confirmed by this RPC'
26
- )
13
+ import type { SolanaRpcType } from '../rpc/types.js'
14
+ import { getTransactionLifetime } from '../utils/getTransactionLifetime.js'
27
15
 
28
16
  /**
29
- * Sends a Solana transaction to multiple RPC endpoints and returns the confirmation
30
- * as soon as any of them confirm the transaction.
31
- * @param client - The SDK client.
32
- * @param signedTransaction - The signed transaction to send.
33
- * @returns - The confirmation result of the transaction.
17
+ * Sends a Solana transaction to every configured RPC and returns as soon as
18
+ * one of them confirms it.
19
+ *
20
+ * The polling horizon comes from the signed transaction's own blockhash and a
21
+ * wall-clock ceiling. It deliberately never comes from `getBlockHeight`: at
22
+ * least one endpoint in the default LI.FI set answers that call with the slot
23
+ * number.
34
24
  */
35
25
  export async function sendAndConfirmTransaction(
36
26
  client: SDKClient,
37
- signedTransaction: Transaction
38
- ): Promise<ConfirmedTransactionResult> {
27
+ signedTransaction: Transaction,
28
+ options?: {
29
+ /** Runs once, when the first RPC accepts a send. */
30
+ onBroadcast?: () => void
31
+ }
32
+ ): Promise<RaceResult<SignatureStatus>> {
39
33
  const solanaRpcs = await getSolanaRpcs(client)
40
34
 
35
+ let broadcastReported = false
36
+ // Distinct from `broadcastReported`, which records whether the integrator
37
+ // callback has *succeeded*. This one answers "did any RPC accept the send?",
38
+ // and only that question may steer the verdict below. Set before the callback
39
+ // runs, so a hook that throws on every resend cannot make a real expiry look
40
+ // like an outage.
41
+ let sendAccepted = false
42
+ const reportBroadcast = (): void => {
43
+ sendAccepted = true
44
+ if (broadcastReported) {
45
+ return
46
+ }
47
+ try {
48
+ options?.onBroadcast?.()
49
+ // Latched only after the callback returned. A callback that threw wrote
50
+ // nothing, so the next successful send must be allowed to try again -
51
+ // latching first made one failed `txLink` write permanent.
52
+ broadcastReported = true
53
+ } catch (_) {
54
+ // This runs integrator code: the callback reaches `updateRouteHook` via
55
+ // `StatusManager.updateAction`. Its failure must never reject the branch
56
+ // that called it - the send has already been accepted by the network at
57
+ // this point, so a throw here would report a landed transaction as an
58
+ // RPC outage. Swallowed rather than surfaced because there is no verdict
59
+ // it could honestly change.
60
+ }
61
+ }
62
+
41
63
  const signedTxSerialized = getBase64EncodedWireTransaction(signedTransaction)
42
- // Create transaction hash (signature)
43
64
  const txSignature = getSignatureFromTransaction(signedTransaction)
44
65
 
45
- if (!txSignature) {
46
- throw new Error('Transaction signature is missing.')
47
- }
66
+ const lifetime = await getTransactionLifetime(signedTransaction)
48
67
 
49
68
  const rawTransactionOptions = {
50
69
  // We can skip preflight check after the first transaction has been sent
@@ -57,111 +76,40 @@ export async function sendAndConfirmTransaction(
57
76
  encoding: 'base64' as const,
58
77
  }
59
78
 
60
- const abortController = new AbortController()
61
-
62
- const confirmPromises = solanaRpcs.map(async (rpc) => {
63
- try {
64
- // Send initial transaction for this RPC
65
- try {
66
- await rpc
67
- .sendTransaction(signedTxSerialized, rawTransactionOptions)
68
- .send()
69
- } catch (_) {
70
- // Continue with confirmation even if initial send fails
71
- }
72
-
73
- const [{ value: blockhashResult }, initialBlockHeight] =
74
- await Promise.all([
75
- rpc
76
- .getLatestBlockhash({
77
- commitment: 'confirmed',
78
- })
79
- .send(),
80
- rpc
81
- .getBlockHeight({
82
- commitment: 'confirmed',
83
- })
84
- .send(),
85
- ])
86
-
87
- let signatureResult: SignatureStatus | null = null
88
- let blockHeight = initialBlockHeight
89
- const pollingPromise = (async () => {
90
- while (
91
- blockHeight < blockhashResult.lastValidBlockHeight &&
92
- !abortController.signal.aborted
93
- ) {
94
- const statusResponse = await rpc
95
- .getSignatureStatuses([txSignature])
96
- .send()
97
-
98
- const status = statusResponse.value[0]
99
- if (
100
- status &&
101
- (status.confirmationStatus === 'confirmed' ||
102
- status.confirmationStatus === 'finalized')
103
- ) {
104
- signatureResult = status
105
- // Immediately abort all other RPCs when we find a result
106
- abortController.abort()
107
- return status
108
- }
109
-
110
- await sleep(400)
111
- }
112
- return null
113
- })()
114
-
115
- const sendingPromise = (async () => {
116
- while (
117
- blockHeight < blockhashResult.lastValidBlockHeight &&
118
- !abortController.signal.aborted &&
119
- !signatureResult
120
- ) {
121
- try {
122
- await rpc
123
- .sendTransaction(signedTxSerialized, rawTransactionOptions)
124
- .send()
125
- } catch (_) {
126
- // Continue trying even if individual sends fail
127
- }
128
-
129
- await sleep(1000)
130
- if (!abortController.signal.aborted) {
131
- blockHeight = await rpc
132
- .getBlockHeight({
133
- commitment: 'confirmed',
134
- })
135
- .send()
136
- }
137
- }
138
- return null
139
- })()
140
-
141
- // Wait for polling to find the result
142
- const result = await Promise.race([pollingPromise, sendingPromise])
143
- return result
144
- } catch (error) {
145
- if (abortController.signal.aborted) {
146
- return null // Don't treat abortion as an error
147
- }
148
- throw error
149
- }
150
- })
151
-
152
- const signatureResult = await Promise.any(
153
- confirmPromises.map(async (promise) => {
154
- const result = await promise
155
- if (!result) {
156
- throw NULL_CONFIRMATION_RESULT
157
- }
158
- return result
159
- })
160
- ).catch(() => null)
79
+ const resend = async (
80
+ rpc: SolanaRpcType,
81
+ signal: AbortSignal
82
+ ): Promise<void> => {
83
+ await rpc
84
+ .sendTransaction(signedTxSerialized, rawTransactionOptions)
85
+ .send({ abortSignal: signal })
86
+ }
161
87
 
162
- if (!abortController.signal.aborted) {
163
- abortController.abort()
88
+ const result = await raceRpcs(
89
+ solanaRpcs,
90
+ (rpc, signal) =>
91
+ confirmSignature({
92
+ rpc,
93
+ signal,
94
+ signature: txSignature,
95
+ lifetimes: [lifetime],
96
+ resend,
97
+ onBroadcast: reportBroadcast,
98
+ }),
99
+ { timeoutMs: BRANCH_TIMEOUT_MS }
100
+ )
101
+
102
+ // Only this scope knows whether ANY branch accepted the send. A branch that
103
+ // polls to its deadline reports `not-confirmed` regardless - correct per
104
+ // branch, but across the whole race it would claim a transaction expired
105
+ // when nothing ever submitted it. That is an outage, not an expiry.
106
+ //
107
+ // Reads `sendAccepted`, never `broadcastReported`: the latter is false
108
+ // whenever the integrator's callback threw, which says nothing about whether
109
+ // the network took the transaction.
110
+ if (result.kind === 'not-confirmed' && !sendAccepted) {
111
+ return { kind: 'rpc-unavailable', errors: result.errors }
164
112
  }
165
113
 
166
- return { signatureResult, txSignature }
114
+ return result
167
115
  }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Sleeps for `ms`, or until `signal` aborts - whichever comes first.
3
+ *
4
+ * Not `@lifi/sdk`'s `sleep`: that one is publicly exported, so making its
5
+ * timer abortable or unref'd would change behaviour for every caller in the
6
+ * repo. The confirmation branches need the abort, because both detached loops
7
+ * here sleep between iterations and a plain timer stays alive for the whole
8
+ * interval after the branch has already returned - one live timer per RPC,
9
+ * holding the Node event loop open past the end of a race.
10
+ *
11
+ * Never rejects on abort. The callers re-check `signal.aborted` and exit
12
+ * through their own loop condition, so a rejection here would only add a
13
+ * catch to every call site.
14
+ */
15
+ export function abortableSleep(ms: number, signal: AbortSignal): Promise<void> {
16
+ if (signal.aborted) {
17
+ return Promise.resolve()
18
+ }
19
+
20
+ return new Promise<void>((resolve) => {
21
+ const settle = (): void => {
22
+ clearTimeout(timer)
23
+ signal.removeEventListener('abort', settle)
24
+ resolve()
25
+ }
26
+
27
+ const timer = setTimeout(settle, ms)
28
+ signal.addEventListener('abort', settle, { once: true })
29
+ })
30
+ }