Class: XRBP::NodeStore::STAmount

Inherits:
Object
  • Object
show all
Includes:
Arithmatic, Comparison, Conversion
Defined in:
lib/xrbp/nodestore/sle/st_amount.rb,
lib/xrbp/nodestore/sle/st_amount_arithmatic.rb,
lib/xrbp/nodestore/sle/st_amount_comparison.rb,
lib/xrbp/nodestore/sle/st_amount_conversion.rb

Overview

Serialized Amount Representation.

From rippled docs:

Internal form:
  1: If amount is zero, then value is zero and offset is -100
  2: Otherwise:
     legal offset range is -96 to +80 inclusive
     value range is 10^15 to (10^16 - 1) inclusive
     amount = value * [10 ^ offset]

Wire form:
  High 8 bits are (offset+142), legal range is, 80 to 22 inclusive
  Low 56 bits are value, legal range is 10^15 to (10^16 - 1) inclusive

Defined Under Namespace

Modules: Arithmatic, Comparison, Conversion

Constant Summary collapse

MIN_OFFSET =

DEFINES FROM STAmount.h

-96
MAX_OFFSET =
80
MIN_VAL =
1000000000000000
MAX_VAL =
9999999999999999
NOT_NATIVE =
0x8000000000000000
POS_NATIVE =
0x4000000000000000
MAX_NATIVE =
100000000000000000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Conversion

#clear, #iou_amount, #negate!, #sn_value, #to_h, #to_wire, #xrp_amount

Methods included from Comparison

#<, #<=>, #==, #>, #>=

Methods included from Arithmatic

#*, #+, #-, #-@, #/

Constructor Details

#initialize(args = {}) ⇒ STAmount

Returns a new instance of STAmount.



58
59
60
61
62
63
64
65
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 58

def initialize(args={})
  @issue    = args[:issue]
  @mantissa = args[:mantissa] || 0
  @exponent = args[:exponent] || 0
  @neg      = !!args[:neg]

  canonicalize
end

Instance Attribute Details

#exponentObject (readonly) Also known as: offset

Returns the value of attribute exponent.



36
37
38
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 36

def exponent
  @exponent
end

#issueObject

Returns the value of attribute issue.



37
38
39
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 37

def issue
  @issue
end

#mantissaObject (readonly) Also known as: value

Returns the value of attribute mantissa.



36
37
38
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 36

def mantissa
  @mantissa
end

#negObject (readonly)

Returns the value of attribute neg.



36
37
38
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 36

def neg
  @neg
end

Class Method Details

.from_quality(rate) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 46

def self.from_quality(rate)
  return STAmount.new(:issue => NodeStore.no_issue) if rate == 0

  mantissa = rate & ~(255 << (64 - 8))

  exponent = (rate >> (64 - 8)).to_int32 - 100

  return STAmount.new(:issue    => NodeStore.no_issue,
                      :mantissa => mantissa,
                      :exponent => exponent)
end

.zero(args = {}) ⇒ Object



42
43
44
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 42

def self.zero(args={})
  STAmount.new args.merge({:mantissa => 0})
end

Instance Method Details

#inspectObject



77
78
79
80
81
82
83
84
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 77

def inspect
  return "0" if zero?

  i = issue.inspect
  i = i == '' ? '' : "(#{i})"
  (native? ? xrp_amount : iou_amount.to_f).to_s +
  (native? ?         "" : i)
end

#native?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 69

def native?
  @issue && @issue.xrp?
end

#to_sObject



86
87
88
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 86

def to_s
  inspect
end

#zero?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 73

def zero?
  @mantissa == 0
end