@lifi/sdk-provider-ethereum 4.2.6 → 4.2.8

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 (174) hide show
  1. package/CHANGELOG.md +33 -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.d.ts +25 -1
  6. package/dist/cjs/actions/waitForBatchTransactionReceipt.js +55 -7
  7. package/dist/cjs/actions/waitForBatchTransactionReceipt.js.map +1 -1
  8. package/dist/cjs/actions/waitForRelayedTransactionReceipt.d.ts +13 -1
  9. package/dist/cjs/actions/waitForRelayedTransactionReceipt.js +19 -5
  10. package/dist/cjs/actions/waitForRelayedTransactionReceipt.js.map +1 -1
  11. package/dist/cjs/actions/waitForSafeTransactionExecution.js +2 -2
  12. package/dist/cjs/actions/waitForSafeTransactionExecution.js.map +1 -1
  13. package/dist/cjs/actions/waitForTransactionReceipt.js +2 -2
  14. package/dist/cjs/actions/waitForTransactionReceipt.js.map +1 -1
  15. package/dist/cjs/client/publicClient.js +14 -5
  16. package/dist/cjs/client/publicClient.js.map +1 -1
  17. package/dist/cjs/core/EthereumStepExecutor.js +1 -1
  18. package/dist/cjs/core/EthereumStepExecutor.js.map +1 -1
  19. package/dist/cjs/core/tasks/EthereumBatchedSignAndExecuteTask.js +3 -0
  20. package/dist/cjs/core/tasks/EthereumBatchedSignAndExecuteTask.js.map +1 -1
  21. package/dist/cjs/core/tasks/EthereumBatchedWaitForTransactionTask.js +7 -3
  22. package/dist/cjs/core/tasks/EthereumBatchedWaitForTransactionTask.js.map +1 -1
  23. package/dist/cjs/core/tasks/EthereumCheckAllowanceTask.js +5 -4
  24. package/dist/cjs/core/tasks/EthereumCheckAllowanceTask.js.map +1 -1
  25. package/dist/cjs/core/tasks/EthereumPrepareTransactionTask.js +10 -1
  26. package/dist/cjs/core/tasks/EthereumPrepareTransactionTask.js.map +1 -1
  27. package/dist/cjs/core/tasks/EthereumRelayedSignAndExecuteTask.js +5 -2
  28. package/dist/cjs/core/tasks/EthereumRelayedSignAndExecuteTask.js.map +1 -1
  29. package/dist/cjs/core/tasks/EthereumRelayedWaitForTransactionTask.js +10 -2
  30. package/dist/cjs/core/tasks/EthereumRelayedWaitForTransactionTask.js.map +1 -1
  31. package/dist/cjs/core/tasks/EthereumResetAllowanceTask.js +2 -2
  32. package/dist/cjs/core/tasks/EthereumResetAllowanceTask.js.map +1 -1
  33. package/dist/cjs/core/tasks/EthereumSetAllowanceTask.js +2 -3
  34. package/dist/cjs/core/tasks/EthereumSetAllowanceTask.js.map +1 -1
  35. package/dist/cjs/core/tasks/EthereumSignAndExecuteTask.js +1 -0
  36. package/dist/cjs/core/tasks/EthereumSignAndExecuteTask.js.map +1 -1
  37. package/dist/cjs/core/tasks/EthereumStandardSignAndExecuteTask.js +2 -0
  38. package/dist/cjs/core/tasks/EthereumStandardSignAndExecuteTask.js.map +1 -1
  39. package/dist/cjs/core/tasks/EthereumWaitForTransactionTask.js +8 -1
  40. package/dist/cjs/core/tasks/EthereumWaitForTransactionTask.js.map +1 -1
  41. package/dist/cjs/core/tasks/helpers/getEthereumExecutionStrategy.d.ts +6 -0
  42. package/dist/cjs/core/tasks/helpers/getEthereumExecutionStrategy.js +15 -0
  43. package/dist/cjs/core/tasks/helpers/getEthereumExecutionStrategy.js.map +1 -1
  44. package/dist/cjs/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.d.ts +17 -0
  45. package/dist/cjs/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.js +27 -0
  46. package/dist/cjs/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.js.map +1 -0
  47. package/dist/cjs/core/tasks/helpers/resolveAllowanceSpender.d.ts +16 -0
  48. package/dist/cjs/core/tasks/helpers/resolveAllowanceSpender.js +21 -0
  49. package/dist/cjs/core/tasks/helpers/resolveAllowanceSpender.js.map +1 -0
  50. package/dist/cjs/core/tasks/helpers/shouldCheckForAllowance.js +2 -1
  51. package/dist/cjs/core/tasks/helpers/shouldCheckForAllowance.js.map +1 -1
  52. package/dist/cjs/core/tasks/helpers/signHyperliquidTypedData.d.ts +5 -1
  53. package/dist/cjs/core/tasks/helpers/signHyperliquidTypedData.js +8 -1
  54. package/dist/cjs/core/tasks/helpers/signHyperliquidTypedData.js.map +1 -1
  55. package/dist/cjs/core/tasks/helpers/signTypedDataEntries.d.ts +6 -2
  56. package/dist/cjs/core/tasks/helpers/signTypedDataEntries.js +7 -2
  57. package/dist/cjs/core/tasks/helpers/signTypedDataEntries.js.map +1 -1
  58. package/dist/cjs/errors/parseEthereumErrors.js +10 -2
  59. package/dist/cjs/errors/parseEthereumErrors.js.map +1 -1
  60. package/dist/cjs/types.d.ts +6 -0
  61. package/dist/cjs/types.js.map +1 -1
  62. package/dist/cjs/version.d.ts +1 -1
  63. package/dist/cjs/version.js +1 -1
  64. package/dist/cjs/version.js.map +1 -1
  65. package/dist/esm/EthereumProvider.d.ts.map +1 -1
  66. package/dist/esm/EthereumProvider.js +5 -1
  67. package/dist/esm/EthereumProvider.js.map +1 -1
  68. package/dist/esm/actions/waitForBatchTransactionReceipt.d.ts +25 -1
  69. package/dist/esm/actions/waitForBatchTransactionReceipt.d.ts.map +1 -1
  70. package/dist/esm/actions/waitForBatchTransactionReceipt.js +55 -7
  71. package/dist/esm/actions/waitForBatchTransactionReceipt.js.map +1 -1
  72. package/dist/esm/actions/waitForRelayedTransactionReceipt.d.ts +13 -1
  73. package/dist/esm/actions/waitForRelayedTransactionReceipt.d.ts.map +1 -1
  74. package/dist/esm/actions/waitForRelayedTransactionReceipt.js +19 -5
  75. package/dist/esm/actions/waitForRelayedTransactionReceipt.js.map +1 -1
  76. package/dist/esm/actions/waitForSafeTransactionExecution.js +2 -2
  77. package/dist/esm/actions/waitForSafeTransactionExecution.js.map +1 -1
  78. package/dist/esm/actions/waitForTransactionReceipt.js +2 -2
  79. package/dist/esm/actions/waitForTransactionReceipt.js.map +1 -1
  80. package/dist/esm/client/publicClient.d.ts.map +1 -1
  81. package/dist/esm/client/publicClient.js +14 -5
  82. package/dist/esm/client/publicClient.js.map +1 -1
  83. package/dist/esm/core/EthereumStepExecutor.d.ts.map +1 -1
  84. package/dist/esm/core/EthereumStepExecutor.js +2 -2
  85. package/dist/esm/core/EthereumStepExecutor.js.map +1 -1
  86. package/dist/esm/core/tasks/EthereumBatchedSignAndExecuteTask.d.ts.map +1 -1
  87. package/dist/esm/core/tasks/EthereumBatchedSignAndExecuteTask.js +4 -1
  88. package/dist/esm/core/tasks/EthereumBatchedSignAndExecuteTask.js.map +1 -1
  89. package/dist/esm/core/tasks/EthereumBatchedWaitForTransactionTask.js +7 -3
  90. package/dist/esm/core/tasks/EthereumBatchedWaitForTransactionTask.js.map +1 -1
  91. package/dist/esm/core/tasks/EthereumCheckAllowanceTask.js +5 -4
  92. package/dist/esm/core/tasks/EthereumCheckAllowanceTask.js.map +1 -1
  93. package/dist/esm/core/tasks/EthereumPrepareTransactionTask.d.ts.map +1 -1
  94. package/dist/esm/core/tasks/EthereumPrepareTransactionTask.js +12 -3
  95. package/dist/esm/core/tasks/EthereumPrepareTransactionTask.js.map +1 -1
  96. package/dist/esm/core/tasks/EthereumRelayedSignAndExecuteTask.d.ts.map +1 -1
  97. package/dist/esm/core/tasks/EthereumRelayedSignAndExecuteTask.js +6 -3
  98. package/dist/esm/core/tasks/EthereumRelayedSignAndExecuteTask.js.map +1 -1
  99. package/dist/esm/core/tasks/EthereumRelayedWaitForTransactionTask.d.ts.map +1 -1
  100. package/dist/esm/core/tasks/EthereumRelayedWaitForTransactionTask.js +10 -2
  101. package/dist/esm/core/tasks/EthereumRelayedWaitForTransactionTask.js.map +1 -1
  102. package/dist/esm/core/tasks/EthereumResetAllowanceTask.js +2 -2
  103. package/dist/esm/core/tasks/EthereumResetAllowanceTask.js.map +1 -1
  104. package/dist/esm/core/tasks/EthereumSetAllowanceTask.js +2 -3
  105. package/dist/esm/core/tasks/EthereumSetAllowanceTask.js.map +1 -1
  106. package/dist/esm/core/tasks/EthereumSignAndExecuteTask.d.ts.map +1 -1
  107. package/dist/esm/core/tasks/EthereumSignAndExecuteTask.js +2 -1
  108. package/dist/esm/core/tasks/EthereumSignAndExecuteTask.js.map +1 -1
  109. package/dist/esm/core/tasks/EthereumStandardSignAndExecuteTask.d.ts.map +1 -1
  110. package/dist/esm/core/tasks/EthereumStandardSignAndExecuteTask.js +3 -1
  111. package/dist/esm/core/tasks/EthereumStandardSignAndExecuteTask.js.map +1 -1
  112. package/dist/esm/core/tasks/EthereumWaitForTransactionTask.d.ts.map +1 -1
  113. package/dist/esm/core/tasks/EthereumWaitForTransactionTask.js +9 -2
  114. package/dist/esm/core/tasks/EthereumWaitForTransactionTask.js.map +1 -1
  115. package/dist/esm/core/tasks/helpers/getEthereumExecutionStrategy.d.ts +6 -0
  116. package/dist/esm/core/tasks/helpers/getEthereumExecutionStrategy.d.ts.map +1 -1
  117. package/dist/esm/core/tasks/helpers/getEthereumExecutionStrategy.js +15 -1
  118. package/dist/esm/core/tasks/helpers/getEthereumExecutionStrategy.js.map +1 -1
  119. package/dist/esm/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.d.ts +17 -0
  120. package/dist/esm/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.d.ts.map +1 -0
  121. package/dist/esm/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.js +26 -0
  122. package/dist/esm/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.js.map +1 -0
  123. package/dist/esm/core/tasks/helpers/resolveAllowanceSpender.d.ts +16 -0
  124. package/dist/esm/core/tasks/helpers/resolveAllowanceSpender.d.ts.map +1 -0
  125. package/dist/esm/core/tasks/helpers/resolveAllowanceSpender.js +20 -0
  126. package/dist/esm/core/tasks/helpers/resolveAllowanceSpender.js.map +1 -0
  127. package/dist/esm/core/tasks/helpers/shouldCheckForAllowance.d.ts.map +1 -1
  128. package/dist/esm/core/tasks/helpers/shouldCheckForAllowance.js +2 -1
  129. package/dist/esm/core/tasks/helpers/shouldCheckForAllowance.js.map +1 -1
  130. package/dist/esm/core/tasks/helpers/signHyperliquidTypedData.d.ts +5 -1
  131. package/dist/esm/core/tasks/helpers/signHyperliquidTypedData.d.ts.map +1 -1
  132. package/dist/esm/core/tasks/helpers/signHyperliquidTypedData.js +8 -1
  133. package/dist/esm/core/tasks/helpers/signHyperliquidTypedData.js.map +1 -1
  134. package/dist/esm/core/tasks/helpers/signTypedDataEntries.d.ts +6 -2
  135. package/dist/esm/core/tasks/helpers/signTypedDataEntries.d.ts.map +1 -1
  136. package/dist/esm/core/tasks/helpers/signTypedDataEntries.js +7 -2
  137. package/dist/esm/core/tasks/helpers/signTypedDataEntries.js.map +1 -1
  138. package/dist/esm/errors/parseEthereumErrors.d.ts.map +1 -1
  139. package/dist/esm/errors/parseEthereumErrors.js +10 -2
  140. package/dist/esm/errors/parseEthereumErrors.js.map +1 -1
  141. package/dist/esm/types.d.ts +6 -0
  142. package/dist/esm/types.d.ts.map +1 -1
  143. package/dist/esm/types.js.map +1 -1
  144. package/dist/esm/version.d.ts +1 -1
  145. package/dist/esm/version.js +1 -1
  146. package/dist/esm/version.js.map +1 -1
  147. package/package.json +3 -3
  148. package/src/EthereumProvider.ts +6 -1
  149. package/src/actions/waitForBatchTransactionReceipt.ts +119 -13
  150. package/src/actions/waitForRelayedTransactionReceipt.ts +45 -10
  151. package/src/actions/waitForSafeTransactionExecution.ts +6 -2
  152. package/src/actions/waitForTransactionReceipt.ts +6 -2
  153. package/src/client/publicClient.ts +20 -8
  154. package/src/core/EthereumStepExecutor.ts +8 -6
  155. package/src/core/tasks/EthereumBatchedSignAndExecuteTask.ts +11 -0
  156. package/src/core/tasks/EthereumBatchedWaitForTransactionTask.ts +17 -13
  157. package/src/core/tasks/EthereumCheckAllowanceTask.ts +5 -7
  158. package/src/core/tasks/EthereumPrepareTransactionTask.ts +47 -4
  159. package/src/core/tasks/EthereumRelayedSignAndExecuteTask.ts +22 -2
  160. package/src/core/tasks/EthereumRelayedWaitForTransactionTask.ts +40 -8
  161. package/src/core/tasks/EthereumResetAllowanceTask.ts +3 -6
  162. package/src/core/tasks/EthereumSetAllowanceTask.ts +4 -8
  163. package/src/core/tasks/EthereumSignAndExecuteTask.ts +5 -0
  164. package/src/core/tasks/EthereumStandardSignAndExecuteTask.ts +10 -0
  165. package/src/core/tasks/EthereumWaitForTransactionTask.ts +33 -2
  166. package/src/core/tasks/helpers/getEthereumExecutionStrategy.ts +28 -0
  167. package/src/core/tasks/helpers/isAllowancePreparedForAnotherStrategy.ts +37 -0
  168. package/src/core/tasks/helpers/resolveAllowanceSpender.ts +24 -0
  169. package/src/core/tasks/helpers/shouldCheckForAllowance.ts +9 -5
  170. package/src/core/tasks/helpers/signHyperliquidTypedData.ts +9 -1
  171. package/src/core/tasks/helpers/signTypedDataEntries.ts +8 -2
  172. package/src/errors/parseEthereumErrors.ts +18 -10
  173. package/src/types.ts +6 -0
  174. package/src/version.ts +1 -1
