Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

Commit

Permalink
LL-6959 Add estimateAccountMaxSpendable (#39)
Browse files Browse the repository at this point in the history
* add estimateAccountMaxSpendable

* add test for estimateMaxSpendable
  • Loading branch information
hzheng-ledger authored Aug 31, 2021
1 parent dface0a commit 83bc0b9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/__tests__/wallet.estimateMaxSpendable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import WalletLedger from '../wallet';
import * as utils from '../utils';
import { Account } from '../account';
import MockBtc from './mocks/Btc';

describe('testing estimateMaxSpendable', () => {
const wallet = new WalletLedger();
let account: Account;
it('should generate an account', async () => {
account = await wallet.generateAccount({
btc: new MockBtc(),
path: "44'/0'",
index: 0,
network: 'mainnet',
derivationMode: 'Legacy',
explorer: 'ledgerv3',
explorerURI: 'https://explorers.api.vault.ledger.com/blockchain/v3/btc',
storage: 'mock',
storageParams: [],
});

expect(account.xpub.xpub).toEqual(
'xpub6CV2NfQJYxHn7MbSQjQip3JMjTZGUbeoKz5xqkBftSZZPc7ssVPdjKrgh6N8U1zoQDxtSo6jLarYAQahpd35SJoUKokfqf1DZgdJWZhSMqP'
);
});

it('should estimate max spendable correctly', async () => {
await wallet.syncAccount(account);
let maxSpendable = await wallet.estimateAccountMaxSpendable(account, 0);
const balance = 109088;
expect(maxSpendable.toNumber()).toEqual(balance);
let feesPerByte = 100;
maxSpendable = await wallet.estimateAccountMaxSpendable(account, feesPerByte);
expect(maxSpendable.toNumber()).toEqual(
balance - feesPerByte * utils.estimateTxSize(2, 1, account.xpub.crypto, account.xpub.derivationMode)
);
feesPerByte = 10000;
maxSpendable = await wallet.estimateAccountMaxSpendable(account, feesPerByte);
expect(maxSpendable.toNumber()).toEqual(0);
}, 60000);
});
13 changes: 13 additions & 0 deletions src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ class WalletLedger {
return flatten(await Promise.all(addresses.map((address) => account.xpub.storage.getAddressUnspentUtxos(address))));
}

// eslint-disable-next-line class-methods-use-this
async estimateAccountMaxSpendable(account: Account, feePerByte: number) {
const addresses = await account.xpub.getXpubAddresses();
const utxos = flatten(
await Promise.all(addresses.map((address) => account.xpub.storage.getAddressUnspentUtxos(address)))
);
const balance = await account.xpub.getXpubBalance();
// fees if we use all utxo
const fees = feePerByte * utils.estimateTxSize(utxos.length, 1, account.xpub.crypto, account.xpub.derivationMode);
const maxSpendable = balance.minus(fees);
return maxSpendable.lt(0) ? new BigNumber(0) : maxSpendable;
}

// eslint-disable-next-line class-methods-use-this
async getAccountBalance(account: Account) {
const balance = await account.xpub.getXpubBalance();
Expand Down

0 comments on commit 83bc0b9

Please sign in to comment.