Class: Zold::Tax

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

Overview

A single tax payment

Constant Summary collapse

EXACT_SCORE =

The exact score a wallet can buy in order to pay taxes.

8
MAX_PAYMENT =

The maximum allowed amount in one transaction.

Amount.new(zld: 1.0)
FEE_TXN_HOUR =

This is how much we charge per one transaction per hour of storage. A wallet of 4096 transactions will pay approximately 16ZLD per year.

Amount.new(zld: 16.0 / (365 * 24) / 4096)
TRIAL =

The maximum debt we can tolerate at the wallet. If the debt is bigger than this threshold, nodes must stop accepting PUSH.

Amount.new(zld: 1.0)
DAYS_INCREMENT =

For how many days to pay at once.

64
PREFIX =

Text prefix for taxes details

'TAXES'

Instance Method Summary collapse

Constructor Details

#initialize(wallet) ⇒ Tax

Returns a new instance of Tax.



57
58
59
60
# File 'lib/zold/tax.rb', line 57

def initialize(wallet)
  raise "The wallet must be of type Wallet: #{wallet.class.name}" unless wallet.is_a?(Wallet)
  @wallet = wallet
end

Instance Method Details

#debtObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zold/tax.rb', line 79

def debt
  txns = @wallet.txns
  scored = txns.map do |t|
    pfx, body = t.details.split(' ', 2)
    next if pfx != Tax::PREFIX || body.nil?
    score = Score.parse_text(body)
    next if !score.valid? || score.value != Tax::EXACT_SCORE
    next if score.strength < Score::STRENGTH
    next if t.amount > Tax::MAX_PAYMENT
    t
  end.reject(&:nil?).uniq(&:details)
  paid = scored.empty? ? Amount::ZERO : scored.map(&:amount).inject(&:+)
  owned = Tax::FEE_TXN_HOUR * txns.count * @wallet.age
  owned - paid
end

#details(best) ⇒ Object



67
68
69
# File 'lib/zold/tax.rb', line 67

def details(best)
  "#{Tax::PREFIX} #{best.reduced(Tax::EXACT_SCORE).to_text}"
end

#exists?(details) ⇒ Boolean

Check whether this tax payment already exists in the wallet.

Returns:

  • (Boolean)


63
64
65
# File 'lib/zold/tax.rb', line 63

def exists?(details)
  !@wallet.txns.find { |t| t.details.start_with?("#{Tax::PREFIX} ") && t.details == details }.nil?
end

#in_debt?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/zold/tax.rb', line 75

def in_debt?
  debt > Tax::TRIAL
end

#pay(pvt, best) ⇒ Object



71
72
73
# File 'lib/zold/tax.rb', line 71

def pay(pvt, best)
  @wallet.sub(Tax::MAX_PAYMENT, best.invoice, pvt, details(best))
end