@ledgerhq/coin-module-framework 11.0.1 → 11.4.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 (88) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/lib/api/getBlock/assertBlockHeight.d.ts +2 -0
  3. package/lib/api/getBlock/assertBlockHeight.d.ts.map +1 -0
  4. package/lib/api/getBlock/assertBlockHeight.js +12 -0
  5. package/lib/api/getBlock/assertBlockHeight.js.map +1 -0
  6. package/lib/api/impl.d.ts +22 -3
  7. package/lib/api/impl.d.ts.map +1 -1
  8. package/lib/api/impl.js +2 -0
  9. package/lib/api/impl.js.map +1 -1
  10. package/lib/api/notSupportedApi.d.ts.map +1 -1
  11. package/lib/api/notSupportedApi.js +1 -0
  12. package/lib/api/notSupportedApi.js.map +1 -1
  13. package/lib/api/types.d.ts +130 -2
  14. package/lib/api/types.d.ts.map +1 -1
  15. package/lib/api/withDefaults.d.ts +13 -2
  16. package/lib/api/withDefaults.d.ts.map +1 -1
  17. package/lib/api/withDefaults.js +36 -1
  18. package/lib/api/withDefaults.js.map +1 -1
  19. package/lib/currencies/formatCurrencyUnit.js +1 -1
  20. package/lib/currencies/formatCurrencyUnit.js.map +1 -1
  21. package/lib/currencies/parseCurrencyUnit.js +1 -1
  22. package/lib/currencies/parseCurrencyUnit.js.map +1 -1
  23. package/lib/errors.d.ts +37 -1
  24. package/lib/errors.d.ts.map +1 -1
  25. package/lib/errors.js +71 -2
  26. package/lib/errors.js.map +1 -1
  27. package/lib/promises/promises.d.ts +1 -1
  28. package/lib/promises/promises.d.ts.map +1 -1
  29. package/lib/promises/promises.js +1 -1
  30. package/lib/promises/promises.js.map +1 -1
  31. package/lib/utils.d.ts +1 -0
  32. package/lib/utils.d.ts.map +1 -1
  33. package/lib/utils.js +9 -0
  34. package/lib/utils.js.map +1 -1
  35. package/lib-es/api/getBlock/assertBlockHeight.d.ts +2 -0
  36. package/lib-es/api/getBlock/assertBlockHeight.d.ts.map +1 -0
  37. package/lib-es/api/getBlock/assertBlockHeight.js +9 -0
  38. package/lib-es/api/getBlock/assertBlockHeight.js.map +1 -0
  39. package/lib-es/api/impl.d.ts +22 -3
  40. package/lib-es/api/impl.d.ts.map +1 -1
  41. package/lib-es/api/impl.js +2 -0
  42. package/lib-es/api/impl.js.map +1 -1
  43. package/lib-es/api/notSupportedApi.d.ts.map +1 -1
  44. package/lib-es/api/notSupportedApi.js +1 -0
  45. package/lib-es/api/notSupportedApi.js.map +1 -1
  46. package/lib-es/api/types.d.ts +130 -2
  47. package/lib-es/api/types.d.ts.map +1 -1
  48. package/lib-es/api/withDefaults.d.ts +13 -2
  49. package/lib-es/api/withDefaults.d.ts.map +1 -1
  50. package/lib-es/api/withDefaults.js +36 -1
  51. package/lib-es/api/withDefaults.js.map +1 -1
  52. package/lib-es/currencies/formatCurrencyUnit.js +1 -1
  53. package/lib-es/currencies/formatCurrencyUnit.js.map +1 -1
  54. package/lib-es/currencies/parseCurrencyUnit.js +1 -1
  55. package/lib-es/currencies/parseCurrencyUnit.js.map +1 -1
  56. package/lib-es/errors.d.ts +37 -1
  57. package/lib-es/errors.d.ts.map +1 -1
  58. package/lib-es/errors.js +64 -1
  59. package/lib-es/errors.js.map +1 -1
  60. package/lib-es/promises/promises.d.ts +1 -1
  61. package/lib-es/promises/promises.d.ts.map +1 -1
  62. package/lib-es/promises/promises.js +1 -1
  63. package/lib-es/promises/promises.js.map +1 -1
  64. package/lib-es/utils.d.ts +1 -0
  65. package/lib-es/utils.d.ts.map +1 -1
  66. package/lib-es/utils.js +8 -0
  67. package/lib-es/utils.js.map +1 -1
  68. package/package.json +3 -3
  69. package/src/api/getBalance/rejectBalanceOptions.test.ts +8 -0
  70. package/src/api/getBlock/assertBlockHeight.test.ts +28 -0
  71. package/src/api/getBlock/assertBlockHeight.ts +10 -0
  72. package/src/api/impl.test.ts +3 -0
  73. package/src/api/impl.ts +24 -1
  74. package/src/api/notSupportedApi.ts +1 -0
  75. package/src/api/types.ts +155 -3
  76. package/src/api/withDefaults.test.ts +154 -1
  77. package/src/api/withDefaults.ts +66 -3
  78. package/src/currencies/formatCurrencyUnit.test.ts +14 -0
  79. package/src/currencies/formatCurrencyUnit.ts +1 -1
  80. package/src/currencies/parseCurrencyUnit.test.ts +5 -0
  81. package/src/currencies/parseCurrencyUnit.ts +1 -1
  82. package/src/errors.test.ts +89 -0
  83. package/src/errors.ts +59 -1
  84. package/src/promises/promises.test.ts +1 -1
  85. package/src/promises/promises.ts +1 -1
  86. package/src/test-utils/capabilities.test.ts +1 -0
  87. package/src/utils.test.ts +24 -1
  88. package/src/utils.ts +11 -0
