Class: Zold::Pay

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

Overview

Money sending command

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Pay.



33
34
35
36
37
# File 'lib/zold/commands/pay.rb', line 33

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

Instance Method Details

#pay(from, invoice, amount, details, opts) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/zold/commands/pay.rb', line 81

def pay(from, invoice, amount, details, opts)
  unless opts.force?
    raise 'The amount can\'t be zero' if amount.zero?
    raise "The amount can't be negative: #{amount}" if amount.negative?
    if !from.root? && from.balance < @amount
      raise "There is not enough funds in #{from} to send #{amount}, only #{payer.balance} left"
    end
  end
  key = Zold::Key.new(file: opts['private-key'])
  txn = from.sub(amount, invoice, key, details)
  @log.debug("#{amount} sent from #{from} to #{txn.bnf}: #{details}")
  @log.info(txn.id)
  txn
end

#run(args = []) ⇒ Object



39
40
41
42
43
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
74
75
76
77
78
79
# File 'lib/zold/commands/pay.rb', line 39

def run(args = [])
  opts = Slop.parse(args, help: true, suppress_errors: true) do |o|
    o.banner = "Usage: zold pay wallet target amount [details] [options]
Where:
'wallet' is the sender's wallet ID
'target' is the beneficiary (either wallet ID or invoice number)'
'amount' is the amount to pay, in ZLD, for example '14.95'
'details' is the optional text to attach to the payment
Available options:"
    o.string '--private-key',
      'The location of RSA private key (default: ~/.ssh/id_rsa)',
      require: true,
      default: '~/.ssh/id_rsa'
    o.bool '--force',
      'Ignore all validations',
      default: false
    o.bool '--dont-pay-taxes',
      'Don\'t pay taxes even if the wallet is in debt',
      default: false
    o.bool '--help', 'Print instructions'
  end
  mine = Args.new(opts, @log).take || return
  raise 'Payer wallet ID is required as the first argument' if mine[0].nil?
  from = @wallets.find(Id.new(mine[0]))
  raise 'Wallet doesn\'t exist, do \'fetch\' first' unless from.exists?
  raise 'Recepient\'s invoice or wallet ID is required as the second argument' if mine[1].nil?
  invoice = mine[1]
  unless invoice.include?('@')
    require_relative 'invoice'
    invoice = Invoice.new(wallets: @wallets, log: @log).run(['invoice', invoice])
  end
  raise 'Amount is required (in ZLD) as the third argument' if mine[2].nil?
  amount = Amount.new(zld: mine[2].to_f)
  details = mine[3] ? mine[3] : '-'
  if Tax.new(from).in_debt? && !opts['dont-pay-taxes']
    Taxes.new(wallets: @wallets, remotes: @remotes, log: @log).run(
      ['taxes', "--private-key=#{opts['private-key']}", id.to_s]
    )
  end
  pay(from, invoice, amount, details, opts)
end