Class: Zold::Taxes

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

Overview

Taxes command

Instance Method Summary collapse

Constructor Details

#initialize(wallets:, remotes:, log: Log::Quiet.new) ⇒ Taxes

Returns a new instance of Taxes.



39
40
41
42
43
# File 'lib/zold/commands/taxes.rb', line 39

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

Instance Method Details

#debt(wallet, _) ⇒ Object



106
107
108
109
110
# File 'lib/zold/commands/taxes.rb', line 106

def debt(wallet, _)
  raise 'The wallet is absent' unless wallet.exists?
  tax = Tax.new(wallet)
  @log.info(tax.debt)
end

#pay(wallet, _) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/zold/commands/taxes.rb', line 85

def pay(wallet, _)
  raise 'The wallet is absent' unless wallet.exists?
  tax = Tax.new(wallet)
  debt = tax.debt
  @log.debug("The current debt is #{debt} (#{debt.to_i} zents)")
  if debt <= Tax::TRIAL
    @log.debug("No need to pay taxes yet, until the debt is less than #{Tax::TRIAL} (#{Tax::TRIAL.to_i} zents)")
    return
  end
  top = top_scores
  while debt > Amount::ZERO
    raise 'No acceptable remote nodes, try later' if top.empty?
    best = top.shift
    txn = tax.pay(Zold::Key.new(file: opts['private-key']), best)
    wallet.add(txn)
    debt -= txn.amount
    @log.info("#{txn.amount} of taxes paid to #{txn.bnf}, #{debt} left to pay")
  end
  @log.info('The wallet is in good standing, all taxes paid')
end

#run(args = []) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
# File 'lib/zold/commands/taxes.rb', line 45

def run(args = [])
  opts = Slop.parse(args, help: true, suppress_errors: true) do |o|
    o.banner = "Usage: zold taxes command [options]
Available commands:
#{Rainbow('taxes pay').green} wallet
  Pay taxes for the given wallet
#{Rainbow('taxes show').green}
  Show taxes status for the given wallet
#{Rainbow('taxes debt').green}
  Show current debt
Available options:"
    o.string '--private-key',
      'The location of RSA private key (default: ~/.ssh/id_rsa)',
      require: true,
      default: '~/.ssh/id_rsa'
    o.bool '--help', 'Print instructions'
  end
  mine = Args.new(opts, @log).take || return
  command = mine[0]
  case command
  when 'show'
    raise 'At least one wallet ID is required' unless mine[1]
    mine[1..-1].each do |id|
      show(@wallets.find(Id.new(id), opts))
    end
  when 'debt'
    raise 'At least one wallet ID is required' unless mine[1]
    mine[1..-1].each do |id|
      debt(@wallets.find(Id.new(id), opts))
    end
  when 'pay'
    raise 'At least one wallet ID is required' unless mine[1]
    mine[1..-1].each do |id|
      pay(@wallets.find(Id.new(id)), opts)
    end
  else
    @log.info(opts.to_s)
  end
end

#show(_, _) ⇒ Object



112
113
114
# File 'lib/zold/commands/taxes.rb', line 112

def show(_, _)
  raise 'Not implemented yet'
end