def estimate(unspents, payees, network:, tx_size: nil, fee_per_kb: nil)
      # 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) if nominal_change > 0

      tx_size ||= estimate_tx_size(unspents, payees)

      # conditions for 0-fee transactions
      small = tx_size < 1000

      min_payee = payees.min_by { |payee| payee.value }
      big_outputs = min_payee.value > 1_000_000
      high_priority = priority(
        size: tx_size,
        unspents: unspents.map { |output| { value: output.value, age: output.confirmations } }
      ) > PRIORITY_THRESHOLD

      # 0-fee requirements met
      return 0 if small && big_outputs && high_priority

      # Otherwise, calculate the fee by size
      fee_for_bytes(tx_size, network: network, fee_per_kb: fee_per_kb)
    end
def estimate(unspents, payees, network:, tx_size: nil, fee_per_kb: nil)
      # 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) if nominal_change > 0

      tx_size ||= estimate_tx_size(unspents, payees)

      # conditions for 0-fee transactions
      small = tx_size < 1000

      min_payee = payees.min_by { |payee| payee.value }
      big_outputs = min_payee.value > 1_000_000
      high_priority = priority(
        size: tx_size,
        unspents: unspents.map { |output| { value: output.value, age: output.confirmations } }
      ) > PRIORITY_THRESHOLD

      # 0-fee requirements met
      return 0 if small && big_outputs && high_priority

      # Otherwise, calculate the fee by size
      fee_for_bytes(tx_size, network: network, fee_per_kb: fee_per_kb)
    end
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 tx_size specifying the transaction size in bytes
   This is useful if you have the scriptSigs for the unspents, because
   you can get a more accurate size than the estimate which is generated
   here by default

 Optionally takes an Integer fee_per_kb specifying the chosen cost per 1000
   bytes to use

 Returns the estimated fee in satoshis.