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, strength: Score::STRENGTH) ⇒ Tax

Returns a new instance of Tax.



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

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

Instance Method Details

#debtObject



96
97
98
# File 'lib/zold/tax.rb', line 96

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

#details(best) ⇒ Object



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

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

#exists?(details) ⇒ Boolean

Check whether this tax payment already exists in the wallet.

Returns:

  • (Boolean)


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

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

#in_debt?Boolean

Returns:

  • (Boolean)


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

def in_debt?
  debt > TRIAL
end


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/zold/tax.rb', line 100

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

#pay(pvt, best) ⇒ Object



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

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

#to_textObject



92
93
94
# File 'lib/zold/tax.rb', line 92

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