@lifi/sdk 3.0.2-beta.0 → 3.1.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 (217) hide show
  1. package/package.json +3 -3
  2. package/src/_cjs/core/EVM/EVMStepExecutor.js +12 -11
  3. package/src/_cjs/core/EVM/EVMStepExecutor.js.map +1 -1
  4. package/src/_cjs/core/EVM/checkAllowance.js +5 -10
  5. package/src/_cjs/core/EVM/checkAllowance.js.map +1 -1
  6. package/src/_cjs/core/EVM/multisig.js +4 -3
  7. package/src/_cjs/core/EVM/multisig.js.map +1 -1
  8. package/src/_cjs/core/EVM/parseEVMErrors.js +38 -0
  9. package/src/_cjs/core/EVM/parseEVMErrors.js.map +1 -0
  10. package/src/_cjs/core/EVM/publicClient.js +8 -3
  11. package/src/_cjs/core/EVM/publicClient.js.map +1 -1
  12. package/src/_cjs/core/EVM/setAllowance.js +2 -3
  13. package/src/_cjs/core/EVM/setAllowance.js.map +1 -1
  14. package/src/_cjs/core/EVM/switchChain.js +4 -3
  15. package/src/_cjs/core/EVM/switchChain.js.map +1 -1
  16. package/src/_cjs/core/Solana/SolanaStepExecutor.js +42 -24
  17. package/src/_cjs/core/Solana/SolanaStepExecutor.js.map +1 -1
  18. package/src/_cjs/core/Solana/connection.js.map +1 -1
  19. package/src/_cjs/core/Solana/parseSolanaErrors.js +33 -0
  20. package/src/_cjs/core/Solana/parseSolanaErrors.js.map +1 -0
  21. package/src/_cjs/core/checkBalance.js +2 -2
  22. package/src/_cjs/core/checkBalance.js.map +1 -1
  23. package/src/_cjs/core/stepComparison.js +3 -3
  24. package/src/_cjs/core/stepComparison.js.map +1 -1
  25. package/src/_cjs/core/waitForReceivingTransaction.js +1 -1
  26. package/src/_cjs/core/waitForReceivingTransaction.js.map +1 -1
  27. package/src/_cjs/errors/SDKError.js +48 -0
  28. package/src/_cjs/errors/SDKError.js.map +1 -0
  29. package/src/_cjs/errors/baseError.js +30 -0
  30. package/src/_cjs/errors/baseError.js.map +1 -0
  31. package/src/_cjs/errors/constants.js +48 -0
  32. package/src/_cjs/errors/constants.js.map +1 -0
  33. package/src/_cjs/errors/errors.js +48 -0
  34. package/src/_cjs/errors/errors.js.map +1 -0
  35. package/src/_cjs/errors/httpError.js +101 -0
  36. package/src/_cjs/errors/httpError.js.map +1 -0
  37. package/src/_cjs/errors/index.js +11 -0
  38. package/src/_cjs/errors/index.js.map +1 -0
  39. package/src/_cjs/errors/utils/baseErrorRootCause.js +21 -0
  40. package/src/_cjs/errors/utils/baseErrorRootCause.js.map +1 -0
  41. package/src/_cjs/errors/utils/rootCause.js +12 -0
  42. package/src/_cjs/errors/utils/rootCause.js.map +1 -0
  43. package/src/_cjs/helpers.js +13 -8
  44. package/src/_cjs/helpers.js.map +1 -1
  45. package/src/_cjs/index.js +2 -4
  46. package/src/_cjs/index.js.map +1 -1
  47. package/src/_cjs/request.js +37 -34
  48. package/src/_cjs/request.js.map +1 -1
  49. package/src/_cjs/services/api.js +61 -132
  50. package/src/_cjs/services/api.js.map +1 -1
  51. package/src/_cjs/services/balance.js +1 -1
  52. package/src/_cjs/services/balance.js.map +1 -1
  53. package/src/_cjs/types/request.js +3 -0
  54. package/src/_cjs/types/request.js.map +1 -0
  55. package/src/_cjs/utils/errors.js +0 -183
  56. package/src/_cjs/utils/errors.js.map +1 -1
  57. package/src/_cjs/utils/index.js +1 -2
  58. package/src/_cjs/utils/index.js.map +1 -1
  59. package/src/_cjs/version.js +1 -1
  60. package/src/_cjs/version.js.map +1 -1
  61. package/src/_esm/core/EVM/EVMStepExecutor.js +8 -7
  62. package/src/_esm/core/EVM/EVMStepExecutor.js.map +1 -1
  63. package/src/_esm/core/EVM/checkAllowance.js +5 -10
  64. package/src/_esm/core/EVM/checkAllowance.js.map +1 -1
  65. package/src/_esm/core/EVM/multisig.js +2 -1
  66. package/src/_esm/core/EVM/multisig.js.map +1 -1
  67. package/src/_esm/core/EVM/parseEVMErrors.js +35 -0
  68. package/src/_esm/core/EVM/parseEVMErrors.js.map +1 -0
  69. package/src/_esm/core/EVM/publicClient.js +9 -4
  70. package/src/_esm/core/EVM/publicClient.js.map +1 -1
  71. package/src/_esm/core/EVM/setAllowance.js +3 -5
  72. package/src/_esm/core/EVM/setAllowance.js.map +1 -1
  73. package/src/_esm/core/EVM/switchChain.js +2 -1
  74. package/src/_esm/core/EVM/switchChain.js.map +1 -1
  75. package/src/_esm/core/Solana/SolanaStepExecutor.js +52 -25
  76. package/src/_esm/core/Solana/SolanaStepExecutor.js.map +1 -1
  77. package/src/_esm/core/Solana/connection.js +2 -3
  78. package/src/_esm/core/Solana/connection.js.map +1 -1
  79. package/src/_esm/core/Solana/parseSolanaErrors.js +29 -0
  80. package/src/_esm/core/Solana/parseSolanaErrors.js.map +1 -0
  81. package/src/_esm/core/checkBalance.js +2 -2
  82. package/src/_esm/core/checkBalance.js.map +1 -1
  83. package/src/_esm/core/stepComparison.js +3 -3
  84. package/src/_esm/core/stepComparison.js.map +1 -1
  85. package/src/_esm/core/waitForReceivingTransaction.js +1 -1
  86. package/src/_esm/core/waitForReceivingTransaction.js.map +1 -1
  87. package/src/_esm/errors/SDKError.js +47 -0
  88. package/src/_esm/errors/SDKError.js.map +1 -0
  89. package/src/_esm/errors/baseError.js +28 -0
  90. package/src/_esm/errors/baseError.js.map +1 -0
  91. package/src/_esm/errors/constants.js +45 -0
  92. package/src/_esm/errors/constants.js.map +1 -0
  93. package/src/_esm/errors/errors.js +38 -0
  94. package/src/_esm/errors/errors.js.map +1 -0
  95. package/src/_esm/errors/httpError.js +97 -0
  96. package/src/_esm/errors/httpError.js.map +1 -0
  97. package/src/_esm/errors/index.js +8 -0
  98. package/src/_esm/errors/index.js.map +1 -0
  99. package/src/_esm/errors/utils/baseErrorRootCause.js +16 -0
  100. package/src/_esm/errors/utils/baseErrorRootCause.js.map +1 -0
  101. package/src/_esm/errors/utils/rootCause.js +8 -0
  102. package/src/_esm/errors/utils/rootCause.js.map +1 -0
  103. package/src/_esm/helpers.js +16 -9
  104. package/src/_esm/helpers.js.map +1 -1
  105. package/src/_esm/index.js +2 -2
  106. package/src/_esm/index.js.map +1 -1
  107. package/src/_esm/request.js +38 -35
  108. package/src/_esm/request.js.map +1 -1
  109. package/src/_esm/services/api.js +61 -133
  110. package/src/_esm/services/api.js.map +1 -1
  111. package/src/_esm/services/balance.js +4 -4
  112. package/src/_esm/services/balance.js.map +1 -1
  113. package/src/_esm/types/request.js +2 -0
  114. package/src/_esm/types/request.js.map +1 -0
  115. package/src/_esm/utils/errors.js +1 -173
  116. package/src/_esm/utils/errors.js.map +1 -1
  117. package/src/_esm/utils/index.js +1 -2
  118. package/src/_esm/utils/index.js.map +1 -1
  119. package/src/_esm/version.js +1 -1
  120. package/src/_esm/version.js.map +1 -1
  121. package/src/_types/core/EVM/EVMStepExecutor.d.ts.map +1 -1
  122. package/src/_types/core/EVM/checkAllowance.d.ts.map +1 -1
  123. package/src/_types/core/EVM/multisig.d.ts.map +1 -1
  124. package/src/_types/core/EVM/parseEVMErrors.d.ts +4 -0
  125. package/src/_types/core/EVM/parseEVMErrors.d.ts.map +1 -0
  126. package/src/_types/core/EVM/publicClient.d.ts.map +1 -1
  127. package/src/_types/core/EVM/setAllowance.d.ts +1 -2
  128. package/src/_types/core/EVM/setAllowance.d.ts.map +1 -1
  129. package/src/_types/core/EVM/switchChain.d.ts.map +1 -1
  130. package/src/_types/core/EVM/types.d.ts +3 -0
  131. package/src/_types/core/EVM/types.d.ts.map +1 -1
  132. package/src/_types/core/Solana/SolanaStepExecutor.d.ts.map +1 -1
  133. package/src/_types/core/Solana/connection.d.ts +2 -3
  134. package/src/_types/core/Solana/connection.d.ts.map +1 -1
  135. package/src/_types/core/Solana/parseSolanaErrors.d.ts +4 -0
  136. package/src/_types/core/Solana/parseSolanaErrors.d.ts.map +1 -0
  137. package/src/_types/core/stepComparison.d.ts.map +1 -1
  138. package/src/_types/core/types.d.ts +3 -0
  139. package/src/_types/core/types.d.ts.map +1 -1
  140. package/src/_types/errors/SDKError.d.ts +12 -0
  141. package/src/_types/errors/SDKError.d.ts.map +1 -0
  142. package/src/_types/errors/baseError.d.ts +7 -0
  143. package/src/_types/errors/baseError.d.ts.map +1 -0
  144. package/src/_types/errors/constants.d.ts +43 -0
  145. package/src/_types/errors/constants.d.ts.map +1 -0
  146. package/src/_types/errors/errors.d.ts +24 -0
  147. package/src/_types/errors/errors.d.ts.map +1 -0
  148. package/src/_types/errors/httpError.d.ts +21 -0
  149. package/src/_types/errors/httpError.d.ts.map +1 -0
  150. package/src/_types/errors/index.d.ts +8 -0
  151. package/src/_types/errors/index.d.ts.map +1 -0
  152. package/src/_types/errors/utils/baseErrorRootCause.d.ts +4 -0
  153. package/src/_types/errors/utils/baseErrorRootCause.d.ts.map +1 -0
  154. package/src/_types/errors/utils/rootCause.d.ts +2 -0
  155. package/src/_types/errors/utils/rootCause.d.ts.map +1 -0
  156. package/src/_types/helpers.d.ts +5 -4
  157. package/src/_types/helpers.d.ts.map +1 -1
  158. package/src/_types/index.d.ts +2 -2
  159. package/src/_types/index.d.ts.map +1 -1
  160. package/src/_types/request.d.ts +1 -5
  161. package/src/_types/request.d.ts.map +1 -1
  162. package/src/_types/services/api.d.ts.map +1 -1
  163. package/src/_types/services/balance.d.ts +3 -3
  164. package/src/_types/types/request.d.ts +4 -0
  165. package/src/_types/types/request.d.ts.map +1 -0
  166. package/src/_types/utils/errors.d.ts +0 -109
  167. package/src/_types/utils/errors.d.ts.map +1 -1
  168. package/src/_types/utils/index.d.ts +1 -2
  169. package/src/_types/utils/index.d.ts.map +1 -1
  170. package/src/_types/version.d.ts +1 -1
  171. package/src/_types/version.d.ts.map +1 -1
  172. package/src/core/EVM/EVMStepExecutor.ts +14 -12
  173. package/src/core/EVM/checkAllowance.ts +5 -10
  174. package/src/core/EVM/multisig.ts +2 -1
  175. package/src/core/EVM/parseEVMErrors.ts +60 -0
  176. package/src/core/EVM/publicClient.ts +9 -4
  177. package/src/core/EVM/setAllowance.ts +2 -6
  178. package/src/core/EVM/switchChain.ts +2 -1
  179. package/src/core/EVM/types.ts +3 -0
  180. package/src/core/Solana/SolanaStepExecutor.ts +67 -34
  181. package/src/core/Solana/connection.ts +2 -3
  182. package/src/core/Solana/parseSolanaErrors.ts +45 -0
  183. package/src/core/checkBalance.ts +2 -2
  184. package/src/core/stepComparison.ts +3 -4
  185. package/src/core/types.ts +3 -0
  186. package/src/core/waitForReceivingTransaction.ts +1 -1
  187. package/src/errors/SDKError.ts +25 -0
  188. package/src/errors/baseError.ts +22 -0
  189. package/src/errors/constants.ts +45 -0
  190. package/src/errors/errors.ts +44 -0
  191. package/src/errors/httpError.ts +93 -0
  192. package/src/errors/index.ts +7 -0
  193. package/src/errors/utils/baseErrorRootCause.ts +18 -0
  194. package/src/errors/utils/rootCause.ts +7 -0
  195. package/src/helpers.ts +23 -16
  196. package/src/index.ts +2 -2
  197. package/src/request.ts +52 -38
  198. package/src/services/api.ts +109 -157
  199. package/src/services/balance.ts +4 -4
  200. package/src/types/request.ts +3 -0
  201. package/src/utils/errors.ts +0 -233
  202. package/src/utils/index.ts +1 -2
  203. package/src/version.ts +1 -1
  204. package/src/_cjs/utils/parseBackendError.js +0 -27
  205. package/src/_cjs/utils/parseBackendError.js.map +0 -1
  206. package/src/_cjs/utils/parseError.js +0 -69
  207. package/src/_cjs/utils/parseError.js.map +0 -1
  208. package/src/_esm/utils/parseBackendError.js +0 -24
  209. package/src/_esm/utils/parseBackendError.js.map +0 -1
  210. package/src/_esm/utils/parseError.js +0 -100
  211. package/src/_esm/utils/parseError.js.map +0 -1
  212. package/src/_types/utils/parseBackendError.d.ts +0 -3
  213. package/src/_types/utils/parseBackendError.d.ts.map +0 -1
  214. package/src/_types/utils/parseError.d.ts +0 -35
  215. package/src/_types/utils/parseError.d.ts.map +0 -1
  216. package/src/utils/parseBackendError.ts +0 -50
  217. package/src/utils/parseError.ts +0 -210
