Class: Beowulf::Type::Amount

Inherits:
Serializer show all
Defined in:
lib/beowulf/type/amount.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#debug, #error, #extract_signatures, #hexlify, #pakArr, #pakC, #pakHash, #pakI, #pakL!, #pakPubKey, #pakQ, #pakS, #pakStr, #pakc, #pakq, #paks, #send_log, #unhexlify, #varint, #warning

Constructor Details

#initialize(value) ⇒ Amount

Returns a new instance of Amount.



6
7
8
9
10
# File 'lib/beowulf/type/amount.rb', line 6

def initialize(value)
  super(:amount, value)
  @amount, @asset = value.strip.split(' ')
  @precision = 5
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



4
5
6
# File 'lib/beowulf/type/amount.rb', line 4

def amount
  @amount
end

#assetObject (readonly)

Returns the value of attribute asset.



4
5
6
# File 'lib/beowulf/type/amount.rb', line 4

def asset
  @asset
end

#precisionObject (readonly)

Returns the value of attribute precision.



4
5
6
# File 'lib/beowulf/type/amount.rb', line 4

def precision
  @precision
end

Instance Method Details

#to_bytesObject



12
13
14
15
16
# File 'lib/beowulf/type/amount.rb', line 12

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

#to_sObject



18
19
20
# File 'lib/beowulf/type/amount.rb', line 18

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