| Conditions | 1 |
| Paths | 1 |
| Total Lines | 91 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var _ = require('lodash'); |
||
| 36 | return wallet.maxSpendable(false).then(function(maxSpendable) { |
||
| 37 | console.log(maxSpendable); |
||
|
|
|||
| 38 | |||
| 39 | // get the address to aggregate the coins to |
||
| 40 | var addr = wallet.getAddressByPath("M/9999'/0/0"); |
||
| 41 | console.log(addr); |
||
| 42 | |||
| 43 | var splitSend = function(value) { |
||
| 44 | var pay = []; |
||
| 45 | var splitValue = Math.max(MINSPLITVALUE, Math.floor(value / SPLITNOUTS)).toFixed(0); |
||
| 46 | var nouts = Math.floor(value / splitValue); |
||
| 47 | |||
| 48 | for (var i = 0; i < nouts; i++) { |
||
| 49 | pay.push({address: addr, value: splitValue}); |
||
| 50 | } |
||
| 51 | |||
| 52 | return wallet.pay(pay, null, false, false, blocktrail.Wallet.FEE_STRATEGY_MIN_RELAY_FEE, false, {allowZeroConfSelf: false}).then(function(txId) { |
||
| 53 | console.log(txId); |
||
| 54 | }); |
||
| 55 | }; |
||
| 56 | |||
| 57 | // start at 20 BTC, * 0.5 every time we can't send such a large amount anymore |
||
| 58 | var value = 20 * ONEBTC; |
||
| 59 | // track retries |
||
| 60 | var retries = 0; |
||
| 61 | |||
| 62 | var keepSpending = function() { |
||
| 63 | return wallet.getBalance().then(function(b) { |
||
| 64 | console.log(b); |
||
| 65 | |||
| 66 | // do a split send |
||
| 67 | return splitSend(Math.min(b[0], value)) |
||
| 68 | .then(function() { |
||
| 69 | // keep sending for as long as possible |
||
| 70 | return keepSpending() |
||
| 71 | .then(function() { |
||
| 72 | // reset retries on success |
||
| 73 | retries = 0; |
||
| 74 | }); |
||
| 75 | }, function(e) { |
||
| 76 | console.error(e.message || e); |
||
| 77 | |||
| 78 | // if TX is too big (or MrSign times out cuz TX is too big to sign) |
||
| 79 | if (e.message.match(/too big/) || e.message.match(/MrSign/)) { |
||
| 80 | // if value reaches <= MINVALUE then we're done(ish) |
||
| 81 | if (value <= MINVALUE) { |
||
| 82 | retries++; |
||
| 83 | |||
| 84 | // retry 3 more times, otherwise done |
||
| 85 | if (retries >= 3) { |
||
| 86 | throw new Error("DONE"); |
||
| 87 | } |
||
| 88 | |||
| 89 | // keep trying |
||
| 90 | return keepSpending(); |
||
| 91 | } else { |
||
| 92 | // halve the value and try again |
||
| 93 | value *= 0.5; |
||
| 94 | console.log('too big, new value:', blocktrail.toBTC(value)); |
||
| 95 | |||
| 96 | return keepSpending(); |
||
| 97 | } |
||
| 98 | } else if (e.message.match(/too low/)) { |
||
| 99 | // halve the value and try again |
||
| 100 | value *= 0.5; |
||
| 101 | console.log('too big, new value:', blocktrail.toBTC(value)); |
||
| 102 | |||
| 103 | return keepSpending(); |
||
| 104 | } else if (e.message.match(/All usable unspent/)) { |
||
| 105 | // halve the value and try again, but with a timeout |
||
| 106 | value *= 0.5; |
||
| 107 | console.log('too big, new value:', blocktrail.toBTC(value)); |
||
| 108 | |||
| 109 | var deferred = q.defer(); |
||
| 110 | |||
| 111 | setTimeout(function() { |
||
| 112 | deferred.resolve(); |
||
| 113 | }, 11000); |
||
| 114 | |||
| 115 | return deferred.promise.then(function() { |
||
| 116 | return keepSpending(); |
||
| 117 | }); |
||
| 118 | } |
||
| 119 | |||
| 120 | throw e; |
||
| 121 | }); |
||
| 122 | }); |
||
| 123 | }; |
||
| 124 | |||
| 125 | return keepSpending(); |
||
| 126 | }); |
||
| 127 | }) |
||
| 131 |