Sha256: a19e779daeeeccc3613bdf1c4d98894a21a09c0c68550b9d8b7705ff93db5042

Contents?: true

Size: 880 Bytes

Versions: 6

Compression:

Stored size: 880 Bytes

Contents

module SolanaRuby
  module DataTypes
    class NearInt64
      attr_reader :size

      V2E32 = 2.pow(32)

      def initialize
        @size = 8
      end

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

      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

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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
solana-ruby-web3js-2.1.0 lib/solana_ruby/data_types/near_int64.rb
solana-ruby-web3js-2.0.2 lib/solana_ruby/data_types/near_int64.rb
solana-ruby-web3js-2.0.1 lib/solana_ruby/data_types/near_int64.rb
solana-ruby-web3js-2.0.0beta2 lib/solana_ruby/data_types/near_int64.rb
solana-ruby-web3js-2.0.0beta1 lib/solana_ruby/data_types/near_int64.rb
solana-ruby-web3js-2.0.0 lib/solana_ruby/data_types/near_int64.rb