@lifi/sdk 3.0.1 → 3.0.2-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 (70) hide show
  1. package/package.json +2 -2
  2. package/src/_cjs/constants.js +2 -1
  3. package/src/_cjs/constants.js.map +1 -1
  4. package/src/_cjs/core/EVM/EVMStepExecutor.js +4 -4
  5. package/src/_cjs/core/EVM/EVMStepExecutor.js.map +1 -1
  6. package/src/_cjs/core/Solana/SolanaStepExecutor.js +18 -14
  7. package/src/_cjs/core/Solana/SolanaStepExecutor.js.map +1 -1
  8. package/src/_cjs/core/Solana/getSolanaBalance.js +1 -2
  9. package/src/_cjs/core/Solana/getSolanaBalance.js.map +1 -1
  10. package/src/_cjs/core/StatusManager.js +3 -3
  11. package/src/_cjs/core/StatusManager.js.map +1 -1
  12. package/src/_cjs/core/checkBalance.js +2 -3
  13. package/src/_cjs/core/checkBalance.js.map +1 -1
  14. package/src/_cjs/core/processMessages.js +61 -0
  15. package/src/_cjs/core/processMessages.js.map +1 -0
  16. package/src/_cjs/core/utils.js +6 -58
  17. package/src/_cjs/core/utils.js.map +1 -1
  18. package/src/_cjs/core/waitForReceivingTransaction.js +2 -2
  19. package/src/_cjs/core/waitForReceivingTransaction.js.map +1 -1
  20. package/src/_cjs/utils/errors.js +1 -0
  21. package/src/_cjs/utils/errors.js.map +1 -1
  22. package/src/_cjs/version.js +1 -1
  23. package/src/_cjs/version.js.map +1 -1
  24. package/src/_esm/constants.js +1 -0
  25. package/src/_esm/constants.js.map +1 -1
  26. package/src/_esm/core/EVM/EVMStepExecutor.js +1 -1
  27. package/src/_esm/core/EVM/EVMStepExecutor.js.map +1 -1
  28. package/src/_esm/core/Solana/SolanaStepExecutor.js +20 -16
  29. package/src/_esm/core/Solana/SolanaStepExecutor.js.map +1 -1
  30. package/src/_esm/core/Solana/getSolanaBalance.js +2 -3
  31. package/src/_esm/core/Solana/getSolanaBalance.js.map +1 -1
  32. package/src/_esm/core/StatusManager.js +1 -1
  33. package/src/_esm/core/StatusManager.js.map +1 -1
  34. package/src/_esm/core/checkBalance.js +2 -3
  35. package/src/_esm/core/checkBalance.js.map +1 -1
  36. package/src/_esm/core/processMessages.js +58 -0
  37. package/src/_esm/core/processMessages.js.map +1 -0
  38. package/src/_esm/core/utils.js +7 -58
  39. package/src/_esm/core/utils.js.map +1 -1
  40. package/src/_esm/core/waitForReceivingTransaction.js +1 -1
  41. package/src/_esm/core/waitForReceivingTransaction.js.map +1 -1
  42. package/src/_esm/utils/errors.js +1 -0
  43. package/src/_esm/utils/errors.js.map +1 -1
  44. package/src/_esm/version.js +1 -1
  45. package/src/_esm/version.js.map +1 -1
  46. package/src/_types/constants.d.ts +1 -0
  47. package/src/_types/constants.d.ts.map +1 -1
  48. package/src/_types/core/EVM/EVMStepExecutor.d.ts.map +1 -1
  49. package/src/_types/core/Solana/SolanaStepExecutor.d.ts.map +1 -1
  50. package/src/_types/core/StatusManager.d.ts.map +1 -1
  51. package/src/_types/core/checkBalance.d.ts.map +1 -1
  52. package/src/_types/core/processMessages.d.ts +4 -0
  53. package/src/_types/core/processMessages.d.ts.map +1 -0
  54. package/src/_types/core/utils.d.ts +4 -5
  55. package/src/_types/core/utils.d.ts.map +1 -1
  56. package/src/_types/utils/errors.d.ts +2 -1
  57. package/src/_types/utils/errors.d.ts.map +1 -1
  58. package/src/_types/version.d.ts +1 -1
  59. package/src/_types/version.d.ts.map +1 -1
  60. package/src/constants.ts +1 -0
  61. package/src/core/EVM/EVMStepExecutor.ts +1 -1
  62. package/src/core/Solana/SolanaStepExecutor.ts +30 -23
  63. package/src/core/Solana/getSolanaBalance.ts +2 -3
  64. package/src/core/StatusManager.ts +1 -1
  65. package/src/core/checkBalance.ts +2 -3
  66. package/src/core/processMessages.ts +81 -0
  67. package/src/core/utils.ts +9 -84
  68. package/src/core/waitForReceivingTransaction.ts +1 -1
  69. package/src/utils/errors.ts +1 -0
  70. package/src/version.ts +1 -1
