@lifi/sdk-provider-solana 4.3.3 → 4.3.4

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 (133) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +9 -0
  3. package/dist/cjs/actions/lookupSignatureStatus.d.ts +66 -0
  4. package/dist/cjs/actions/lookupSignatureStatus.js +258 -0
  5. package/dist/cjs/actions/lookupSignatureStatus.js.map +1 -0
  6. package/dist/cjs/actions/sendAndConfirmTransaction.d.ts +7 -0
  7. package/dist/cjs/actions/sendAndConfirmTransaction.js +1 -0
  8. package/dist/cjs/actions/sendAndConfirmTransaction.js.map +1 -1
  9. package/dist/cjs/confirmation/confirmBundle.d.ts +1 -1
  10. package/dist/cjs/confirmation/confirmSignature.d.ts +1 -1
  11. package/dist/cjs/confirmation/createConfirmationDeadline.d.ts +6 -0
  12. package/dist/cjs/confirmation/createConfirmationDeadline.js +24 -8
  13. package/dist/cjs/confirmation/createConfirmationDeadline.js.map +1 -1
  14. package/dist/cjs/confirmation/pollUntilDeadline.js +9 -1
  15. package/dist/cjs/confirmation/pollUntilDeadline.js.map +1 -1
  16. package/dist/cjs/confirmation/raceRpcs.d.ts +8 -0
  17. package/dist/cjs/confirmation/raceRpcs.js +9 -1
  18. package/dist/cjs/confirmation/raceRpcs.js.map +1 -1
  19. package/dist/cjs/confirmation/types.d.ts +3 -0
  20. package/dist/cjs/confirmation/types.js.map +1 -1
  21. package/dist/cjs/core/SolanaStepExecutor.js +2 -4
  22. package/dist/cjs/core/SolanaStepExecutor.js.map +1 -1
  23. package/dist/cjs/core/tasks/SolanaJitoWaitForTransactionTask.js +69 -33
  24. package/dist/cjs/core/tasks/SolanaJitoWaitForTransactionTask.js.map +1 -1
  25. package/dist/cjs/core/tasks/SolanaSignAndExecuteTask.js +13 -5
  26. package/dist/cjs/core/tasks/SolanaSignAndExecuteTask.js.map +1 -1
  27. package/dist/cjs/core/tasks/SolanaStandardWaitForTransactionTask.js +71 -38
  28. package/dist/cjs/core/tasks/SolanaStandardWaitForTransactionTask.js.map +1 -1
  29. package/dist/cjs/core/tasks/SolanaWaitForTransactionTask.js +13 -1
  30. package/dist/cjs/core/tasks/SolanaWaitForTransactionTask.js.map +1 -1
  31. package/dist/cjs/core/tasks/confirmationError.d.ts +29 -0
  32. package/dist/cjs/core/tasks/confirmationError.js +26 -0
  33. package/dist/cjs/core/tasks/confirmationError.js.map +1 -0
  34. package/dist/cjs/core/tasks/resumeFromStore.d.ts +31 -0
  35. package/dist/cjs/core/tasks/resumeFromStore.js +81 -0
  36. package/dist/cjs/core/tasks/resumeFromStore.js.map +1 -0
  37. package/dist/cjs/core/tasks/transactionOutcome.d.ts +107 -0
  38. package/dist/cjs/core/tasks/transactionOutcome.js +158 -0
  39. package/dist/cjs/core/tasks/transactionOutcome.js.map +1 -0
  40. package/dist/cjs/types.d.ts +13 -2
  41. package/dist/cjs/types.js.map +1 -1
  42. package/dist/cjs/utils/storedTransactions.d.ts +36 -0
  43. package/dist/cjs/utils/storedTransactions.js +57 -0
  44. package/dist/cjs/utils/storedTransactions.js.map +1 -0
  45. package/dist/cjs/version.d.ts +1 -1
  46. package/dist/cjs/version.js +1 -1
  47. package/dist/cjs/version.js.map +1 -1
  48. package/dist/esm/actions/lookupSignatureStatus.d.ts +66 -0
  49. package/dist/esm/actions/lookupSignatureStatus.d.ts.map +1 -0
  50. package/dist/esm/actions/lookupSignatureStatus.js +252 -0
  51. package/dist/esm/actions/lookupSignatureStatus.js.map +1 -0
  52. package/dist/esm/actions/sendAndConfirmTransaction.d.ts +7 -0
  53. package/dist/esm/actions/sendAndConfirmTransaction.d.ts.map +1 -1
  54. package/dist/esm/actions/sendAndConfirmTransaction.js +1 -0
  55. package/dist/esm/actions/sendAndConfirmTransaction.js.map +1 -1
  56. package/dist/esm/confirmation/confirmBundle.d.ts +1 -1
  57. package/dist/esm/confirmation/confirmSignature.d.ts +1 -1
  58. package/dist/esm/confirmation/createConfirmationDeadline.d.ts +6 -0
  59. package/dist/esm/confirmation/createConfirmationDeadline.d.ts.map +1 -1
  60. package/dist/esm/confirmation/createConfirmationDeadline.js +24 -8
  61. package/dist/esm/confirmation/createConfirmationDeadline.js.map +1 -1
  62. package/dist/esm/confirmation/pollUntilDeadline.js +9 -1
  63. package/dist/esm/confirmation/pollUntilDeadline.js.map +1 -1
  64. package/dist/esm/confirmation/raceRpcs.d.ts +8 -0
  65. package/dist/esm/confirmation/raceRpcs.d.ts.map +1 -1
  66. package/dist/esm/confirmation/raceRpcs.js +9 -1
  67. package/dist/esm/confirmation/raceRpcs.js.map +1 -1
  68. package/dist/esm/confirmation/types.d.ts +3 -0
  69. package/dist/esm/confirmation/types.d.ts.map +1 -1
  70. package/dist/esm/confirmation/types.js.map +1 -1
  71. package/dist/esm/core/SolanaStepExecutor.d.ts.map +1 -1
  72. package/dist/esm/core/SolanaStepExecutor.js +3 -5
  73. package/dist/esm/core/SolanaStepExecutor.js.map +1 -1
  74. package/dist/esm/core/tasks/SolanaJitoWaitForTransactionTask.d.ts.map +1 -1
  75. package/dist/esm/core/tasks/SolanaJitoWaitForTransactionTask.js +69 -33
  76. package/dist/esm/core/tasks/SolanaJitoWaitForTransactionTask.js.map +1 -1
  77. package/dist/esm/core/tasks/SolanaSignAndExecuteTask.d.ts.map +1 -1
  78. package/dist/esm/core/tasks/SolanaSignAndExecuteTask.js +14 -6
  79. package/dist/esm/core/tasks/SolanaSignAndExecuteTask.js.map +1 -1
  80. package/dist/esm/core/tasks/SolanaStandardWaitForTransactionTask.d.ts.map +1 -1
  81. package/dist/esm/core/tasks/SolanaStandardWaitForTransactionTask.js +71 -38
  82. package/dist/esm/core/tasks/SolanaStandardWaitForTransactionTask.js.map +1 -1
  83. package/dist/esm/core/tasks/SolanaWaitForTransactionTask.d.ts.map +1 -1
  84. package/dist/esm/core/tasks/SolanaWaitForTransactionTask.js +13 -1
  85. package/dist/esm/core/tasks/SolanaWaitForTransactionTask.js.map +1 -1
  86. package/dist/esm/core/tasks/confirmationError.d.ts +29 -0
  87. package/dist/esm/core/tasks/confirmationError.d.ts.map +1 -0
  88. package/dist/esm/core/tasks/confirmationError.js +25 -0
  89. package/dist/esm/core/tasks/confirmationError.js.map +1 -0
  90. package/dist/esm/core/tasks/resumeFromStore.d.ts +31 -0
  91. package/dist/esm/core/tasks/resumeFromStore.d.ts.map +1 -0
  92. package/dist/esm/core/tasks/resumeFromStore.js +80 -0
  93. package/dist/esm/core/tasks/resumeFromStore.js.map +1 -0
  94. package/dist/esm/core/tasks/transactionOutcome.d.ts +107 -0
  95. package/dist/esm/core/tasks/transactionOutcome.d.ts.map +1 -0
  96. package/dist/esm/core/tasks/transactionOutcome.js +150 -0
  97. package/dist/esm/core/tasks/transactionOutcome.js.map +1 -0
  98. package/dist/esm/types.d.ts +13 -2
  99. package/dist/esm/types.d.ts.map +1 -1
  100. package/dist/esm/types.js.map +1 -1
  101. package/dist/esm/utils/storedTransactions.d.ts +36 -0
  102. package/dist/esm/utils/storedTransactions.d.ts.map +1 -0
  103. package/dist/esm/utils/storedTransactions.js +54 -0
  104. package/dist/esm/utils/storedTransactions.js.map +1 -0
  105. package/dist/esm/version.d.ts +1 -1
  106. package/dist/esm/version.js +1 -1
  107. package/dist/esm/version.js.map +1 -1
  108. package/package.json +2 -2
  109. package/src/actions/lookupSignatureStatus.ts +444 -0
  110. package/src/actions/sendAndConfirmTransaction.ts +18 -0
  111. package/src/confirmation/createConfirmationDeadline.ts +43 -11
  112. package/src/confirmation/pollUntilDeadline.ts +14 -1
  113. package/src/confirmation/raceRpcs.ts +27 -1
  114. package/src/confirmation/types.ts +5 -0
  115. package/src/core/SolanaStepExecutor.ts +15 -8
  116. package/src/core/tasks/SolanaJitoWaitForTransactionTask.ts +133 -71
  117. package/src/core/tasks/SolanaSignAndExecuteTask.ts +51 -10
  118. package/src/core/tasks/SolanaStandardWaitForTransactionTask.ts +130 -76
  119. package/src/core/tasks/SolanaWaitForTransactionTask.ts +19 -1
  120. package/src/core/tasks/confirmationError.ts +60 -0
  121. package/src/core/tasks/resumeFromStore.ts +160 -0
  122. package/src/core/tasks/transactionOutcome.ts +273 -0
  123. package/src/types.ts +13 -1
  124. package/src/utils/storedTransactions.ts +88 -0
  125. package/src/version.ts +1 -1
  126. package/dist/cjs/core/tasks/unwrapConfirmation.d.ts +0 -24
  127. package/dist/cjs/core/tasks/unwrapConfirmation.js +0 -24
  128. package/dist/cjs/core/tasks/unwrapConfirmation.js.map +0 -1
  129. package/dist/esm/core/tasks/unwrapConfirmation.d.ts +0 -24
  130. package/dist/esm/core/tasks/unwrapConfirmation.d.ts.map +0 -1
  131. package/dist/esm/core/tasks/unwrapConfirmation.js +0 -23
  132. package/dist/esm/core/tasks/unwrapConfirmation.js.map +0 -1
  133. package/src/core/tasks/unwrapConfirmation.ts +0 -59
