@lifi/sdk 3.3.0-alpha.2 → 3.3.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/package.json +3 -7
  2. package/src/_cjs/config.js +1 -0
  3. package/src/_cjs/config.js.map +1 -1
  4. package/src/_cjs/core/EVM/EVMStepExecutor.js +15 -3
  5. package/src/_cjs/core/EVM/EVMStepExecutor.js.map +1 -1
  6. package/src/_cjs/core/EVM/checkAllowance.js +5 -1
  7. package/src/_cjs/core/EVM/checkAllowance.js.map +1 -1
  8. package/src/_cjs/core/EVM/switchChain.js +5 -1
  9. package/src/_cjs/core/EVM/switchChain.js.map +1 -1
  10. package/src/_cjs/core/Solana/SolanaStepExecutor.js +10 -2
  11. package/src/_cjs/core/Solana/SolanaStepExecutor.js.map +1 -1
  12. package/src/_cjs/core/StatusManager.js +2 -1
  13. package/src/_cjs/core/StatusManager.js.map +1 -1
  14. package/src/_cjs/core/UTXO/UTXOStepExecutor.js +39 -17
  15. package/src/_cjs/core/UTXO/UTXOStepExecutor.js.map +1 -1
  16. package/src/_cjs/core/UTXO/getUTXOPublicClient.js +1 -0
  17. package/src/_cjs/core/UTXO/getUTXOPublicClient.js.map +1 -1
  18. package/src/_cjs/core/UTXO/parseUTXOErrors.js +4 -0
  19. package/src/_cjs/core/UTXO/parseUTXOErrors.js.map +1 -1
  20. package/src/_cjs/core/UTXO/utxo-stack/actions/getBlock.js +31 -0
  21. package/src/_cjs/core/UTXO/utxo-stack/actions/getBlock.js.map +1 -0
  22. package/src/_cjs/core/UTXO/utxo-stack/actions/getBlockStats.js +27 -0
  23. package/src/_cjs/core/UTXO/utxo-stack/actions/getBlockStats.js.map +1 -0
  24. package/src/_cjs/core/UTXO/utxo-stack/actions/getUTXOTransaction.js +18 -9
  25. package/src/_cjs/core/UTXO/utxo-stack/actions/getUTXOTransaction.js.map +1 -1
  26. package/src/_cjs/core/UTXO/utxo-stack/actions/waitForTransaction.js +185 -0
  27. package/src/_cjs/core/UTXO/utxo-stack/actions/waitForTransaction.js.map +1 -0
  28. package/src/_cjs/core/UTXO/utxo-stack/actions/watchBlockNumber.js +45 -0
  29. package/src/_cjs/core/UTXO/utxo-stack/actions/watchBlockNumber.js.map +1 -0
  30. package/src/_cjs/core/UTXO/utxo-stack/types/blockStats.js +3 -0
  31. package/src/_cjs/core/UTXO/utxo-stack/types/blockStats.js.map +1 -0
  32. package/src/_cjs/core/UTXO/utxo-stack/utils/cancelTransaction.js +51 -0
  33. package/src/_cjs/core/UTXO/utxo-stack/utils/cancelTransaction.js.map +1 -0
  34. package/src/_cjs/core/UTXO/utxo-stack/utils/modifyFee.js +50 -0
  35. package/src/_cjs/core/UTXO/utxo-stack/utils/modifyFee.js.map +1 -0
  36. package/src/_cjs/core/UTXO/utxo-stack/utils/observe.js +48 -0
  37. package/src/_cjs/core/UTXO/utxo-stack/utils/observe.js.map +1 -0
  38. package/src/_cjs/core/UTXO/utxo-stack/utils/poll.js +28 -0
  39. package/src/_cjs/core/UTXO/utxo-stack/utils/poll.js.map +1 -0
  40. package/src/_cjs/errors/constants.js +1 -0
  41. package/src/_cjs/errors/constants.js.map +1 -1
  42. package/src/_cjs/services/api.js +1 -1
  43. package/src/_cjs/services/balance.js +9 -2
  44. package/src/_cjs/services/balance.js.map +1 -1
  45. package/src/_cjs/version.js +1 -1
  46. package/src/_cjs/version.js.map +1 -1
  47. package/src/_esm/config.js +1 -0
  48. package/src/_esm/config.js.map +1 -1
  49. package/src/_esm/core/EVM/EVMStepExecutor.js +15 -3
  50. package/src/_esm/core/EVM/EVMStepExecutor.js.map +1 -1
  51. package/src/_esm/core/EVM/checkAllowance.js +5 -1
  52. package/src/_esm/core/EVM/checkAllowance.js.map +1 -1
  53. package/src/_esm/core/EVM/switchChain.js +5 -1
  54. package/src/_esm/core/EVM/switchChain.js.map +1 -1
  55. package/src/_esm/core/Solana/SolanaStepExecutor.js +10 -2
  56. package/src/_esm/core/Solana/SolanaStepExecutor.js.map +1 -1
  57. package/src/_esm/core/StatusManager.js +11 -4
  58. package/src/_esm/core/StatusManager.js.map +1 -1
  59. package/src/_esm/core/UTXO/UTXOStepExecutor.js +41 -35
  60. package/src/_esm/core/UTXO/UTXOStepExecutor.js.map +1 -1
  61. package/src/_esm/core/UTXO/getUTXOPublicClient.js +1 -0
  62. package/src/_esm/core/UTXO/getUTXOPublicClient.js.map +1 -1
  63. package/src/_esm/core/UTXO/parseUTXOErrors.js +5 -0
  64. package/src/_esm/core/UTXO/parseUTXOErrors.js.map +1 -1
  65. package/src/_esm/core/UTXO/utxo-stack/actions/getBlock.js +28 -0
  66. package/src/_esm/core/UTXO/utxo-stack/actions/getBlock.js.map +1 -0
  67. package/src/_esm/core/UTXO/utxo-stack/actions/getBlockStats.js +24 -0
  68. package/src/_esm/core/UTXO/utxo-stack/actions/getBlockStats.js.map +1 -0
  69. package/src/_esm/core/UTXO/utxo-stack/actions/getUTXOTransaction.js +18 -10
  70. package/src/_esm/core/UTXO/utxo-stack/actions/getUTXOTransaction.js.map +1 -1
  71. package/src/_esm/core/UTXO/utxo-stack/actions/waitForTransaction.js +230 -0
  72. package/src/_esm/core/UTXO/utxo-stack/actions/waitForTransaction.js.map +1 -0
  73. package/src/_esm/core/UTXO/utxo-stack/actions/watchBlockNumber.js +58 -0
  74. package/src/_esm/core/UTXO/utxo-stack/actions/watchBlockNumber.js.map +1 -0
  75. package/src/_esm/core/UTXO/utxo-stack/types/blockStats.js +2 -0
  76. package/src/_esm/core/UTXO/utxo-stack/types/blockStats.js.map +1 -0
  77. package/src/_esm/core/UTXO/utxo-stack/utils/cancelTransaction.js +55 -0
  78. package/src/_esm/core/UTXO/utxo-stack/utils/cancelTransaction.js.map +1 -0
  79. package/src/_esm/core/UTXO/utxo-stack/utils/modifyFee.js +53 -0
  80. package/src/_esm/core/UTXO/utxo-stack/utils/modifyFee.js.map +1 -0
  81. package/src/_esm/core/UTXO/utxo-stack/utils/observe.js +51 -0
  82. package/src/_esm/core/UTXO/utxo-stack/utils/observe.js.map +1 -0
  83. package/src/_esm/core/UTXO/utxo-stack/utils/poll.js +28 -0
  84. package/src/_esm/core/UTXO/utxo-stack/utils/poll.js.map +1 -0
  85. package/src/_esm/errors/constants.js +1 -0
  86. package/src/_esm/errors/constants.js.map +1 -1
  87. package/src/_esm/services/api.js +1 -1
  88. package/src/_esm/services/balance.js +9 -2
  89. package/src/_esm/services/balance.js.map +1 -1
  90. package/src/_esm/version.js +1 -1
  91. package/src/_esm/version.js.map +1 -1
  92. package/src/_types/config.d.ts.map +1 -1
  93. package/src/_types/core/EVM/EVMStepExecutor.d.ts.map +1 -1
  94. package/src/_types/core/EVM/checkAllowance.d.ts.map +1 -1
  95. package/src/_types/core/Solana/SolanaStepExecutor.d.ts.map +1 -1
  96. package/src/_types/core/StatusManager.d.ts +18 -8
  97. package/src/_types/core/StatusManager.d.ts.map +1 -1
  98. package/src/_types/core/UTXO/UTXOStepExecutor.d.ts.map +1 -1
  99. package/src/_types/core/UTXO/getUTXOPublicClient.d.ts.map +1 -1
  100. package/src/_types/core/UTXO/utxo-stack/actions/getBlock.d.ts +13 -0
  101. package/src/_types/core/UTXO/utxo-stack/actions/getBlock.d.ts.map +1 -0
  102. package/src/_types/core/UTXO/utxo-stack/actions/getBlockStats.d.ts +15 -0
  103. package/src/_types/core/UTXO/utxo-stack/actions/getBlockStats.d.ts.map +1 -0
  104. package/src/_types/core/UTXO/utxo-stack/actions/getUTXOTransaction.d.ts +2 -2
  105. package/src/_types/core/UTXO/utxo-stack/actions/getUTXOTransaction.d.ts.map +1 -1
  106. package/src/_types/core/UTXO/utxo-stack/actions/sendUTXOTransaction.d.ts +1 -1
  107. package/src/_types/core/UTXO/utxo-stack/actions/sendUTXOTransaction.d.ts.map +1 -1
  108. package/src/_types/core/UTXO/utxo-stack/actions/waitForTransaction.d.ts +76 -0
  109. package/src/_types/core/UTXO/utxo-stack/actions/waitForTransaction.d.ts.map +1 -0
  110. package/src/_types/core/UTXO/utxo-stack/actions/watchBlockNumber.d.ts +26 -0
  111. package/src/_types/core/UTXO/utxo-stack/actions/watchBlockNumber.d.ts.map +1 -0
  112. package/src/_types/core/UTXO/utxo-stack/transports/utxo/types.d.ts +16 -0
  113. package/src/_types/core/UTXO/utxo-stack/transports/utxo/types.d.ts.map +1 -1
  114. package/src/_types/core/UTXO/utxo-stack/types/blockStats.d.ts +35 -0
  115. package/src/_types/core/UTXO/utxo-stack/types/blockStats.d.ts.map +1 -0
  116. package/src/_types/core/UTXO/utxo-stack/utils/cancelTransaction.d.ts +3 -0
  117. package/src/_types/core/UTXO/utxo-stack/utils/cancelTransaction.d.ts.map +1 -0
  118. package/src/_types/core/UTXO/utxo-stack/utils/modifyFee.d.ts +3 -0
  119. package/src/_types/core/UTXO/utxo-stack/utils/modifyFee.d.ts.map +1 -0
  120. package/src/_types/core/UTXO/utxo-stack/utils/observe.d.ts +19 -0
  121. package/src/_types/core/UTXO/utxo-stack/utils/observe.d.ts.map +1 -0
  122. package/src/_types/core/UTXO/utxo-stack/utils/poll.d.ts +13 -0
  123. package/src/_types/core/UTXO/utxo-stack/utils/poll.d.ts.map +1 -0
  124. package/src/_types/errors/constants.d.ts +2 -1
  125. package/src/_types/errors/constants.d.ts.map +1 -1
  126. package/src/_types/services/balance.d.ts.map +1 -1
  127. package/src/_types/types/internal.d.ts +1 -0
  128. package/src/_types/types/internal.d.ts.map +1 -1
  129. package/src/_types/version.d.ts +1 -1
  130. package/src/_types/version.d.ts.map +1 -1
  131. package/src/config.ts +1 -0
  132. package/src/core/EVM/EVMStepExecutor.ts +12 -10
  133. package/src/core/EVM/checkAllowance.ts +4 -3
  134. package/src/core/EVM/switchChain.ts +4 -4
  135. package/src/core/Solana/SolanaStepExecutor.ts +8 -7
  136. package/src/core/StatusManager.ts +24 -22
  137. package/src/core/UTXO/UTXOStepExecutor.ts +56 -53
  138. package/src/core/UTXO/getUTXOPublicClient.ts +1 -0
  139. package/src/core/UTXO/parseUTXOErrors.ts +11 -0
  140. package/src/core/UTXO/utxo-stack/actions/getBlock.ts +58 -0
  141. package/src/core/UTXO/utxo-stack/actions/getBlockStats.ts +55 -0
  142. package/src/core/UTXO/utxo-stack/actions/getUTXOTransaction.ts +24 -11
  143. package/src/core/UTXO/utxo-stack/actions/sendUTXOTransaction.ts +1 -1
  144. package/src/core/UTXO/utxo-stack/actions/waitForTransaction.ts +387 -0
  145. package/src/core/UTXO/utxo-stack/actions/watchBlockNumber.ts +105 -0
  146. package/src/core/UTXO/utxo-stack/transports/utxo/types.ts +16 -0
  147. package/src/core/UTXO/utxo-stack/types/blockStats.ts +35 -0
  148. package/src/core/UTXO/utxo-stack/utils/cancelTransaction.ts +75 -0
  149. package/src/core/UTXO/utxo-stack/utils/modifyFee.ts +78 -0
  150. package/src/core/UTXO/utxo-stack/utils/observe.ts +81 -0
  151. package/src/core/UTXO/utxo-stack/utils/poll.ts +46 -0
  152. package/src/errors/constants.ts +1 -0
  153. package/src/services/api.ts +1 -1
  154. package/src/services/balance.ts +11 -2
  155. package/src/types/internal.ts +1 -0
  156. package/src/version.ts +1 -1
