diff --git a/bitgo/bitgo.py b/bitgo/bitgo.py index 5db14cf..ca5006e 100644 --- a/bitgo/bitgo.py +++ b/bitgo/bitgo.py @@ -41,7 +41,7 @@ from pycoin.tx import Spendable from pycoin.tx.pay_to.ScriptMultisig import ScriptMultisig -from pycoin.tx.pay_to import SolvingError +from pycoin.tx.exceptions import SolvingError from pycoin.tx.script import tools from pycoin.tx.script.check_signature import parse_signature_blob from pycoin import ecdsa @@ -291,6 +291,7 @@ def send(self, wallet_id, passcode, address, amount, message='', fee=None, fan_u """ MINIMAL_FEE = 20000 MINIMAL_SPLIT = 10000000 + MIN_UNSPENTS_FAN = 5 wallet = self.get_wallet(wallet_id) if not wallet['spendingAccount']: @@ -333,9 +334,11 @@ def send(self, wallet_id, passcode, address, amount, message='', fee=None, fan_u cipher = sjcl.SJCL() xprv = cipher.decrypt(data, passcode) - unspents = self.get_unspents(wallet_id) + unspents = self.get_unspents(wallet_id)['unspents'] + order_unspents = sorted(unspents, key=lambda k: k['confirmations'], reverse=True) + total_value = 0 - for d in unspents['unspents'][::-1]: + for d in order_unspents: path = keychain_path + d['chainPath'] chain_paths.append(path) p2sh.append(h2b(d["redeemScript"])) @@ -349,7 +352,7 @@ def send(self, wallet_id, passcode, address, amount, message='', fee=None, fan_u break # make many outputs? - if len(unspents['unspents']) < 5 and (total_value > (amount + MINIMAL_SPLIT)) and fan_unspend > 0: + if len(order_unspents) < MIN_UNSPENTS_FAN and (total_value > (amount + MINIMAL_SPLIT)) and fan_unspend > 0: fee = self.calculate_fee(len(spendables), fan_unspend) value = (total_value - amount - fee) / fan_unspend for i in range(fan_unspend):