package/src/errors.ts CHANGED
@@ -121,11 +121,69 @@ export class UnknownError extends Error {
121
121
 
122
122
  export class BlockNotFoundError extends Error {
123
123
  override name = 'BlockNotFoundError'
124
- constructor(message?: string) {
124
+ readonly statusCode = 404
125
+ constructor(message?: string, fields?: Record<string, unknown>, options?: { cause?: unknown }) {
125
126
  super(message || 'BlockNotFoundError')
127
+ if (fields) Object.assign(this, fields)
128
+ if (options?.cause !== undefined) Object.assign(this, { cause: options.cause })
129
+ }
130
+ }
131
+
132
+ export class InvalidBlockHeightError extends Error {
133
+ override name = 'InvalidBlockHeightError'
134
+ readonly statusCode = 400
135
+ constructor(message?: string, fields?: Record<string, unknown>, options?: { cause?: unknown }) {
136
+ super(message || 'InvalidBlockHeightError')
137
+ if (fields) Object.assign(this, fields)
138
+ if (options?.cause !== undefined) Object.assign(this, { cause: options.cause })
139
+ }
140
+ }
141
+
142
+ export class RateLimitError extends Error {
143
+ override name = 'RateLimitError'
144
+ readonly statusCode = 429
145
+ constructor(message?: string, fields?: Record<string, unknown>, options?: { cause?: unknown }) {
146
+ super(message || 'Upstream rate limit exceeded (429), please retry later')
147
+ if (fields) Object.assign(this, fields)
148
+ if (options?.cause !== undefined) Object.assign(this, { cause: options.cause })
149
+ }
150
+ }
151
+
152
+ export class InvalidExplorerResponse extends Error {
153
+ override name = 'InvalidExplorerResponse'
154
+ constructor(message?: string, fields?: Record<string, unknown>, options?: { cause?: unknown }) {
155
+ super(message || 'InvalidExplorerResponse')
156
+ if (fields) Object.assign(this, fields)
157
+ if (options?.cause !== undefined) Object.assign(this, { cause: options.cause })
126
158
  }
127
159
  }
128
160
 
161
+ export class GetBlockInfoError extends Error {
162
+ override name = 'GetBlockInfoError'
163
+ constructor(message?: string, fields?: Record<string, unknown>, options?: { cause?: unknown }) {
164
+ super(message || 'GetBlockInfoError')
165
+ if (fields) Object.assign(this, fields)
166
+ if (options?.cause !== undefined) Object.assign(this, { cause: options.cause })
167
+ }
168
+ }
169
+
170
+ export class GetBlockError extends Error {
171
+ override name = 'GetBlockError'
172
+ constructor(message?: string, fields?: Record<string, unknown>, options?: { cause?: unknown }) {
173
+ super(message || 'GetBlockError')
174
+ if (fields) Object.assign(this, fields)
175
+ if (options?.cause !== undefined) Object.assign(this, { cause: options.cause })
176
+ }
177
+ }
178
+
179
+ export function statusCodeOf(error: unknown): number | undefined {
180
+ if (error instanceof Error && 'statusCode' in error && typeof error.statusCode === 'number') {
181
+ return error.statusCode
182
+ }
183
+
184
+ return undefined
185
+ }
186
+
129
187
  export class UnexpectedGetBalanceError extends Error {
130
188
  override name = 'UnexpectedGetBalanceError'
131
189
  cause: unknown
@@ -44,7 +44,7 @@ describe('promise', () => {
44
44
  await retry(() => testFunction(), {
45
45
  maxRetry: 5,
46
46
  interval: 0,
47
- retryCondition: (_) => true,
47
+ retryCondition: () => true,
48
48
  })
49
49
  } catch (e) {
50
50
  expect(e).toBe(5)
@@ -9,7 +9,7 @@ const defaults = {
9
9
  interval: 300,
10
10
  intervalMultiplicator: 1.5,
11
11
  context: '',
12
- retryCondition: (_: unknown) => true,
12
+ retryCondition: (_error: unknown) => true,
13
13
  }
14
14
 
15
15
  export function retry<A>(f: () => Promise<A>, options?: Partial<typeof defaults>): Promise<A> {
@@ -36,6 +36,7 @@ const ALL_CAPABILITIES = [
36
36
  'getRewards',
37
37
  'getStakes',
38
38
  'getValidators',
39
+ 'listHoldings',
39
40
  'register',
40
41
  'validateAddress',
41
42
  'validateIntent',
package/src/utils.test.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import BigNumber from 'bignumber.js'
5
- import { fromBigNumberToBigInt } from './utils'
5
+ import { fromBigNumberToBigInt, isHttpTooManyRequests } from './utils'
6
6
 
7
7
  describe('bigNumberToBigInt', () => {
8
8
  it('should convert BigNumber to BigInt', () => {
@@ -24,3 +24,26 @@ describe('bigNumberToBigInt', () => {
24
24
  expect(valBigint).toEqual(BigInt(valueStr))
25
25
  })
26
26
  })
27
+
28
+ describe('isHttpTooManyRequests', () => {
29
+ it('detects an error whose response status is 429', () => {
30
+ expect(
31
+ isHttpTooManyRequests(Object.assign(new Error('429'), { response: { status: 429 } }))
32
+ ).toBe(true)
33
+ })
34
+
35
+ it.each<[string, unknown]>([
36
+ [
37
+ 'a response with another status',
38
+ Object.assign(new Error('500'), { response: { status: 500 } }),
39
+ ],
40
+ ['a response without status', Object.assign(new Error('x'), { response: {} })],
41
+ ['a null response', Object.assign(new Error('x'), { response: null })],
42
+ ['a non-object response', Object.assign(new Error('x'), { response: 429 })],
43
+ ['an error without response', new Error('x')],
44
+ ['a non-error with a 429 response', { response: { status: 429 } }],
45
+ ['nothing', undefined],
46
+ ])('rejects %s', (_label, error) => {
47
+ expect(isHttpTooManyRequests(error)).toBe(false)
48
+ })
49
+ })
package/src/utils.ts CHANGED
@@ -26,3 +26,14 @@ export function isSendTransactionIntent(tx: TransactionIntent): tx is SendTransa
26
26
  export function isStakingTransactionIntent(tx: TransactionIntent): tx is StakingTransactionIntent {
27
27
  return tx.intentType === 'staking'
28
28
  }
29
+
30
+ export function isHttpTooManyRequests(error: unknown): boolean {
31
+ return (
32
+ error instanceof Error &&
33
+ 'response' in error &&
34
+ typeof error.response === 'object' &&
35
+ error.response !== null &&
36
+ 'status' in error.response &&
37
+ error.response.status === 429
38
+ )
39
+ }