@lifi/sdk-provider-ethereum 4.2.6 → 4.2.7

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 (166) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +9 -0
  3. package/dist/cjs/EthereumProvider.js +5 -1
  4. package/dist/cjs/EthereumProvider.js.map +1 -1
  5. package/dist/cjs/actions/waitForBatchTransactionReceipt.js +7 -2
  6. package/dist/cjs/actions/waitForBatchTransactionReceipt.js.map +1 -1
  7. package/dist/cjs/actions/waitForRelayedTransactionReceipt.d.ts +13 -1
  8. package/dist/cjs/actions/waitForRelayedTransactionReceipt.js +19 -5
  9. package/dist/cjs/actions/waitForRelayedTransactionReceipt.js.map +1 -1
  10. package/dist/cjs/actions/waitForSafeTransactionExecution.js +2 -2
  11. package/dist/cjs/actions/waitForSafeTransactionExecution.js.map +1 -1
  12. package/dist/cjs/actions/waitForTransactionReceipt.js +2 -2
  13. package/dist/cjs/actions/waitForTransactionReceipt.js.map +1 -1
  14. package/dist/cjs/client/publicClient.js +14 -5
  15. package/dist/cjs/client/publicClient.js.map +1 -1
  16. package/dist/cjs/core/EthereumStepExecutor.js +1 -1
  17. package/dist/cjs/core/EthereumStepExecutor.js.map +1 -1
  18. package/dist/cjs/core/tasks/EthereumBatchedSignAndExecuteTask.js +2 -0
  19. package/dist/cjs/core/tasks/EthereumBatchedSignAndExecuteTask.js.map +1 -1
  20. package/dist/cjs/core/tasks/EthereumCheckAllowanceTask.js +5 -4
  21. package/dist/cjs/core/tasks/EthereumCheckAllowanceTask.js.map +1 -1
  22. package/dist/cjs/core/tasks/EthereumPrepareTransactionTask.js +10 -1
  23. package/dist/cjs/core/tasks/EthereumPrepareTransactionTask.js.map +1 -1
  24. package/dist/cjs/core/tasks/EthereumRelayedSignAndExecuteTask.js +5 -2
  25. package/dist/cjs/core/tasks/EthereumRelayedSignAndExecuteTask.js.map +1 -1
  26. package/dist/cjs/core/tasks/EthereumRelayedWaitForTransactionTask.js +10 -2
  27. package/dist/cjs/core/tasks/EthereumRelayedWaitForTransactionTask.js.map +1 -1
  28. package/dist/cjs/core/tasks/EthereumResetAllowanceTask.js +2 -2
  29. package/dist/cjs/core/tasks/EthereumResetAllowanceTask.js.map +1 -1
  30. package/dist/cjs/core/tasks/EthereumSetAllowanceTask.js +2 -3
  31. package/dist/cjs/core/tasks/EthereumSetAllowanceTask.js.map +1 -1
  32. package/dist/cjs/core/tasks/EthereumSignAndExecuteTask.js +1 -0
  33. package/dist/cjs/core/tasks/EthereumSignAndExecuteTask.js.map +1 -1
  34. package/dist/cjs/core/tasks/EthereumStandardSignAndExecuteTask.js +2 -0
  35. package/dist/cjs/core/tasks/EthereumStandardSignAndExecuteTask.js.map +1 -1
  36. package/dist/cjs/core/tasks/EthereumWaitForTransactionTask.js +8 -1
  37. package/dist/cjs/core/tasks/EthereumWaitForTransactionTask.js.map +1 -1
  38. package/dist/cjs/core/tasks/helpers/getEthereumExecutionStrategy.d.ts +6 -0
  39. package/dist/cjs/core/tasks/helpers/getEthereumExecutionStrategy.js +15 -0
  40. package/dist/cjs/core/tasks/helpers/getEthereumExecutionStrategy.js.map +1 -1
  41. package/dist/cjs/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.d.ts +17 -0
  42. package/dist/cjs/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.js +27 -0
  43. package/dist/cjs/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.js.map +1 -0
  44. package/dist/cjs/core/tasks/helpers/resolveAllowanceSpender.d.ts +16 -0
  45. package/dist/cjs/core/tasks/helpers/resolveAllowanceSpender.js +21 -0
  46. package/dist/cjs/core/tasks/helpers/resolveAllowanceSpender.js.map +1 -0
  47. package/dist/cjs/core/tasks/helpers/shouldCheckForAllowance.js +2 -1
  48. package/dist/cjs/core/tasks/helpers/shouldCheckForAllowance.js.map +1 -1
  49. package/dist/cjs/core/tasks/helpers/signHyperliquidTypedData.d.ts +5 -1
  50. package/dist/cjs/core/tasks/helpers/signHyperliquidTypedData.js +8 -1
  51. package/dist/cjs/core/tasks/helpers/signHyperliquidTypedData.js.map +1 -1
  52. package/dist/cjs/core/tasks/helpers/signTypedDataEntries.d.ts +6 -2
  53. package/dist/cjs/core/tasks/helpers/signTypedDataEntries.js +7 -2
  54. package/dist/cjs/core/tasks/helpers/signTypedDataEntries.js.map +1 -1
  55. package/dist/cjs/errors/parseEthereumErrors.js +2 -1
  56. package/dist/cjs/errors/parseEthereumErrors.js.map +1 -1
  57. package/dist/cjs/types.d.ts +6 -0
  58. package/dist/cjs/types.js.map +1 -1
  59. package/dist/cjs/version.d.ts +1 -1
  60. package/dist/cjs/version.js +1 -1
  61. package/dist/cjs/version.js.map +1 -1
  62. package/dist/esm/EthereumProvider.d.ts.map +1 -1
  63. package/dist/esm/EthereumProvider.js +5 -1
  64. package/dist/esm/EthereumProvider.js.map +1 -1
  65. package/dist/esm/actions/waitForBatchTransactionReceipt.js +7 -2
  66. package/dist/esm/actions/waitForBatchTransactionReceipt.js.map +1 -1
  67. package/dist/esm/actions/waitForRelayedTransactionReceipt.d.ts +13 -1
  68. package/dist/esm/actions/waitForRelayedTransactionReceipt.d.ts.map +1 -1
  69. package/dist/esm/actions/waitForRelayedTransactionReceipt.js +19 -5
  70. package/dist/esm/actions/waitForRelayedTransactionReceipt.js.map +1 -1
  71. package/dist/esm/actions/waitForSafeTransactionExecution.js +2 -2
  72. package/dist/esm/actions/waitForSafeTransactionExecution.js.map +1 -1
  73. package/dist/esm/actions/waitForTransactionReceipt.js +2 -2
  74. package/dist/esm/actions/waitForTransactionReceipt.js.map +1 -1
  75. package/dist/esm/client/publicClient.d.ts.map +1 -1
  76. package/dist/esm/client/publicClient.js +14 -5
  77. package/dist/esm/client/publicClient.js.map +1 -1
  78. package/dist/esm/core/EthereumStepExecutor.d.ts.map +1 -1
  79. package/dist/esm/core/EthereumStepExecutor.js +2 -2
  80. package/dist/esm/core/EthereumStepExecutor.js.map +1 -1
  81. package/dist/esm/core/tasks/EthereumBatchedSignAndExecuteTask.d.ts.map +1 -1
  82. package/dist/esm/core/tasks/EthereumBatchedSignAndExecuteTask.js +3 -1
  83. package/dist/esm/core/tasks/EthereumBatchedSignAndExecuteTask.js.map +1 -1
  84. package/dist/esm/core/tasks/EthereumCheckAllowanceTask.js +5 -4
  85. package/dist/esm/core/tasks/EthereumCheckAllowanceTask.js.map +1 -1
  86. package/dist/esm/core/tasks/EthereumPrepareTransactionTask.d.ts.map +1 -1
  87. package/dist/esm/core/tasks/EthereumPrepareTransactionTask.js +12 -3
  88. package/dist/esm/core/tasks/EthereumPrepareTransactionTask.js.map +1 -1
  89. package/dist/esm/core/tasks/EthereumRelayedSignAndExecuteTask.d.ts.map +1 -1
  90. package/dist/esm/core/tasks/EthereumRelayedSignAndExecuteTask.js +6 -3
  91. package/dist/esm/core/tasks/EthereumRelayedSignAndExecuteTask.js.map +1 -1
  92. package/dist/esm/core/tasks/EthereumRelayedWaitForTransactionTask.d.ts.map +1 -1
  93. package/dist/esm/core/tasks/EthereumRelayedWaitForTransactionTask.js +10 -2
  94. package/dist/esm/core/tasks/EthereumRelayedWaitForTransactionTask.js.map +1 -1
  95. package/dist/esm/core/tasks/EthereumResetAllowanceTask.js +2 -2
  96. package/dist/esm/core/tasks/EthereumResetAllowanceTask.js.map +1 -1
  97. package/dist/esm/core/tasks/EthereumSetAllowanceTask.js +2 -3
  98. package/dist/esm/core/tasks/EthereumSetAllowanceTask.js.map +1 -1
  99. package/dist/esm/core/tasks/EthereumSignAndExecuteTask.d.ts.map +1 -1
  100. package/dist/esm/core/tasks/EthereumSignAndExecuteTask.js +2 -1
  101. package/dist/esm/core/tasks/EthereumSignAndExecuteTask.js.map +1 -1
  102. package/dist/esm/core/tasks/EthereumStandardSignAndExecuteTask.d.ts.map +1 -1
  103. package/dist/esm/core/tasks/EthereumStandardSignAndExecuteTask.js +3 -1
  104. package/dist/esm/core/tasks/EthereumStandardSignAndExecuteTask.js.map +1 -1
  105. package/dist/esm/core/tasks/EthereumWaitForTransactionTask.d.ts.map +1 -1
  106. package/dist/esm/core/tasks/EthereumWaitForTransactionTask.js +9 -2
  107. package/dist/esm/core/tasks/EthereumWaitForTransactionTask.js.map +1 -1
  108. package/dist/esm/core/tasks/helpers/getEthereumExecutionStrategy.d.ts +6 -0
  109. package/dist/esm/core/tasks/helpers/getEthereumExecutionStrategy.d.ts.map +1 -1
  110. package/dist/esm/core/tasks/helpers/getEthereumExecutionStrategy.js +15 -1
  111. package/dist/esm/core/tasks/helpers/getEthereumExecutionStrategy.js.map +1 -1
  112. package/dist/esm/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.d.ts +17 -0
  113. package/dist/esm/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.d.ts.map +1 -0
  114. package/dist/esm/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.js +26 -0
  115. package/dist/esm/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.js.map +1 -0
  116. package/dist/esm/core/tasks/helpers/resolveAllowanceSpender.d.ts +16 -0
  117. package/dist/esm/core/tasks/helpers/resolveAllowanceSpender.d.ts.map +1 -0
  118. package/dist/esm/core/tasks/helpers/resolveAllowanceSpender.js +20 -0
  119. package/dist/esm/core/tasks/helpers/resolveAllowanceSpender.js.map +1 -0
  120. package/dist/esm/core/tasks/helpers/shouldCheckForAllowance.d.ts.map +1 -1
  121. package/dist/esm/core/tasks/helpers/shouldCheckForAllowance.js +2 -1
  122. package/dist/esm/core/tasks/helpers/shouldCheckForAllowance.js.map +1 -1
  123. package/dist/esm/core/tasks/helpers/signHyperliquidTypedData.d.ts +5 -1
  124. package/dist/esm/core/tasks/helpers/signHyperliquidTypedData.d.ts.map +1 -1
  125. package/dist/esm/core/tasks/helpers/signHyperliquidTypedData.js +8 -1
  126. package/dist/esm/core/tasks/helpers/signHyperliquidTypedData.js.map +1 -1
  127. package/dist/esm/core/tasks/helpers/signTypedDataEntries.d.ts +6 -2
  128. package/dist/esm/core/tasks/helpers/signTypedDataEntries.d.ts.map +1 -1
  129. package/dist/esm/core/tasks/helpers/signTypedDataEntries.js +7 -2
  130. package/dist/esm/core/tasks/helpers/signTypedDataEntries.js.map +1 -1
  131. package/dist/esm/errors/parseEthereumErrors.d.ts.map +1 -1
  132. package/dist/esm/errors/parseEthereumErrors.js +2 -1
  133. package/dist/esm/errors/parseEthereumErrors.js.map +1 -1
  134. package/dist/esm/types.d.ts +6 -0
  135. package/dist/esm/types.d.ts.map +1 -1
  136. package/dist/esm/types.js.map +1 -1
  137. package/dist/esm/version.d.ts +1 -1
  138. package/dist/esm/version.js +1 -1
  139. package/dist/esm/version.js.map +1 -1
  140. package/package.json +3 -3
  141. package/src/EthereumProvider.ts +6 -1
  142. package/src/actions/waitForBatchTransactionReceipt.ts +25 -3
  143. package/src/actions/waitForRelayedTransactionReceipt.ts +45 -10
  144. package/src/actions/waitForSafeTransactionExecution.ts +6 -2
  145. package/src/actions/waitForTransactionReceipt.ts +6 -2
  146. package/src/client/publicClient.ts +20 -8
  147. package/src/core/EthereumStepExecutor.ts +8 -6
  148. package/src/core/tasks/EthereumBatchedSignAndExecuteTask.ts +10 -0
  149. package/src/core/tasks/EthereumCheckAllowanceTask.ts +5 -7
  150. package/src/core/tasks/EthereumPrepareTransactionTask.ts +47 -4
  151. package/src/core/tasks/EthereumRelayedSignAndExecuteTask.ts +22 -2
  152. package/src/core/tasks/EthereumRelayedWaitForTransactionTask.ts +40 -8
  153. package/src/core/tasks/EthereumResetAllowanceTask.ts +3 -6
  154. package/src/core/tasks/EthereumSetAllowanceTask.ts +4 -8
  155. package/src/core/tasks/EthereumSignAndExecuteTask.ts +5 -0
  156. package/src/core/tasks/EthereumStandardSignAndExecuteTask.ts +10 -0
  157. package/src/core/tasks/EthereumWaitForTransactionTask.ts +33 -2
  158. package/src/core/tasks/helpers/getEthereumExecutionStrategy.ts +28 -0
  159. package/src/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.ts +37 -0
  160. package/src/core/tasks/helpers/resolveAllowanceSpender.ts +24 -0
  161. package/src/core/tasks/helpers/shouldCheckForAllowance.ts +9 -5
  162. package/src/core/tasks/helpers/signHyperliquidTypedData.ts +9 -1
  163. package/src/core/tasks/helpers/signTypedDataEntries.ts +8 -2
  164. package/src/errors/parseEthereumErrors.ts +8 -4
  165. package/src/types.ts +6 -0
  166. package/src/version.ts +1 -1
