From 8b4e86647283d6d10d002f44814f657a3308f33c Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 3 Nov 2014 08:40:53 -0300 Subject: [PATCH] add amount param --- js/models/Wallet.js | 4 ++-- util/swipeWallet.js | 30 +++++++++++++++++++----------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 4bf9e5272..8c4d6bce2 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -2381,7 +2381,7 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, comment, utxos var selectedUtxos = b.getSelectedUnspent(); if (selectedUtxos.size > TX_MAX_INS) - throw new Error('Resulting TX is TOO big:' + selectedUtxos.size + ' inputs. Aborting'); + throw new Error('BIG: Resulting TX is too big:' + selectedUtxos.size + ' inputs. Aborting'); var inputChainPaths = selectedUtxos.map(function(utxo) { @@ -2403,7 +2403,7 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, comment, utxos var txSize = tx.getSize(); if (txSize/1024 > TX_MAX_SIZE_KB) - throw new Error('Resulting TX is TOO big ' + txSize + ' bytes. Aborting'); + throw new Error('BIG: Resulting TX is too big ' + txSize + ' bytes. Aborting'); var me = {}; diff --git a/util/swipeWallet.js b/util/swipeWallet.js index c446d74d2..448fc5bdb 100755 --- a/util/swipeWallet.js +++ b/util/swipeWallet.js @@ -22,7 +22,8 @@ program .option('-d, --destination ', 'Destination Address') .option('-n, --required ', 'Required number of signatures', parseInt) .option('-k, --keys ', 'master private keys', list) - .option('-f, --fee [n]', 'Set fee in BTC (default 0.0001 BTC)', parseFloat) + .option('-a, --amount ', 'Optional, amount to transfer, in Satoshis', parseInt) + .option('-f, --fee [n]', 'Optional, fee in BTC (default 0.0001 BTC)', parseFloat) .parse(process.argv); // Fee to asign to the tx. Please put a bigger number if you get 'unsufficient unspent' @@ -38,6 +39,7 @@ var requiredCopayers = program.required; var extPrivKeys = program.keys; var totalCopayers = extPrivKeys.length; var destAddr = program.destination; +var amount = program.amount; if (!requiredCopayers || !extPrivKeys || !extPrivKeys.length || !destAddr){ program.outputHelp(); @@ -140,20 +142,26 @@ firstWallet.updateIndexes(function() { console.log('Balance per address:', balanceByAddr); //TODO if (!balance) { - console.log('Could not find any coins in the generated wallet'); //TODO - process.exit(1); + throw ('Could not find any coins in the generated wallet'); } - // rl.question("\n\tShould I swipe the wallet (destination address is:" + destAddr + ")?\n\t(`yes` to continue)\n\t", function(answer) { - // if (answer !== 'yes') - // process.exit(1); - var amount = balance - fee * bitcore.util.COIN; + if (amount && amount >= balance) + throw ('Not enought balance fund to fullfill ' + amount + ' satoshis'); + + rl.question("\n\tShould I swipe the wallet (destination address is:" + destAddr + " Amount: "+ amount + "Satoshis )?\n\t(`yes` to continue)\n\t", function(answer) { + if (answer !== 'yes') + process.exit(1); + + amount = amount || balance - fee * bitcore.util.COIN; -console.log('[swipeWallet.js.136]'); //TODO firstWallet.createTx(destAddr, amount, '', {}, function(err, ntxid) { if (err || !ntxid) { - throw new Error('Could not create tx' + err + '. Try a bigger fee (--fee).'); + if (err && err.toString().match('BIG')) { + throw new Error('Could not create tx' + err ); + } else { + throw new Error('Could not create tx' + err + '. Try a bigger fee (--fee).'); + } } console.log('\n\t### Tx Proposal Created...\n\tWith copayer 0 signature.'); @@ -191,7 +199,7 @@ console.log('[swipeWallet.js.136]'); //TODO console.log('\t FULLY SIGNED. BROADCASTING NOW....'); var tx = p.builder.build(); var txHex = tx.serialize().toString('hex'); - console.log('\t RAW TX: ', txHex); + //console.log('\t RAW TX: ', txHex); firstWallet.sendTx(ntxid, function(txid) { console.log('\t ####### SENT TXID:', txid); process.exit(1); @@ -202,6 +210,6 @@ console.log('[swipeWallet.js.136]'); //TODO } ) }); - // }); + }); }); });