@@ -37,19 +37,23 @@ export class EthereumBatchedWaitForTransactionTask extends BaseStepExecutionTask
37
37
  const transactionReceipt = await waitForBatchTransactionReceipt(
38
38
  updatedClient,
39
39
  action.taskId as Hash,
40
- (result) => {
41
- const receipt = result.receipts?.find((r) => r.status === 'reverted') as
42
- | WalletCallReceipt
43
- | undefined
44
- if (receipt) {
45
- updateActionWithReceipt(
46
- statusManager,
47
- step,
48
- fromChain,
49
- receipt,
50
- action
51
- )
52
- }
40
+ {
41
+ onFailed: (result) => {
42
+ const receipt = result.receipts?.find(
43
+ (r) => r.status === 'reverted'
44
+ ) as WalletCallReceipt | undefined
45
+ if (receipt) {
46
+ updateActionWithReceipt(
47
+ statusManager,
48
+ step,
49
+ fromChain,
50
+ receipt,
51
+ action
52
+ )
53
+ }
54
+ },
55
+ signedAt: step.execution?.signedAt,
56
+ callCount: action.callCount,
53
57
  }
54
58
  )
55
59
 
@@ -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
  }
@@ -3,6 +3,26 @@ import { isBatchingSupported } from '../../../actions/isBatchingSupported.js'
3
3
  import type { EthereumStepExecutorContext } from '../../../types.js'
