Skip to content

Commit

Permalink
fix: "track multiple receiving addresses" test
Browse files Browse the repository at this point in the history
  • Loading branch information
limpbrains committed Nov 27, 2024
1 parent 8c51787 commit 7da6ce0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as bip39 from 'bip39';
import * as bitcoin from 'bitcoinjs-lib';
import { Network, networks } from 'bitcoinjs-lib';
import BIP32Factory, { BIP32Interface } from 'bip32';
Expand Down Expand Up @@ -109,7 +110,6 @@ import { Transaction } from '../transaction';
import { GAP_LIMIT, GAP_LIMIT_CHANGE, TRANSACTION_DEFAULTS } from './constants';
import cloneDeep from 'lodash.clonedeep';
import { btcToSats } from '../utils/conversion';
import * as bip39 from 'bip39';

const bip32 = BIP32Factory(ecc);

Expand Down Expand Up @@ -1974,7 +1974,6 @@ export class Wallet {
[key: string]: number[];
} = {};
for (const tx of receivedTxs) {
this.sendMessage('transactionReceived', tx);
// No need to scan an address with a saved UTXO.
if (utxoScriptHashes.has(tx.transaction.scriptHash)) continue;
for (const addressType in addresses) {
Expand Down Expand Up @@ -2011,6 +2010,10 @@ export class Wallet {
}
}

for (const tx of receivedTxs) {
this.sendMessage('transactionReceived', tx);
}

return ok(notificationTxid);
}

Expand Down
25 changes: 10 additions & 15 deletions tests/receive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ beforeEach(async function () {
lookAheadChange: 2,
lookBehindChange: 2
},
addressTypesToMonitor: [EAddressType.p2wpkh],
onMessage: ml.onMessage
});
if (res.isErr()) throw res.error;
Expand Down Expand Up @@ -118,8 +119,7 @@ describe('Receive', async function () {
expect(wallet.utxos.length).to.equal(1);
});

// failing, WIP
it.skip('Should track multiple receiving addresses', async () => {
it('Should track multiple receiving addresses', async () => {
const r1 = await wallet.getNextAvailableAddress();
if (r1.isErr()) throw r1.error;
const address1 = r1.value.addressIndex.address;
Expand All @@ -141,22 +141,17 @@ describe('Receive', async function () {
// After a transaction, addresses should differ
expect(address1).to.not.equal(address3);

const receivePromise = ml.waitFor('transactionReceived');

// Second transaction
const receivePromise = ml.waitFor('transactionReceived');
await rpc.sendToAddress(address3, '0.2');
await rpc.generateToAddress(1, await rpc.getNewAddress());
await waitForElectrum();

// await sleep(1000);
await receivePromise;
while (wallet.isRefreshing) {
console.info('wallet.isRefreshing', wallet.isRefreshing);
await sleep(10);
}
// await wallet.refreshWallet();

console.log(wallet.data);
// unfortinately it is possible that Electrum server still does not
// have second transaction parsed so we need to wait a bit
while (wallet.data.utxos.length === 1) {
await sleep(100);
await wallet.refreshWallet();
}

// Check balances
expect(wallet.balance).to.equal(0.3 * 10e7); // 0.3 BTC in sats
Expand All @@ -167,7 +162,7 @@ describe('Receive', async function () {
expect(balance1.value.confirmed).to.equal(0.1 * 10e7);
const balance3 = await wallet.getAddressBalance(address3);
if (balance3.isErr()) throw balance3.error;
expect(balance3.value.confirmed).to.equal(0.2 * 10e7);
expect(balance3.value.unconfirmed).to.equal(0.2 * 10e7);

// Test getAddressesBalance
const combinedBalance = await wallet.getAddressesBalance([
Expand Down

0 comments on commit 7da6ce0

Please sign in to comment.