change estimated_fee to include 34 bytes per output instead of hard-coded 80 (issue...
authorCodingAnarchy <mtanous22@gmail.com>
Wed, 5 Mar 2014 08:01:09 +0000 (01:01 -0700)
committerCodingAnarchy <mtanous22@gmail.com>
Wed, 5 Mar 2014 08:01:09 +0000 (01:01 -0700)
lib/wallet.py

index 9eff108..3948a95 100644 (file)
@@ -1087,7 +1087,7 @@ class NewWallet:
         return [x[1] for x in coins]
 
 
-    def choose_tx_inputs( self, amount, fixed_fee, domain = None ):
+    def choose_tx_inputs( self, amount, fixed_fee, num_outputs, domain = None ):
         """ todo: minimize tx size """
         total = 0
         fee = self.fee if fixed_fee is None else fixed_fee
@@ -1107,7 +1107,7 @@ class NewWallet:
             v = item.get('value')
             total += v
             inputs.append(item)
-            fee = self.estimated_fee(inputs) if fixed_fee is None else fixed_fee
+            fee = self.estimated_fee(inputs, num_outputs) if fixed_fee is None else fixed_fee
             if total >= amount + fee: break
         else:
             inputs = []
@@ -1120,8 +1120,8 @@ class NewWallet:
             self.fee = fee
             self.storage.put('fee_per_kb', self.fee, True)
         
-    def estimated_fee(self, inputs):
-        estimated_size =  len(inputs) * 180 + 80    # this assumes non-compressed keys
+    def estimated_fee(self, inputs, num_outputs):
+        estimated_size =  len(inputs) * 180 + num_outputs * 34    # this assumes non-compressed keys
         fee = self.fee * int(math.ceil(estimated_size/1000.))
         return fee
 
@@ -1282,7 +1282,7 @@ class NewWallet:
         for address, x in outputs:
             assert is_valid(address), "Address " + address + " is invalid!"
         amount = sum( map(lambda x:x[1], outputs) )
-        inputs, total, fee = self.choose_tx_inputs( amount, fee, domain )
+        inputs, total, fee = self.choose_tx_inputs( amount, fee, len(outputs), domain )
         if not inputs:
             raise ValueError("Not enough funds")
         self.add_input_info(inputs)