Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept mining fee for TransactionBuilder #15

Merged
merged 1 commit into from
Jun 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/btcruby/transaction_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class TransactionBuilder
# Default is Transaction::DEFAULT_FEE_RATE
attr_accessor :fee_rate

# Mining fee for this transaction.
# If mining_fee is set, this values will be used as the minig fee.
attr_reader :mining_fee

# Minimum amount of change below which transaction is not composed.
# If change amount is non-zero and below this value, more unspent outputs are used.
# If change amount is zero, change output is not even created and this attribute is not used.
Expand Down Expand Up @@ -92,10 +96,14 @@ class TransactionBuilder
# A total size of all outputs in bytes (including change output).
attr_reader :outputs_size


# Implementation of the attributes declared above
# ===============================================

def initialize(options = {})
options.symbolize_keys!
@mining_fee = options[:mining_fee]
end

def network
@network ||= Network.default
end
Expand Down Expand Up @@ -217,7 +225,7 @@ def build
change_output = TransactionOutput.new(value: 0, script: self.change_address.public_address.script)
result.transaction.add_output(change_output)

result.fee = compute_fee_for_transaction(result.transaction, self.fee_rate)
result.fee = mining_fee || compute_fee_for_transaction(result.transaction, self.fee_rate)
result.outputs_amount = result.inputs_amount - result.fee
result.change_amount = 0

Expand Down Expand Up @@ -329,7 +337,7 @@ def build
# Before computing the fee, quick check if we have enough inputs to cover the outputs.
# If not, go and add one more utxo before wasting time computing fees.
if result.inputs_amount >= result.outputs_amount
fee = compute_fee_for_transaction(result.transaction, self.fee_rate)
fee = mining_fee || compute_fee_for_transaction(result.transaction, self.fee_rate)

change = result.inputs_amount - result.outputs_amount - fee

Expand Down