4
4
  import { isPermit2AllowanceLane } from '../../../utils/getTypedDataLane.js'
5
5
 
6
+ /**
7
+ * Retry param set by `EthereumPrepareTransactionTask`: the strategy prepare
8
+ * established when the allowance work done for `batched` did not fit it. The
9
+ * replay starts in this strategy.
10
+ */
11
+ export const STRATEGY_AFTER_PREPARE = 'strategyAfterPrepare'
12
+
13
+ // A `Record` over the union, so a new strategy fails to compile here instead of
14
+ // being ignored by the replay.
15
+ const TRANSACTION_METHOD_TYPES: Record<TransactionMethodType, true> = {
16
+ standard: true,
17
+ relayed: true,
18
+ batched: true,
19
+ }
20
+
21
+ const isTransactionMethodType = (
22
+ value: unknown
23
+ ): value is TransactionMethodType =>
24
+ typeof value === 'string' && Object.hasOwn(TRANSACTION_METHOD_TYPES, value)
25
+
6
26
  /**
7
27
  * Determines the execution strategy: 'relayed', 'batched', or 'standard'.
8
28
  * Falls back to 'standard' when EIP-5792 batching is unavailable,
@@ -32,6 +52,14 @@ export async function getEthereumExecutionStrategy(
32
52
  return executionStrategyContext
33
53
  }
34
54
 
55
+ // A replay starts in the strategy its first prepare established. Prepare
56
+ // restored the step's typed data before the replay, so nothing on the step
57
+ // shows that strategy yet.
58
+ const strategyAfterPrepare = retryParams?.[STRATEGY_AFTER_PREPARE]
59
+ if (!afterPrepare && isTransactionMethodType(strategyAfterPrepare)) {
60
+ return strategyAfterPrepare
61
+ }
62
+
35
63
  const atomicityNotReady = !!retryParams?.atomicityNotReady
36
64
  // Declared by the backend. It is the only signal that reaches the allowance
37
65
  // tasks, which run before a `transactionRequest` can exist.
@@ -0,0 +1,37 @@
1
+ import type { TransactionMethodType } from '@lifi/sdk'
2
+ import type { EthereumStepExecutorContext } from '../../../types.js'
3
+ import { resolveAllowanceSpender } from './resolveAllowanceSpender.js'
4
+
5
+ /**
6
+ * Whether the allowance work done in `preparedFor` does not fit `needed`, the
7
+ * strategy prepare established. Two things can break:
8
+ * - Calls queued for a batch are sent only by the batched task.
9
+ * - The spender: `resolvePermit2Support` never picks Permit2 for `batched`, but
10
+ * can for the other strategies. An allowance that is sufficient for the wrong
11
+ * spender queues nothing, so the calls alone do not show it.
12
+ *
13
+ * The spender recorded by `EthereumCheckAllowanceTask` is compared, because
14
+ * prepare has already replaced the step with the re-quote.
15
+ */
16
+ export const isAllowancePreparedForAnotherStrategy = async (
17
+ context: EthereumStepExecutorContext,
18
+ preparedFor: TransactionMethodType,
19
+ needed: TransactionMethodType
20
+ ): Promise<boolean> => {
21
+ if (preparedFor === needed) {
22
+ return false
23
+ }
24
+ if (preparedFor === 'batched' && context.calls.length) {
25
+ return true
26
+ }
27
+ // A signed native permit covers the allowance, and the re-quote carries it.
28
+ if (context.hasMatchingPermit) {
29
+ return false
30
+ }
31
+ const { allowanceSpender } = context
32
+ if (!allowanceSpender) {
33
+ return false
34
+ }
35
+ const { spenderAddress } = await resolveAllowanceSpender(context, needed)
36
+ return allowanceSpender.toLowerCase() !== spenderAddress?.toLowerCase()
37
+ }
@@ -0,0 +1,24 @@
1
+ import type { TransactionMethodType } from '@lifi/sdk'
2
+ import type { Address } from 'viem'
3
+ import type { EthereumStepExecutorContext } from '../../../types.js'
4
+ import { resolvePermit2Support } from './resolvePermit2Support.js'
5
+
6
+ /**
7
+ * The spender the allowance tasks check and approve in `strategy`: canonical
8
+ * Permit2 when the step takes the Permit2 flow there, `approvalAddress`
9
+ * otherwise. Every allowance task and the prepare-time check use this one
10
+ * derivation, so they cannot disagree.
11
+ */
12
+ export const resolveAllowanceSpender = async (
13
+ context: EthereumStepExecutorContext,
14
+ strategy: TransactionMethodType
15
+ ): Promise<{ permit2Supported: boolean; spenderAddress: Address }> => {
16
+ const permit2Supported = await resolvePermit2Support(context, strategy)
17
+ const { fromChain, step } = context
18
+ return {
19
+ permit2Supported,
20
+ spenderAddress: (permit2Supported
21
+ ? fromChain.permit2
22
+ : step.estimate.approvalAddress) as Address,
23
+ }
24
+ }
@@ -1,4 +1,8 @@
1
- import type { LiFiStepExtended, StatusManager } from '@lifi/sdk'
1
+ import {
2
+ hasOpenTransaction,
3
+ type LiFiStepExtended,
4
+ type StatusManager,
5
+ } from '@lifi/sdk'
2
6
 
