class DPay::Type::Amount

See: github.com/xeroc/piston-lib/blob/34a7525cee119ec9b24a99577ede2d54466fca0e/dpaybase/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/dpay/type/amount.rb, line 16
def initialize(value)
  super(:amount, value)

  case value
  when Array
    @amount, @precision, @nai = value
    @asset = case @nai
    when '@@000000013' then 'BBD'
    when '@@000000021' then 'BEX'
    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 'BBD'
    when '@@000000021' then 'BEX'
    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 'BEX' then 3
    when 'VESTS' then 6
    when 'BBD' then 3
    else; raise TypeError, "Asset #{@asset} unknown."
    end
  end
end
to_h(amount) click to toggle source
# File lib/dpay/type/amount.rb, line 8
def self.to_h(amount)
  new(amount).to_h
end
to_s(amount) click to toggle source
# File lib/dpay/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/dpay/type/amount.rb, line 68
def to_a
  case @asset
  when 'BEX' 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 'BBD' then [(@amount.to_f * 1000).to_i.to_s, 3, '@@000000013']
  end
end
to_bytes() click to toggle source
# File lib/dpay/type/amount.rb, line 59
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/dpay/type/amount.rb, line 76
def to_h
  case @asset
  when 'BEX' 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 'BBD' then {
    amount: (@amount.to_f * 1000).to_i.to_s,
    precision: 3,
    nai: '@@000000013'
  }
  end
end
to_s() click to toggle source
# File lib/dpay/type/amount.rb, line 96
def to_s
  "#{@amount} #{@asset}"
end