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/must buy in order to pay taxes.

8
MAX_PAYMENT =

The maximum allowed amount in one transaction. The correct amount should be 1 ZLD, but we allow bigger amounts now since the amount of nodes in the network is still small. When the network grows up, let’s put this number back to 1 ZLD.

Amount.new(zld: 16.0)
FEE =

This is how much we charge per one transaction per hour of storage. A wallet of 4096 transactions will pay approximately 16ZLD per year. Here is the formula: 16.0 / (365 * 24) / 4096 = 1915 But I like the 1917 number better.

Amount.new(zents: 1917)
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)

Instance Method Summary collapse

Constructor Details

#initialize(wallet, ignore_score_weakness: false) ⇒ Tax

Returns a new instance of Tax.



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

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

Instance Method Details

#debtObject



87
88
89
# File 'lib/zold/tax.rb', line 87

def debt
  FEE * @wallet.txns.count * @wallet.age - paid
end

#details(best) ⇒ Object



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

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

#exists?(details) ⇒ Boolean

Check whether this tax payment already exists in the wallet.

Returns:

  • (Boolean)


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

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

#in_debt?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/zold/tax.rb', line 79

def in_debt?
  debt > TRIAL
end


91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/zold/tax.rb', line 91

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

#pay(pvt, best) ⇒ Object



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

def pay(pvt, best)
  @wallet.sub([MAX_PAYMENT, debt].min, best.invoice, pvt, details(best))
end

#to_textObject



83
84
85
# File 'lib/zold/tax.rb', line 83

def to_text
  "A=#{@wallet.age.round} hours, F=#{FEE.to_i}z/th, T=#{@wallet.txns.count}t, Paid=#{paid}"
end