Class: SolanaRuby::DataTypes::NearInt64

Inherits:
Object
  • Object
show all
Defined in:
lib/solana_ruby/data_types/near_int64.rb

Constant Summary collapse

V2E32 =
2.pow(32)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNearInt64

Returns a new instance of NearInt64.



8
9
10
# File 'lib/solana_ruby/data_types/near_int64.rb', line 8

def initialize
  @size = 8
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



4
5
6
# File 'lib/solana_ruby/data_types/near_int64.rb', line 4

def size
  @size
end

Instance Method Details

#deserialize(bytes) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/solana_ruby/data_types/near_int64.rb', line 18

def deserialize(bytes)
  raise "Invalid serialization (wrong size)" if @size && bytes.size != @size
  uint = UnsignedInt.new(32)
  half_size = @size/2

  lo, hi = [bytes[0..half_size-1], bytes[half_size..-1]].map{|x| uint.deserialize(x)}

  rounded_int64(hi, lo)
end

#divmod_int64(obj) ⇒ Object



28
29
30
31
32
33
# File 'lib/solana_ruby/data_types/near_int64.rb', line 28

def divmod_int64(obj)
  obj = obj * 1.0
  hi32 = (obj / V2E32).floor
  lo32 = (obj - (hi32 * V2E32)).floor
  [lo32, hi32]
end

#rounded_int64(hi32, lo32) ⇒ Object



35
36
37
# File 'lib/solana_ruby/data_types/near_int64.rb', line 35

def rounded_int64(hi32, lo32)
  hi32 * V2E32 + lo32
end

#serialize(obj) ⇒ Object



12
13
14
15
16
# File 'lib/solana_ruby/data_types/near_int64.rb', line 12

def serialize(obj)
  uint = UnsignedInt.new(32)
  numbers = divmod_int64(obj)
  numbers.map{|x| uint.serialize(x)}.flatten
end