Class: Steem::Type::Amount

Inherits:
BaseType show all
Defined in:
lib/steem/type/amount.rb

Overview

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Amount

Returns a new instance of Amount.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/steem/type/amount.rb', line 14

def initialize(value)
  super(:amount, value)
  
  case value
  when Array
    a, p, t = value
    @asset = case t
    when '@@000000013' then 'SBD'
    when '@@000000021' then 'STEEM'
    when '@@000000037' then 'VESTS'
    else; raise TypeError, "Asset #{@asset} unknown."
    end
    @precision = p
    @amount = "%.#{p}f" % (a.to_f / 10 ** p)
  else
    @amount, @asset = value.strip.split(' ') rescue ['', '']
    @precision = case @asset
    when 'STEEM' then 3
    when 'VESTS' then 6
    when 'SBD' then 3
    else; raise TypeError, "Asset #{@asset} unknown."
    end
  end
end

Class Method Details

.to_nia(amount) ⇒ Object



6
7
8
# File 'lib/steem/type/amount.rb', line 6

def self.to_nia(amount)
  new(amount).to_nia
end

.to_s(amount) ⇒ Object



10
11
12
# File 'lib/steem/type/amount.rb', line 10

def self.to_s(amount)
  new(amount).to_s
end

Instance Method Details

#to_bytesObject



39
40
41
42
43
44
45
46
# File 'lib/steem/type/amount.rb', line 39

def to_bytes
  asset = @asset.ljust(7, "\x00")
  amount = (@amount.to_f * 10 ** @precision).round
  
  [amount].pack('q') +
  [@precision].pack('c') +
  asset
end

#to_niaObject



48
49
50
51
52
53
54
# File 'lib/steem/type/amount.rb', line 48

def to_nia
  case @asset
  when 'STEEM' then [(@amount.to_f * 1000).to_i.to_s, 3, '@@000000021']
  when 'VESTS' then [(@amount.to_f * 1000000).to_i.to_s, 6, '@@000000037']
  when 'SBD' then [(@amount.to_f * 1000).to_i.to_s, 3, '@@000000013']
  end
end

#to_sObject



56
57
58
# File 'lib/steem/type/amount.rb', line 56

def to_s
  "#{@amount} #{@asset}"
end