Module: CoinOp::Bit::Fee

Defined in:
lib/coin-op/bit/fee.rb

Constant Summary collapse

PRIORITY_THRESHOLD =

en.bitcoin.it/wiki/Transaction_fees#Including_in_Blocks

en.bitcoin.it/wiki/Transaction_fees#Technical_info > Transactions need to have a priority above 57,600,000 to avoid the > enforced limit.… This threshold is written in the code as > COIN * 144 / 250, suggesting that the threshold represents a one day > old, 1 btc coin (144 is the expected number of blocks per day) and a > transaction size of 250 bytes.

57_600_000

Class Method Summary collapse

Class Method Details

.estimate(unspents, payees, tx_size = nil, network:) ⇒ Object

Given an array of unspent Outputs and an array of Outputs for a Transaction, estimate the fee required for the transaction to be included in a block. Optionally takes an Integer specifying the transaction size in bytes, which is necessary when using unspents that deviate from the customary single signature.

Returns the estimated fee in satoshis.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/coin-op/bit/fee.rb', line 15

def estimate(unspents, payees, tx_size=nil, network:)
  # https://en.bitcoin.it/wiki/Transaction_fees

  # dupe because we'll need to add a change output
  payees = payees.dup

  unspent_total = unspents.inject(0) {|sum, output| sum += output.value}
  payee_total = payees.inject(0) {|sum, payee| sum += payee.value}
  nominal_change = unspent_total - payee_total
  payees << Output.new(value: nominal_change, network: network)

  tx_size ||= estimate_tx_size(unspents.size, payees.size)
  min = payees.min_by {|payee| payee.value }

  small = tx_size < 1000
  big_outputs = min.value > 1_000_000

  p = priority :size => tx_size, :unspents => (unspents.map do |output|
    {:value => output.value, :age => output.confirmations}
  end)
  high_priority = p > PRIORITY_THRESHOLD

  if small && big_outputs && high_priority
    0
  else
    fee_for_bytes(tx_size, network: network)
  end

end

.estimate_tx_size(num_inputs, num_outputs) ⇒ Object

From bitcoinfees.com. This estimation is only valid for transactions with all inputs using the common “public key hash” method for authorization.



56
57
58
59
# File 'lib/coin-op/bit/fee.rb', line 56

def estimate_tx_size(num_inputs, num_outputs)
  # From http://bitcoinfees.com.
  (148 * num_inputs) + (34 * num_outputs) + 10
end

.fee_for_bytes(bytes, network:) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/coin-op/bit/fee.rb', line 45

def fee_for_bytes(bytes, network:) 
  # https://en.bitcoin.it/wiki/Transaction_fees
  # > the reference implementation will round up the transaction size to the
  # > next thousand bytes and add a fee of 0.1 mBTC (0.0001 BTC) per thousand bytes
  size = (bytes / 1000) + 1
  CoinOp.syncbit(network) { Bitcoin.network[:min_tx_fee] * size }
end

.priority(params) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/coin-op/bit/fee.rb', line 72

def priority(params)
  tx_size, unspents = params.values_at :size, :unspents
  sum = unspents.inject(0) do |sum, output|
    age = output[:age] || 0
    sum += (output[:value] * age)
    sum
  end
  sum / tx_size
end