@@ -11,20 +11,52 @@ import {
11
11
  import type { Hash } from 'viem'
12
12
  import type { WalletCallReceipt } from '../types.js'
13
13
 
14
+ /**
15
+ * Polls the relayer until the relayed transaction is done or has failed.
16
+ *
17
+ * @param client - {@link SDKClient}
18
+ * @param taskId - The relayer task id.
19
+ * @param step - The step the task belongs to.
20
+ * @param timeout - How long to wait for a result in milliseconds. Defaults to 24 hours.
21
+ * @param signal - Ends the wait: no status request starts after it aborts, and the request in flight and the sleep end at once.
22
+ * @returns The receipt of the relayed transaction.
23
+ * @throws {TransactionError} If the transaction failed, was not found, or no result arrived before the timeout. Only the failed transaction is a final outcome.
24
+ * @throws The abort reason of `signal` once it aborts.
25
+ */
14
26
  export const waitForRelayedTransactionReceipt = async (
15
27
  client: SDKClient,
16
28
  taskId: Hash,
17
- step: LiFiStep
29
+ step: LiFiStep,
30
+ timeout: number = 3_600_000 * 24,
31
+ signal?: AbortSignal
18
32
  ): Promise<WalletCallReceipt> => {
33
+ const startTime = Date.now()
19
34
  return waitForResult(
20
35
  async () => {
21
- const result = await getRelayedTransactionStatus(client, {
22
- taskId,
23
- fromChain: step.action.fromChainId,
24
- toChain: step.action.toChainId,
25
- ...(step.tool !== 'custom' && { bridge: step.tool }),
26
- }).catch((e) => {
27
- if (process.env.NODE_ENV === 'development') {
36
+ // Without this check, a task that stays PENDING is polled forever:
37
+ // PENDING is not an error, so the retry limit does not apply.
38
+ // TransactionFailed is not retried, so this ends the poll. Not final:
39
+ // the relayer may still execute the task, so the action keeps its task
40
+ // id and a resume waits for it again instead of signing a new one.
41
+ if (Date.now() - startTime > timeout) {
42
+ throw new TransactionError(
43
+ LiFiErrorCode.TransactionFailed,
44
+ 'Relayed transaction timed out waiting for a result.'
45
+ )
46
+ }
47
+
48
+ const result = await getRelayedTransactionStatus(
49
+ client,
50
+ {
51
+ taskId,
52
+ fromChain: step.action.fromChainId,
53
+ toChain: step.action.toChainId,
54
+ ...(step.tool !== 'custom' && { bridge: step.tool }),
55
+ },
56
+ { signal }
57
+ ).catch((e) => {
58
+ // A request that the abort ended is no failure of the relayer.
59
+ if (process.env.NODE_ENV === 'development' && !signal?.aborted) {
28
60
  console.debug('Fetching status from relayer failed.', e)
29
61
  }
30
62
  return undefined
@@ -48,7 +80,9 @@ export const waitForRelayedTransactionReceipt = async (
48
80
  case 'FAILED':
49
81
  throw new TransactionError(
50
82
  LiFiErrorCode.TransactionFailed,
51
- 'Transaction was reverted.'
83
+ 'Transaction was reverted.',
84
+ undefined,
85
+ { final: true }
52
86
  )
53
87
  default:
54
88
  throw new TransactionError(
@@ -64,6 +98,7 @@ export const waitForRelayedTransactionReceipt = async (
64
98
  error instanceof TransactionError &&
65
99
  error.code === LiFiErrorCode.TransactionFailed
66
100
  )
67
- }
101
+ },
102
+ signal
68
103
  )
69
104
  }
@@ -77,7 +77,9 @@ export async function waitForSafeTransactionExecution(
77
77
  if (!tx.isSuccessful) {
78
78
  throw new TransactionError(
79
79
  LiFiErrorCode.TransactionFailed,
80
- 'Safe transaction failed.'
80
+ 'Safe transaction failed.',
81
+ undefined,
82
+ { final: true }
81
83
  )
82
84
  }
83
85
  if (!tx.transactionHash) {
@@ -101,7 +103,9 @@ export async function waitForSafeTransactionExecution(
101
103
  if (replaced) {
102
104
  throw new TransactionError(
103
105
  LiFiErrorCode.TransactionCanceled,
104
- 'Safe transaction was replaced by another transaction.'
106
+ 'Safe transaction was replaced by another transaction.',
107
+ undefined,
108
+ { final: true }
105
109
  )
106
110
  }
107
111
  }
@@ -47,7 +47,9 @@ export async function waitForTransactionReceipt(
47
47
  if (transactionReceipt?.status === 'reverted') {
48
48
  throw new TransactionError(
49
49
  LiFiErrorCode.TransactionFailed,
50
- 'Transaction was reverted.'
50
+ 'Transaction was reverted.',
51
+ undefined,
52
+ { final: true }
51
53
  )
52
54
  }
53
55
  // We should only allow repriced transaction to continue the execution.
@@ -55,7 +57,9 @@ export async function waitForTransactionReceipt(
55
57
  if (replacementReason === 'cancelled' || replacementReason === 'replaced') {
56
58
  throw new TransactionError(
57
59
  LiFiErrorCode.TransactionCanceled,
58
- 'Transaction was canceled or replaced.'
60
+ 'Transaction was canceled or replaced.',
61
+ undefined,
62
+ { final: true }
59
63
  )
60
64
  }
61
65
 
@@ -5,8 +5,10 @@ import { type Chain, mainnet } from 'viem/chains'
5
5
  import { UNS_PROXY_READER_ADDRESSES } from '../actions/constants.js'
6
6
  import type { EthereumSDKProvider } from '../types.js'
7
7
 
8
- // cached providers
9
- const publicClients: Record<number, Client> = {}
8
+ // One client per chain. The cache holds the build promise, not the client,
9
+ // so concurrent first calls share one build: each extra client would keep
10
+ // its own fallback ranking loop running.
11
+ const publicClients = new Map<number, Promise<Client>>()
10
12
 
11
13
  /**
12
14
  * Get an instance of a provider for a specific chain
@@ -14,14 +16,26 @@ const publicClients: Record<number, Client> = {}
14
16
  * @param chainId - Id of the chain the provider is for
15
17
  * @returns The public client for the given chain
16
18
  */
17
- export const getPublicClient = async (
19
+ export const getPublicClient = (
18
20
  client: SDKClient,
19
21
  chainId: number
20
22
  ): Promise<Client> => {
21
- if (publicClients[chainId]) {
22
- return publicClients[chainId]
23
+ let publicClient = publicClients.get(chainId)
24
+ if (!publicClient) {
25
+ // A failed build is removed, so the next call builds again.
26
+ publicClient = buildPublicClient(client, chainId).catch((error) => {
27
+ publicClients.delete(chainId)
28
+ throw error
29
+ })
30
+ publicClients.set(chainId, publicClient)
23
31
  }
32
+ return publicClient
33
+ }
24
34
 
35
+ const buildPublicClient = async (
36
+ client: SDKClient,
37
+ chainId: number
38
+ ): Promise<Client> => {
25
39
  const urls = await client.getRpcUrlsByChainId(chainId)
26
40
  const fallbackTransports = urls.map((url) =>
27
41
  url.startsWith('wss')
@@ -63,7 +77,7 @@ export const getPublicClient = async (
63
77
  const provider = client.getProvider(ChainType.EVM) as
64
78
  | EthereumSDKProvider
65
79
  | undefined
66
- publicClients[chainId] = createClient({
80
+ return createClient({
67
81
  chain: chain,
68
82
  transport: fallback(
69
83
  fallbackTransports,
@@ -73,6 +87,4 @@ export const getPublicClient = async (
73
87
  multicall: true,
74
88
  },
75
89
  })
76
-
77
- return publicClients[chainId]
78
90
  }
@@ -4,6 +4,7 @@ import {
4
4
  type ExecuteStepRetryError,
5
5
  type ExecuteStepRetryParams,
6
6
  type ExecutionAction,
7
+ hasOpenTransaction,
7
8
  LiFiErrorCode,
8
9
  type LiFiStepExtended,
9
10
  type SDKClient,
@@ -170,12 +171,13 @@ export class EthereumStepExecutor extends BaseStepExecutor {
170
171
  step,
171
172
  isBridgeExecution ? 'CROSS_CHAIN' : 'SWAP'
172
173
  )
173
- firstTask =
174
- swapOrBridgeAction?.txHash || swapOrBridgeAction?.taskId
175
- ? swapOrBridgeAction?.status === 'DONE'
176
- ? EthereumWaitForTransactionStatusTask
177
- : EthereumWaitForTransactionTask
178
- : EthereumCheckBalanceTask
174
+ // Same predicate as the pre-sign guard: an open transaction is waited
175
+ // for, a FAILED action with a final outcome signs again.
176
+ firstTask = hasOpenTransaction(swapOrBridgeAction)
177
+ ? swapOrBridgeAction?.status === 'DONE'
178
+ ? EthereumWaitForTransactionStatusTask
179
+ : EthereumWaitForTransactionTask
180
+ : EthereumCheckBalanceTask
179
181
  }
180
182
 
181
183
  // Compare classes, not names: a minifier can give two task classes the
@@ -1,5 +1,7 @@
1
1
  import {
2
+ assertNoOpenTransaction,
2
3
  BaseStepExecutionTask,
4
+ CLEARED_TRANSACTION_FIELDS,
3
5
  LiFiErrorCode,
4
6
  type TaskResult,
5
7
  TransactionError,
@@ -56,6 +58,11 @@ export class EthereumBatchedSignAndExecuteTask extends BaseStepExecutionTask {
56
58
 
57
59
  calls.push(transferCall)
58
60
 
61
+ // Checked again right before the wallet: a late write of an older run
62
+ // can merge its transaction into this action during the awaits above
63
+ // (strategy, chain switch).
64
+ assertNoOpenTransaction(statusManager.findAction(step, action.type))
65
+
59
66
  const { id } = await getAction(
60
67
  updatedClient,
61
68
  sendCalls,
@@ -66,6 +73,9 @@ export class EthereumBatchedSignAndExecuteTask extends BaseStepExecutionTask {
66
73
  })
67
74
 
68
75
  statusManager.updateAction(step, action.type, 'PENDING', {
76
+ // A new transaction: nothing of the previous one may survive, least of
77
+ // all its `txFinal` verdict.
78
+ ...CLEARED_TRANSACTION_FIELDS,
69
79
  taskId: id as Hash,
70
80
  txType: 'batched',
71
81
  signedAt: Date.now(),
@@ -3,7 +3,7 @@ import type { Address } from 'viem'
3
3
  import { getAllowance } from '../../actions/getAllowance.js'
4
4
  import type { EthereumStepExecutorContext } from '../../types.js'
5
5
  import { getEthereumExecutionStrategy } from './helpers/getEthereumExecutionStrategy.js'
6
- import { resolvePermit2Support } from './helpers/resolvePermit2Support.js'
6
+ import { resolveAllowanceSpender } from './helpers/resolveAllowanceSpender.js'
7
7
 
8
8
  export class EthereumCheckAllowanceTask extends BaseStepExecutionTask {
9
9
  override async shouldRun(
@@ -13,7 +13,7 @@ export class EthereumCheckAllowanceTask extends BaseStepExecutionTask {
13
13
  }
14
14
 
15
15
  async run(context: EthereumStepExecutorContext): Promise<TaskResult> {
16
- const { step, checkClient, fromChain, client, statusManager } = context
16
+ const { step, checkClient, client, statusManager } = context
17
17
 
18
18
  // Start new allowance check
19
19
  const action = statusManager.initializeAction({
@@ -24,13 +24,10 @@ export class EthereumCheckAllowanceTask extends BaseStepExecutionTask {
24
24
  })
25
25
 
26
26
  const executionStrategy = await getEthereumExecutionStrategy(context)
27
- const permit2Supported = await resolvePermit2Support(
27
+ const { spenderAddress } = await resolveAllowanceSpender(
28
28
  context,
29
29
  executionStrategy
30
30
  )
31
- const spenderAddress = permit2Supported
32
- ? fromChain.permit2
33
- : step.estimate.approvalAddress
34
31
 
35
32
  const fromAmount = BigInt(step.action.fromAmount)
36
33
 
@@ -44,7 +41,7 @@ export class EthereumCheckAllowanceTask extends BaseStepExecutionTask {
44
41
  updatedClient,
45
42
  step.action.fromToken.address as Address,
46
43
  updatedClient.account!.address,
47
- spenderAddress as Address
44
+ spenderAddress
48
45
  )
49
46
 
50
47
  statusManager.updateAction(step, action.type, 'DONE')
@@ -55,6 +52,7 @@ export class EthereumCheckAllowanceTask extends BaseStepExecutionTask {
55
52
  hasAllowance: allowance > 0n,
56
53
  hasSufficientAllowance: fromAmount <= allowance,
57
54
  executionStrategy,
55
+ allowanceSpender: spenderAddress,
58
56
  },
59
57
  }
60
58
  }
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  BaseStepExecutionTask,
3
+ ExecuteStepRetryError,
3
4
  LiFiErrorCode,
4
5
  stepComparison,
5
6
  type TaskResult,
@@ -8,8 +9,12 @@ import {
8
9
  } from '@lifi/sdk'
9
10
  import { getMaxPriorityFeePerGas } from '../../actions/getMaxPriorityFeePerGas.js'
10
11
  import type { EthereumStepExecutorContext } from '../../types.js'
11
- import { getEthereumExecutionStrategy } from './helpers/getEthereumExecutionStrategy.js'
12
+ import {
13
+ getEthereumExecutionStrategy,
14
+ STRATEGY_AFTER_PREPARE,
15
+ } from './helpers/getEthereumExecutionStrategy.js'
12
16
  import { getUpdatedStep } from './helpers/getUpdatedStep.js'
17
+ import { isAllowancePreparedForAnotherStrategy } from './helpers/isAllowancePreparedForAnotherStrategy.js'
13
18
  import { preservePermit2Allowances } from './helpers/preservePermit2Allowances.js'
14
19
 
15
20
  export class EthereumPrepareTransactionTask extends BaseStepExecutionTask {
@@ -39,6 +44,9 @@ export class EthereumPrepareTransactionTask extends BaseStepExecutionTask {
39
44
  )
40
45
  }
41
46
 
47
+ // A replay restores it, so the replay re-quotes like this attempt did.
48
+ const typedDataBeforePrepare = step.typedData
49
+
42
50
  // Try to prepare a new transaction request and update the step with typed data
43
51
  const updatedStep = await getUpdatedStep(
44
52
  client,
@@ -75,6 +83,44 @@ export class EthereumPrepareTransactionTask extends BaseStepExecutionTask {
75
83
  )
76
84
  }
77
85
 
86
+ // Recompute execution strategy after PrepareTransaction mutates step
87
+ const executionStrategy = await getEthereumExecutionStrategy(context, true)
88
+
89
+ // The allowance tasks ran before prepare, in the strategy the step seemed
90
+ // to have. In `batched` they only queued their work. If the re-quote moves
91
+ // the step to a strategy that work does not fit, replay the step in the new
92
+ // strategy (JUMEMB-102): only the batched task sends queued calls, and the
93
+ // new strategy can need another spender. Sending the queued calls instead
94
+ // could approve that wrong spender.
95
+ //
96
+ // A first run in `standard` is not replayed: its approval can already be
97
+ // on-chain, and a signer that fails the Permit2 probe cannot use the relayed
98
+ // lane anyway. A replay is checked again, whatever its strategy.
99
+ const allowanceStrategy = context.executionStrategy
100
+ const isReplay = context.retryParams !== undefined
101
+ if (
102
+ allowanceStrategy &&
103
+ (allowanceStrategy === 'batched' || isReplay) &&
104
+ (await isAllowancePreparedForAnotherStrategy(
105
+ context,
106
+ allowanceStrategy,
107
+ executionStrategy
108
+ ))
109
+ ) {
110
+ // `executeRoute` replays a step only once, so a second change fails it.
111
+ if (isReplay) {
112
+ throw new TransactionError(
113
+ LiFiErrorCode.TransactionUnprepared,
114
+ `Unable to prepare transaction. The step resolved to the ${executionStrategy} strategy after its allowance was prepared for ${allowanceStrategy}.`
115
+ )
116
+ }
117
+ step.typedData = typedDataBeforePrepare
118
+ throw new ExecuteStepRetryError(
119
+ `The step resolved to the ${executionStrategy} strategy after its allowance was prepared for ${allowanceStrategy}; retry in that strategy`,
120
+ { [STRATEGY_AFTER_PREPARE]: executionStrategy }
121
+ )
122
+ }
123
+
78
124
  let transactionRequest: TransactionParameters | undefined
79
125
  if (step.transactionRequest) {
80
126
  // Only fetch maxPriorityFeePerGas for local accounts
@@ -126,9 +172,6 @@ export class EthereumPrepareTransactionTask extends BaseStepExecutionTask {
126
172
  }
127
173
  }
128
174
 
129
- // Recompute execution strategy after PrepareTransaction mutates step
130
- const executionStrategy = await getEthereumExecutionStrategy(context, true)
131
-
132
175
  return {
133
176
  status: 'COMPLETED',
134
177
  context: { transactionRequest, executionStrategy },
@@ -1,5 +1,7 @@
1
1
  import {
2
+ assertNoOpenTransaction,
2
3
  BaseStepExecutionTask,
4
+ CLEARED_TRANSACTION_FIELDS,
3
5
  LiFiErrorCode,
4
6
  relayTransaction,
5
7
  type SignedTypedData,
@@ -55,13 +57,20 @@ export class EthereumRelayedSignAndExecuteTask extends BaseStepExecutionTask {
55
57
  !isTypedDataAlreadySigned(currentSignedTypedData, typedData)
56
58
  )
57
59
 
60
+ // Checked again right before each signature: a late write of an older
61
+ // run can merge its transaction into this action during the awaits
62
+ // before it (strategy, chain switch, earlier prompts).
63
+ const assertNoMergedTransaction = (): void =>
64
+ assertNoOpenTransaction(statusManager.findAction(step, action.type))
65
+
58
66
  let signedTypedData: SignedTypedData[]
59
67
  if (isHyperliquidAgentStep(step)) {
60
68
  statusManager.updateAction(step, action.type, 'MESSAGE_REQUIRED')
61
69
 
62
70
  const signedResults = await signHyperliquidTypedData(
63
71
  context,
64
- unsignedTypedData
72
+ unsignedTypedData,
73
+ assertNoMergedTransaction
65
74
  )
66
75
 
67
76
  if (!signedResults) {
@@ -74,7 +83,8 @@ export class EthereumRelayedSignAndExecuteTask extends BaseStepExecutionTask {
74
83
  context,
75
84
  unsignedTypedData,
76
85
  action.type,
77
- 'MESSAGE_REQUIRED'
86
+ 'MESSAGE_REQUIRED',
87
+ assertNoMergedTransaction
78
88
  )
79
89
  if (result.status === 'PAUSED') {
80
90
  return { status: 'PAUSED' }
@@ -82,6 +92,13 @@ export class EthereumRelayedSignAndExecuteTask extends BaseStepExecutionTask {
82
92
  signedTypedData = result.signedTypedData
83
93
  }
84
94
 
95
+ // Checked once more after the wallet, also when every entry was signed
96
+ // already: a stopped run's late write can merge its task id while a
97
+ // prompt is open. Nothing awaits from here to `relayTransaction`. A
98
+ // merge during the relay await is not caught: both are relayed, and the
99
+ // write below replaces the merged task id.
100
+ assertNoMergedTransaction()
101
+
85
102
  statusManager.updateAction(step, action.type, 'PENDING')
86
103
 
87
104
  const { execution, ...stepBase } = step
@@ -91,6 +108,9 @@ export class EthereumRelayedSignAndExecuteTask extends BaseStepExecutionTask {
91
108
  })
92
109
 
93
110
  statusManager.updateAction(step, action.type, 'PENDING', {
111
+ // A new transaction: nothing of the previous one may survive, least of
112
+ // all its `txFinal` verdict.
113
+ ...CLEARED_TRANSACTION_FIELDS,
94
114
  taskId: relayedTransaction.taskId as Hash,
95
115
  txType: 'relayed',
96
116
  txLink: relayedTransaction.txLink,
@@ -6,13 +6,29 @@ import {
6
6
  } from '@lifi/sdk'
7
7
  import type { Hash } from 'viem'
8
8
  import { waitForRelayedTransactionReceipt } from '../../actions/waitForRelayedTransactionReceipt.js'
9
- import type { EthereumStepExecutorContext } from '../../types.js'
9
+ import type {
10
+ EthereumStepExecutorContext,
11
+ WalletCallReceipt,
12
+ } from '../../types.js'
10
13
  import { updateActionWithReceipt } from './helpers/updateActionWithReceipt.js'
11
14
 
15
+ /** The same check as the SDK's internal `isAbortError`. */
16
+ const isAbortError = (error: unknown): boolean =>
17
+ typeof error === 'object' &&
18
+ error !== null &&
19
+ 'name' in error &&
20
+ error.name === 'AbortError'
21
+
12
22
  export class EthereumRelayedWaitForTransactionTask extends BaseStepExecutionTask {
13
23
  async run(context: EthereumStepExecutorContext): Promise<TaskResult> {
14
- const { client, step, statusManager, fromChain, isBridgeExecution } =
15
- context
24
+ const {
25
+ client,
26
+ step,
27
+ statusManager,
28
+ fromChain,
29
+ isBridgeExecution,
30
+ signal,
31
+ } = context
16
32
 
17
33
  const action = statusManager.findAction(
18
34
  step,
@@ -25,11 +41,27 @@ export class EthereumRelayedWaitForTransactionTask extends BaseStepExecutionTask
25
41
  )
26
42
  }
27
43
 
28
- const transactionReceipt = await waitForRelayedTransactionReceipt(
29
- client,
30
- action.taskId as Hash,
31
- step
32
- )
44
+ let transactionReceipt: WalletCallReceipt
45
+ try {
46
+ // The wait starts after the relay request, so it may take the signal of
47
+ // `stopRouteExecution`.
48
+ transactionReceipt = await waitForRelayedTransactionReceipt(
49
+ client,
50
+ action.taskId as Hash,
51
+ step,
52
+ undefined,
53
+ signal
54
+ )
55
+ } catch (error) {
56
+ // `stopRouteExecution` ended the wait; the outcome of the relayed
57
+ // transaction is still unknown. Pause like a stopped step, with no
58
+ // write: the stored action keeps its task id, and a resume waits for it
59
+ // again.
60
+ if (signal?.aborted && isAbortError(error)) {
61
+ return { status: 'PAUSED' }
62
+ }
63
+ throw error
64
+ }
33
65
 
34
66
  updateActionWithReceipt(
35
67
  statusManager,
@@ -6,7 +6,7 @@ import { waitForTransactionReceipt } from '../../actions/waitForTransactionRecei
6
6
  import type { EthereumStepExecutorContext } from '../../types.js'
7
7
  import { getEthereumExecutionStrategy } from './helpers/getEthereumExecutionStrategy.js'
8
8
  import { getTxLink } from './helpers/getTxLink.js'
9
- import { resolvePermit2Support } from './helpers/resolvePermit2Support.js'
9
+ import { resolveAllowanceSpender } from './helpers/resolveAllowanceSpender.js'
10
10
 
11
11
  export class EthereumResetAllowanceTask extends BaseStepExecutionTask {
12
12
  override async shouldRun(
@@ -53,13 +53,10 @@ export class EthereumResetAllowanceTask extends BaseStepExecutionTask {
53
53
 
54
54
  const executionStrategy = await getEthereumExecutionStrategy(context)
55
55
  const batchingSupported = executionStrategy === 'batched'
56
- const permit2Supported = await resolvePermit2Support(
56
+ const { spenderAddress } = await resolveAllowanceSpender(
57
57
  context,
58
58
  executionStrategy
59
59
  )
60
- const spenderAddress = permit2Supported
61
- ? fromChain.permit2
62
- : step.estimate.approvalAddress
63
60
 
64
61
  const updatedClient = await checkClient(step)
65
62
  if (!updatedClient) {
@@ -71,7 +68,7 @@ export class EthereumResetAllowanceTask extends BaseStepExecutionTask {
71
68
  client,
72
69
  updatedClient,
73
70
  step.action.fromToken.address as Address,
74
- spenderAddress as Address,
71
+ spenderAddress,
75
72
  0n,
76
73
  executionOptions,
77
74
  batchingSupported
@@ -7,7 +7,7 @@ import type { EthereumStepExecutorContext } from '../../types.js'
7
7
  import { getApprovalAmount } from './helpers/getApprovalAmount.js'
8
8
  import { getEthereumExecutionStrategy } from './helpers/getEthereumExecutionStrategy.js'
9
9
  import { getTxLink } from './helpers/getTxLink.js'
10
- import { resolvePermit2Support } from './helpers/resolvePermit2Support.js'
10
+ import { resolveAllowanceSpender } from './helpers/resolveAllowanceSpender.js'
11
11
 
12
12
  export class EthereumSetAllowanceTask extends BaseStepExecutionTask {
13
13
  override async shouldRun(
@@ -47,7 +47,8 @@ export class EthereumSetAllowanceTask extends BaseStepExecutionTask {
47
47
 
48
48
  const executionStrategy = await getEthereumExecutionStrategy(context)
49
49
  const batchingSupported = executionStrategy === 'batched'
50
- const permit2Supported = await resolvePermit2Support(
50
+ // Check if chain has Permit2 contract deployed. Permit2 should not be available for atomic batch.
51
+ const { permit2Supported, spenderAddress } = await resolveAllowanceSpender(
51
52
  context,
52
53
  executionStrategy
53
54
  )
@@ -55,11 +56,6 @@ export class EthereumSetAllowanceTask extends BaseStepExecutionTask {
55
56
  // Set new allowance
56
57
  const approveAmount = getApprovalAmount(context, permit2Supported)
57
58
 
58
- // Check if chain has Permit2 contract deployed. Permit2 should not be available for atomic batch.
59
- const spenderAddress = permit2Supported
60
- ? fromChain.permit2
61
- : step.estimate.approvalAddress
62
-
63
59
  const updatedClient = await checkClient(step)
64
60
  if (!updatedClient) {
65
61
  return { status: 'PAUSED' }
@@ -69,7 +65,7 @@ export class EthereumSetAllowanceTask extends BaseStepExecutionTask {
69
65
  client,
70
66
  updatedClient,
71
67
  step.action.fromToken.address as Address,
72
- spenderAddress as Address,
68
+ spenderAddress,
73
69
  approveAmount,
74
70
  executionOptions,
75
71
  // We need to return the populated transaction when batching is supported
@@ -1,4 +1,5 @@
1
1
  import {
2
+ assertNoOpenTransaction,
2
3
  BaseStepExecutionTask,
3
4
  LiFiErrorCode,
4
5
  type TaskResult,
@@ -31,6 +32,10 @@ export class EthereumSignAndExecuteTask extends BaseStepExecutionTask {
31
32
  )
32
33
  }
33
34
 
35
+ // Defence in depth for every lane below: the selector never routes an open
36
+ // transaction here, and signing again could execute the swap twice.
37
+ assertNoOpenTransaction(action)
38
+
34
39
  statusManager.updateAction(step, action.type, 'ACTION_REQUIRED')
35
40
 
36
41
  if (!allowUserInteraction) {
@@ -1,5 +1,7 @@
1
1
  import {
2
+ assertNoOpenTransaction,
2
3
  BaseStepExecutionTask,
4
+ CLEARED_TRANSACTION_FIELDS,
3
5
  LiFiErrorCode,
4
6
  type TaskResult,
5
7
  TransactionError,
@@ -111,6 +113,11 @@ export class EthereumStandardSignAndExecuteTask extends BaseStepExecutionTask {
111
113
  )
112
114
  }
113
115
 
116
+ // Checked again right before the wallet: a late write of an older run
117
+ // can merge its transaction into this action during the awaits above
118
+ // (strategy, chain switch, permit prompt, gas estimate).
119
+ assertNoOpenTransaction(statusManager.findAction(step, action.type))
120
+
114
121
  const txHash = await getAction(
115
122
  updatedClient,
116
123
  sendTransaction,
@@ -135,6 +142,9 @@ export class EthereumStandardSignAndExecuteTask extends BaseStepExecutionTask {
135
142
  )
136
143
 
137
144
  statusManager.updateAction(step, action.type, 'PENDING', {
145
+ // A new transaction: nothing of the previous one may survive, least of
146
+ // all its `txFinal` verdict.
147
+ ...CLEARED_TRANSACTION_FIELDS,
138
148
  txHash: resolvedTxHash,
139
149
  txLink: getTxLink(fromChain, resolvedTxHash),
140
150
  txType: 'standard',
@@ -1,13 +1,44 @@
1
- import { BaseStepExecutionTask, type TaskResult } from '@lifi/sdk'
1
+ import {
2
+ BaseStepExecutionTask,
3
+ hasOpenTransaction,
4
+ type TaskResult,
5
+ type TransactionMethodType,
6
+ } from '@lifi/sdk'
2
7
  import type { EthereumStepExecutorContext } from '../../types.js'
3
8
  import { EthereumBatchedWaitForTransactionTask } from './EthereumBatchedWaitForTransactionTask.js'
4
9
  import { EthereumRelayedWaitForTransactionTask } from './EthereumRelayedWaitForTransactionTask.js'
5
10
  import { EthereumStandardWaitForTransactionTask } from './EthereumStandardWaitForTransactionTask.js'
6
11
  import { getEthereumExecutionStrategy } from './helpers/getEthereumExecutionStrategy.js'
7
12
 
13
+ const TRANSACTION_METHOD_TYPES: readonly TransactionMethodType[] = [
14
+ 'standard',
15
+ 'batched',
16
+ 'relayed',
17
+ ]
18
+
8
19
  export class EthereumWaitForTransactionTask extends BaseStepExecutionTask {
9
20
  async run(context: EthereumStepExecutorContext): Promise<TaskResult> {
10
- const executionStrategy = await getEthereumExecutionStrategy(context)
21
+ const { step, statusManager, isBridgeExecution } = context
22
+
23
+ const action = statusManager.findAction(
24
+ step,
25
+ isBridgeExecution ? 'CROSS_CHAIN' : 'SWAP'
26
+ )
27
+ // The lane that sent the open transaction waits for it. The sign task
28
+ // stores that lane in `txType`, and a resume keeps it. The step content
29
+ // cannot always tell it again: a step that prepare sent to the relayer
30
+ // only because it had no `transactionRequest` reads as a standard or a
31
+ // batched step on resume. The guard skips a `txType` left on an action
32
+ // without an open transaction. Routes stored without `txType` derive the
33
+ // lane from the step.
34
+ const storedStrategy =
35
+ hasOpenTransaction(action) &&
36
+ action?.txType &&
37
+ TRANSACTION_METHOD_TYPES.includes(action.txType)
38
+ ? action.txType
39
+ : undefined
40
+ const executionStrategy =
41
+ storedStrategy ?? (await getEthereumExecutionStrategy(context))
11
42
  if (executionStrategy === 'batched') {
12
43
  return new EthereumBatchedWaitForTransactionTask().run(context)
13
44
  }