Class: Steem::Type::Amount

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

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Amount

Returns a new instance of Amount.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/steem/type/amount.rb', line 20

def initialize(value)
  super(:amount, value)
  
  case value
  when Array
    @amount, @precision, @nai = value
    @asset = case @nai
    when '@@000000013' then 'SBD'
    when '@@000000021' then 'STEEM'
    when '@@000000037' then 'VESTS'
    else; raise TypeError, "Asset #{@nai} unknown."
    end
    
    @amount = "%.#{@precision}f" % (@amount.to_f / 10 ** @precision)
  when Hash
    @amount, @precision, @nai = value.map do |k, v|
      v if %i(amount precision nai).include? k.to_sym
    end.compact
    
    @asset = case @nai
    when '@@000000013' then 'SBD'
    when '@@000000021' then 'STEEM'
    when '@@000000037' then 'VESTS'
    else; raise TypeError, "Asset #{@nai} unknown."
    end
    
    @amount = "%.#{@precision}f" % (@amount.to_f / 10 ** @precision)
  when Amount
    @precision = value.precision
    @nai = value.nai
    @asset = value.asset
    @amount = value.amount
  else
    @amount, @asset = value.strip.split(' ') rescue ['', '']
    @precision = case @asset
    when 'STEEM' then 3
    when 'VESTS' then 6
    when 'SBD' then 3
    when 'TESTS' then 3
    when 'TBD' then 3
    else; raise TypeError, "Asset #{@asset} unknown."
    end
  end
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



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

def amount
  @amount
end

#assetObject (readonly)

Returns the value of attribute asset.



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

def asset
  @asset
end

#naiObject (readonly)

Returns the value of attribute nai.



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

def nai
  @nai
end

#precisionObject (readonly)

Returns the value of attribute precision.



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

def precision
  @precision
end

Class Method Details

.to_bytes(amount) ⇒ Object



16
17
18
# File 'lib/steem/type/amount.rb', line 16

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

.to_h(amount) ⇒ Object



8
9
10
# File 'lib/steem/type/amount.rb', line 8

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

.to_s(amount) ⇒ Object



12
13
14
# File 'lib/steem/type/amount.rb', line 12

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

Instance Method Details

#to_aObject



74
75
76
77
78
79
80
# File 'lib/steem/type/amount.rb', line 74

def to_a
  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_bytesObject



65
66
67
68
69
70
71
72
# File 'lib/steem/type/amount.rb', line 65

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

#to_hObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/steem/type/amount.rb', line 82

def to_h
  case @asset
  when 'STEEM' then {
    amount: (@amount.to_f * 1000).to_i.to_s,
    precision: 3,
    nai: '@@000000021'
  }
  when 'VESTS' then {
    amount: (@amount.to_f * 1000000).to_i.to_s,
    precision: 6,
    nai: '@@000000037'
  }
  when 'SBD' then {
    amount: (@amount.to_f * 1000).to_i.to_s,
    precision: 3,
    nai: '@@000000013'
  }
  end
end

#to_sObject



102
103
104
# File 'lib/steem/type/amount.rb', line 102

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