class Glueby::FeeProvider::Tasks

Constants

STATUS

Attributes

fee_provider[R]

Public Class Methods

new() click to toggle source
# File lib/glueby/fee_provider/tasks.rb, line 15
def initialize
  @fee_provider = Glueby::FeeProvider.new
end

Public Instance Methods

address() click to toggle source

Show the address of Fee Provider

# File lib/glueby/fee_provider/tasks.rb, line 84
def address
  puts wallet.receive_address
end
manage_utxo_pool() click to toggle source

Create UTXOs for paying fee from TPC amount of the wallet FeeProvider has. Then show the status.

About the UTXO Pool FeeProvider have the UTXO pool. the pool is manged to keep some number of UTXOs that have fixed fee value. The value is configurable by :fixed_fee. This method do the management to the pool.

# File lib/glueby/fee_provider/tasks.rb, line 24
def manage_utxo_pool
  txb = Tapyrus::TxBuilder.new

  sum, utxos = collect_outputs
  return if utxos.empty?

  utxos.each { |utxo| txb.add_utxo(utxo) }
  address = wallet.receive_address

  shortage = [fee_provider.utxo_pool_size - current_utxo_pool_size, 0].max
  can_create = (sum - fee_provider.fixed_fee) / fee_provider.fixed_fee
  fee_outputs_count_to_be_created = [shortage, can_create].min

  return if fee_outputs_count_to_be_created == 0

  fee_outputs_count_to_be_created.times do
    txb.pay(address, fee_provider.fixed_fee)
  end

  tx = txb.change_address(address)
          .fee(fee_provider.fixed_fee)
          .build
  tx = wallet.sign_tx(tx)
  wallet.broadcast(tx, without_fee_provider: true)
ensure
  status
end
status() click to toggle source

Show the status of the UTXO pool

# File lib/glueby/fee_provider/tasks.rb, line 53
      def status
        status = :ready

        if current_utxo_pool_size < fee_provider.utxo_pool_size
          if tpc_amount < value_to_fill_utxo_pool
            status = :insufficient_amount
            message = <<~MESSAGE
            1. Please replenishment TPC which is for paying fee to FeeProvider. 
               FeeProvider needs #{value_to_fill_utxo_pool} tapyrus at least for paying 20 transaction fees. 
               FeeProvider wallet's address is '#{wallet.receive_address}'
            2. Then create UTXOs for paying in UTXO pool with 'rake glueby:fee_provider:manage_utxo_pool'
            MESSAGE
          else
            message = "Please create UTXOs for paying in UTXO pool with 'rake glueby:fee_provider:manage_utxo_pool'\n"
          end
        end

        status = :not_ready if current_utxo_pool_size == 0

        puts <<~EOS
        Status: #{STATUS[status]}
        TPC amount: #{delimit(tpc_amount)}
        UTXO pool size: #{delimit(current_utxo_pool_size)}
        #{"\n" if message}#{message}
        Configuration:
          fixed_fee = #{delimit(fee_provider.fixed_fee)}
          utxo_pool_size = #{delimit(fee_provider.utxo_pool_size)}
        EOS
      end

Private Instance Methods

check_wallet_amount!() click to toggle source
# File lib/glueby/fee_provider/tasks.rb, line 90
      def check_wallet_amount!
        if tpc_amount < fee_provider.fixed_fee
          raise InsufficientTPC, <<~MESSAGE
            FeeProvider has insufficient TPC to create fee outputs to fill the UTXO pool.
            1. Please replenishment TPC which is for paying fee to FeeProvider. FeeProvider needs #{fee_provider.utxo_pool_size * fee_provider.fixed_fee} tapyrus at least. FeeProvider wallet's address is '#{wallet.receive_address}'
            2. Then create UTXOs for paying in UTXO pool with 'rake glueby:fee_provider:manage_utxo_pool'
          MESSAGE
        end
      end
collect_outputs() click to toggle source
# File lib/glueby/fee_provider/tasks.rb, line 104
def collect_outputs
  wallet.list_unspent.inject([0, []]) do |sum, output|
    next sum if output[:color_id] || output[:amount] == fee_provider.fixed_fee

    new_sum = sum[0] + output[:amount]
    new_outputs = sum[1] << {
      txid: output[:txid],
      script_pubkey: output[:script_pubkey],
      value: output[:amount],
      index: output[:vout] ,
      finalized: output[:finalized]
    }
    return [new_sum, new_outputs] if new_sum >= value_to_fill_utxo_pool

    [new_sum, new_outputs]
  end
end
current_utxo_pool_size() click to toggle source
# File lib/glueby/fee_provider/tasks.rb, line 122
def current_utxo_pool_size
  wallet
    .list_unspent(false)
    .count { |o| !o[:color_id] && o[:amount] == fee_provider.fixed_fee }
end
delimit(num) click to toggle source
# File lib/glueby/fee_provider/tasks.rb, line 136
def delimit(num)
  num.to_s.reverse.scan(/.{1,3}/).join('_').reverse
end
tpc_amount() click to toggle source
# File lib/glueby/fee_provider/tasks.rb, line 100
def tpc_amount
  wallet.balance(false)
end
value_to_fill_utxo_pool() click to toggle source
# File lib/glueby/fee_provider/tasks.rb, line 128
def value_to_fill_utxo_pool
  fee_provider.fixed_fee * (fee_provider.utxo_pool_size + 1) #  +1 is for paying fee
end
wallet() click to toggle source
# File lib/glueby/fee_provider/tasks.rb, line 132
def wallet
  fee_provider.wallet
end