@@ -1,29 +1,47 @@
1
1
  import {
2
2
  BaseStepExecutionTask,
3
+ type ExecutionAction,
3
4
  LiFiErrorCode,
4
5
  type TaskResult,
5
6
  TransactionError,
6
7
  } from '@lifi/sdk'
7
- import { getBase64EncodedWireTransaction } from '@solana/kit'
8
+ import {
9
+ getBase64EncodedWireTransaction,
10
+ type Signature,
11
+ type Transaction,
12
+ } from '@solana/kit'
8
13
  import { sendAndConfirmTransaction } from '../../actions/sendAndConfirmTransaction.js'
9
14
  import { callSolanaRpcsWithRetry } from '../../rpc/utils.js'
10
15
  import type { SolanaStepExecutorContext } from '../../types.js'
16
+ import {
17
+ getTransactionLifetime,
18
+ type TransactionLifetime,
19
+ } from '../../utils/getTransactionLifetime.js'
11
20
  import { SolanaTransactionDetailsError } from '../../utils/solanaErrorCause.js'
21
+ import type { ConfirmationMessages } from './confirmationError.js'
12
22
  import { readSignature } from './readSignature.js'
13
- import { unwrapConfirmation } from './unwrapConfirmation.js'
23
+ import { resumeFromStore } from './resumeFromStore.js'
24
+ import {
25
+ canResendStored,
26
+ clearStoredTransactions,
27
+ failureOf,
28
+ getTxLink,
29
+ sendAndSettle,
30
+ } from './transactionOutcome.js'
31
+
32
+ const MESSAGES: ConfirmationMessages = {
33
+ rpcUnavailable:
34
+ 'Unable to confirm transaction: no Solana RPC returned a usable response.',
35
+ notConfirmed: 'Transaction was not confirmed before the SDK stopped waiting.',
36
+ allRpcsFailed: 'All Solana RPCs failed',
37
+ someRpcsFailed:
38
+ 'Some Solana RPCs failed while the confirmation window was open',
39
+ }
14
40
 