@@ -27,8 +27,8 @@ import type {
27
27
  import { config } from '../config.js'
28
28
  import { request } from '../request.js'
29
29
  import { isRoutesRequest, isStep } from '../typeguards.js'
30
- import { ValidationError } from '../utils/errors.js'
31
- import { parseBackendError } from '../utils/parseBackendError.js'
30
+ import { ValidationError } from '../errors/errors.js'
31
+ import { SDKError } from '../errors/SDKError.js'
32
32
  /**
33
33
  * Fetch information about a Token
34
34
  * @param chain - Id or key of the chain that contains the token
@@ -43,25 +43,24 @@ export const getToken = async (
43
43
  options?: RequestOptions
44
44
  ): Promise<Token> => {
45
45
  if (!chain) {
46
- throw new ValidationError('Required parameter "chain" is missing.')
46
+ throw new SDKError(
47
+ new ValidationError('Required parameter "chain" is missing.')
48
+ )
47
49
  }
48
50
  if (!token) {
49
- throw new ValidationError('Required parameter "token" is missing.')
50
- }
51
- try {
52
- const response = await request<Token>(
53
- `${config.get().apiUrl}/token?${new URLSearchParams({
54
- chain,
55
- token,
56
- } as Record<string, string>)}`,
57
- {
58
- signal: options?.signal,
59
- }
51
+ throw new SDKError(
52
+ new ValidationError('Required parameter "token" is missing.')
60
53
  )
61
- return response
62
- } catch (e) {
63
- throw await parseBackendError(e)
64
54
  }
55
+ return await request<Token>(
56
+ `${config.get().apiUrl}/token?${new URLSearchParams({
57
+ chain,
58
+ token,
59
+ } as Record<string, string>)}`,
60
+ {
61
+ signal: options?.signal,
62
+ }
63
+ )
65
64
  }
66
65
 
67
66
  /**
@@ -85,8 +84,10 @@ export const getQuote = async (
85
84
  ]
86
85
  requiredParameters.forEach((requiredParameter) => {
87
86
  if (!params[requiredParameter]) {
88
- throw new ValidationError(
89
- `Required parameter "${requiredParameter}" is missing.`
87
+ throw new SDKError(
88
+ new ValidationError(
89
+ `Required parameter "${requiredParameter}" is missing.`
90
+ )
90
91
  )
91
92
  }
92
93
  })
@@ -110,19 +111,14 @@ export const getQuote = async (
110
111
  delete params[key as keyof QuoteRequest]
111
112
  )
112
113
 
113
- try {
114
- const response = await request<LiFiStep>(
115
- `${_config.apiUrl}/quote?${new URLSearchParams(
116
- params as unknown as Record<string, string>
117
- )}`,
118
- {
119
- signal: options?.signal,
120
- }
121
- )
122
- return response
123
- } catch (e) {
124
- throw await parseBackendError(e)
125
- }
114
+ return await request<LiFiStep>(
115
+ `${_config.apiUrl}/quote?${new URLSearchParams(
116
+ params as unknown as Record<string, string>
117
+ )}`,
118
+ {
119
+ signal: options?.signal,
120
+ }
121
+ )
126
122
  }
127
123
 
128
124
  /**
@@ -148,8 +144,10 @@ export const getContractCallsQuote = async (
148
144
  ]
149
145
  requiredParameters.forEach((requiredParameter) => {
150
146
  if (!params[requiredParameter]) {
151
- throw new ValidationError(
152
- `Required parameter "${requiredParameter}" is missing.`
147
+ throw new SDKError(
148
+ new ValidationError(
149
+ `Required parameter "${requiredParameter}" is missing.`
150
+ )
153
151
  )
154
152
  }
155
153
  })
@@ -167,22 +165,14 @@ export const getContractCallsQuote = async (
167
165
  params.denyExchanges ??= _config.routeOptions?.exchanges?.deny
168
166
  params.preferExchanges ??= _config.routeOptions?.exchanges?.prefer
169
167
  // send request
170
- try {
171
- const response = await request<LiFiStep>(
172
- `${_config.apiUrl}/quote/contractCalls`,
173
- {
174
- method: 'POST',
175
- headers: {
176
- 'Content-Type': 'application/json',
177
- },
178
- body: JSON.stringify(params),
179
- signal: options?.signal,
180
- }
181
- )
182
- return response
183
- } catch (e) {
184
- throw await parseBackendError(e)
185
- }
168
+ return await request<LiFiStep>(`${_config.apiUrl}/quote/contractCalls`, {
169
+ method: 'POST',
170
+ headers: {
171
+ 'Content-Type': 'application/json',
172
+ },
173
+ body: JSON.stringify(params),
174
+ signal: options?.signal,
175
+ })
186
176
  }
187
177
 
188
178
  /**
@@ -197,22 +187,19 @@ export const getStatus = async (
197
187
  options?: RequestOptions
198
188
  ): Promise<StatusResponse> => {
199
189
  if (!params.txHash) {
200
- throw new ValidationError('Required parameter "txHash" is missing.')
190
+ throw new SDKError(
191
+ new ValidationError('Required parameter "txHash" is missing.')
192
+ )
201
193
  }
202
194
  const queryParams = new URLSearchParams(
203
195
  params as unknown as Record<string, string>
204
196
  )
205
- try {
206
- const response = await request<StatusResponse>(
207
- `${config.get().apiUrl}/status?${queryParams}`,
208
- {
209
- signal: options?.signal,
210
- }
211
- )
212
- return response
213
- } catch (e) {
214
- throw await parseBackendError(e)
215
- }
197
+ return await request<StatusResponse>(
198
+ `${config.get().apiUrl}/status?${queryParams}`,
199
+ {
200
+ signal: options?.signal,
201
+ }
202
+ )
216
203
  }
217
204
 
218
205
  /**
@@ -233,19 +220,16 @@ export const getChains = async (
233
220
  delete params[key as keyof ChainsRequest]
234
221
  )
235
222
  }
236
- try {
237
- const response = await request<ChainsResponse>(
238
- `${config.get().apiUrl}/chains?${new URLSearchParams(
239
- params as Record<string, string>
240
- )}`,
241
- {
242
- signal: options?.signal,
243
- }
244
- )
245
- return response.chains
246
- } catch (e) {
247
- throw await parseBackendError(e)
248
- }
223
+
224
+ const response = await request<ChainsResponse>(
225
+ `${config.get().apiUrl}/chains?${new URLSearchParams(
226
+ params as Record<string, string>
227
+ )}`,
228
+ {
229
+ signal: options?.signal,
230
+ }
231
+ )
232
+ return response.chains
249
233
  }
250
234
 
251
235
  /**
@@ -260,7 +244,7 @@ export const getRoutes = async (
260
244
  options?: RequestOptions
261
245
  ): Promise<RoutesResponse> => {
262
246
  if (!isRoutesRequest(params)) {
263
- throw new ValidationError('Invalid routes request.')
247
+ throw new SDKError(new ValidationError('Invalid routes request.'))
264
248
  }
265
249
  const _config = config.get()
266
250
  // apply defaults
@@ -269,23 +253,15 @@ export const getRoutes = async (
269
253
  ..._config.routeOptions,
270
254
  ...params.options,
271
255
  }
272
- // send request
273
- try {
274
- const response = await request<RoutesResponse>(
275
- `${_config.apiUrl}/advanced/routes`,
276
- {
277
- method: 'POST',
278
- headers: {
279
- 'Content-Type': 'application/json',
280
- },
281
- body: JSON.stringify(params),
282
- signal: options?.signal,
283
- }
284
- )
285
- return response
286
- } catch (e) {
287
- throw await parseBackendError(e)
288
- }
256
+
257
+ return await request<RoutesResponse>(`${_config.apiUrl}/advanced/routes`, {
258
+ method: 'POST',
259
+ headers: {
260
+ 'Content-Type': 'application/json',
261
+ },
262
+ body: JSON.stringify(params),
263
+ signal: options?.signal,
264
+ })
289
265
  }
290
266
 
291
267
  /**
@@ -304,22 +280,18 @@ export const getStepTransaction = async (
304
280
  // eslint-disable-next-line no-console
305
281
  console.warn('SDK Validation: Invalid Step', step)
306
282
  }
307
- try {
308
- const response = await request<LiFiStep>(
309
- `${config.get().apiUrl}/advanced/stepTransaction`,
310
- {
311
- method: 'POST',
312
- headers: {
313
- 'Content-Type': 'application/json',
314
- },
315
- body: JSON.stringify(step),
316
- signal: options?.signal,
317
- }
318
- )
319
- return response
320
- } catch (e) {
321
- throw await parseBackendError(e)
322
- }
283
+
284
+ return await request<LiFiStep>(
285
+ `${config.get().apiUrl}/advanced/stepTransaction`,
286
+ {
287
+ method: 'POST',
288
+ headers: {
289
+ 'Content-Type': 'application/json',
290
+ },
291
+ body: JSON.stringify(step),
292
+ signal: options?.signal,
293
+ }
294
+ )
323
295
  }
324
296
 
325
297
  /**
@@ -339,19 +311,14 @@ export const getTools = async (
339
311
  delete params[key as keyof ToolsRequest]
340
312
  )
341
313
  }
342
- try {
343
- const response = await request<ToolsResponse>(
344
- `${config.get().apiUrl}/tools?${new URLSearchParams(
345
- params as Record<string, string>
346
- )}`,
347
- {
348
- signal: options?.signal,
349
- }
350
- )
351
- return response
352
- } catch (e) {
353
- throw await parseBackendError(e)
354
- }
314
+ return await request<ToolsResponse>(
315
+ `${config.get().apiUrl}/tools?${new URLSearchParams(
316
+ params as Record<string, string>
317
+ )}`,
318
+ {
319
+ signal: options?.signal,
320
+ }
321
+ )
355
322
  }
356
323
 
357
324
  /**
@@ -372,19 +339,14 @@ export const getTokens = async (
372
339
  )
373
340
  }
374
341
 
375
- try {
376
- const response = await request<TokensResponse>(
377
- `${config.get().apiUrl}/tokens?${new URLSearchParams(
378
- params as Record<string, string>
379
- )}`,
380
- {
381
- signal: options?.signal,
382
- }
383
- )
384
- return response
385
- } catch (e) {
386
- throw await parseBackendError(e)
387
- }
342
+ return await request<TokensResponse>(
343
+ `${config.get().apiUrl}/tokens?${new URLSearchParams(
344
+ params as Record<string, string>
345
+ )}`,
346
+ {
347
+ signal: options?.signal,
348
+ }
349
+ )
388
350
  }
389
351
 
390
352
  /**
@@ -399,7 +361,9 @@ export const getGasRecommendation = async (
399
361
  options?: RequestOptions
400
362
  ): Promise<GasRecommendationResponse> => {
401
363
  if (!params.chainId) {
402
- throw new ValidationError('Required parameter "chainId" is missing.')
364
+ throw new SDKError(
365
+ new ValidationError('Required parameter "chainId" is missing.')
366
+ )
403
367
  }
404
368
 
405
369
  const url = new URL(`${config.get().apiUrl}/gas/suggestion/${params.chainId}`)
@@ -410,14 +374,9 @@ export const getGasRecommendation = async (
410
374
  url.searchParams.append('fromToken', params.fromToken)
411
375
  }
412
376
 
413
- try {
414
- const response = await request<GasRecommendationResponse>(url.toString(), {
415
- signal: options?.signal,
416
- })
417
- return response
418
- } catch (e) {
419
- throw await parseBackendError(e)
420
- }
377
+ return await request<GasRecommendationResponse>(url.toString(), {
378
+ signal: options?.signal,
379
+ })
421
380
  }
422
381
 
423
382
  /**
@@ -465,12 +424,8 @@ export const getConnections = async (
465
424
  })
466
425
  }
467
426
  })
468
- try {
469
- const response = await request<ConnectionsResponse>(url, options)
470
- return response
471
- } catch (e) {
472
- throw await parseBackendError(e)
473
- }
427
+
428
+ return await request<ConnectionsResponse>(url, options)
474
429
  }
475
430
 
476
431
  export const getTransactionHistory = async (
@@ -478,7 +433,9 @@ export const getTransactionHistory = async (
478
433
  options?: RequestOptions
479
434
  ): Promise<TransactionAnalyticsResponse> => {
480
435
  if (!wallet) {
481
- throw new ValidationError('Required parameter "wallet" is missing.')
436
+ throw new SDKError(
437
+ new ValidationError('Required parameter "wallet" is missing.')
438
+ )
482
439
  }
483
440
 
484
441
  const _config = config.get()
@@ -500,10 +457,5 @@ export const getTransactionHistory = async (
500
457
  url.searchParams.append('toTimestamp', toTimestamp.toString())
501
458
  }
502
459
 
503
- try {
504
- const response = await request<TransactionAnalyticsResponse>(url, options)
505
- return response
506
- } catch (e) {
507
- throw await parseBackendError(e)
508
- }
460
+ return await request<TransactionAnalyticsResponse>(url, options)
509
461
  }
@@ -1,14 +1,14 @@
1
1
  import type { Token, TokenAmount } from '@lifi/types'
2
2
  import { config } from '../config.js'
3
3
  import { isToken } from '../typeguards.js'
4
- import { ValidationError } from '../utils/errors.js'
4
+ import { ValidationError } from '../errors/errors.js'
5
5
 
6
6
  /**
7
7
  * Returns the balances of a specific token a wallet holds across all aggregated chains.
8
8
  * @param walletAddress - A wallet address.
9
9
  * @param token - A Token object.
10
10
  * @returns An object containing the token and the amounts on different chains.
11
- * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
11
+ * @throws {BaseError} Throws a ValidationError if parameters are invalid.
12
12
  */
13
13
  export const getTokenBalance = async (
14
14
  walletAddress: string,
@@ -23,7 +23,7 @@ export const getTokenBalance = async (
23
23
  * @param walletAddress - A wallet address.
24
24
  * @param tokens - A list of Token objects.
25
25
  * @returns A list of objects containing the tokens and the amounts on different chains.
26
- * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
26
+ * @throws {BaseError} Throws a ValidationError if parameters are invalid.
27
27
  */
28
28
  export const getTokenBalances = async (
29
29
  walletAddress: string,
@@ -53,7 +53,7 @@ export const getTokenBalances = async (
53
53
  * @param walletAddress - A walletaddress.
54
54
  * @param tokensByChain - A list of Token objects organized by chain ids.
55
55
  * @returns A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
56
- * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
56
+ * @throws {BaseError} Throws a ValidationError if parameters are invalid.
57
57
  */
58
58
  export const getTokenBalancesByChain = async (
59
59
  walletAddress: string,
@@ -0,0 +1,3 @@
1
+ export interface ExtendedRequestInit extends RequestInit {
2
+ retries?: number
3
+ }
@@ -1,233 +0,0 @@
1
- enum ErrorType {
2
- RPCError = 'RPCError',
3
- ProviderError = 'ProviderError',
4
- ServerError = 'ServerError',
5
- TransactionError = 'TransactionError',
6
- ValidationError = 'ValidationError',
7
- NotFoundError = 'NotFoundError',
8
- UnknownError = 'UnknownError',
9
- SlippageError = 'SlippageError',
10
- }
11
-
12
- export enum LiFiErrorCode {
13
- InternalError = 1000,
14
- ValidationError = 1001,
15
- TransactionUnderpriced = 1002,
16
- TransactionFailed = 1003,
17
- Timeout = 1004,
18
- ProviderUnavailable = 1005,
19
- NotFound = 1006,
20
- ChainSwitchError = 1007,
21
- TransactionUnprepared = 1008,
22
- GasLimitError = 1009,
23
- TransactionCanceled = 1010,
24
- SlippageError = 1011,
25
- SignatureRejected = 1012,
26
- BalanceError = 1013,
27
- AllowanceRequired = 1014,
28
- InsufficientFunds = 1015,
29
- ExchangeRateUpdateCanceled = 1016,
30
- WalletChangedDuringExecution = 1017,
31
- TransactionExpired = 1018,
32
- }
33
-
34
- export enum EthersErrorType {
35
- ActionRejected = 'ACTION_REJECTED',
36
- CallExecption = 'CALL_EXCEPTION',
37
- InsufficientFunds = 'INSUFFICIENT_FUNDS',
38
- }
39
-
40
- export enum EthersErrorMessage {
41
- ERC20Allowance = 'ERC20: transfer amount exceeds allowance',
42
- LowGas = 'intrinsic gas too low',
43
- OutOfGas = 'out of gas',
44
- Underpriced = 'underpriced',
45
- LowReplacementFee = 'replacement fee too low',
46
- }
47
-
48
- export enum ErrorMessage {
49
- UnknownError = 'Unknown error occurred.',
50
- SlippageError = 'The slippage is larger than the defined threshold. Please request a new route to get a fresh quote.',
51
- GasLimitLow = 'Gas limit is too low.',
52
- TransactionUnderpriced = 'Transaction is underpriced.',
53
- Default = 'Something went wrong.',
54
- }
55
-
56
- export enum MetaMaskRPCErrorCode {
57
- invalidInput = -32000,
58
- resourceNotFound = -32001,
59
- resourceUnavailable = -32002,
60
- transactionRejected = -32003,
61
- methodNotSupported = -32004,
62
- limitExceeded = -32005,
63
- parse = -32700,
64
- invalidRequest = -32600,
65
- methodNotFound = -32601,
66
- invalidParams = -32602,
67
- internal = -32603,
68
- }
69
-
70
- export enum MetaMaskProviderErrorCode {
71
- userRejectedRequest = 4001,
72
- unauthorized = 4100,
73
- unsupportedMethod = 4200,
74
- disconnected = 4900,
75
- chainDisconnected = 4901,
76
- }
77
-
78
- export type ErrorCode =
79
- | LiFiErrorCode
80
- | MetaMaskRPCErrorCode
81
- | MetaMaskProviderErrorCode
82
-
83
- export class LiFiError extends Error {
84
- code: ErrorCode
85
- htmlMessage?: string
86
-
87
- constructor(
88
- type: ErrorType,
89
- code: number,
90
- message: string,
91
- htmlMessage?: string,
92
- stack?: string
93
- ) {
94
- super(message)
95
-
96
- // Set the prototype explicitly: https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
97
- Object.setPrototypeOf(this, LiFiError.prototype)
98
-
99
- this.code = code
100
-
101
- // the name property is used by toString(). It is a string and we can't use our custom ErrorTypes, that's why we have to cast
102
- this.name = type.toString()
103
-
104
- this.htmlMessage = htmlMessage
105
-
106
- // passing a stack allows us to preserve the stack from errors that we caught and just want to transform in one of our custom errors
107
- if (stack) {
108
- this.stack = stack
109
- }
110
- }
111
- }
112
-
113
- export class RPCError extends LiFiError {
114
- constructor(
115
- code: ErrorCode,
116
- message: string,
117
- htmlMessage?: string,
118
- stack?: string
119
- ) {
120
- super(ErrorType.RPCError, code, message, htmlMessage, stack)
121
- }
122
- }
123
-
124
- export class ProviderError extends LiFiError {
125
- constructor(
126
- code: ErrorCode,
127
- message: string,
128
- htmlMessage?: string,
129
- stack?: string
130
- ) {
131
- super(ErrorType.ProviderError, code, message, htmlMessage, stack)
132
- }
133
- }
134
-
135
- export class ServerError extends LiFiError {
136
- constructor(message: string, htmlMessage?: string, stack?: string) {
137
- super(
138
- ErrorType.ServerError,
139
- LiFiErrorCode.InternalError,
140
- message,
141
- htmlMessage,
142
- stack
143
- )
144
- }
145
- }
146
-
147
- export class ValidationError extends LiFiError {
148
- constructor(message: string, htmlMessage?: string, stack?: string) {
149
- super(
150
- ErrorType.ValidationError,
151
- LiFiErrorCode.ValidationError,
152
- message,
153
- htmlMessage,
154
- stack
155
- )
156
- }
157
- }
158
-
159
- export class TransactionError extends LiFiError {
160
- constructor(
161
- code: ErrorCode,
162
- message: string,
163
- htmlMessage?: string,
164
- stack?: string
165
- ) {
166
- super(ErrorType.TransactionError, code, message, htmlMessage, stack)
167
- }
168
- }
169
-
170
- export class SlippageError extends LiFiError {
171
- constructor(message: string, htmlMessage?: string, stack?: string) {
172
- super(
173
- ErrorType.SlippageError,
174
- LiFiErrorCode.SlippageError,
175
- message,
176
- htmlMessage,
177
- stack
178
- )
179
- }
180
- }
181
-
182
- export class BalanceError extends LiFiError {
183
- constructor(message: string, htmlMessage?: string, stack?: string) {
184
- super(
185
- ErrorType.ValidationError,
186
- LiFiErrorCode.BalanceError,
187
- message,
188
- htmlMessage,
189
- stack
190
- )
191
- }
192
- }
193
-
194
- export class NotFoundError extends LiFiError {
195
- constructor(message: string, htmlMessage?: string, stack?: string) {
196
- super(
197
- ErrorType.NotFoundError,
198
- LiFiErrorCode.NotFound,
199
- message,
200
- htmlMessage,
201
- stack
202
- )
203
- }
204
- }
205
-
206
- export class UnknownError extends LiFiError {
207
- constructor(
208
- code: ErrorCode,
209
- message: string,
210
- htmlMessage?: string,
211
- stack?: string
212
- ) {
213
- super(ErrorType.UnknownError, code, message, htmlMessage, stack)
214
- }
215
- }
216
-
217
- export class HTTPError extends Error {
218
- public response: Response
219
- public status: number
220
-
221
- constructor(response: Response) {
222
- const code = response.status || response.status === 0 ? response.status : ''
223
- const title = response.statusText || ''
224
- const status = `${code} ${title}`.trim()
225
- const reason = status ? `status code ${status}` : 'an unknown error'
226
-
227
- super(`Request failed with ${reason}`)
228
-
229
- this.name = 'HTTPError'
230
- this.response = response
231
- this.status = response.status
232
- }
233
- }
@@ -1,5 +1,4 @@
1
- export * from './errors.js'
1
+ export * from '../errors/index.js'
2
2
  export * from './getTransactionMessage.js'
3
3
  export * from './median.js'
4
- export * from './parseError.js'
5
4
  export * from './utils.js'
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export const name = '@lifi/sdk'
2
- export const version = '3.0.2-beta.0'
2
+ export const version = '3.1.0'