diff --git a/openassets/transactions.py b/openassets/transactions.py index ee45efb..85e858d 100644 --- a/openassets/transactions.py +++ b/openassets/transactions.py @@ -174,8 +174,7 @@ def asset_asset_swap( return self.transfer( [(asset1_id, asset1_transfer_spec), (asset2_id, asset2_transfer_spec)], btc_transfer_spec, fees) - @staticmethod - def _collect_uncolored_outputs(unspent_outputs, amount): + def _collect_uncolored_outputs(self, unspent_outputs, amount): """ Returns a list of uncolored outputs for the specified amount. @@ -191,10 +190,14 @@ def _collect_uncolored_outputs(unspent_outputs, amount): result.append(output) total_amount += output.output.value - if total_amount >= amount: + # Make sure we don't create dust outputs + if total_amount == amount or (total_amount >= amount and (total_amount - amount) >= self._dust_amount): return result, total_amount - raise InsufficientFundsError + if total_amount >= amount and (total_amount - amount) < self._dust_amount: + raise DustOutputError + else: + raise InsufficientFundsError @staticmethod def _collect_colored_outputs(unspent_outputs, asset_id, asset_quantity): diff --git a/tests/test_transactions.py b/tests/test_transactions.py index caae478..8b99e28 100644 --- a/tests/test_transactions.py +++ b/tests/test_transactions.py @@ -153,6 +153,17 @@ def test_transfer_bitcoin_dust_change(self): openassets.transactions.DustOutputError, self.target.transfer_bitcoin, spec, 10) + def test_transfer_bitcoin_dust_output_more_inputs(self): + outputs = self.generate_outputs([ + (10, b'source', None, 0), + (11, b'source', None, 0), + (10, b'source', None, 0) + ]) + + spec = openassets.transactions.TransferParameters(outputs, b'target', b'change', 10) + result = self.target.transfer_bitcoin(spec, 10) + self.assertEqual(3, len(result.vin)) + def test_transfer_assets_with_change(self): outputs = self.generate_outputs([ (10, b'source', b'a1', 50),