Module: XRBP::NodeStore::STAmount::Conversion::ClassMethods

Defined in:
lib/xrbp/nodestore/sle/st_amount_conversion.rb

Instance Method Summary collapse

Instance Method Details

#from_wire(data) ⇒ Object

See Also:

  • XRBP::NodeStore::STAmount::Conversion::ClassMethods.{NodeStore{NodeStore::Parser{NodeStore::Parser::parse_amount}


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/xrbp/nodestore/sle/st_amount_conversion.rb', line 7

def from_wire(data)
  native = (data &  STAmount::NOT_NATIVE) == 0
     neg = (data & ~STAmount::NOT_NATIVE &  STAmount::POS_NATIVE) == 0
   value = (data & ~STAmount::NOT_NATIVE & ~STAmount::POS_NATIVE)

  if native
    STAmount.new :issue => NodeStore.xrp_issue,
                 :neg   => neg,
                 :mantissa => value
  else
     exp = (value >> 54) - 97
    mant = value & 0x3fffffffffffff
    STAmount.new :neg => neg,
            :exponent => exp,
            :mantissa => mant
  end
end

#parse(str, issue = nil) ⇒ Object

Convert string to STAmount

See Also:

  • (in rippled)


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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/xrbp/nodestore/sle/st_amount_conversion.rb', line 28

def parse(str, issue=nil)
  match = "^"+                      # the beginning of the string
          "([-+]?)"+                # (optional) + or - character
          "(0|[1-9][0-9]*)"+        # a number (no leading zeroes, unless 0)
          "(\\.([0-9]+))?"+         # (optional) period followed by any number
          "([eE]([+-]?)([0-9]+))?"+ # (optional) E, optional + or -, any number
          "$"
  match = Regexp.new(match)
  match = str.match(match)
  raise "Number '#{str}' is not valid" unless match

  # Match fields:
  #
  #   0 = whole input
  #   1 = sign
  #   2 = integer portion
  #   3 = whole fraction (with '.')
  #   4 = fraction (without '.')
  #   5 = whole exponent (with 'e')
  #   6 = exponent sign
  #   7 = exponent number

  raise "Number '#{str}' is overlong" if ((match[2] || "").length +
                                          (match[4] || "").length) > 32

  neg = !!match[1] && match[1] == '-'

  raise "XRP must be specified in integral drops" if issue && issue.xrp? && !!match[3]

  mantissa = 0
  exponent = 0

  if !match[4]
    # integral only
    mantissa = match[2].to_i

  else
    # integer and fraction
    mantissa = (match[2] + match[4]).to_i
    exponent = -(match[4].length)
  end

  if !!match[5]
    # exponent
    if match[6] && match[6] == '-'
      exponent -= match[7].to_i
    else
      exponent += match[7].to_i
    end
  end

  return STAmount.new :issue => issue,
                   :mantissa => mantissa,
                   :exponent => exponent,
                        :neg => neg
end