@bigmi/core 0.4.0 → 0.4.3

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 (176) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/cjs/clients/decorators/public.d.ts +2 -2
  3. package/dist/cjs/clients/decorators/public.js +1 -1
  4. package/dist/cjs/clients/decorators/public.js.map +1 -1
  5. package/dist/cjs/errors/request.d.ts +2 -1
  6. package/dist/cjs/errors/request.js +2 -2
  7. package/dist/cjs/errors/request.js.map +1 -1
  8. package/dist/cjs/errors/rpc.d.ts +2 -1
  9. package/dist/cjs/errors/rpc.js +1 -0
  10. package/dist/cjs/errors/rpc.js.map +1 -1
  11. package/dist/cjs/factories/createClient.d.ts +2 -2
  12. package/dist/cjs/factories/createClient.js.map +1 -1
  13. package/dist/cjs/index.d.ts +42 -43
  14. package/dist/cjs/index.js +75 -75
  15. package/dist/cjs/index.js.map +1 -1
  16. package/dist/cjs/transports/ankr/ankr.js +1 -0
  17. package/dist/cjs/transports/ankr/ankr.js.map +1 -1
  18. package/dist/cjs/transports/ankr/getBalance.js +2 -1
  19. package/dist/cjs/transports/ankr/getBalance.js.map +1 -1
  20. package/dist/cjs/transports/blockchair/blockchair.js +1 -0
  21. package/dist/cjs/transports/blockchair/blockchair.js.map +1 -1
  22. package/dist/cjs/transports/blockchair/getBalance.js +7 -2
  23. package/dist/cjs/transports/blockchair/getBalance.js.map +1 -1
  24. package/dist/cjs/transports/blockchair/getTransactionFee.js +7 -2
  25. package/dist/cjs/transports/blockchair/getTransactionFee.js.map +1 -1
  26. package/dist/cjs/transports/blockchair/getUTXOs.js +4 -1
  27. package/dist/cjs/transports/blockchair/getUTXOs.js.map +1 -1
  28. package/dist/cjs/transports/blockcypher/blockcypher.js +1 -0
  29. package/dist/cjs/transports/blockcypher/blockcypher.js.map +1 -1
  30. package/dist/cjs/transports/blockcypher/getBalance.js +5 -1
  31. package/dist/cjs/transports/blockcypher/getBalance.js.map +1 -1
  32. package/dist/cjs/transports/blockcypher/getTransactionFee.js +2 -1
  33. package/dist/cjs/transports/blockcypher/getTransactionFee.js.map +1 -1
  34. package/dist/cjs/transports/blockcypher/getUTXOs.js +3 -2
  35. package/dist/cjs/transports/blockcypher/getUTXOs.js.map +1 -1
  36. package/dist/cjs/transports/blockcypher/utils.d.ts +2 -0
  37. package/dist/cjs/transports/blockcypher/utils.js +11 -0
  38. package/dist/cjs/transports/blockcypher/utils.js.map +1 -0
  39. package/dist/cjs/transports/getHttpRpcClient.js +5 -1
  40. package/dist/cjs/transports/getHttpRpcClient.js.map +1 -1
  41. package/dist/cjs/transports/mempool/mempool.js +1 -0
  42. package/dist/cjs/transports/mempool/mempool.js.map +1 -1
  43. package/dist/cjs/utils/deepEqual.js +1 -1
  44. package/dist/cjs/utils/deepEqual.js.map +1 -1
  45. package/dist/cjs/utils/getAction.d.ts +2 -2
  46. package/dist/cjs/utils/getAction.js.map +1 -1
  47. package/dist/cjs/utils/poll.js +1 -1
  48. package/dist/cjs/utils/poll.js.map +1 -1
  49. package/dist/cjs/utils/withTimeout.js.map +1 -1
  50. package/dist/cjs/version.d.ts +1 -1
  51. package/dist/cjs/version.js +1 -1
  52. package/dist/esm/actions/waitForTransaction.js +1 -1
  53. package/dist/esm/actions/waitForTransaction.js.map +1 -1
  54. package/dist/esm/clients/decorators/public.d.ts +2 -2
  55. package/dist/esm/clients/decorators/public.js +1 -1
  56. package/dist/esm/clients/decorators/public.js.map +1 -1
  57. package/dist/esm/errors/request.d.ts +2 -1
  58. package/dist/esm/errors/request.js +2 -2
  59. package/dist/esm/errors/request.js.map +1 -1
  60. package/dist/esm/errors/rpc.d.ts +5 -1
  61. package/dist/esm/errors/rpc.js +4 -0
  62. package/dist/esm/errors/rpc.js.map +1 -1
  63. package/dist/esm/factories/createClient.d.ts +2 -2
  64. package/dist/esm/factories/createClient.js.map +1 -1
  65. package/dist/esm/index.d.ts +42 -43
  66. package/dist/esm/index.js +29 -29
  67. package/dist/esm/index.js.map +1 -1
  68. package/dist/esm/transports/ankr/ankr.js +1 -0
  69. package/dist/esm/transports/ankr/ankr.js.map +1 -1
  70. package/dist/esm/transports/ankr/getBalance.js +2 -1
  71. package/dist/esm/transports/ankr/getBalance.js.map +1 -1
  72. package/dist/esm/transports/blockchair/blockchair.js +1 -0
  73. package/dist/esm/transports/blockchair/blockchair.js.map +1 -1
  74. package/dist/esm/transports/blockchair/getBalance.js +7 -2
  75. package/dist/esm/transports/blockchair/getBalance.js.map +1 -1
  76. package/dist/esm/transports/blockchair/getTransactionFee.js +7 -2
  77. package/dist/esm/transports/blockchair/getTransactionFee.js.map +1 -1
  78. package/dist/esm/transports/blockchair/getUTXOs.js +4 -1
  79. package/dist/esm/transports/blockchair/getUTXOs.js.map +1 -1
  80. package/dist/esm/transports/blockcypher/blockcypher.js +1 -0
  81. package/dist/esm/transports/blockcypher/blockcypher.js.map +1 -1
  82. package/dist/esm/transports/blockcypher/getBalance.js +5 -1
  83. package/dist/esm/transports/blockcypher/getBalance.js.map +1 -1
  84. package/dist/esm/transports/blockcypher/getTransactionFee.js +2 -1
  85. package/dist/esm/transports/blockcypher/getTransactionFee.js.map +1 -1
  86. package/dist/esm/transports/blockcypher/getUTXOs.js +3 -2
  87. package/dist/esm/transports/blockcypher/getUTXOs.js.map +1 -1
  88. package/dist/esm/transports/blockcypher/utils.d.ts +2 -0
  89. package/dist/esm/transports/blockcypher/utils.js +8 -0
  90. package/dist/esm/transports/blockcypher/utils.js.map +1 -0
  91. package/dist/esm/transports/getHttpRpcClient.js +5 -1
  92. package/dist/esm/transports/getHttpRpcClient.js.map +1 -1
  93. package/dist/esm/transports/http.js.map +1 -1
  94. package/dist/esm/transports/mempool/mempool.js +1 -0
  95. package/dist/esm/transports/mempool/mempool.js.map +1 -1
  96. package/dist/esm/utils/cancelTransaction.js +1 -1
  97. package/dist/esm/utils/cancelTransaction.js.map +1 -1
  98. package/dist/esm/utils/deepEqual.js +2 -2
  99. package/dist/esm/utils/deepEqual.js.map +1 -1
  100. package/dist/esm/utils/getAction.d.ts +2 -2
  101. package/dist/esm/utils/getAction.js.map +1 -1
  102. package/dist/esm/utils/getAddressInfo.js.map +1 -1
  103. package/dist/esm/utils/modifyFee.js +1 -1
  104. package/dist/esm/utils/modifyFee.js.map +1 -1
  105. package/dist/esm/utils/poll.js +1 -1
  106. package/dist/esm/utils/poll.js.map +1 -1
  107. package/dist/esm/utils/withTimeout.js.map +1 -1
  108. package/dist/esm/version.d.ts +1 -1
  109. package/dist/esm/version.js +1 -1
  110. package/dist/types/actions/waitForTransaction.d.ts.map +1 -1
  111. package/dist/types/clients/decorators/public.d.ts +2 -2
  112. package/dist/types/clients/decorators/public.d.ts.map +1 -1
  113. package/dist/types/errors/request.d.ts +2 -1
  114. package/dist/types/errors/request.d.ts.map +1 -1
  115. package/dist/types/errors/rpc.d.ts +5 -1
  116. package/dist/types/errors/rpc.d.ts.map +1 -1
  117. package/dist/types/factories/createClient.d.ts +2 -2
  118. package/dist/types/factories/createClient.d.ts.map +1 -1
  119. package/dist/types/index.d.ts +42 -43
  120. package/dist/types/index.d.ts.map +1 -1
  121. package/dist/types/transports/ankr/ankr.d.ts.map +1 -1
  122. package/dist/types/transports/ankr/getBalance.d.ts.map +1 -1
  123. package/dist/types/transports/blockchair/blockchair.d.ts.map +1 -1
  124. package/dist/types/transports/blockchair/getBalance.d.ts.map +1 -1
  125. package/dist/types/transports/blockchair/getTransactionFee.d.ts.map +1 -1
  126. package/dist/types/transports/blockchair/getUTXOs.d.ts.map +1 -1
  127. package/dist/types/transports/blockcypher/blockcypher.d.ts.map +1 -1
  128. package/dist/types/transports/blockcypher/getBalance.d.ts.map +1 -1
  129. package/dist/types/transports/blockcypher/getTransactionFee.d.ts.map +1 -1
  130. package/dist/types/transports/blockcypher/getUTXOs.d.ts.map +1 -1
  131. package/dist/types/transports/blockcypher/utils.d.ts +3 -0
  132. package/dist/types/transports/blockcypher/utils.d.ts.map +1 -0
  133. package/dist/types/transports/fallback.d.ts.map +1 -1
  134. package/dist/types/transports/getHttpRpcClient.d.ts.map +1 -1
  135. package/dist/types/transports/http.d.ts.map +1 -1
  136. package/dist/types/transports/mempool/mempool.d.ts.map +1 -1
  137. package/dist/types/utils/cancelTransaction.d.ts.map +1 -1
  138. package/dist/types/utils/getAction.d.ts +2 -2
  139. package/dist/types/utils/getAction.d.ts.map +1 -1
  140. package/dist/types/utils/modifyFee.d.ts.map +1 -1
  141. package/dist/types/utils/withTimeout.d.ts.map +1 -1
  142. package/dist/types/version.d.ts +1 -1
  143. package/package.json +2 -2
  144. package/package.json.tmp +7 -3
  145. package/src/actions/waitForTransaction.ts +1 -1
  146. package/src/clients/decorators/public.ts +6 -7
  147. package/src/errors/request.ts +3 -1
  148. package/src/errors/rpc.ts +4 -0
  149. package/src/factories/createClient.ts +2 -4
  150. package/src/index.ts +103 -114
  151. package/src/transports/ankr/ankr.ts +2 -1
  152. package/src/transports/ankr/getBalance.ts +2 -1
  153. package/src/transports/blockchair/blockchair.spec.ts +85 -61
  154. package/src/transports/blockchair/blockchair.ts +1 -1
  155. package/src/transports/blockchair/getBalance.ts +10 -2
  156. package/src/transports/blockchair/getTransactionFee.ts +10 -2
  157. package/src/transports/blockchair/getUTXOs.ts +5 -1
  158. package/src/transports/blockcypher/blockcypher.ts +1 -0
  159. package/src/transports/blockcypher/getBalance.ts +5 -1
  160. package/src/transports/blockcypher/getTransactionFee.ts +2 -1
  161. package/src/transports/blockcypher/getUTXOs.ts +3 -2
  162. package/src/transports/blockcypher/utils.ts +8 -0
  163. package/src/transports/custom.ts +1 -1
  164. package/src/transports/fallback.ts +3 -3
  165. package/src/transports/getHttpRpcClient.ts +5 -1
  166. package/src/transports/http.ts +1 -1
  167. package/src/transports/mempool/mempool.spec.ts +3 -4
  168. package/src/transports/mempool/mempool.ts +1 -0
  169. package/src/utils/cancelTransaction.ts +1 -1
  170. package/src/utils/deepEqual.ts +2 -2
  171. package/src/utils/getAction.ts +2 -3
  172. package/src/utils/getAddressInfo.ts +1 -1
  173. package/src/utils/modifyFee.ts +1 -1
  174. package/src/utils/poll.ts +1 -1
  175. package/src/utils/withTimeout.ts +3 -1
  176. package/src/version.ts +1 -1
