Class: Glueby::FeeProvider::Tasks

Inherits:
Object
  • Object
show all
Defined in:
lib/glueby/fee_provider/tasks.rb

Constant Summary collapse

STATUS =
{
  # FeeProvider is ready to pay fees.
  ready: 'Ready',
  # FeeProvider is ready to pay fees, but it doesn't have enough amount to fill the UTXO pool by UTXOs which is for paying fees.
  insufficient_amount: 'Insufficient Amount',
  # FeeProvider is not ready to pay fees. It has no UTXOs for paying fee and amounts.
  not_ready: 'Not Ready'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTasks

Returns a new instance of Tasks.



15
16
17
# File 'lib/glueby/fee_provider/tasks.rb', line 15

def initialize
  @fee_provider = Glueby::FeeProvider.new
end

Instance Attribute Details

#fee_providerObject (readonly)

Returns the value of attribute fee_provider.



4
5
6
# File 'lib/glueby/fee_provider/tasks.rb', line 4

def fee_provider
  @fee_provider
end

Instance Method Details

#manage_utxo_poolObject

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.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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)
    sum -= fee_provider.fixed_fee
  end
  fee = fee_provider.fixed_fee
  fee += (sum - fee) if sum - fee < DUST_LIMIT

  tx = txb.change_address(address)
          .fee(fee)
          .build
  tx = wallet.sign_tx(tx)
  wallet.broadcast(tx, without_fee_provider: true)
ensure
  status
end

Show the address of Fee Provider



87
88
89
# File 'lib/glueby/fee_provider/tasks.rb', line 87

def print_address
  puts wallet.receive_address
end

#statusObject

Show the status of the UTXO pool



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/glueby/fee_provider/tasks.rb', line 56

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