@@ -1,4 +1,5 @@
1
1
  import type {
2
+ ChainId,
2
3
  Execution,
3
4
  ExecutionStatus,
4
5
  LiFiStep,
@@ -10,19 +11,12 @@ import { executionState } from './executionState.js'
10
11
  import { getProcessMessage } from './processMessages.js'
11
12
  import type { LiFiStepExtended } from './types.js'
12
13
 
13
- type OptionalParameters = Partial<
14
- Pick<
15
- Process,
16
- | 'doneAt'
17
- | 'failedAt'
18
- | 'txHash'
19
- | 'txLink'
20
- | 'error'
21
- | 'substatus'
22
- | 'substatusMessage'
23
- | 'multisigTxHash'
24
- >
25
- >
14
+ export type FindOrCreateProcessProps = {
15
+ step: LiFiStepExtended
16
+ type: ProcessType
17
+ chainId?: ChainId
18
+ status?: ProcessStatus
19
+ }
26
20
 
27
21
  /**
28
22
  * Manages status updates of a route and provides various functions for tracking processes
@@ -88,16 +82,23 @@ export class StatusManager {
88
82
 
89
83
  /**
90
84
  * Create and push a new process into the execution.
91
- * @param step The step that should contain the new process.
92
- * @param type Type of the process. Used to identify already existing processes.
93
- * @param status By default created procces is set to the STARTED status. We can override new process with the needed status.
85
+ * @param step.step The step that should contain the new process.
86
+ * @param step.type Type of the process. Used to identify already existing processes.
87
+ * @param step.chainId Chain Id of the process.
88
+ * @param step.status By default created procces is set to the STARTED status. We can override new process with the needed status.
89
+ * @param root0
90
+ * @param root0.step
91
+ * @param root0.type
92
+ * @param root0.chainId
93
+ * @param root0.status
94
94
  * @returns Returns process.
95
95
  */
96
- findOrCreateProcess = (
97
- step: LiFiStepExtended,
98
- type: ProcessType,
99
- status?: ProcessStatus
100
- ): Process => {
96
+ findOrCreateProcess = ({
97
+ step,
98
+ type,
99
+ chainId,
100
+ status,
101
+ }: FindOrCreateProcessProps): Process => {
101
102
  if (!step.execution?.process) {
102
103
  throw new Error("Execution hasn't been initialized.")
103
104
  }
@@ -117,6 +118,7 @@ export class StatusManager {
117
118
  startedAt: Date.now(),
118
119
  message: getProcessMessage(type, status ?? 'STARTED'),
119
120
  status: status ?? 'STARTED',
121
+ chainId: chainId,
120
122
  }
121
123
 
122
124
  step.execution.process.push(newProcess)
@@ -136,7 +138,7 @@ export class StatusManager {
136
138
  step: LiFiStepExtended,
137
139
  type: ProcessType,
138
140
  status: ProcessStatus,
139
- params?: OptionalParameters
141
+ params?: Partial<Process>
140
142
  ): Process => {
141
143
  if (!step.execution) {
142
144
  throw new Error("Can't update an empty step execution.")
@@ -4,15 +4,12 @@ import {
4
4
  type FullStatusData,
5
5
  } from '@lifi/types'
6
6
  import { address, networks, Psbt } from 'bitcoinjs-lib'
7
- import { withRetry, withTimeout, type Client } from 'viem'
7
+ import { withTimeout, type Client } from 'viem'
8
8
  import { config } from '../../config.js'
9
9
  import { LiFiErrorCode } from '../../errors/constants.js'
10
10
  import { TransactionError } from '../../errors/errors.js'
11
11
  import { getStepTransaction } from '../../services/api.js'
12
- import {
13
- getTransactionFailedMessage,
14
- waitForResult,
15
- } from '../../utils/index.js'
12
+ import { getTransactionFailedMessage } from '../../utils/index.js'
16
13
  import { BaseStepExecutor } from '../BaseStepExecutor.js'
17
14
  import { checkBalance } from '../checkBalance.js'
18
15
  import { getSubstatusMessage } from '../processMessages.js'
@@ -26,6 +23,10 @@ import { waitForReceivingTransaction } from '../waitForReceivingTransaction.js'
26
23
  import { getUTXOPublicClient } from './getUTXOPublicClient.js'
27
24
  import { parseUTXOErrors } from './parseUTXOErrors.js'
28
25
  import { signPsbt } from './utxo-stack/actions/signPsbt.js'
26
+ import {
27
+ waitForTransaction,
28
+ type ReplacementReason,
29
+ } from './utxo-stack/actions/waitForTransaction.js'
29
30
 
30
31
  export interface UTXOStepExecutorOptions extends StepExecutorOptions {
31
32
  client: Client
@@ -60,22 +61,25 @@ export class UTXOStepExecutor extends BaseStepExecutor {
60
61
  const currentProcessType = isBridgeExecution ? 'CROSS_CHAIN' : 'SWAP'
61
62
 
62
63
  // STEP 2: Get transaction
63
- let process = this.statusManager.findOrCreateProcess(
64
+ let process = this.statusManager.findOrCreateProcess({
64
65
  step,
65
- currentProcessType
66
- )
66
+ type: currentProcessType,
67
+ chainId: fromChain.id,
68
+ })
67
69
 
68
70
  const publicClient = await getUTXOPublicClient(ChainId.BTC)
69
71
 
70
72
  if (process.status !== 'DONE') {
71
73
  try {
72
74
  let txHash: string
75
+ let txHex: string
73
76
  if (process.txHash) {
74
77
  // Make sure that the chain is still correct
75
78
  this.checkClient(step)
76
79
 
77
80
  // Wait for exiting transaction
78
81
  txHash = process.txHash
82
+ txHex = process.txHex
79
83
  } else {
80
84
  process = this.statusManager.updateProcess(
81
85
  step,
@@ -146,24 +150,7 @@ export class UTXOStepExecutor extends BaseStepExecutor {
146
150
 
147
151
  this.checkClient(step)
148
152
 
149
- // const toolbox = getToolboxByChain('BTC')({})
150
-
151
- // const apiClient = blockchairApi({ chain: Chain.Bitcoin })
152
- // const txFeeRate = await apiClient.getSuggestedTxFee()
153
-
154
- // const tx = await toolbox.buildTx({
155
- // assetValue: {
156
- // bigIntValue: step.transactionRequest.value,
157
- // },
158
- // recipient: step.transactionRequest.to,
159
- // memo: step.transactionRequest.data,
160
- // sender: this.client.account?.address,
161
- // chain: 'BTC',
162
- // apiClient: blockchairApi({ chain: Chain.Bitcoin }),
163
- // feeRate: txFeeRate,
164
- // })
165
-
166
- let psbtHex = transactionRequest.data // tx.psbt.toHex()
153
+ const psbtHex = transactionRequest.data
167
154
 
168
155
  const psbt = Psbt.fromHex(psbtHex, { network: networks.bitcoin })
169
156
 
@@ -190,8 +177,6 @@ export class UTXOStepExecutor extends BaseStepExecutor {
190
177
  .values()
191
178
  )
192
179
 
193
- psbtHex = psbt.toHex()
194
-
195
180
  // We give users 10 minutes to sign the transaction or it should be considered expired
196
181
  const signedPsbtHex = await withTimeout(
197
182
  () =>
@@ -211,10 +196,10 @@ export class UTXOStepExecutor extends BaseStepExecutor {
211
196
 
212
197
  const signedPsbt = Psbt.fromHex(signedPsbtHex).finalizeAllInputs()
213
198
 
214
- const transactionHex = signedPsbt.extractTransaction().toHex()
199
+ txHex = signedPsbt.extractTransaction().toHex()
215
200
 
216
201
  txHash = await publicClient.sendUTXOTransaction({
217
- hex: transactionHex,
202
+ hex: txHex,
218
203
  })
219
204
 
220
205
  process = this.statusManager.updateProcess(
@@ -224,31 +209,48 @@ export class UTXOStepExecutor extends BaseStepExecutor {
224
209
  {
225
210
  txHash: txHash,
226
211
  txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${txHash}`,
212
+ txHex,
227
213
  }
228
214
  )
229
215
  }
230
216
 
231
- // TODO: improve logic to detect RBF transactions
232
- await waitForResult(
233
- () =>
234
- withRetry(
235
- async () => {
236
- const tx = await publicClient.getUTXOTransaction({
237
- txId: txHash,
238
- })
239
- // If there are no confirmations, it means the transaction is still in the mempool
240
- if (!tx.confirmations) {
241
- return
242
- }
243
- return tx
244
- },
217
+ let replacementReason: ReplacementReason | undefined
218
+ const transaction = await waitForTransaction(publicClient, {
219
+ txId: txHash,
220
+ txHex,
221
+ senderAddress: this.client.account?.address,
222
+ onReplaced: (response) => {
223
+ replacementReason = response.reason
224
+ process = this.statusManager.updateProcess(
225
+ step,
226
+ process.type,
227
+ 'PENDING',
245
228
  {
246
- retryCount: 10,
247
- delay: 1000,
229
+ txHash: response.transaction.txid,
230
+ txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${response.transaction.txid}`,
248
231
  }
249
- ),
250
- 10_000
251
- )
232
+ )
233
+ },
234
+ })
235
+
236
+ if (replacementReason === 'cancelled') {
237
+ throw new TransactionError(
238
+ LiFiErrorCode.TransactionCanceled,
239
+ 'User canceled transaction.'
240
+ )
241
+ }
242
+
243
+ if (transaction.txid !== txHash) {
244
+ process = this.statusManager.updateProcess(
245
+ step,
246
+ process.type,
247
+ 'PENDING',
248
+ {
249
+ txHash: transaction.txid,
250
+ txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${transaction.txid}`,
251
+ }
252
+ )
253
+ }
252
254
 
253
255
  if (isBridgeExecution) {
254
256
  process = this.statusManager.updateProcess(step, process.type, 'DONE')
@@ -274,11 +276,12 @@ export class UTXOStepExecutor extends BaseStepExecutor {
274
276
  // STEP 5: Wait for the receiving chain
275
277
  const processTxHash = process.txHash
276
278
  if (isBridgeExecution) {
277
- process = this.statusManager.findOrCreateProcess(
279
+ process = this.statusManager.findOrCreateProcess({
278
280
  step,
279
- 'RECEIVING_CHAIN',
280
- 'PENDING'
281
- )
281
+ type: 'RECEIVING_CHAIN',
282
+ status: 'PENDING',
283
+ chainId: toChain.id,
284
+ })
282
285
  }
283
286
  let statusResponse: FullStatusData
284
287
  try {
@@ -48,6 +48,7 @@ export const getUTXOPublicClient = async (chainId: number) => {
48
48
  chain,
49
49
  rpcSchema: rpcSchema<UTXOSchema>(),
50
50
  transport: fallback(fallbackTransports),
51
+ pollingInterval: 10_000,
51
52
  }).extend(UTXOActions)
52
53
  publicClients[chainId] = client
53
54
  }
@@ -21,6 +21,17 @@ export const parseUTXOErrors = async (
21
21
  }
22
22
 
23
23
  const handleSpecificErrors = (e: any) => {
24
+ // txn-mempool-conflict
25
+ if (
26
+ e.details?.includes?.('conflict') ||
27
+ e.cause?.message?.includes?.('conflict')
28
+ ) {
29
+ return new TransactionError(
30
+ LiFiErrorCode.TransactionConflict,
31
+ 'Your transaction conflicts with another transaction already in the mempool. One or more inputs have been spent by another transaction.',
32
+ e
33
+ )
34
+ }
24
35
  if (e.code === 4001 || e.code === -32000 || e.cause?.includes?.('rejected')) {
25
36
  return new TransactionError(LiFiErrorCode.SignatureRejected, e.message, e)
26
37
  }
@@ -0,0 +1,58 @@
1
+ import { Block } from 'bitcoinjs-lib'
2
+ import {
3
+ BlockNotFoundError,
4
+ type Account,
5
+ type Chain,
6
+ type Client,
7
+ type Transport,
8
+ } from 'viem'
9
+ import type { UTXOSchema } from '../transports/utxo/types.js'
10
+
11
+ export type GetBlockParameters =
12
+ | {
13
+ blockHash: string
14
+ blockNumber?: never
15
+ }
16
+ | {
17
+ blockHash?: never
18
+ blockNumber: number
19
+ }
20
+
21
+ export type GetBlockReturnType = Block
22
+
23
+ export async function getBlock<
24
+ C extends Chain | undefined,
25
+ A extends Account | undefined = Account | undefined,
26
+ >(
27
+ client: Client<Transport, C, A, UTXOSchema>,
28
+ { blockHash, blockNumber }: GetBlockParameters
29
+ ): Promise<GetBlockReturnType> {
30
+ let blockHex: string | undefined
31
+ try {
32
+ let _blockHash = blockHash
33
+ if (!_blockHash && blockNumber) {
34
+ _blockHash = await client.request(
35
+ {
36
+ method: 'getblockhash',
37
+ params: [blockNumber],
38
+ },
39
+ { dedupe: true }
40
+ )
41
+ }
42
+ if (_blockHash) {
43
+ blockHex = await client.request(
44
+ {
45
+ method: 'getblock',
46
+ params: [_blockHash, 0],
47
+ },
48
+ { dedupe: true }
49
+ )
50
+ }
51
+ } catch (error) {
52
+ throw new BlockNotFoundError({ blockHash, blockNumber } as never)
53
+ }
54
+ if (!blockHex) {
55
+ throw new BlockNotFoundError({ blockHash, blockNumber } as never)
56
+ }
57
+ return Block.fromHex(blockHex)
58
+ }
@@ -0,0 +1,55 @@
1
+ import {
2
+ BlockNotFoundError,
3
+ type Account,
4
+ type Chain,
5
+ type Client,
6
+ type Transport,
7
+ } from 'viem'
8
+ import type { UTXOSchema } from '../transports/utxo/types.js'
9
+ import type { BlockStats, BlockStatsKeys } from '../types/blockStats.js'
10
+
11
+ export type GetBlockStatsParameters = (
12
+ | {
13
+ blockHash: string
14
+ blockNumber?: never
15
+ }
16
+ | {
17
+ blockHash?: never
18
+ blockNumber: number
19
+ }
20
+ ) & {
21
+ stats?: Array<BlockStatsKeys>
22
+ }
23
+
24
+ export type GetBlockStatsReturnType = BlockStats
25
+
26
+ export async function getBlockStats<
27
+ C extends Chain | undefined,
28
+ A extends Account | undefined = Account | undefined,
29
+ >(
30
+ client: Client<Transport, C, A, UTXOSchema>,
31
+ { blockHash, blockNumber, stats }: GetBlockStatsParameters
32
+ ): Promise<GetBlockStatsReturnType> {
33
+ const blockHashOrNumber = blockHash || blockNumber
34
+ if (!blockHashOrNumber) {
35
+ throw new BlockNotFoundError({ blockHash, blockNumber } as never)
36
+ }
37
+ try {
38
+ const params: [string | number, Array<BlockStatsKeys>?] = [
39
+ blockHashOrNumber,
40
+ ]
41
+ if (stats) {
42
+ params.push(stats)
43
+ }
44
+ const data = await client.request(
45
+ {
46
+ method: 'getblockstats',
47
+ params: params,
48
+ },
49
+ { dedupe: true }
50
+ )
51
+ return data
52
+ } catch (error) {
53
+ throw new BlockNotFoundError({ blockHash, blockNumber } as never)
54
+ }
55
+ }
@@ -1,4 +1,10 @@
1
- import { type Account, type Chain, type Client, type Transport } from 'viem'
1
+ import {
2
+ TransactionNotFoundError,
3
+ type Account,
4
+ type Chain,
5
+ type Client,
6
+ type Transport,
7
+ } from 'viem'
2
8
  import type { UTXOSchema } from '../transports/utxo/types.js'
3
9
  import type { UTXOTransaction } from '../types/transaction.js'
4
10
 
@@ -6,7 +12,7 @@ export type GetUTXOTransactionParameters = {
6
12
  /** The Id of the transaction */
7
13
  txId: string
8
14
  /** The block in which to look for the transaction */
9
- blockhash?: string
15
+ blockHash?: string
10
16
  }
11
17
 
12
18
  export type GetUTXOTransactionReturnType = UTXOTransaction
@@ -16,15 +22,22 @@ export async function getUTXOTransaction<
16
22
  A extends Account | undefined = Account | undefined,
17
23
  >(
18
24
  client: Client<Transport, C, A, UTXOSchema>,
19
- { txId, blockhash }: GetUTXOTransactionParameters
25
+ { txId, blockHash }: GetUTXOTransactionParameters
20
26
  ): Promise<GetUTXOTransactionReturnType> {
21
- const params: [string, boolean, string?] = [txId, true]
22
- if (blockhash) {
23
- params.push(blockhash)
27
+ try {
28
+ const params: [string, boolean, string?] = [txId, true]
29
+ if (blockHash) {
30
+ params.push(blockHash)
31
+ }
32
+ const data = await client.request({
33
+ method: 'getrawtransaction',
34
+ params: params,
35
+ })
36
+ return data
37
+ } catch (error) {
38
+ throw new TransactionNotFoundError({
39
+ blockHash: blockHash as never,
40
+ hash: txId as never,
41
+ })
24
42
  }
25
- const data = await client.request({
26
- method: 'getrawtransaction',
27
- params: params,
28
- })
29
- return data
30
43
  }
@@ -2,7 +2,7 @@ import { type Account, type Chain, type Client, type Transport } from 'viem'
2
2
  import type { UTXOSchema } from '../transports/utxo/types.js'
3
3
 
4
4
  export type SendUTXOTransactionParameters = {
5
- /** The hex string of the raw transaction */
5
+ /** The hex string of the raw transaction. */
6
6
  hex: string
7
7
  /** Rejects transactions whose fee rate is higher than the specified value, expressed in BTC/kB. Set to 0 to accept any fee rate. Default = 0.10 */
8
8
  maxFeeRate?: number