class Crea::Type::Amount

See: github.com/xeroc/piston-lib/blob/34a7525cee119ec9b24a99577ede2d54466fca0e/creabase/operations.py

Attributes

amount[R]
asset[R]
nai[R]
precision[R]

Public Class Methods

new(value) click to toggle source
Calls superclass method
# File lib/crea/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 'CBD'
    when '@@000000021' then 'CREA'
    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 'CBD'
    when '@@000000021' then 'CREA'
    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 'CREA' then 3
    when 'VESTS' then 6
    when 'CBD' then 3
    when 'TESTS' then 3
    when 'TBD' then 3
    else; raise TypeError, "Asset #{@asset} unknown."
    end
  end
end
to_bytes(amount) click to toggle source
# File lib/crea/type/amount.rb, line 16
def self.to_bytes(amount)
  new(amount).to_bytes
end
to_h(amount) click to toggle source
# File lib/crea/type/amount.rb, line 8
def self.to_h(amount)
  new(amount).to_h
end
to_s(amount) click to toggle source
# File lib/crea/type/amount.rb, line 12
def self.to_s(amount)
  new(amount).to_s
end

Public Instance Methods

to_a() click to toggle source
# File lib/crea/type/amount.rb, line 74
def to_a
  case @asset
  when 'CREA' 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 'CBD' then [(@amount.to_f * 1000).to_i.to_s, 3, '@@000000013']
  end
end
to_bytes() click to toggle source
# File lib/crea/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_h() click to toggle source
# File lib/crea/type/amount.rb, line 82
def to_h
  case @asset
  when 'CREA' 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 'CBD' then {
    amount: (@amount.to_f * 1000).to_i.to_s,
    precision: 3,
    nai: '@@000000013'
  }
  end
end
to_s() click to toggle source
# File lib/crea/type/amount.rb, line 102
def to_s
  "#{@amount} #{@asset}"
end