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.

16
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.

32
PREFIX =

Text prefix for taxes details

'TAXES'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(wallet) ⇒ Tax

Returns a new instance of Tax.



55
56
57
# File 'lib/zold/tax.rb', line 55

def initialize(wallet)
  @wallet = wallet
end

Instance Method Details

#debtObject



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

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

#details(best) ⇒ Object



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

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

#exists?(txn) ⇒ Boolean

Check whether this tax payment already exists in the wallet.

Returns:

  • (Boolean)


60
61
62
# File 'lib/zold/tax.rb', line 60

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

#in_debt?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/zold/tax.rb', line 73

def in_debt?
  debt > Tax::TRIAL
end

#pay(pvt, best) ⇒ Object



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

def pay(pvt, best)
  fee = Tax::FEE_TXN_HOUR * @wallet.txns.count * Tax::DAYS_INCREMENT * 24
  @wallet.sub(fee, best.invoice, pvt, details(best))
end