Skip to content

Commit 4d70af6

Browse files
Merge pull request #5232 from BitGo/COIN-2432
fix: use max spendable amount to sweep balance
2 parents f06febc + 19eba51 commit 4d70af6

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

modules/bitgo/test/v2/unit/wallet.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,41 @@ describe('V2 Wallet:', function () {
17901790
});
17911791
});
17921792

1793+
describe('sweep wallet', function () {
1794+
let basecoin;
1795+
let wallet;
1796+
1797+
before(async function () {
1798+
basecoin = bitgo.coin('ttrx');
1799+
const walletData = {
1800+
id: '5b34252f1bf349930e34020a',
1801+
coin: 'ttrx',
1802+
keys: ['5b3424f91bf349930e340175'],
1803+
};
1804+
wallet = new Wallet(bitgo, basecoin, walletData);
1805+
});
1806+
1807+
it('should use maximum spendable balance of wallet to sweep funds ', async function () {
1808+
const path = `/api/v2/${wallet.coin()}/wallet/${wallet.id()}/maximumSpendable`;
1809+
const response = nock(bgUrl).get(path).reply(200, {
1810+
coin: 'ttrx',
1811+
maximumSpendable: 65000,
1812+
});
1813+
const body = {
1814+
coin: 'ttrx',
1815+
address: '2MwvR24yqym2CgHMp7zwvdeqBa4F8KTqunS',
1816+
};
1817+
try {
1818+
await wallet.sweep(body);
1819+
} catch (e) {
1820+
// the sweep method will probably throw an exception for not having all of the correct nocks
1821+
// we only care about maximum spendable balance being used to sweep funds
1822+
}
1823+
1824+
response.isDone().should.be.true();
1825+
});
1826+
});
1827+
17931828
describe('Transaction prebuilds', function () {
17941829
let ethWallet;
17951830

modules/sdk-core/src/bitgo/wallet/wallet.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -937,19 +937,23 @@ export class Wallet implements IWallet {
937937
'cannot sweep when unconfirmed funds exist on the wallet, please wait until all inbound transactions confirm'
938938
);
939939
}
940-
941-
const value = this.spendableBalanceString();
942-
if (_.isUndefined(value) || value === '0') {
940+
const value = await this.bitgo.get(this.url('/maximumSpendable')).result();
941+
const maximumSpendable = new BigNumber(value.maximumSpendable);
942+
if (value !== undefined || maximumSpendable.isZero()) {
943943
throw new Error('no funds to sweep');
944944
}
945-
(params as any).recipients = [
946-
{
947-
address: params.address,
948-
amount: value,
949-
},
950-
];
951945

952-
return this.sendMany(params);
946+
const sendManyParams: SendManyOptions = {
947+
...params,
948+
recipients: [
949+
{
950+
address: params.address || '', // Ensure address is always a string
951+
amount: maximumSpendable.toString(),
952+
},
953+
],
954+
};
955+
956+
return this.sendMany(sendManyParams);
953957
}
954958
// the following flow works for all UTXO coins
955959

0 commit comments

Comments
 (0)