15
41
  export class SolanaStandardWaitForTransactionTask extends BaseStepExecutionTask {
16
42
  async run(context: SolanaStepExecutorContext): Promise<TaskResult> {
17
- const {
18
- client,
19
- step,
20
- statusManager,
21
- fromChain,
22
- isBridgeExecution,
23
- signedTransactions: contextSignedTransactions,
24
- } = context
25
-
26
- const signedTransactions = contextSignedTransactions ?? []
43
+ const { step, statusManager, isBridgeExecution, signedTransactions } =
44
+ context
27
45
 
28
46
  const action = statusManager.findAction(
29
47
  step,
@@ -36,6 +54,21 @@ export class SolanaStandardWaitForTransactionTask extends BaseStepExecutionTask
36
54
  )
37
55
  }
38
56
 
57
+ // Resume mode: the sign task did not run in this session, so the signed
58
+ // bytes come from `txHex`, or only the hash from `txHash`.
59
+ if (!signedTransactions) {
60
+ return resumeFromStore(context, action, {
61
+ messages: MESSAGES,
62
+ send: (stored, lifetimes) =>
63
+ sendSigned(context, action, {
64
+ transaction: stored.transactions[0],
65
+ signature: stored.signature,
66
+ lifetimes,
67
+ resuming: true,
68
+ }),
69
+ })
70
+ }
71
+
39
72
  if (!signedTransactions.length) {
40
73
  throw new TransactionError(
41
74
  LiFiErrorCode.TransactionUnprepared,
@@ -46,78 +79,99 @@ export class SolanaStandardWaitForTransactionTask extends BaseStepExecutionTask
46
79
  // Use regular transaction submission
47
80
  const signedTransaction = signedTransactions[0]
48
81
 
49
- const encodedTransaction =
50
- getBase64EncodedWireTransaction(signedTransaction)
51
-
52
- if (!context.skipSimulation) {
53
- const simulationResult = await callSolanaRpcsWithRetry(
54
- client,
55
- (connection) =>
56
- connection
57
- .simulateTransaction(encodedTransaction, {
58
- commitment: 'confirmed',
59
- replaceRecentBlockhash: true,
60
- encoding: 'base64',
61
- })
62
- .send()
63
- )
64
-
65
- if (simulationResult.value.err) {
66
- const cause = new SolanaTransactionDetailsError(
67
- simulationResult.value.err,
68
- simulationResult.value.logs
69
- )
70
- throw new TransactionError(
71
- LiFiErrorCode.TransactionSimulationFailed,
72
- `Transaction simulation failed: ${cause.message}`,
73
- cause
74
- )
82
+ let txSignature: Signature
83
+ let lifetime: TransactionLifetime
84
+ try {
85
+ if (!context.skipSimulation) {
86
+ await simulate(context, signedTransaction)
75
87
  }
88
+ txSignature = readSignature(signedTransaction)
89
+ lifetime = await getTransactionLifetime(signedTransaction)
90
+ } catch (error) {
91
+ // Nothing left the SDK: the bytes go, so "Try again" signs again
92
+ // instead of resending a transaction that failed before it was sent.
93
+ clearStoredTransactions(context, action)
94
+ throw error
76
95
  }
77
96
 
78
- const txSignature = readSignature(signedTransaction)
79
- const txLink = `${fromChain.metamask.blockExplorerUrls[0]}tx/${txSignature}`
80
-
81
- const result = await sendAndConfirmTransaction(client, signedTransaction, {
82
- onBroadcast: () => {
83
- // The earliest honest point for both: an RPC has accepted the
84
- // transaction, so the signature now resolves on chain.
85
- statusManager.updateAction(step, action.type, 'PENDING', {
86
- txHash: txSignature,
87
- txLink,
88
- })
89
- },
97
+ return sendSigned(context, action, {
98
+ transaction: signedTransaction,
99
+ signature: txSignature,
100
+ lifetimes: [lifetime],
101
+ resuming: false,
90
102
  })
103
+ }
104
+ }
91
105
 
92
- const status = unwrapConfirmation(result, {
93
- rpcUnavailable:
94
- 'Unable to confirm transaction: no Solana RPC returned a usable response.',
95
- notConfirmed:
96
- 'Transaction was not confirmed before the SDK stopped waiting.',
97
- allRpcsFailed: 'All Solana RPCs failed',
98
- someRpcsFailed:
99
- 'Some Solana RPCs failed while the confirmation window was open',
100
- })
106
+ /**
107
+ * Sends one signed transaction, confirms it and applies the result rules of
108
+ * `sendAndSettle`, on the first run and on a resume alike.
109
+ */
110
+ function sendSigned(
111
+ context: SolanaStepExecutorContext,
112
+ action: ExecutionAction,
113
+ options: {
114
+ transaction: Transaction
115
+ signature: Signature
116
+ lifetimes: TransactionLifetime[]
117
+ /** An earlier run may already have sent these bytes. */
118
+ resuming: boolean
119
+ }
120
+ ): Promise<TaskResult> {
121
+ const { client, step, statusManager } = context
122
+ const { transaction, signature, lifetimes, resuming } = options
123
+ const txLink = getTxLink(context, signature)
101
124
 
102
- if (status.err) {
103
- const cause = new SolanaTransactionDetailsError(status.err)
104
- throw new TransactionError(
105
- LiFiErrorCode.TransactionFailed,
106
- `Transaction failed: ${cause.message}`,
107
- cause
108
- )
109
- }
125
+ return sendAndSettle(context, action, {
126
+ signature,
127
+ send: () =>
128
+ sendAndConfirmTransaction(client, transaction, {
129
+ onBroadcast: () => {
130
+ // The earliest honest point for both: an RPC has accepted the
131
+ // transaction, so the signature now resolves on chain.
132
+ statusManager.updateAction(step, action.type, 'PENDING', {
133
+ txHash: signature,
134
+ txLink,
135
+ })
136
+ },
137
+ // Read at every send: bytes without their own expiry go out only
138
+ // inside the resend age cap, however long the resend loop runs.
139
+ mayResend: () => canResendStored(lifetimes, step.execution?.signedAt),
140
+ }),
141
+ confirmedFailure: failureOf,
142
+ resuming,
143
+ messages: MESSAGES,
144
+ })
145
+ }
110
146
 
111
- // Transaction has been confirmed and we can update the action
112
- statusManager.updateAction(step, action.type, 'PENDING', {
113
- txHash: txSignature,
114
- txLink,
115
- })
147
+ /** First run only: a resend never simulates. */
148
+ async function simulate(
149
+ context: SolanaStepExecutorContext,
150
+ signedTransaction: Transaction
151
+ ): Promise<void> {
152
+ const encodedTransaction = getBase64EncodedWireTransaction(signedTransaction)
116
153
 
117
- if (isBridgeExecution) {
118
- statusManager.updateAction(step, action.type, 'DONE')
119
- }
154
+ const simulationResult = await callSolanaRpcsWithRetry(
155
+ context.client,
156
+ (connection) =>
157
+ connection
158
+ .simulateTransaction(encodedTransaction, {
159
+ commitment: 'confirmed',
160
+ replaceRecentBlockhash: true,
161
+ encoding: 'base64',
162
+ })
163
+ .send()
164
+ )
120
165
 
121
- return { status: 'COMPLETED' }
166
+ if (simulationResult.value.err) {
167
+ const cause = new SolanaTransactionDetailsError(
168
+ simulationResult.value.err,
169
+ simulationResult.value.logs
170
+ )
171
+ throw new TransactionError(
172
+ LiFiErrorCode.TransactionSimulationFailed,
173
+ `Transaction simulation failed: ${cause.message}`,
174
+ cause
175
+ )
122
176
  }
123
177
  }
@@ -1,15 +1,33 @@
1
1
  import { BaseStepExecutionTask, type TaskResult } from '@lifi/sdk'
2
2
  import type { SolanaStepExecutorContext } from '../../types.js'
3
+ import { isStoredBundle } from '../../utils/storedTransactions.js'
3
4
  import { SolanaJitoWaitForTransactionTask } from './SolanaJitoWaitForTransactionTask.js'
4
5
  import { SolanaStandardWaitForTransactionTask } from './SolanaStandardWaitForTransactionTask.js'
5
6
 
7
+ /**
8
+ * On a resume the sign task did not run, so the flag is missing and the
9
+ * stored bytes keep the shape instead: a leading `[` marks a bundle. With a
10
+ * `txHash` alone the standard task looks the signature up, which works for a
11
+ * bundle's first signature too.
12
+ */
13
+ function isBundleExecution(context: SolanaStepExecutorContext): boolean {
14
+ if (context.isBundleExecution !== undefined) {
15
+ return context.isBundleExecution
16
+ }
17
+ const action = context.statusManager.findAction(
18
+ context.step,
19
+ context.isBridgeExecution ? 'CROSS_CHAIN' : 'SWAP'
20
+ )
21
+ return action?.txHex ? isStoredBundle(action.txHex) : false
22
+ }
23
+
6
24
  export class SolanaWaitForTransactionTask extends BaseStepExecutionTask {
7
25
  async run(context: SolanaStepExecutorContext): Promise<TaskResult> {
8
26
  // The submission method is determined by the shape of the backend's
9
27
  // `transactionRequest.data` (resolved in SolanaSignAndExecuteTask):
10
28
  // a bundle (array) must go through `sendBundle`, a single transaction
11
29
  // (string) through `sendTransaction`.
12
- if (context.isBundleExecution) {
30
+ if (isBundleExecution(context)) {
13
31
  return new SolanaJitoWaitForTransactionTask().run(context)
14
32
  }
15
33
  return new SolanaStandardWaitForTransactionTask().run(context)
@@ -0,0 +1,60 @@
1
+ import { LiFiErrorCode, RPCError, TransactionError } from '@lifi/sdk'
2
+ import type { UnconfirmedRaceResult } from '../../confirmation/raceRpcs.js'
3
+
4
+ /** Only the nouns differ between the standard and bundle paths. */
5
+ export type ConfirmationMessages = {
6
+ /** Every branch failed: an outage, not a verdict about the transaction. */
7
+ rpcUnavailable: string
8
+ /** A branch polled to its deadline and saw nothing, or the blockhash
9
+ * expired. Also the text of a final "dropped" verdict. */
10
+ notConfirmed: string
11
+ /** Heading of the `AggregateError` chained onto `rpcUnavailable`. */
12
+ allRpcsFailed: string
13
+ /** Heading of the `AggregateError` chained onto `notConfirmed`. */
14
+ someRpcsFailed: string
15
+ }
16
+
17
+ /** `cause` is the first collected error on purpose: `BaseError` overwrites its
18
+ * stack with `getRootCause(cause).stack`, and an `AggregateError` has no
19
+ * `cause`, so without it the stack would point at this file. */
20
+ const chainErrors = (
21
+ errors: Error[],
22
+ message: string
23
+ ): AggregateError | undefined =>
24
+ errors.length
25
+ ? new AggregateError(errors, message, { cause: errors[0] })
26
+ : undefined
27
+
28
+ /**
29
+ * The error the integrator sees for a result that did not confirm.
30
+ *
31
+ * `rpc-unavailable` and `not-confirmed` must stay distinct: collapsing them
32
+ * reported a live RPC defect as an expired transaction.
33
+ *
34
+ * `expired` maps exactly like `not-confirmed` - same code, same message - so
35
+ * no text changes. The wait tasks tell the two apart by `result.kind` before
36
+ * they call this: only they can run the history lookup and the status API a
37
+ * final "dropped" needs, so the final marker is theirs to add.
38
+ */
39
+ export function confirmationError(
40
+ result: UnconfirmedRaceResult,
41
+ messages: ConfirmationMessages
42
+ ): RPCError | TransactionError {
43
+ if (result.kind === 'rpc-unavailable') {
44
+ return new RPCError(
45
+ LiFiErrorCode.RpcUnavailable,
46
+ messages.rpcUnavailable,
47
+ chainErrors(result.errors, messages.allRpcsFailed)
48
+ )
49
+ }
50
+
51
+ // The verdict came from branches that polled to their deadline or saw the
52
+ // blockhash expire, but other branches may have died trying - and their
53
+ // errors are the only trail explaining, say, an endpoint that never
54
+ // answered.
55
+ return new TransactionError(
56
+ LiFiErrorCode.TransactionExpired,
57
+ messages.notConfirmed,
58
+ chainErrors(result.errors, messages.someRpcsFailed)
59
+ )
60
+ }
@@ -0,0 +1,160 @@
1
+ import {
2
+ type ExecutionAction,
3
+ LiFiErrorCode,
4
+ type TaskResult,
5
+ TransactionError,
6
+ } from '@lifi/sdk'
7
+ import type { Signature } from '@solana/kit'
8
+ import { lookupSignatureStatus } from '../../actions/lookupSignatureStatus.js'
9
+ import { isConfirmedCommitment } from '../../confirmation/types.js'
10
+ import type { SolanaStepExecutorContext } from '../../types.js'
11
+ import {
12
+ getTransactionLifetime,
13
+ type TransactionLifetime,
14
+ } from '../../utils/getTransactionLifetime.js'
15
+ import {
16
+ decodeStoredTransactions,
17
+ type StoredTransactions,
18
+ } from '../../utils/storedTransactions.js'
19
+ import {
20
+ type ConfirmationMessages,
21
+ confirmationError,
22
+ } from './confirmationError.js'
23
+ import {
24
+ canResendStored,
25
+ clearStoredTransactions,
26
+ failureOf,
27
+ recordLanded,
28
+ resolveUnconfirmed,
29
+ } from './transactionOutcome.js'
30
+
31
+ /** What a resumed wait task can work from. */
32
+ type ResumeSource =
33
+ /** Signed bytes that may still need to be sent. */
34
+ | { kind: 'bytes'; stored: StoredTransactions }
35
+ /** A broadcast signature alone: a route stored before the upgrade, or
36
+ * bytes that no longer decode. */
37
+ | { kind: 'hash'; signature: Signature }
38
+ /** Bytes whose first signature is not the stored txHash (damaged
39
+ * storage). They prove nothing about that transaction, and they may have
40
+ * been sent: nothing is sent and nothing is dropped. */
41
+ | { kind: 'mismatch'; signature: Signature }
42
+
43
+ function readResumeSource(
44
+ context: SolanaStepExecutorContext,
45
+ action: ExecutionAction
46
+ ): ResumeSource {
47
+ if (action.txHex) {
48
+ const stored = decodeStoredTransactions(action.txHex)
49
+ if (stored) {
50
+ if (action.txHash && action.txHash !== stored.signature) {
51
+ return { kind: 'mismatch', signature: action.txHash as Signature }
52
+ }
53
+ return { kind: 'bytes', stored }
54
+ }
55
+ // The sign task stores only bytes that decode, so these were damaged
56
+ // after the write - by the integrator's storage, say. They would fail
57
+ // every resume the same way.
58
+ clearStoredTransactions(context, action)
59
+ }
60
+
61
+ if (action.txHash) {
62
+ return { kind: 'hash', signature: action.txHash as Signature }
63
+ }
64
+
65
+ // Without a hash no RPC ever accepted a send - `txHash` is written on the
66
+ // first accepted one - so nothing can land. No final marker: "Try again"
67
+ // signs again.
68
+ throw new TransactionError(
69
+ LiFiErrorCode.TransactionUnprepared,
70
+ 'Unable to prepare transaction. Signed transactions are not found.'
71
+ )
72
+ }
73
+
74
+ /**
75
+ * Resume mode of both wait tasks: the sign task did not run in this session,
76
+ * so the transaction comes from the action.
77
+ *
78
+ * 1. Look the signature up on every RPC. A confirmed status goes straight to
79
+ * the result rules: the transaction may have landed after the page closed.
80
+ * This look only has to find, so it asks for no canary.
81
+ * 2. Otherwise resend the stored bytes through `send` when that is allowed -
82
+ * the task's normal send path, without simulation.
83
+ * 3. Otherwise - a hash alone, or bytes past the resend age cap - nothing is
84
+ * sent, and only the dropped rule can make the outcome final.
85
+ *
86
+ * Bytes that are not the stored txHash's transaction are never sent, and the
87
+ * dropped rule never runs for them: the outcome stays unknown unless the
88
+ * lookup by txHash finds it confirmed.
89
+ */
90
+ export async function resumeFromStore(
91
+ context: SolanaStepExecutorContext,
92
+ action: ExecutionAction,
93
+ options: {
94
+ messages: ConfirmationMessages
95
+ /** Sends the stored bytes and applies the result rules, as the first
96
+ * run does, but never simulates: a transaction that already landed, or
97
+ * funds it already spent, would fail the simulation falsely. */
98
+ send: (
99
+ stored: StoredTransactions,
100
+ lifetimes: TransactionLifetime[]
101
+ ) => Promise<TaskResult>
102
+ }
103
+ ): Promise<TaskResult> {
104
+ const { client, step } = context
105
+ const source = readResumeSource(context, action)
106
+ const signature =
107
+ source.kind === 'bytes' ? source.stored.signature : source.signature
108
+
109
+ const lookup = await lookupSignatureStatus(client, signature)
110
+ if (
111
+ lookup.kind === 'found' &&
112
+ isConfirmedCommitment(lookup.status.confirmationStatus)
113
+ ) {
114
+ return recordLanded(context, action, {
115
+ signature,
116
+ failure: failureOf(lookup.status),
117
+ })
118
+ }
119
+
120
+ const lifetimes =
121
+ source.kind === 'bytes'
122
+ ? await Promise.all(
123
+ source.stored.transactions.map((transaction) =>
124
+ getTransactionLifetime(transaction)
125
+ )
126
+ )
127
+ : []
128
+
129
+ // A `processed` status resends too: a signature executes at most once.
130
+ if (
131
+ source.kind === 'bytes' &&
132
+ canResendStored(lifetimes, step.execution?.signedAt)
133
+ ) {
134
+ return options.send(source.stored, lifetimes)
135
+ }
136
+
137
+ // No RPC answered at all: an outage, as today. Otherwise the transaction
138
+ // was simply not seen.
139
+ const silent = lookup.kind === 'unknown' && !lookup.answered
140
+ const error = confirmationError(
141
+ silent
142
+ ? { kind: 'rpc-unavailable', errors: lookup.errors }
143
+ : { kind: 'not-confirmed', errors: [] },
144
+ options.messages
145
+ )
146
+ if (source.kind === 'mismatch') {
147
+ // Damaged storage: no dropped rule, so the outcome stays unknown.
148
+ throw error
149
+ }
150
+ const status = await resolveUnconfirmed(context, action, {
151
+ signature,
152
+ error,
153
+ expiredAtSlot: undefined,
154
+ messages: options.messages,
155
+ })
156
+ return recordLanded(context, action, {
157
+ signature,
158
+ failure: failureOf(status),
159
+ })
160
+ }