File tree Expand file tree Collapse file tree 2 files changed +49
-10
lines changed
sdk-core/src/bitgo/wallet Expand file tree Collapse file tree 2 files changed +49
-10
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments