@exodus/bitcoin-api 4.15.9 → 4.16.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 CHANGED
@@ -3,6 +3,26 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [4.16.0](https://github.com/ExodusMovement/assets/compare/@exodus/bitcoin-api@4.15.10...@exodus/bitcoin-api@4.16.0) (2026-06-29)
7
+
8
+
9
+ ### Features
10
+
11
+
12
+ * feat: add optional getHistoricalBalance to asset api (#8247)
13
+
14
+
15
+
16
+ ## [4.15.10](https://github.com/ExodusMovement/assets/compare/@exodus/bitcoin-api@4.15.9...@exodus/bitcoin-api@4.15.10) (2026-06-18)
17
+
18
+
19
+ ### Bug Fixes
20
+
21
+
22
+ * fix(bitcoin): yield between address derivation batches during gap scan (#8255)
23
+
24
+
25
+
6
26
  ## [4.15.9](https://github.com/ExodusMovement/assets/compare/@exodus/bitcoin-api@4.15.8...@exodus/bitcoin-api@4.15.9) (2026-06-08)
7
27
 
8
28
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/bitcoin-api",
3
- "version": "4.15.9",
3
+ "version": "4.16.0",
4
4
  "description": "Bitcoin transaction and fee monitors, RPC with the blockchain node, other networking code.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -63,5 +63,5 @@
63
63
  "type": "git",
64
64
  "url": "git+https://github.com/ExodusMovement/assets.git"
65
65
  },
66
- "gitHead": "35f528942e03aa95179f6f8d6b42f1f2317cec5e"
66
+ "gitHead": "d57432d4ef29f2dc1bc269d501fd29d9a0063f1c"
67
67
  }
@@ -0,0 +1,63 @@
1
+ import assert from 'minimalistic-assert'
2
+
3
+ const toAddressString = (address) => (typeof address === 'string' ? address : address?.address)
4
+
5
+ /**
6
+ * Builds the historical balance as of `date`, entirely from the tx log: per
7
+ * address, sum the outputs paying into the wallet (`data.internalOutputs`) and
8
+ * subtract the inputs it spent (`data.inputs`). Every address used by `date` is
9
+ * retained, including ones fully spent (so with a zero balance). Entries are
10
+ * coalesced by address.
11
+ *
12
+ * Only bitcoin, bitcoinregtest and bitcointestnet populate `data.inputs`, so
13
+ * the plugin wires this in for those assets only.
14
+ */
15
+ export const getHistoricalBalanceFactory = ({ assetClientInterface }) => {
16
+ assert(assetClientInterface, 'assetClientInterface is required')
17
+
18
+ return async ({ asset, walletAccount, date }) => {
19
+ assert(asset, 'asset is required')
20
+ assert(walletAccount, 'walletAccount is required')
21
+ assert(date instanceof Date, 'date is required')
22
+
23
+ const txLog = await assetClientInterface.getTxLog({ assetName: asset.name, walletAccount })
24
+
25
+ const byAddress = new Map()
26
+ const apply = (address, amount, txDate) => {
27
+ if (!address) return
28
+ const existing = byAddress.get(address)
29
+ if (existing) {
30
+ existing.amount = existing.amount.add(amount)
31
+ if (txDate < existing.date) existing.date = txDate
32
+ } else {
33
+ byAddress.set(address, { address, amount, date: txDate })
34
+ }
35
+ }
36
+
37
+ const cutoff = date.getTime()
38
+
39
+ for (const tx of txLog) {
40
+ const txDate = new Date(tx.date).getTime()
41
+ if (txDate > cutoff || tx.failed || tx.pending) continue
42
+
43
+ for (const out of tx.data.internalOutputs ?? []) {
44
+ apply(toAddressString(out.address), asset.currency.baseUnit(out.amount || 0), txDate)
45
+ }
46
+
47
+ for (const group of Object.values(tx.data.inputs ?? {})) {
48
+ for (const utxo of group.utxos ?? []) {
49
+ apply(toAddressString(group.address), asset.currency.parse(utxo.value).negate(), txDate)
50
+ }
51
+ }
52
+ }
53
+
54
+ let total = asset.currency.ZERO
55
+ const entries = []
56
+ for (const entry of byAddress.values()) {
57
+ total = total.add(entry.amount)
58
+ entries.push(entry)
59
+ }
60
+
61
+ return { total: total.clampLowerZero(), entries }
62
+ }
63
+ }
package/src/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './account-state.js'
2
2
  export * from './balances.js'
3
+ export { getHistoricalBalanceFactory } from './get-historical-balance.js'
3
4
  export * from './btc-address.js'
4
5
  export * from './btc-like-address.js'
5
6
  export * from './btc-like-keys.js'
@@ -14,6 +14,10 @@ const SENT_TIME_TO_DROP = ms('2m')
14
14
 
15
15
  const COINBASE_MATURITY_HEIGHT = 100
16
16
 
17
+ // Derive addresses in batches, yielding between them, so a long multisig gap
18
+ // scan (synchronous secp256k1 work) doesn't starve the event loop (timers, RPC).
19
+ const ADDRESS_DERIVATION_BATCH_SIZE = 10
20
+
17
21
  export class BitcoinMonitorScanner {
18
22
  #asset
19
23
  #insightClient
@@ -162,7 +166,7 @@ export class BitcoinMonitorScanner {
162
166
  // NOTE: the hope is that this gets replaced with a fetch by xpub method and then following the chain here won't be necessary
163
167
 
164
168
  const aggregateAddresses = async (chainObjects) => {
165
- const promises = []
169
+ const tasks = []
166
170
  for (const chainObject of chainObjects) {
167
171
  const { purpose, chainIndex, startAddressIndex, endAddressIndex } = chainObject
168
172
  for (
@@ -172,7 +176,7 @@ export class BitcoinMonitorScanner {
172
176
  ) {
173
177
  const index = multisigDataLength ?? 1
174
178
  for (let i = 0; i < index; i++) {
175
- promises.push(
179
+ tasks.push(() =>
176
180
  assetClientInterface
177
181
  .getAddress({
178
182
  assetName,
@@ -209,12 +213,20 @@ export class BitcoinMonitorScanner {
209
213
  }
210
214
 
211
215
  const addresses = []
212
- const allAddresses = await Promise.all(promises)
213
- const result = allAddresses.filter(Boolean)
214
- result.forEach(({ address, purpose }) => {
215
- purposeMap[String(address)] = purpose
216
- addresses.push(String(address))
217
- })
216
+ for (let i = 0; i < tasks.length; i += ADDRESS_DERIVATION_BATCH_SIZE) {
217
+ const batch = await Promise.all(
218
+ tasks.slice(i, i + ADDRESS_DERIVATION_BATCH_SIZE).map((task) => task())
219
+ )
220
+ batch.filter(Boolean).forEach(({ address, purpose }) => {
221
+ purposeMap[String(address)] = purpose
222
+ addresses.push(String(address))
223
+ })
224
+ // Yield so pending timers / RPC can run instead of being starved for the whole scan.
225
+ if (i + ADDRESS_DERIVATION_BATCH_SIZE < tasks.length) {
226
+ await new Promise((resolve) => setTimeout(resolve, 0))
227
+ }
228
+ }
229
+
218
230
  return addresses
219
231
  }
220
232