Skip to content

Commit

Permalink
fix(ref-imp): updated bitcoin listunspent RPC call to include unconfi…
Browse files Browse the repository at this point in the history
…rmed transactions
  • Loading branch information
thehenrytsai committed Mar 23, 2021
1 parent c3940ad commit 1a028d1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/bitcoin/BitcoinClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,15 @@ export default class BitcoinClient {

const addressToSearch = address.toString();
Logger.info(`Getting unspent coins for ${addressToSearch}`);

// We are setting minimum required confirmations when fetching unspent transactions to 0
// so that transaction(s) waiting to be confirmed are included. This allows:
// 1. Accurate calculation of wallet balance (otherwise no transaction returned by the `listunspent` call yields balance of 0).
// 2. Both lock monitor and core to write a transaction using the UTXO in the unconfirmed transaction generated by each other.
const request = {
method: 'listunspent',
params: [
null,
0, // Minimum required confirmation.
null,
[addressToSearch]
]
Expand Down
6 changes: 4 additions & 2 deletions tests/bitcoin/BitcoinClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,8 @@ describe('BitcoinClient', async () => {
it('should query for unspent output coins given an address', async (done) => {
const coin = BitcoinDataGenerator.generateUnspentCoin(bitcoinWalletImportString, 1);

const coinSpy = mockRpcCall('listunspent', [null, null, [walletAddressFromBitcoinClient.toString()]], [
const minimumConfirmations = 0;
const coinSpy = mockRpcCall('listunspent', [minimumConfirmations, null, [walletAddressFromBitcoinClient.toString()]], [
{
txId: coin.txId,
outputIndex: coin.outputIndex,
Expand All @@ -638,7 +639,8 @@ describe('BitcoinClient', async () => {
});

it('should return empty if no coins were found', async (done) => {
const coinSpy = mockRpcCall('listunspent', [null, null, [walletAddressFromBitcoinClient.toString()]], []);
const minimumConfirmations = 0;
const coinSpy = mockRpcCall('listunspent', [minimumConfirmations, null, [walletAddressFromBitcoinClient.toString()]], []);
const actual = await bitcoinClient['getUnspentOutputs'](walletAddressFromBitcoinClient);
expect(coinSpy).toHaveBeenCalled();
expect(actual).toEqual([]);
Expand Down

0 comments on commit 1a028d1

Please sign in to comment.