@ledgerhq/coin-module-framework 11.0.1 → 11.3.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.
- package/CHANGELOG.md +24 -0
- package/lib/api/getBlock/assertBlockHeight.d.ts +2 -0
- package/lib/api/getBlock/assertBlockHeight.d.ts.map +1 -0
- package/lib/api/getBlock/assertBlockHeight.js +12 -0
- package/lib/api/getBlock/assertBlockHeight.js.map +1 -0
- package/lib/api/impl.d.ts +19 -1
- package/lib/api/impl.d.ts.map +1 -1
- package/lib/api/types.d.ts +79 -2
- package/lib/api/types.d.ts.map +1 -1
- package/lib/api/withDefaults.d.ts +13 -2
- package/lib/api/withDefaults.d.ts.map +1 -1
- package/lib/api/withDefaults.js +35 -1
- package/lib/api/withDefaults.js.map +1 -1
- package/lib/currencies/formatCurrencyUnit.js +1 -1
- package/lib/currencies/formatCurrencyUnit.js.map +1 -1
- package/lib/currencies/parseCurrencyUnit.js +1 -1
- package/lib/currencies/parseCurrencyUnit.js.map +1 -1
- package/lib/errors.d.ts +37 -1
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +71 -2
- package/lib/errors.js.map +1 -1
- package/lib/promises/promises.d.ts +1 -1
- package/lib/promises/promises.d.ts.map +1 -1
- package/lib/promises/promises.js +1 -1
- package/lib/promises/promises.js.map +1 -1
- package/lib/utils.d.ts +1 -0
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +9 -0
- package/lib/utils.js.map +1 -1
- package/lib-es/api/getBlock/assertBlockHeight.d.ts +2 -0
- package/lib-es/api/getBlock/assertBlockHeight.d.ts.map +1 -0
- package/lib-es/api/getBlock/assertBlockHeight.js +9 -0
- package/lib-es/api/getBlock/assertBlockHeight.js.map +1 -0
- package/lib-es/api/impl.d.ts +19 -1
- package/lib-es/api/impl.d.ts.map +1 -1
- package/lib-es/api/types.d.ts +79 -2
- package/lib-es/api/types.d.ts.map +1 -1
- package/lib-es/api/withDefaults.d.ts +13 -2
- package/lib-es/api/withDefaults.d.ts.map +1 -1
- package/lib-es/api/withDefaults.js +35 -1
- package/lib-es/api/withDefaults.js.map +1 -1
- package/lib-es/currencies/formatCurrencyUnit.js +1 -1
- package/lib-es/currencies/formatCurrencyUnit.js.map +1 -1
- package/lib-es/currencies/parseCurrencyUnit.js +1 -1
- package/lib-es/currencies/parseCurrencyUnit.js.map +1 -1
- package/lib-es/errors.d.ts +37 -1
- package/lib-es/errors.d.ts.map +1 -1
- package/lib-es/errors.js +64 -1
- package/lib-es/errors.js.map +1 -1
- package/lib-es/promises/promises.d.ts +1 -1
- package/lib-es/promises/promises.d.ts.map +1 -1
- package/lib-es/promises/promises.js +1 -1
- package/lib-es/promises/promises.js.map +1 -1
- package/lib-es/utils.d.ts +1 -0
- package/lib-es/utils.d.ts.map +1 -1
- package/lib-es/utils.js +8 -0
- package/lib-es/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/api/getBalance/rejectBalanceOptions.test.ts +8 -0
- package/src/api/getBlock/assertBlockHeight.test.ts +28 -0
- package/src/api/getBlock/assertBlockHeight.ts +10 -0
- package/src/api/impl.ts +22 -1
- package/src/api/types.ts +97 -3
- package/src/api/withDefaults.test.ts +124 -1
- package/src/api/withDefaults.ts +65 -3
- package/src/currencies/formatCurrencyUnit.test.ts +14 -0
- package/src/currencies/formatCurrencyUnit.ts +1 -1
- package/src/currencies/parseCurrencyUnit.test.ts +5 -0
- package/src/currencies/parseCurrencyUnit.ts +1 -1
- package/src/errors.test.ts +89 -0
- package/src/errors.ts +59 -1
- package/src/promises/promises.test.ts +1 -1
- package/src/promises/promises.ts +1 -1
- package/src/utils.test.ts +24 -1
- package/src/utils.ts +11 -0
package/src/api/withDefaults.ts
CHANGED
|
@@ -2,13 +2,21 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import type { CurrencyConfig } from '../config'
|
|
5
|
-
import type {
|
|
5
|
+
import type {
|
|
6
|
+
CoinModuleImpl,
|
|
7
|
+
OptionalApiKey,
|
|
8
|
+
ReadOption,
|
|
9
|
+
ReadOptionMethod,
|
|
10
|
+
ReadOptionsDeclaration,
|
|
11
|
+
} from './impl'
|
|
6
12
|
import type { CoinModuleApi, Memo, MemoNotSupported, TxData, TxDataNotSupported } from './types'
|
|
13
|
+
import { InvalidParameterError } from '../errors'
|
|
7
14
|
import { optionalApiKeys } from './impl'
|
|
8
15
|
import { isNotSupportedStub, notSupported } from './notSupported'
|
|
9
16
|
|
|
10
17
|
/**
|
|
11
|
-
* What {@link withDefaults} returns: the full consumer contract, plus `supports()
|
|
18
|
+
* What {@link withDefaults} returns: the full consumer contract, plus `supports()` and
|
|
19
|
+
* `supportsReadOption()`.
|
|
12
20
|
*
|
|
13
21
|
* `getAccountInfo` is narrowed to required. It is the one managed capability that
|
|
14
22
|
* {@link CoinModuleApi} itself declares optional, and `withDefaults` always supplies it — so
|
|
@@ -22,6 +30,13 @@ export type WithDefaultsResult<
|
|
|
22
30
|
> = CoinModuleApi<ConfigType, MemoType, TxDataType> &
|
|
23
31
|
Required<Pick<CoinModuleApi<ConfigType, MemoType, TxDataType>, 'getAccountInfo'>> & {
|
|
24
32
|
supports: (method: OptionalApiKey) => boolean
|
|
33
|
+
/**
|
|
34
|
+
* Whether `method` honours the read `option`: the module implements the method and declares
|
|
35
|
+
* the option in `readOptions`. When the method is implemented but does not declare the option,
|
|
36
|
+
* an opted-in call rejects with an {@link InvalidParameterError}. When the method is not
|
|
37
|
+
* implemented, every call throws `"<method> is not supported"`, as without the option.
|
|
38
|
+
*/
|
|
39
|
+
supportsReadOption: (method: ReadOptionMethod, option: ReadOption) => boolean
|
|
25
40
|
}
|
|
26
41
|
|
|
27
42
|
/**
|
|
@@ -35,6 +50,33 @@ function implemented<T>(value: T | undefined): T | undefined {
|
|
|
35
50
|
return value === undefined || isNotSupportedStub(value) ? undefined : value
|
|
36
51
|
}
|
|
37
52
|
|
|
53
|
+
function declares(
|
|
54
|
+
readOptions: ReadOptionsDeclaration | undefined,
|
|
55
|
+
method: ReadOptionMethod,
|
|
56
|
+
option: ReadOption
|
|
57
|
+
): boolean {
|
|
58
|
+
return readOptions?.[method]?.includes(option) ?? false
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Rejects an opted-in call to a method that does not declare the `feesAsset` read option.
|
|
63
|
+
*
|
|
64
|
+
* Both guarded methods take their options as the third argument. A call without the option goes
|
|
65
|
+
* through with its arguments and its receiver unchanged: an implementation may read `this`.
|
|
66
|
+
*/
|
|
67
|
+
function guardFeesAsset<Args extends [unknown, unknown, ({ feesAsset?: boolean } | undefined)?], R>(
|
|
68
|
+
method: ReadOptionMethod,
|
|
69
|
+
fn: (...args: Args) => Promise<R>,
|
|
70
|
+
declared: boolean
|
|
71
|
+
): (...args: Args) => Promise<R> {
|
|
72
|
+
if (declared) return fn
|
|
73
|
+
return function (this: unknown, ...args: Args): Promise<R> {
|
|
74
|
+
return args[2]?.feesAsset
|
|
75
|
+
? Promise.reject(new InvalidParameterError(`${method} does not support the feesAsset option`))
|
|
76
|
+
: fn.apply(this, args)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
38
80
|
/**
|
|
39
81
|
* Widens a {@link CoinModuleImpl} authoring value to the full {@link CoinModuleApi}
|
|
40
82
|
* consumer contract by backfilling the capability methods the author omitted, and attaches
|
|
@@ -49,6 +91,9 @@ function implemented<T>(value: T | undefined): T | undefined {
|
|
|
49
91
|
*
|
|
50
92
|
* A capability method is considered implemented only when the author supplied a real
|
|
51
93
|
* function — a `notSupported()` stub is treated as if the method had been omitted.
|
|
94
|
+
*
|
|
95
|
+
* `listOperations` and `getBlock` reject a call that sets a read option the module does not
|
|
96
|
+
* declare in `readOptions` (see `ReadOptionsDeclaration`). Calls without it are unchanged.
|
|
52
97
|
*/
|
|
53
98
|
export function withDefaults<
|
|
54
99
|
ConfigType extends CurrencyConfig,
|
|
@@ -61,12 +106,28 @@ export function withDefaults<
|
|
|
61
106
|
const implementedMethods = new Set<OptionalApiKey>(
|
|
62
107
|
optionalApiKeys.filter((k) => implemented(impl[k]) !== undefined)
|
|
63
108
|
)
|
|
109
|
+
// A stub stays the tagged stub, not a guarded wrapper: callers detect it as "not supported".
|
|
110
|
+
// listOperations is required, but a read-only module built on notSupportedApi() may stub it.
|
|
111
|
+
const listOperations = implemented(impl.listOperations)
|
|
112
|
+
const getBlock = implemented(impl.getBlock)
|
|
113
|
+
const supportsReadOption = (method: ReadOptionMethod, option: ReadOption): boolean =>
|
|
114
|
+
(method === 'listOperations' ? listOperations !== undefined : getBlock !== undefined) &&
|
|
115
|
+
declares(impl.readOptions, method, option)
|
|
64
116
|
|
|
65
117
|
// Spread first so anything the module exposes beyond the API surface survives, then
|
|
66
118
|
// override every capability method with either the real implementation or the default.
|
|
67
119
|
return {
|
|
68
120
|
...impl,
|
|
69
|
-
|
|
121
|
+
listOperations: listOperations
|
|
122
|
+
? guardFeesAsset(
|
|
123
|
+
'listOperations',
|
|
124
|
+
listOperations,
|
|
125
|
+
supportsReadOption('listOperations', 'feesAsset')
|
|
126
|
+
)
|
|
127
|
+
: impl.listOperations,
|
|
128
|
+
getBlock: getBlock
|
|
129
|
+
? guardFeesAsset('getBlock', getBlock, supportsReadOption('getBlock', 'feesAsset'))
|
|
130
|
+
: notSupported('getBlock'),
|
|
70
131
|
getBlockInfo: implemented(impl.getBlockInfo) ?? notSupported('getBlockInfo'),
|
|
71
132
|
getStakes: implemented(impl.getStakes) ?? notSupported('getStakes'),
|
|
72
133
|
getRewards: implemented(impl.getRewards) ?? notSupported('getRewards'),
|
|
@@ -83,5 +144,6 @@ export function withDefaults<
|
|
|
83
144
|
// of a throwing stub. Callers read `type` and branch on it.
|
|
84
145
|
getAccountInfo: implemented(impl.getAccountInfo) ?? (() => Promise.resolve({ type: 'none' })),
|
|
85
146
|
supports: (method: OptionalApiKey): boolean => implementedMethods.has(method),
|
|
147
|
+
supportsReadOption,
|
|
86
148
|
}
|
|
87
149
|
}
|
|
@@ -105,6 +105,20 @@ describe('formatCurrencyUnit', () => {
|
|
|
105
105
|
)
|
|
106
106
|
})
|
|
107
107
|
|
|
108
|
+
test('showAllDigits with magnitude above default DECIMAL_PLACES', () => {
|
|
109
|
+
const highMagUnit: Unit = { ...unit, magnitude: 24 }
|
|
110
|
+
expect(formatCurrencyUnit(highMagUnit, new BigNumber(1), { showAllDigits: true })).toBe(
|
|
111
|
+
'0.000000000000000000000001'
|
|
112
|
+
)
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
test('disableRounding with magnitude above default DECIMAL_PLACES', () => {
|
|
116
|
+
const highMagUnit: Unit = { ...unit, magnitude: 24 }
|
|
117
|
+
expect(formatCurrencyUnit(highMagUnit, new BigNumber(1), { disableRounding: true })).toBe(
|
|
118
|
+
'0.000000000000000000000001'
|
|
119
|
+
)
|
|
120
|
+
})
|
|
121
|
+
|
|
108
122
|
test('filters undefined options', () => {
|
|
109
123
|
expect(
|
|
110
124
|
// @ts-expect-error explicit undefined, for JS callers
|
|
@@ -100,7 +100,7 @@ const buildFormatState = (
|
|
|
100
100
|
staticSignificantDigits,
|
|
101
101
|
} = resolvedOptions
|
|
102
102
|
const { magnitude, code } = unit
|
|
103
|
-
const floatValue = value.
|
|
103
|
+
const floatValue = value.shiftedBy(-magnitude)
|
|
104
104
|
const floatValueAbs = floatValue.abs()
|
|
105
105
|
const minimumFractionDigits = showAllDigits ? magnitude : 0
|
|
106
106
|
const maximumFractionDigits = disableRounding
|
|
@@ -26,4 +26,9 @@ describe('parseCurrencyUnit', () => {
|
|
|
26
26
|
test('returns zero on invalid input', () => {
|
|
27
27
|
expect(parseCurrencyUnit(unit, 'invalid')).toEqual(new BigNumber(0))
|
|
28
28
|
})
|
|
29
|
+
|
|
30
|
+
test('parses magnitude above default DECIMAL_PLACES', () => {
|
|
31
|
+
const highMagUnit: Unit = { ...unit, magnitude: 24 }
|
|
32
|
+
expect(parseCurrencyUnit(highMagUnit, '0.000000000000000000000001')).toEqual(new BigNumber(1))
|
|
33
|
+
})
|
|
29
34
|
})
|
|
@@ -9,5 +9,5 @@ export const parseCurrencyUnit = (unit: Unit, valueString: string): BigNumber =>
|
|
|
9
9
|
const str = valueString.replace(/,/g, '.')
|
|
10
10
|
const value = new BigNumber(str)
|
|
11
11
|
if (value.isNaN()) return new BigNumber(0)
|
|
12
|
-
return value.
|
|
12
|
+
return value.shiftedBy(unit.magnitude).integerValue()
|
|
13
13
|
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: © 2026 LEDGER SAS
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
BlockNotFoundError,
|
|
6
|
+
GetBlockError,
|
|
7
|
+
GetBlockInfoError,
|
|
8
|
+
InvalidBlockHeightError,
|
|
9
|
+
InvalidExplorerResponse,
|
|
10
|
+
RateLimitError,
|
|
11
|
+
statusCodeOf,
|
|
12
|
+
} from './errors'
|
|
13
|
+
|
|
14
|
+
type ErrorClass = new (
|
|
15
|
+
message?: string,
|
|
16
|
+
fields?: Record<string, unknown>,
|
|
17
|
+
options?: { cause?: unknown }
|
|
18
|
+
) => Error
|
|
19
|
+
|
|
20
|
+
const ERRORS: Array<[string, ErrorClass, string | undefined]> = [
|
|
21
|
+
['BlockNotFoundError', BlockNotFoundError, undefined],
|
|
22
|
+
['InvalidBlockHeightError', InvalidBlockHeightError, undefined],
|
|
23
|
+
['RateLimitError', RateLimitError, 'Upstream rate limit exceeded (429), please retry later'],
|
|
24
|
+
['InvalidExplorerResponse', InvalidExplorerResponse, undefined],
|
|
25
|
+
['GetBlockInfoError', GetBlockInfoError, undefined],
|
|
26
|
+
['GetBlockError', GetBlockError, undefined],
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
describe.each(ERRORS)('%s', (name, ErrorType, defaultMessage) => {
|
|
30
|
+
it('is an Error named after its class with a default message', () => {
|
|
31
|
+
const error = new ErrorType()
|
|
32
|
+
|
|
33
|
+
expect(error).toBeInstanceOf(Error)
|
|
34
|
+
expect(error.name).toBe(name)
|
|
35
|
+
expect(error.message).toBe(defaultMessage ?? name)
|
|
36
|
+
expect(error).not.toHaveProperty('cause')
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('keeps a custom message', () => {
|
|
40
|
+
expect(new ErrorType('custom').message).toBe('custom')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('exposes the fields as attributes', () => {
|
|
44
|
+
expect(new ErrorType('custom', { height: 12 })).toHaveProperty('height', 12)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('attaches the cause', () => {
|
|
48
|
+
const cause = new Error('root')
|
|
49
|
+
|
|
50
|
+
expect(new ErrorType('custom', { height: 12 }, { cause })).toHaveProperty('cause', cause)
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
describe('statusCode', () => {
|
|
55
|
+
it.each<[string, ErrorClass, number]>([
|
|
56
|
+
['BlockNotFoundError', BlockNotFoundError, 404],
|
|
57
|
+
['InvalidBlockHeightError', InvalidBlockHeightError, 400],
|
|
58
|
+
['RateLimitError', RateLimitError, 429],
|
|
59
|
+
])('%s carries %d', (_name, ErrorType, statusCode) => {
|
|
60
|
+
expect(new ErrorType()).toHaveProperty('statusCode', statusCode)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('can be overridden through the fields', () => {
|
|
64
|
+
expect(new BlockNotFoundError(undefined, { statusCode: 500 })).toHaveProperty('statusCode', 500)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it.each<[string, ErrorClass]>([
|
|
68
|
+
['InvalidExplorerResponse', InvalidExplorerResponse],
|
|
69
|
+
['GetBlockInfoError', GetBlockInfoError],
|
|
70
|
+
['GetBlockError', GetBlockError],
|
|
71
|
+
])('%s carries none', (_name, ErrorType) => {
|
|
72
|
+
expect(new ErrorType()).not.toHaveProperty('statusCode')
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
describe('statusCodeOf', () => {
|
|
77
|
+
it('returns the numeric status code of an error', () => {
|
|
78
|
+
expect(statusCodeOf(new BlockNotFoundError())).toEqual(404)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('returns nothing when the error has no numeric status code', () => {
|
|
82
|
+
expect(statusCodeOf(new Error('plain'))).toBeUndefined()
|
|
83
|
+
expect(statusCodeOf(Object.assign(new Error('plain'), { statusCode: '404' }))).toBeUndefined()
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('returns nothing when nothing was thrown', () => {
|
|
87
|
+
expect(statusCodeOf(undefined)).toBeUndefined()
|
|
88
|
+
})
|
|
89
|
+
})
|
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
|
-
|
|
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
|
package/src/promises/promises.ts
CHANGED
|
@@ -9,7 +9,7 @@ const defaults = {
|
|
|
9
9
|
interval: 300,
|
|
10
10
|
intervalMultiplicator: 1.5,
|
|
11
11
|
context: '',
|
|
12
|
-
retryCondition: (
|
|
12
|
+
retryCondition: (_error: unknown) => true,
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export function retry<A>(f: () => Promise<A>, options?: Partial<typeof defaults>): Promise<A> {
|
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
|
+
}
|