Class: Zold::Routines::Bonuses

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/commands/routines/bonuses.rb

Overview

Pay bonuses to random nodes

Instance Method Summary collapse

Constructor Details

#initialize(opts, wallets, remotes, copies, farm, log: Log::Quiet.new) ⇒ Bonuses

Returns a new instance of Bonuses.



35
36
37
38
39
40
41
42
# File 'lib/zold/commands/routines/bonuses.rb', line 35

def initialize(opts, wallets, remotes, copies, farm, log: Log::Quiet.new)
  @opts = opts
  @wallets = wallets
  @remotes = remotes
  @copies = copies
  @farm = farm
  @log = log
end

Instance Method Details

#exec(_ = 0) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/zold/commands/routines/bonuses.rb', line 44

def exec(_ = 0)
  sleep(@opts['bonus-time'] * 60) unless @opts['routine-immediately']
  raise '--private-key is required to pay bonuses' unless @opts['private-key']
  raise '--bonus-wallet is required to pay bonuses' unless @opts['bonus-wallet']
  raise '--bonus-amount is required to pay bonuses' unless @opts['bonus-amount']
  winners = Remote.new(remotes: @remotes, log: @log, farm: @farm).run(
    ['remote', 'elect', @opts['bonus-wallet'], '--private-key', @opts['private-key']] +
    (@opts['ignore-score-weakness'] ? ['--ignore-score-weakness'] : [])
  )
  return if winners.empty?
  unless @wallets.find(Id.new(@opts['bonus-wallet'])).exists?
    Pull.new(wallets: @wallets, remotes: @remotes, copies: @copies, log: @log).run(
      ['pull', @opts['bonus-wallet']] +
      (@opts['ignore-score-weakness'] ? ['--ignore-score-weakness'] : [])
    )
  end
  winners.each do |score|
    Pay.new(wallets: @wallets, remotes: @remotes, log: @log).run(
      [
        'pay', @opts['bonus-wallet'], score.invoice, @opts['bonus-amount'].to_s,
        "Hosting bonus for #{score.host} #{score.port} #{score.value}",
        '--private-key', @opts['private-key']
      ]
    )
  end
  Push.new(wallets: @wallets, remotes: @remotes, log: @log).run(
    ['push', @opts['bonus-wallet']] +
    (@opts['ignore-score-weakness'] ? ['--ignore-score-weakness'] : [])
  )
end