@@ -1,3 +1,3 @@
1
1
  export declare const name = "@lifi/sdk";
2
- export declare const version = "3.0.1";
2
+ export declare const version = "3.0.2-beta.0";
3
3
  //# sourceMappingURL=version.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,IAAI,cAAc,CAAA;AAC/B,eAAO,MAAM,OAAO,UAAU,CAAA"}
1
+ {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,IAAI,cAAc,CAAA;AAC/B,eAAO,MAAM,OAAO,iBAAiB,CAAA"}
package/src/constants.ts CHANGED
@@ -2,3 +2,4 @@ export const AddressZero = '0x0000000000000000000000000000000000000000'
2
2
  export const AlternativeAddressZero =
3
3
  '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
4
4
  export const wrappedSolAddress = 'So11111111111111111111111111111111111111112'
5
+ export const SolSystemProgram = '11111111111111111111111111111111'
@@ -22,13 +22,13 @@ import {
22
22
  } from '../../utils/index.js'
23
23
  import { BaseStepExecutor } from '../BaseStepExecutor.js'
24
24
  import { checkBalance } from '../checkBalance.js'
25
+ import { getSubstatusMessage } from '../processMessages.js'
25
26
  import { stepComparison } from '../stepComparison.js'
26
27
  import type {
27
28
  LiFiStepExtended,
28
29
  StepExecutorOptions,
29
30
  TransactionParameters,
30
31
  } from '../types.js'
31
- import { getSubstatusMessage } from '../utils.js'
32
32
  import { waitForReceivingTransaction } from '../waitForReceivingTransaction.js'
33
33
  import { checkAllowance } from './checkAllowance.js'
34
34
  import { updateMultisigRouteProcess } from './multisig.js'
@@ -1,6 +1,7 @@
1
1
  import type { ExtendedTransactionInfo, FullStatusData } from '@lifi/types'
2
2
  import { type SignerWalletAdapter } from '@solana/wallet-adapter-base'
3
3
  import {
4
+ TransactionExpiredBlockheightExceededError,
4
5
  VersionedTransaction,
5
6
  type SendOptions,
6
7
  type SignatureResult,
@@ -16,13 +17,14 @@ import {
16
17
  } from '../../utils/index.js'
17
18
  import { BaseStepExecutor } from '../BaseStepExecutor.js'
18
19
  import { checkBalance } from '../checkBalance.js'
20
+ import { getSubstatusMessage } from '../processMessages.js'
19
21
  import { stepComparison } from '../stepComparison.js'
20
22
  import type {
21
23
  LiFiStepExtended,
22
24
  StepExecutorOptions,
23
25
  TransactionParameters,
24
26
  } from '../types.js'
25
- import { getSubstatusMessage } from '../utils.js'
27
+ import { sleep } from '../utils.js'
26
28
  import { waitForReceivingTransaction } from '../waitForReceivingTransaction.js'
27
29
  import { getSolanaConnection } from './connection.js'
28
30
 
@@ -30,7 +32,7 @@ export interface SolanaStepExecutorOptions extends StepExecutorOptions {
30
32
  walletAdapter: SignerWalletAdapter
31
33
  }
32
34
 
33
- const TX_RETRY_INTERVAL = 500
35
+ const TX_RETRY_INTERVAL = 1000
34
36
  // https://solana.com/docs/advanced/confirmation
35
37
  const TIMEOUT_PERIOD = 60_000
36
38
 
@@ -117,6 +119,10 @@ export class SolanaStepExecutor extends BaseStepExecutor {
117
119
  data: step.transactionRequest.data,
118
120
  }
119
121
 
122
+ const blockhashResult = await connection.getLatestBlockhash({
123
+ commitment: 'confirmed',
124
+ })
125
+
120
126
  if (this.executionOptions?.updateTransactionRequestHook) {
121
127
  const customizedTransactionRequest: TransactionParameters =
122
128
  await this.executionOptions.updateTransactionRequestHook({
@@ -141,14 +147,6 @@ export class SolanaStepExecutor extends BaseStepExecutor {
141
147
  base64ToUint8Array(transactionRequest.data)
142
148
  )
143
149
 
144
- const blockhashResult = await connection.getLatestBlockhashAndContext({
145
- commitment: 'confirmed',
146
- })
147
-
148
- // Update transaction recent blockhash with the latest blockhash
149
- versionedTransaction.message.recentBlockhash =
150
- blockhashResult.value.blockhash
151
-
152
150
  this.checkWalletAdapter(step)
153
151
 
154
152
  const signedTx =
@@ -179,12 +177,14 @@ export class SolanaStepExecutor extends BaseStepExecutor {
179
177
  // In the following section, we wait and constantly check for the transaction to be confirmed
180
178
  // and resend the transaction if it is not confirmed within a certain time interval
181
179
  // thus handling tx retries on the client side rather than relying on the RPC
180
+ const abortController = new AbortController()
182
181
  const confirmTransactionPromise = connection
183
182
  .confirmTransaction(
184
183
  {
185
184
  signature: txSignature,
186
- blockhash: blockhashResult.value.blockhash,
187
- lastValidBlockHeight: blockhashResult.value.lastValidBlockHeight,
185
+ blockhash: blockhashResult.blockhash,
186
+ lastValidBlockHeight: blockhashResult.lastValidBlockHeight,
187
+ abortSignal: abortController.signal,
188
188
  },
189
189
  'confirmed'
190
190
  )
@@ -194,29 +194,36 @@ export class SolanaStepExecutor extends BaseStepExecutor {
194
194
  const startTime = Date.now()
195
195
 
196
196
  while (!confirmedTx && Date.now() - startTime <= TIMEOUT_PERIOD) {
197
+ await connection.sendRawTransaction(
198
+ signedTx.serialize(),
199
+ rawTransactionOptions
200
+ )
197
201
  confirmedTx = await Promise.race([
198
202
  confirmTransactionPromise,
199
- new Promise<null>((resolve) =>
200
- setTimeout(() => {
201
- resolve(null)
202
- }, TX_RETRY_INTERVAL)
203
- ),
203
+ sleep(TX_RETRY_INTERVAL),
204
204
  ])
205
205
  if (confirmedTx) {
206
206
  break
207
207
  }
208
-
209
- await connection.sendRawTransaction(
210
- signedTx.serialize(),
211
- rawTransactionOptions
212
- )
213
208
  }
214
209
 
210
+ // Stop waiting for tx confirmation
211
+ abortController.abort()
212
+
215
213
  if (confirmedTx?.err) {
216
214
  const reason =
217
215
  typeof confirmedTx.err === 'object'
218
216
  ? JSON.stringify(confirmedTx.err)
219
217
  : confirmedTx.err
218
+ if (
219
+ confirmedTx.err instanceof
220
+ TransactionExpiredBlockheightExceededError
221
+ ) {
222
+ throw new TransactionError(
223
+ LiFiErrorCode.TransactionExpired,
224
+ `${reason}`
225
+ )
226
+ }
220
227
  throw new TransactionError(
221
228
  LiFiErrorCode.TransactionFailed,
222
229
  `Transaction failed: ${reason}`
@@ -225,7 +232,7 @@ export class SolanaStepExecutor extends BaseStepExecutor {
225
232
 
226
233
  if (!confirmedTx) {
227
234
  throw new TransactionError(
228
- LiFiErrorCode.TransactionFailed,
235
+ LiFiErrorCode.TransactionExpired,
229
236
  'Failed to land the transaction'
230
237
  )
231
238
  }
@@ -1,6 +1,6 @@
1
1
  import type { ChainId, Token, TokenAmount } from '@lifi/types'
2
2
  import { PublicKey } from '@solana/web3.js'
3
- import { wrappedSolAddress } from '../../constants.js'
3
+ import { SolSystemProgram } from '../../constants.js'
4
4
  import { getSolanaConnection } from './connection.js'
5
5
  import { TokenProgramAddress } from './types.js'
6
6
 
@@ -53,8 +53,7 @@ const getSolanaBalanceDefault = async (
53
53
  {} as Record<string, bigint>
54
54
  )
55
55
  : {}
56
- walletTokenAmounts[wrappedSolAddress] ??= 0n
57
- walletTokenAmounts[wrappedSolAddress] += solBalance
56
+ walletTokenAmounts[SolSystemProgram] = solBalance
58
57
  const tokenAmounts: TokenAmount[] = tokens.map((token) => {
59
58
  if (walletTokenAmounts[token.address]) {
60
59
  return {
@@ -7,8 +7,8 @@ import type {
7
7
  ProcessType,
8
8
  } from '@lifi/types'
9
9
  import { executionState } from './executionState.js'
10
+ import { getProcessMessage } from './processMessages.js'
10
11
  import type { LiFiStepExtended } from './types.js'
11
- import { getProcessMessage } from './utils.js'
12
12
 
13
13
  type OptionalParameters = Partial<
14
14
  Pick<
@@ -2,6 +2,7 @@ import type { LiFiStep } from '@lifi/types'
2
2
  import { formatUnits } from 'viem'
3
3
  import { getTokenBalance } from '../services/balance.js'
4
4
  import { BalanceError } from '../utils/errors.js'
5
+ import { sleep } from './utils.js'
5
6
 
6
7
  export const checkBalance = async (
7
8
  walletAddress: string,
@@ -15,9 +16,7 @@ export const checkBalance = async (
15
16
 
16
17
  if (currentBalance < neededBalance) {
17
18
  if (depth <= 3) {
18
- await new Promise((resolve) => {
19
- setTimeout(resolve, 200)
20
- })
19
+ await sleep(200)
21
20
  await checkBalance(walletAddress, step, depth + 1)
22
21
  } else if (
23
22
  (neededBalance * BigInt((1 - step.action.slippage) * 1_000_000_000)) /
@@ -0,0 +1,81 @@
1
+ import {
2
+ type ProcessStatus,
3
+ type ProcessType,
4
+ type StatusMessage,
5
+ type Substatus,
6
+ } from '@lifi/types'
7
+
8
+ const processMessages: Record<
9
+ ProcessType,
10
+ Partial<Record<ProcessStatus, string>>
11
+ > = {
12
+ TOKEN_ALLOWANCE: {
13
+ STARTED: 'Setting token allowance.',
14
+ PENDING: 'Waiting for token allowance.',
15
+ DONE: 'Token allowance set.',
16
+ },
17
+ SWITCH_CHAIN: {
18
+ PENDING: 'Chain switch required.',
19
+ DONE: 'Chain switched successfully.',
20
+ },
21
+ SWAP: {
22
+ STARTED: 'Preparing swap transaction.',
23
+ ACTION_REQUIRED: 'Please sign the transaction.',
24
+ PENDING: 'Waiting for swap transaction.',
25
+ DONE: 'Swap completed.',
26
+ },
27
+ CROSS_CHAIN: {
28
+ STARTED: 'Preparing bridge transaction.',
29
+ ACTION_REQUIRED: 'Please sign the transaction.',
30
+ PENDING: 'Waiting for bridge transaction.',
31
+ DONE: 'Bridge transaction confirmed.',
32
+ },
33
+ RECEIVING_CHAIN: {
34
+ PENDING: 'Waiting for destination chain.',
35
+ DONE: 'Bridge completed.',
36
+ },
37
+ TRANSACTION: {},
38
+ }
39
+ const substatusMessages: Record<
40
+ StatusMessage,
41
+ Partial<Record<Substatus, string>>
42
+ > = {
43
+ PENDING: {
44
+ BRIDGE_NOT_AVAILABLE: 'Bridge communication is temporarily unavailable.',
45
+ CHAIN_NOT_AVAILABLE: 'RPC communication is temporarily unavailable.',
46
+ UNKNOWN_ERROR:
47
+ 'An unexpected error occurred. Please seek assistance in the LI.FI discord server.',
48
+ WAIT_SOURCE_CONFIRMATIONS:
49
+ 'The bridge deposit has been received. The bridge is waiting for more confirmations to start the off-chain logic.',
50
+ WAIT_DESTINATION_TRANSACTION:
51
+ 'The bridge off-chain logic is being executed. Wait for the transaction to appear on the destination chain.',
52
+ },
53
+ DONE: {
54
+ PARTIAL:
55
+ 'Some of the received tokens are not the requested destination tokens.',
56
+ REFUNDED: 'The tokens were refunded to the sender address.',
57
+ COMPLETED: 'The transfer is complete.',
58
+ },
59
+ FAILED: {},
60
+ INVALID: {},
61
+ NOT_FOUND: {},
62
+ }
63
+
64
+ export function getProcessMessage(
65
+ type: ProcessType,
66
+ status: ProcessStatus
67
+ ): string | undefined {
68
+ const processMessage = processMessages[type][status]
69
+ return processMessage
70
+ }
71
+
72
+ export function getSubstatusMessage(
73
+ status: StatusMessage,
74
+ substatus?: Substatus
75
+ ): string | undefined {
76
+ if (!substatus) {
77
+ return
78
+ }
79
+ const message = substatusMessages[status][substatus]
80
+ return message
81
+ }
package/src/core/utils.ts CHANGED
@@ -1,92 +1,11 @@
1
- import {
2
- type LiFiStep,
3
- type ProcessStatus,
4
- type ProcessType,
5
- type StatusMessage,
6
- type Substatus,
7
- } from '@lifi/types'
8
-
9
- const processMessages: Record<
10
- ProcessType,
11
- Partial<Record<ProcessStatus, string>>
12
- > = {
13
- TOKEN_ALLOWANCE: {
14
- STARTED: 'Setting token allowance.',
15
- PENDING: 'Waiting for token allowance.',
16
- DONE: 'Token allowance set.',
17
- },
18
- SWITCH_CHAIN: {
19
- PENDING: 'Chain switch required.',
20
- DONE: 'Chain switched successfully.',
21
- },
22
- SWAP: {
23
- STARTED: 'Preparing swap transaction.',
24
- ACTION_REQUIRED: 'Please sign the transaction.',
25
- PENDING: 'Waiting for swap transaction.',
26
- DONE: 'Swap completed.',
27
- },
28
- CROSS_CHAIN: {
29
- STARTED: 'Preparing bridge transaction.',
30
- ACTION_REQUIRED: 'Please sign the transaction.',
31
- PENDING: 'Waiting for bridge transaction.',
32
- DONE: 'Bridge transaction confirmed.',
33
- },
34
- RECEIVING_CHAIN: {
35
- PENDING: 'Waiting for destination chain.',
36
- DONE: 'Bridge completed.',
37
- },
38
- TRANSACTION: {},
39
- }
40
- const substatusMessages: Record<
41
- StatusMessage,
42
- Partial<Record<Substatus, string>>
43
- > = {
44
- PENDING: {
45
- BRIDGE_NOT_AVAILABLE: 'Bridge communication is temporarily unavailable.',
46
- CHAIN_NOT_AVAILABLE: 'RPC communication is temporarily unavailable.',
47
- UNKNOWN_ERROR:
48
- 'An unexpected error occurred. Please seek assistance in the LI.FI discord server.',
49
- WAIT_SOURCE_CONFIRMATIONS:
50
- 'The bridge deposit has been received. The bridge is waiting for more confirmations to start the off-chain logic.',
51
- WAIT_DESTINATION_TRANSACTION:
52
- 'The bridge off-chain logic is being executed. Wait for the transaction to appear on the destination chain.',
53
- },
54
- DONE: {
55
- PARTIAL:
56
- 'Some of the received tokens are not the requested destination tokens.',
57
- REFUNDED: 'The tokens were refunded to the sender address.',
58
- COMPLETED: 'The transfer is complete.',
59
- },
60
- FAILED: {},
61
- INVALID: {},
62
- NOT_FOUND: {},
63
- }
64
-
65
- export function getProcessMessage(
66
- type: ProcessType,
67
- status: ProcessStatus
68
- ): string | undefined {
69
- const processMessage = processMessages[type][status]
70
- return processMessage
71
- }
72
-
73
- export function getSubstatusMessage(
74
- status: StatusMessage,
75
- substatus?: Substatus
76
- ): string | undefined {
77
- if (!substatus) {
78
- return
79
- }
80
- const message = substatusMessages[status][substatus]
81
- return message
82
- }
1
+ import { type LiFiStep } from '@lifi/types'
83
2
 
84
3
  /**
85
4
  * Used to check if changed exchange rate is in the range of slippage threshold.
86
5
  * We use a slippage value as a threshold to trigger the rate change hook.
87
6
  * This can result in almost doubled slippage for the user and need to be revisited.
88
- * @param oldStep
89
- * @param newStep
7
+ * @param oldStep - old step
8
+ * @param newStep - new step
90
9
  * @returns Boolean
91
10
  */
92
11
  export function checkStepSlippageThreshold(
@@ -106,3 +25,9 @@ export function checkStepSlippageThreshold(
106
25
  }
107
26
  return actualSlippage <= setSlippage
108
27
  }
28
+
29
+ export function sleep(ms: number): Promise<null> {
30
+ return new Promise((resolve) => {
31
+ setTimeout(() => resolve(null), ms)
32
+ })
33
+ }
@@ -8,7 +8,7 @@ import { getStatus } from '../services/api.js'
8
8
  import { ServerError } from '../utils/errors.js'
9
9
  import { repeatUntilDone } from '../utils/utils.js'
10
10
  import type { StatusManager } from './StatusManager.js'
11
- import { getSubstatusMessage } from './utils.js'
11
+ import { getSubstatusMessage } from './processMessages.js'
12
12
 
13
13
  const TRANSACTION_HASH_OBSERVERS: Record<string, Promise<StatusResponse>> = {}
14
14
 
@@ -28,6 +28,7 @@ export enum LiFiErrorCode {
28
28
  InsufficientFunds = 1015,
29
29
  ExchangeRateUpdateCanceled = 1016,
30
30
  WalletChangedDuringExecution = 1017,
31
+ TransactionExpired = 1018,
31
32
  }
32
33
 
33
34
  export enum EthersErrorType {
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export const name = '@lifi/sdk'
2
- export const version = '3.0.1'
2
+ export const version = '3.0.2-beta.0'