3
7
  export const shouldCheckForAllowance = (
4
8
  step: LiFiStepExtended,
@@ -11,10 +15,10 @@ export const shouldCheckForAllowance = (
11
15
  const swapOrBridgeAction = statusManager.findAction(step, exchangeActionType)
12
16
 
13
17
  return (
14
- // No existing swap/bridge transaction is pending
15
- !swapOrBridgeAction?.txHash &&
16
- // No existing swap/bridge batch/order is pending
17
- !swapOrBridgeAction?.taskId &&
18
+ // No swap/bridge transaction (hash, batch/relay id or stored bytes) that may
19
+ // still land. A FAILED action with a final outcome does not count, so the
20
+ // allowance is checked again before the new transaction.
21
+ !hasOpenTransaction(swapOrBridgeAction) &&
18
22
  // Token is not native (address is not zero)
19
23
  !isFromNativeToken &&
20
24
  // Approval address is required for allowance checks, but may be null in special cases (e.g. direct transfers)
@@ -11,9 +11,14 @@ import type { EthereumStepExecutorContext } from '../../../types.js'
11
11
  import { getDomainChainId } from '../../../utils/getDomainChainId.js'
12
12
  import { assertValidSignature } from '../../../utils/isValidSignature.js'
13
13
 
14
+ /**
15
+ * `beforeSignature` runs right before each signature, the agent's included,
16
+ * after every await before it; it throws to stop the signing.
17
+ */
14
18
  export const signHyperliquidTypedData = async (
15
19
  context: EthereumStepExecutorContext,
16
- allowanceTypedData: TypedData[]
20
+ allowanceTypedData: TypedData[],
21
+ beforeSignature?: () => void
17
22
  ): Promise<SignedTypedData[] | undefined> => {
18
23
  const {
19
24
  step,
@@ -63,6 +68,7 @@ export const signHyperliquidTypedData = async (
63
68
  return
64
69
  }
65
70
 
71
+ beforeSignature?.()
66
72
  const signature = await getAction(
67
73
  updatedClient,
68
74
  signTypedData,
@@ -85,6 +91,7 @@ export const signHyperliquidTypedData = async (
85
91
  return
86
92
  }
87
93
 
94
+ beforeSignature?.()
88
95
  const signature = await getAction(
89
96
  updatedClient,
90
97
  signTypedData,
@@ -99,6 +106,7 @@ export const signHyperliquidTypedData = async (
99
106
  assertValidSignature(signature)
100
107
  signedResults.push({ ...typedData, signature })
101
108
  } else if (isHyperliquidOrderMessage(typedData)) {
109
+ beforeSignature?.()
102
110
  const signature = await agentAccount.signTypedData({
103
111
  domain: typedData.domain,
104
112
  types: typedData.types,
@@ -14,12 +14,17 @@ export type SignTypedDataEntriesResult =
14
14
  | { status: 'PAUSED' }
15
15
  | { status: 'COMPLETED'; signedTypedData: SignedTypedData[] }
16
16
 
17
- /** Signs the entries in order, switching chains per entry, and appends the signatures. */
17
+ /**
18
+ * Signs the entries in order, switching chains per entry, and appends the
19
+ * signatures. `beforeSignature` runs right before each wallet call, after the
20
+ * chain switch; it throws to stop the signing.
21
+ */
18
22
  export async function signTypedDataEntries(
19
23
  context: EthereumStepExecutorContext,
20
24
  entries: TypedData[],
21
25
  actionType: ExecutionActionType,
22
- actionStatus: ExecutionActionStatus = 'ACTION_REQUIRED'
26
+ actionStatus: ExecutionActionStatus = 'ACTION_REQUIRED',
27
+ beforeSignature?: () => void
23
28
  ): Promise<SignTypedDataEntriesResult> {
24
29
  const {
25
30
  step,
@@ -50,6 +55,7 @@ export async function signTypedDataEntries(
50
55
  return { status: 'PAUSED' }
51
56
  }
52
57
 
58
+ beforeSignature?.()
53
59
  const signature = await getAction(
54
60
  client,
55
61
  signTypedData,