@@ -47,20 +47,24 @@ describe('Blockchair Transport', () => {
47
47
 
48
48
  describe('getBalance', () => {
49
49
  it('should fetch correct balance for valid address', async () => {
50
- vi.spyOn(global, 'fetch').mockResolvedValue(
51
- createMockResponse(getBalanceReponse as BlockchairAddressBalanceData)
52
- )
50
+ if (USE_MOCK) {
51
+ vi.spyOn(global, 'fetch').mockResolvedValue(
52
+ createMockResponse(getBalanceReponse as BlockchairAddressBalanceData)
53
+ )
54
+ }
53
55
 
54
56
  const balance = await getBalance(publicClient, { address })
55
57
  expect(balance).toBeTypeOf('bigint')
56
58
  })
57
59
 
58
60
  it('should throw error for non-existent address', async () => {
59
- vi.spyOn(global, 'fetch').mockResolvedValue(
60
- createMockResponse(
61
- getInvalidBalanceReponse as BlockchairAddressBalanceData
61
+ if (USE_MOCK) {
62
+ vi.spyOn(global, 'fetch').mockResolvedValue(
63
+ createMockResponse(
64
+ getInvalidBalanceReponse as BlockchairAddressBalanceData
65
+ )
62
66
  )
63
- )
67
+ }
64
68
 
65
69
  const nonExistentAddress =
66
70
  '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNaNUIBNSUENopnoidsacn'
@@ -72,11 +76,13 @@ describe('Blockchair Transport', () => {
72
76
 
73
77
  describe('getUTXOs', () => {
74
78
  it('should return utxos with correct structure', async () => {
75
- vi.spyOn(global, 'fetch').mockResolvedValue(
76
- createMockResponse(
77
- getUTXOsResponse as BlockchairResponse<BlockChairDashboardAddressResponse>
79
+ if (USE_MOCK) {
80
+ vi.spyOn(global, 'fetch').mockResolvedValue(
81
+ createMockResponse(
82
+ getUTXOsResponse as BlockchairResponse<BlockChairDashboardAddressResponse>
83
+ )
78
84
  )
79
- )
85
+ }
80
86
 
81
87
  const utxos = await getUTXOs(publicClient, { address })
82
88
 
@@ -91,27 +97,29 @@ describe('Blockchair Transport', () => {
91
97
  })
92
98
 
93
99
  it('should throw error when minValue exceeds balance', async () => {
94
- vi.spyOn(global, 'fetch').mockImplementation((request) => {
95
- const url = new URL(request.toString())
100
+ if (USE_MOCK) {
101
+ vi.spyOn(global, 'fetch').mockImplementation((request) => {
102
+ const url = new URL(request.toString())
96
103
 
97
- if (url.pathname.includes('/addresses/balances')) {
98
- return Promise.resolve(
99
- createMockResponse(
100
- getBalanceReponse as BlockchairAddressBalanceData
104
+ if (url.pathname.includes('/addresses/balances')) {
105
+ return Promise.resolve(
106
+ createMockResponse(
107
+ getBalanceReponse as BlockchairAddressBalanceData
108
+ )
101
109
  )
102
- )
103
- }
110
+ }
104
111
 
105
- if (url.pathname.includes('/dashboards/addresses')) {
106
- return Promise.resolve(
107
- createMockResponse(
108
- getUTXOsResponse as BlockchairResponse<BlockChairDashboardAddressResponse>
112
+ if (url.pathname.includes('/dashboards/addresses')) {
113
+ return Promise.resolve(
114
+ createMockResponse(
115
+ getUTXOsResponse as BlockchairResponse<BlockChairDashboardAddressResponse>
116
+ )
109
117
  )
110
- )
111
- }
118
+ }
112
119
 
113
- throw new BaseError(`Unexpected URL: ${url.pathname}`)
114
- })
120
+ throw new BaseError(`Unexpected URL: ${url.pathname}`)
121
+ })
122
+ }
115
123
 
116
124
  const hugeValue = 999999999999999999n
117
125
  await expect(
@@ -123,14 +131,26 @@ describe('Blockchair Transport', () => {
123
131
  })
124
132
 
125
133
  it('should handle empty UTXO response', async () => {
126
- vi.spyOn(global, 'fetch').mockResolvedValue(
127
- createMockResponse(getUTXOsInvalidResponse as BlockchairResponse<null>)
128
- )
134
+ if (USE_MOCK) {
135
+ vi.spyOn(global, 'fetch').mockResolvedValue(
136
+ createMockResponse(
137
+ getUTXOsInvalidResponse as BlockchairResponse<null>
138
+ )
139
+ )
140
+ }
129
141
 
130
142
  const emptyAddress = '12LRT14SgNFFQ3hMRThAyXNao24BBy5cyU'
131
- const utxos = await getUTXOs(publicClient, { address: emptyAddress })
132
143
 
133
- expect(utxos.length).toBe(0)
144
+ if (USE_MOCK) {
145
+ const utxos = await getUTXOs(publicClient, { address: emptyAddress })
146
+ expect(utxos.length).toBe(0)
147
+ } else {
148
+ await expect(
149
+ getUTXOs(publicClient, { address: emptyAddress })
150
+ ).rejects.toMatchObject({
151
+ status: 404,
152
+ })
153
+ }
134
154
  })
135
155
 
136
156
  describe('pagination', () => {
@@ -155,37 +175,39 @@ describe('Blockchair Transport', () => {
155
175
  })
156
176
 
157
177
  it('should handle pagination correctly', async () => {
158
- vi.spyOn(global, 'fetch').mockImplementation((request) => {
159
- const url = new URL(request.toString())
160
-
161
- if (url.pathname.includes('/addresses/balances')) {
162
- return Promise.resolve(
163
- createMockResponse({
164
- data: {
165
- [addressWithManyUTXOs]: 1000000000000,
166
- },
167
- context: { code: 200 },
168
- } as BlockchairResponse<BlockchairAddressBalanceData>)
169
- )
170
- }
178
+ if (USE_MOCK) {
179
+ vi.spyOn(global, 'fetch').mockImplementation((request) => {
180
+ const url = new URL(request.toString())
181
+
182
+ if (url.pathname.includes('/addresses/balances')) {
183
+ return Promise.resolve(
184
+ createMockResponse({
185
+ data: {
186
+ [addressWithManyUTXOs]: 1000000000000,
187
+ },
188
+ context: { code: 200 },
189
+ } as BlockchairResponse<BlockchairAddressBalanceData>)
190
+ )
191
+ }
171
192
 
172
- if (url.pathname.includes('/dashboards/addresses')) {
173
- const offset = Number.parseInt(
174
- url.searchParams.get('offset')?.split(',')[1] || '0'
175
- )
176
- const limit = 100
177
- return Promise.resolve(
178
- createMockResponse(
179
- createPaginatedMockResponse(
180
- offset,
181
- limit
182
- ) as BlockchairResponse<BlockChairDashboardAddressResponse>
193
+ if (url.pathname.includes('/dashboards/addresses')) {
194
+ const offset = Number.parseInt(
195
+ url.searchParams.get('offset')?.split(',')[1] || '0'
183
196
  )
184
- )
185
- }
197
+ const limit = 100
198
+ return Promise.resolve(
199
+ createMockResponse(
200
+ createPaginatedMockResponse(
201
+ offset,
202
+ limit
203
+ ) as BlockchairResponse<BlockChairDashboardAddressResponse>
204
+ )
205
+ )
206
+ }
186
207
 
187
- throw new BaseError(`Unexpected URL: ${url.pathname}`)
188
- })
208
+ throw new BaseError(`Unexpected URL: ${url.pathname}`)
209
+ })
210
+ }
189
211
 
190
212
  const utxos = await getUTXOs(publicClient, {
191
213
  address: addressWithManyUTXOs,
@@ -194,7 +216,9 @@ describe('Blockchair Transport', () => {
194
216
 
195
217
  expect(utxos.length).toBeGreaterThan(0)
196
218
  expect(new Set(utxos.map((utxo) => utxo.txId)).size).toBe(utxos.length)
197
- expect(global.fetch).toHaveBeenCalled()
219
+ if (USE_MOCK) {
220
+ expect(global.fetch).toHaveBeenCalled()
221
+ }
198
222
 
199
223
  const totalValue = utxos.reduce((sum, utxo) => sum + utxo.value, 0)
200
224
  expect(totalValue).toBeGreaterThanOrEqual(minValue)
@@ -1,4 +1,3 @@
1
- import type { TransportConfig } from '../../types/transport.js'
2
1
  import type { HttpTransportConfig } from '../http.js'
3
2
  import type { UTXOMethod } from '../types.js'
4
3
  import { utxo } from '../utxo.js'
@@ -11,6 +10,7 @@ type BlockchairConfig = {
11
10
 
12
11
  export const blockchair = (config?: BlockchairConfig) =>
13
12
  utxo(config?.baseUrl || 'https://api.blockchair.com', {
13
+ name: 'Blockchair API',
14
14
  key: 'blockchair',
15
15
  includeChainToURL: true,
16
16
  methods: {
@@ -1,3 +1,4 @@
1
+ import { RpcErrorCode } from '../../errors/rpc.js'
1
2
  import { urlWithParams } from '../../utils/url.js'
2
3
  import type { RpcMethodHandler } from '../types.js'
3
4
  import type {
@@ -18,10 +19,17 @@ export const getBalance: RpcMethodHandler<'getBalance'> = async (
18
19
  url: apiUrl,
19
20
  fetchOptions: { method: 'GET' },
20
21
  })) as unknown as BlockchairResponse<BlockchairAddressBalanceData>
21
- if (response.context?.code !== 200) {
22
+ if (
23
+ response.data[address] === undefined ||
24
+ response.context.error ||
25
+ response.context?.code !== 200
26
+ ) {
22
27
  return {
23
28
  error: {
24
- code: response.context?.code,
29
+ code:
30
+ response.context.code === 429
31
+ ? RpcErrorCode.ACCESS_DENIED
32
+ : RpcErrorCode.MISC_ERROR,
25
33
  message: response.context?.error,
26
34
  },
27
35
  }
@@ -1,3 +1,4 @@
1
+ import { RpcErrorCode } from '../../errors/rpc.js'
1
2
  import { urlWithParams } from '../../utils/url.js'
2
3
  import type { RpcMethodHandler } from '../types.js'
3
4
  import type { BlockchairTransactionResponse } from './blockchair.types.js'
@@ -17,10 +18,17 @@ export const getTransactionFee: RpcMethodHandler<'getTransactionFee'> = async (
17
18
  },
18
19
  })) as unknown as BlockchairTransactionResponse
19
20
 
20
- if (response.context?.code !== 200) {
21
+ if (
22
+ !response.data[txId] ||
23
+ response.context.error ||
24
+ response.context?.code !== 200
25
+ ) {
21
26
  return {
22
27
  error: {
23
- code: response.context?.code,
28
+ code:
29
+ response.context.code === 429
30
+ ? RpcErrorCode.ACCESS_DENIED
31
+ : RpcErrorCode.MISC_ERROR,
24
32
  message: response.context?.error,
25
33
  },
26
34
  }
@@ -1,4 +1,5 @@
1
1
  import { RpcRequestError } from '../../errors/request.js'
2
+ import { RpcErrorCode } from '../../errors/rpc.js'
2
3
  import { InsufficientUTXOBalanceError } from '../../errors/utxo.js'
3
4
  import type { UTXO } from '../../types/transaction.js'
4
5
 
@@ -57,7 +58,10 @@ export const getUTXOs: RpcMethodHandler<'getUTXOs'> = async (
57
58
  },
58
59
  },
59
60
  error: {
60
- code: response.context.code,
61
+ code:
62
+ response.context.code === 429
63
+ ? RpcErrorCode.ACCESS_DENIED
64
+ : RpcErrorCode.MISC_ERROR,
61
65
  message: response.context.error || 'Error fetching utxos',
62
66
  },
63
67
  })
@@ -10,6 +10,7 @@ type BlockcypherConfig = {
10
10
 
11
11
  export const blockcypher = (config?: BlockcypherConfig) =>
12
12
  utxo(config?.baseUrl || 'https://api.blockcypher.com/v1/btc/main', {
13
+ name: 'Blockcypher API',
13
14
  key: 'blockcypher',
14
15
  methods: {
15
16
  include: Object.keys(blockcypherMethods) as UTXOMethod[],
@@ -1,6 +1,7 @@
1
1
  import { urlWithParams } from '../../utils/url.js'
2
2
  import type { RpcMethodHandler } from '../types.js'
3
3
  import type { BlockcypherBalanceResponse } from './blockcypher.types.js'
4
+ import { getRpcErrorCode } from './utils.js'
4
5
 
5
6
  export const getBalance: RpcMethodHandler<'getBalance'> = async (
6
7
  client,
@@ -16,7 +17,10 @@ export const getBalance: RpcMethodHandler<'getBalance'> = async (
16
17
  })) as unknown as BlockcypherBalanceResponse
17
18
  if (response.error) {
18
19
  return {
19
- error: { code: -1, message: response.error },
20
+ error: {
21
+ code: getRpcErrorCode(response.error),
22
+ message: response.error,
23
+ },
20
24
  }
21
25
  }
22
26
  return {
@@ -1,6 +1,7 @@
1
1
  import { urlWithParams } from '../../utils/url.js'
2
2
  import type { RpcMethodHandler } from '../types.js'
3
3
  import type { BlockcypherTransactionReponse } from './blockcypher.types.js'
4
+ import { getRpcErrorCode } from './utils.js'
4
5
 
5
6
  export const getTransactionFee: RpcMethodHandler<'getTransactionFee'> = async (
6
7
  client,
@@ -18,7 +19,7 @@ export const getTransactionFee: RpcMethodHandler<'getTransactionFee'> = async (
18
19
 
19
20
  if (response.error) {
20
21
  return {
21
- error: { code: -1, message: response.error },
22
+ error: { code: getRpcErrorCode(response.error), message: response.error },
22
23
  }
23
24
  }
24
25
 
@@ -7,6 +7,7 @@ import type {
7
7
  BlockcypherUTXO,
8
8
  BlockcypherUTXOsResponse,
9
9
  } from './blockcypher.types.js'
10
+ import { getRpcErrorCode } from './utils.js'
10
11
 
11
12
  const blockcypherUTXOTransformer = (utxo: BlockcypherUTXO): UTXO => ({
12
13
  blockHeight: utxo.block_height,
@@ -27,7 +28,7 @@ export const getUTXOs: RpcMethodHandler<'getUTXOs'> = async (
27
28
  ) => {
28
29
  async function* fetchUTXOs() {
29
30
  let hasMore = true
30
- let beforeBlock = undefined
31
+ let beforeBlock: number | undefined
31
32
 
32
33
  while (hasMore) {
33
34
  const apiUrl = urlWithParams(`${baseUrl}/addrs/${address}`, {
@@ -53,7 +54,7 @@ export const getUTXOs: RpcMethodHandler<'getUTXOs'> = async (
53
54
  },
54
55
  },
55
56
  error: {
56
- code: -1,
57
+ code: getRpcErrorCode(response.error),
57
58
  message: response.error,
58
59
  },
59
60
  })
@@ -0,0 +1,8 @@
1
+ import { RpcErrorCode } from '../../errors/rpc.js'
2
+
3
+ export function getRpcErrorCode(error: string): RpcErrorCode {
4
+ if (error.toLowerCase().startsWith('rate limit exceeded')) {
5
+ return RpcErrorCode.ACCESS_DENIED
6
+ }
7
+ return RpcErrorCode.MISC_ERROR
8
+ }
@@ -23,7 +23,7 @@ export type CustomTransportConfig = {
23
23
 
24
24
  export type CustomTransport = Transport<
25
25
  'custom',
26
- // biome-ignore lint/complexity/noBannedTypes: <explanation>
26
+ // biome-ignore lint/complexity/noBannedTypes: part of the types
27
27
  {},
28
28
  BitcoinProvider['request']
29
29
  >
@@ -43,9 +43,9 @@ type RankOptions = {
43
43
  /**
44
44
  * Ping method to determine latency.
45
45
  */
46
- ping?: (parameters: { transport: ReturnType<Transport> }) =>
47
- | Promise<unknown>
48
- | undefined
46
+ ping?: (parameters: {
47
+ transport: ReturnType<Transport>
48
+ }) => Promise<unknown> | undefined
49
49
  /**
50
50
  * The number of previous samples to perform ranking on.
51
51
  * @default 10
@@ -98,7 +98,11 @@ export function getHttpRpcClient(
98
98
  return response
99
99
  },
100
100
  {
101
- errorInstance: new TimeoutError({ body: body ?? {}, url }),
101
+ errorInstance: new TimeoutError({
102
+ body: body ?? {},
103
+ url: params.url ?? url,
104
+ timeout,
105
+ }),
102
106
  timeout,
103
107
  signal: true,
104
108
  }
@@ -13,9 +13,9 @@ import type {
13
13
  } from '../types/transport.js'
14
14
  import { createBatchScheduler } from '../utils/createBatchScheduler.js'
15
15
  import {
16
+ getHttpRpcClient,
16
17
  type HttpRpcClientOptions,
17
18
  type RpcRequest,
18
- getHttpRpcClient,
19
19
  } from './getHttpRpcClient.js'
20
20
  import { UTXOAPISchemaMethods } from './types.js'
21
21
 
@@ -1,18 +1,17 @@
1
1
  import { beforeEach, describe, expect, it, vi } from 'vitest'
2
2
  import { getBalance } from '../../actions/getBalance'
3
+ import { getTransactionFee } from '../../actions/getTransactionFee'
3
4
  import { getTransactions } from '../../actions/getTransactions'
4
5
  import { bitcoin } from '../../chains/bitcoin'
6
+ import { BaseError } from '../../errors/base'
5
7
  import { createClient, rpcSchema } from '../../factories/createClient'
6
8
  import { createMockResponse } from '../../test/utils'
7
- import type { UTXOSchema } from '../types'
8
-
9
- import { getTransactionFee } from '../../actions/getTransactionFee'
10
- import { BaseError } from '../../errors/base'
11
9
  import {
12
10
  INVALID_TX_ID,
13
11
  TX_FEE,
14
12
  VALID_TX_ID,
15
13
  } from '../__mocks__/getTransactionFee'
14
+ import type { UTXOSchema } from '../types'
16
15
  import getBalanceInValidResponse from './__mocks__/getBalance/invalid.json'
17
16
  import getBalanceValidResponse from './__mocks__/getBalance/valid.json'
18
17
  import getTransactionFeeInvalidResponse from './__mocks__/getTransactionFee/invalid.json'
@@ -9,6 +9,7 @@ type MempoolConfig = {
9
9
 
10
10
  export const mempool = (config?: MempoolConfig) =>
11
11
  utxo(config?.baseUrl || 'https://mempool.space/api', {
12
+ name: 'Mempool.space API',
12
13
  key: 'mempool',
13
14
  methods: {
14
15
  include: Object.keys(mempoolMethods) as UTXOMethod[],
@@ -1,4 +1,4 @@
1
- import { Psbt, address } from 'bitcoinjs-lib'
1
+ import { address, Psbt } from 'bitcoinjs-lib'
2
2
 
3
3
  interface InputData {
4
4
  hash: Uint8Array
@@ -40,7 +40,7 @@ export function deepEqual(a: any, b: any) {
40
40
  }
41
41
 
42
42
  for (i = length; i-- !== 0; ) {
43
- if (!Object.prototype.hasOwnProperty.call(b, keys[i]!)) {
43
+ if (!Object.hasOwn(b, keys[i]!)) {
44
44
  return false
45
45
  }
46
46
  }
@@ -57,6 +57,6 @@ export function deepEqual(a: any, b: any) {
57
57
  }
58
58
 
59
59
  // true if both NaN, false otherwise
60
- // biome-ignore lint/suspicious/noSelfCompare: <explanation>
60
+ // biome-ignore lint/suspicious/noSelfCompare: allowed in utils
61
61
  return a !== a && b !== b
62
62
  }
@@ -1,11 +1,10 @@
1
1
  import type { PublicActions } from '../clients/decorators/public.js'
2
2
  import type { WalletActions } from '../clients/decorators/wallet.js'
3
3
  import type { UTXOSchema } from '../transports/types.js'
4
- import type { Client } from '../types/client.js'
5
- import type { Transport } from '../types/transport.js'
6
-
7
4
  import type { Account } from '../types/account.js'
8
5
  import type { Chain } from '../types/chain.js'
6
+ import type { Client } from '../types/client.js'
7
+ import type { Transport } from '../types/transport.js'
9
8
 
10
9
  /**
11
10
  * Retrieves and returns an action from the client (if exists), and falls
@@ -1,5 +1,5 @@
1
1
  import { sha256 } from '@noble/hashes/sha256'
2
- import { type Decoded, bech32, bech32m } from 'bech32'
2
+ import { bech32, bech32m, type Decoded } from 'bech32'
3
3
  import bs58 from 'bs58'
4
4
 
5
5
  import {
@@ -1,4 +1,4 @@
1
- import { Psbt, address } from 'bitcoinjs-lib'
1
+ import { address, Psbt } from 'bitcoinjs-lib'
2
2
 
3
3
  interface InputData {
4
4
  hash: Uint8Array
package/src/utils/poll.ts CHANGED
@@ -23,7 +23,7 @@ export function poll<T>(
23
23
  }
24
24
 
25
25
  const watch = async () => {
26
- let data: T | undefined = undefined
26
+ let data: T | undefined
27
27
  if (emitOnBegin) {
28
28
  data = await fn({ unpoll: unwatch })
29
29
  }
@@ -5,7 +5,9 @@ export type WithTimeoutErrorType = ErrorType
5
5
  export function withTimeout<data>(
6
6
  fn: ({
7
7
  signal,
8
- }: { signal: AbortController['signal'] | null }) => Promise<data>,
8
+ }: {
9
+ signal: AbortController['signal'] | null
10
+ }) => Promise<data>,
9
11
  {
10
12
  errorInstance = new Error('timed out'),
11
13
  timeout,
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export const name = '@bigmi/core'
2
- export const version = '0.4.0'
2
+ export const version = '0.4.3'