@exodus/bitcoin-api 2.3.6 → 2.3.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/bitcoin-api",
3
- "version": "2.3.6",
3
+ "version": "2.3.7",
4
4
  "description": "Exodus bitcoin-api",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -38,7 +38,8 @@
38
38
  "url-join": "4.0.0"
39
39
  },
40
40
  "devDependencies": {
41
- "@exodus/bitcoin-meta": "^1.0.1"
41
+ "@exodus/bitcoin-meta": "^1.0.1",
42
+ "jest-when": "^3.5.1"
42
43
  },
43
- "gitHead": "0ac89c74616ae95eee3c4628ea3babd98dd1495b"
44
+ "gitHead": "7a83d2ff5d672615938fba868779293df74ff51d"
44
45
  }
@@ -1,7 +1,7 @@
1
1
  /* @flow */
2
2
  import { orderTxs } from '../insight-api-client/util'
3
3
  import { Address, UtxoCollection } from '@exodus/models'
4
- import { isEqual, compact } from 'lodash'
4
+ import { isEqual, compact, uniq } from 'lodash'
5
5
  import ms from 'ms'
6
6
 
7
7
  import assert from 'minimalistic-assert'
@@ -505,4 +505,56 @@ export class BitcoinMonitorScanner {
505
505
  changedUnusedAddressIndexes,
506
506
  }
507
507
  }
508
+
509
+ async rescanOnNewBlock({ walletAccount }) {
510
+ const aci = this.#assetClientInterface
511
+ const asset = this.#asset
512
+ const assetName = this.#asset.name
513
+
514
+ const accountState = await aci.getAccountState({ assetName, walletAccount })
515
+
516
+ const allStoredUtxos = getUtxos({ accountState, asset })
517
+
518
+ const currentTxs = Array.from(await aci.getTxLog({ assetName, walletAccount }))
519
+
520
+ const unconfirmedTxIds = uniq([
521
+ ...allStoredUtxos
522
+ .toArray()
523
+ .filter((utxos) => !utxos.confirmations)
524
+ .map((utxos) => utxos.txId),
525
+ ...currentTxs.filter((tx) => !tx.dropped && !tx.confirmations).map((tx) => tx.txId),
526
+ ])
527
+
528
+ const confirmationsList = (
529
+ await Promise.all(
530
+ unconfirmedTxIds.map(async (txId) => {
531
+ const txStatus = await this.#insightClient.fetchTx(txId)
532
+ if (!txStatus?.confirmations) {
533
+ return undefined
534
+ }
535
+ return { txId, confirmations: txStatus.confirmations }
536
+ })
537
+ )
538
+ ).filter(Boolean)
539
+
540
+ const updatedPropertiesTxs = currentTxs
541
+ .map((tx) => {
542
+ const updatedProperties = {}
543
+ const confirmations = confirmationsList.find(({ txId }) => tx.txId === txId)?.confirmations
544
+ if (!tx.dropped && !tx.confirmations && confirmations > 0) {
545
+ updatedProperties.confirmations = confirmations
546
+ }
547
+ return { txId: tx.txId, ...updatedProperties }
548
+ })
549
+ .filter((tx) => Object.keys(tx).length > 1)
550
+
551
+ const txConfirmedUtxos = confirmationsList.length
552
+ ? allStoredUtxos.updateConfirmations(confirmationsList)
553
+ : null
554
+
555
+ return {
556
+ utxos: txConfirmedUtxos,
557
+ txsToUpdate: updatedPropertiesTxs,
558
+ }
559
+ }
508
560
  }
@@ -94,7 +94,6 @@ export class Monitor extends BaseMonitor {
94
94
  }
95
95
 
96
96
  #connectWS = async (wsUrl) => {
97
- const aci = this.aci
98
97
  if (this.#ws) {
99
98
  this.#ws.close()
100
99
  this.#ws = null
@@ -125,46 +124,7 @@ export class Monitor extends BaseMonitor {
125
124
  )
126
125
  this.tickWalletAccounts({ walletAccount })
127
126
  })
128
- this.#ws.on('block', async () => {
129
- await this.#yieldToUI(100)
130
- const assetName = this.asset.name
131
-
132
- const walletAccounts = await aci.getWalletAccounts({ assetName })
133
- for (const walletAccount of walletAccounts) {
134
- const currentTxs = Array.from(await aci.getTxLog({ assetName, walletAccount }))
135
- const updatedPropertiesTxs = await Promise.all(
136
- currentTxs.map(async (tx) => {
137
- if (tx.dropped || (tx.confirmations && tx.confirmations > 0)) return null
138
- const txStatus = await this.#insightClient.fetchTx(tx.txId)
139
- if (!txStatus) return null
140
- if (txStatus.confirmations <= 0) return null
141
- return { txId: tx.txId, confirmations: txStatus.confirmations }
142
- })
143
- ).then((res) => res.filter((status) => !!status))
144
- if (updatedPropertiesTxs.length) {
145
- await this.updateTxLog({ assetName, walletAccount, logItems: updatedPropertiesTxs })
146
-
147
- const accountState = await aci.getAccountState({ assetName, walletAccount })
148
-
149
- const utxos = accountState.utxos.updateConfirmations(updatedPropertiesTxs)
150
- await aci.updateAccountState({
151
- accountState,
152
- assetName,
153
- walletAccount,
154
- newData: { utxos },
155
- })
156
-
157
- if (['bitcoin', 'bitcoinregtest', 'bitcointestnet'].includes(assetName)) {
158
- updateUnconfirmedAncestorData({
159
- asset: this.asset,
160
- walletAccount,
161
- accountState,
162
- insightClient: this.#insightClient,
163
- })
164
- }
165
- }
166
- }
167
- })
127
+ this.#ws.on('block', () => this.onNewBlock())
168
128
  this.#ws.on('disconnect', () => {
169
129
  this.#logWsStatus('disconnect')
170
130
  this.timer.setNewInterval(this.interval)
@@ -175,6 +135,42 @@ export class Monitor extends BaseMonitor {
175
135
  })
176
136
  }
177
137
 
138
+ async onNewBlock() {
139
+ await this.#yieldToUI(100)
140
+ const aci = this.aci
141
+ const asset = this.asset
142
+ const assetName = asset.name
143
+ const walletAccounts = await aci.getWalletAccounts({ assetName })
144
+ for (const walletAccount of walletAccounts) {
145
+ const { txsToUpdate, utxos } = await this.#scanner.rescanOnNewBlock({
146
+ walletAccount,
147
+ })
148
+
149
+ if (utxos) {
150
+ await aci.updateAccountState({
151
+ assetName,
152
+ walletAccount,
153
+ newData: {
154
+ utxos,
155
+ },
156
+ })
157
+ }
158
+
159
+ if (txsToUpdate.length) {
160
+ const accountState = await aci.getAccountState({ assetName, walletAccount })
161
+ await this.updateTxLog({ assetName, walletAccount, logItems: txsToUpdate })
162
+ if (['bitcoin', 'bitcoinregtest', 'bitcointestnet'].includes(assetName)) {
163
+ updateUnconfirmedAncestorData({
164
+ asset,
165
+ walletAccount,
166
+ accountState,
167
+ insightClient: this.#insightClient,
168
+ })
169
+ }
170
+ }
171
+ }
172
+ }
173
+
178
174
  #subscribeToNewAddresses = async () => {
179
175
  const newAddressesByWalletAccount = await this.#getReceiveAddressesByWalletAccount()
180
176
  if (!isEqual(newAddressesByWalletAccount, this.#addressesByWalletAccount)) {