Class: XRBP::NodeStore::STAmount

Inherits:
Object
  • Object
show all
Defined in:
lib/xrbp/nodestore/sle/st_amount.rb

Overview

Serialized Amount Representation

Constant Summary collapse

MIN_VAL =
1000000000000000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ STAmount

Returns a new instance of STAmount.



29
30
31
32
33
34
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 29

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

Instance Attribute Details

#exponentObject (readonly) Also known as: offset

Returns the value of attribute exponent.



8
9
10
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 8

def exponent
  @exponent
end

#issueObject

Returns the value of attribute issue.



9
10
11
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 9

def issue
  @issue
end

#mantissaObject (readonly) Also known as: value

Returns the value of attribute mantissa.



8
9
10
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 8

def mantissa
  @mantissa
end

#negObject (readonly)

Returns the value of attribute neg.



8
9
10
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 8

def neg
  @neg
end

Class Method Details

.from_quality(rate) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 18

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

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

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

.zero(args = {}) ⇒ Object



14
15
16
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 14

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

Instance Method Details

#*(o) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 146

def *(o)
  return STAmount.new :issue => issue if zero? || o.zero?

  if native?
    min = sn_value < o.sn_value ?   sn_value : o.sn_value
    max = sn_value < o.sn_value ? o.sn_value :   sn_value

    return STAmount.new :mantissa => min * max
  end

  m1 =   mantissa
  m2 = o.mantissa
  e1 =   exponent
  e2 = o.exponent

  if native?
    while nm < MIN_VAL
      m1 *= 10
      e1 -= 1
    end
  end

  if o.native?
    while dm < MIN_VAL
      m2 *= 10
      e2 -= 1
    end
  end

  # see note: https://github.com/ripple/rippled/blob/b53fda1e1a7f4d09b766724274329df1c29988ab/src/ripple/protocol/impl/STAmount.cpp#L1131
  STAmount.new :issue => issue,
               :mantissa => (m1 * m2)/(10**14),
               :exponent => (e1 + e2 + 14),
               :neg      => (neg != o.neg)
end

#+(v) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 76

def +(v)
  e1 = exponent
  e2 = v.exponent

  m1 = mantissa
  m2 = v.mantissa

  m1 *= -1 if   neg
  m2 *= -1 if v.neg

  while e1 < e2
    m1 /= 10
    e1 += 1
  end

  while e2 < e1
    m2 /= 10
    e2 += 1
  end

  m = m1 + m2
  return STAmount.new :issue => issue if m >= -10 && m <= 10
  return STAmount.new :mantissa => m,
                      :exponent => e1,
                      :issue    => issue if m >= 0
  return STAmount.new :mantissa => -m,
                      :exponent => e1,
                      :issue    => issue
end

#-(v) ⇒ Object



106
107
108
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 106

def -(v)
  self + (-v)
end

#-@Object



182
183
184
185
186
187
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 182

def -@
  STAmount.new(:mantissa => mantissa,
               :exponent => exponent,
               :issue    => issue,
               :neg      => !neg)
end

#/(v) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 110

def /(v)
  if v.is_a?(Rate)
    return self if v == Rate.parity
    return self / v.to_amount
  end

  raise "divide by zero" if v.zero?
  return STAmount.new :issue => issue

  nm = mantissa
  dm = v.mantissa

  ne = exponent
  de = v.exponent

  if native?
    while nm < MIN_VAL
      nm *= 10
      ne -= 1
    end
  end

  if v.native?
    while dm < MIN_VAL
      dm *= 10
      de -= 1
    end
  end

  # see note: https://github.com/ripple/rippled/blob/b53fda1e1a7f4d09b766724274329df1c29988ab/src/ripple/protocol/impl/STAmount.cpp#L1075
  STAmount.new :issue => issue,
               :mantissa => (nm * 10**17)/dm,
               :exponent => (ne - de - 17),
               :neg      => (neg != v.neg)
end

#<(o) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 189

def <(o)
  return neg if neg && !o.neg
  if mantissa == 0
    return false if o.neg
    return o.mantissa != 0
  end

  return false if o.mantissa == 0
  return  neg  if exponent > o.exponent
  return !neg  if exponent < o.exponent
  return  neg  if mantissa > o.mantissa
  return !neg  if mantissa < o.mantissa

  return false
end

#<=>(o) ⇒ Object



219
220
221
222
223
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 219

def <=>(o)
  return  0 if self == o
  return -1 if self  < o
  return  1 if self  > o
end

#==(o) ⇒ Object



213
214
215
216
217
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 213

def ==(o)
       neg == o.neg      &&
  mantissa == o.mantissa &&
  exponent == o.exponent
end

#>(o) ⇒ Object



209
210
211
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 209

def >(o)
  self >= o && self != o
end

#>=(o) ⇒ Object



205
206
207
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 205

def >=(o)
  !(self < o)
end

#clearObject



44
45
46
47
48
49
50
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 44

def clear
  # see: https://github.com/ripple/rippled/blob/b53fda1e1a7f4d09b766724274329df1c29988ab/src/ripple/protocol/STAmount.h#L224
  @exponent = native? ? 0 : -100

  @neg = false
  @mantissa = 0
end

#iou_amountObject



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

def iou_amount
  (neg ? -1 : 1) * mantissa * 10 ** (exponent-97)
end

#native?Boolean

Returns:

  • (Boolean)


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

def native?
  @issue.xrp?
end

#negate!Object



52
53
54
55
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 52

def negate!
  return if zero?
  @neg = !@neg
end

#sn_valueObject



57
58
59
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 57

def sn_value
  neg ? (-mantissa) : mantissa
end

#xrp_amountObject Also known as: drops

In drops!



64
65
66
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 64

def xrp_amount
  neg ? (-value) : value
end

#zero?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/xrbp/nodestore/sle/st_amount.rb', line 40

def zero?
  @mantissa == 0
end