Prepare to calculate tx fee given a tx size
This commit is contained in:
parent
39af17bc23
commit
a4dd5acc48
|
@ -32,8 +32,7 @@ class CoinChooser(PrintError):
|
||||||
added to the transaction fee.'''
|
added to the transaction fee.'''
|
||||||
amount = sum(map(lambda x: x[2], outputs))
|
amount = sum(map(lambda x: x[2], outputs))
|
||||||
total = 0
|
total = 0
|
||||||
inputs = []
|
tx = Transaction.from_io([], outputs)
|
||||||
tx = Transaction.from_io(inputs, outputs)
|
|
||||||
fee = fee_estimator(tx)
|
fee = fee_estimator(tx)
|
||||||
# add inputs, sorted by age
|
# add inputs, sorted by age
|
||||||
for item in coins:
|
for item in coins:
|
||||||
|
|
|
@ -689,14 +689,22 @@ class Transaction:
|
||||||
def get_fee(self):
|
def get_fee(self):
|
||||||
return self.input_value() - self.output_value()
|
return self.input_value() - self.output_value()
|
||||||
|
|
||||||
@profiler
|
@classmethod
|
||||||
def estimated_fee(self, fee_per_kb):
|
def estimated_fee_for_size(self, fee_per_kb, size):
|
||||||
estimated_size = len(self.serialize(-1)) / 2
|
'''Given a fee per kB in satoshis, and a tx size in bytes,
|
||||||
fee = int(fee_per_kb * estimated_size / 1000.)
|
returns the transaction fee.'''
|
||||||
|
fee = int(fee_per_kb * size / 1000.)
|
||||||
if fee < MIN_RELAY_TX_FEE:
|
if fee < MIN_RELAY_TX_FEE:
|
||||||
fee = MIN_RELAY_TX_FEE
|
fee = MIN_RELAY_TX_FEE
|
||||||
return fee
|
return fee
|
||||||
|
|
||||||
|
@profiler
|
||||||
|
def estimated_fee(self, fee_per_kb):
|
||||||
|
'''Return an estimated fee given a fee per kB in satoshis.'''
|
||||||
|
# Remember self.serialize returns an ASCII hex string
|
||||||
|
size = len(self.serialize(-1)) / 2
|
||||||
|
return self.estimated_fee_for_size(fee_per_kb, size)
|
||||||
|
|
||||||
def signature_count(self):
|
def signature_count(self):
|
||||||
r = 0
|
r = 0
|
||||||
s = 0
|
s = 0
|
||||||
|
|
|
@ -927,11 +927,14 @@ class Abstract_Wallet(PrintError):
|
||||||
else:
|
else:
|
||||||
change_addrs = [address]
|
change_addrs = [address]
|
||||||
|
|
||||||
|
# Fee estimator
|
||||||
|
if fixed_fee is None:
|
||||||
fee_per_kb = self.fee_per_kb(config)
|
fee_per_kb = self.fee_per_kb(config)
|
||||||
def fee_estimator(tx):
|
def fee_estimator(tx):
|
||||||
if fixed_fee is not None:
|
|
||||||
return fixed_fee
|
|
||||||
return tx.estimated_fee(fee_per_kb)
|
return tx.estimated_fee(fee_per_kb)
|
||||||
|
else:
|
||||||
|
def fee_estimator(tx):
|
||||||
|
return fixed_fee
|
||||||
|
|
||||||
# Change <= dust threshold is added to the tx fee
|
# Change <= dust threshold is added to the tx fee
|
||||||
dust_threshold = 182 * 3 * MIN_RELAY_TX_FEE / 1000
|
dust_threshold = 182 * 3 * MIN_RELAY_TX_FEE / 1000
|
||||||
|
|
Loading…
Reference in New Issue