class EthereumContractABI::ContractInterface::AbiTypes::Fixed

Public Class Methods

from_string(string_type) click to toggle source
# File lib/ethereum-contract-abi/contract/abi_types/fixed.rb, line 37
def self.from_string(string_type)
  /(?<type>fixed)((?<bits>\d+)x(?<precision>\d+))?/ =~ string_type
  return nil unless type

  bits && precision ? self.new(bits.to_i, precision.to_i) : self.new
end
new(bits = 128, precision = 18) click to toggle source
# File lib/ethereum-contract-abi/contract/abi_types/fixed.rb, line 10
def initialize(bits = 128, precision = 18)
  raise ArgumentError.new("8 must be a factor of bits") unless bits % 8 === 0
  raise ArgumentError.new("bits must be: 8 <= bits <= 256") unless 8 <= bits && bits <= 256
  raise ArgumentError.new("precision must be: 0 < precision <= 80") unless 0 < precision && precision <= 80
  @bits = bits
  @precision = precision
end

Public Instance Methods

encode_value(number) click to toggle source
# File lib/ethereum-contract-abi/contract/abi_types/fixed.rb, line 32
def encode_value(number)
  raise ArgumentError unless valid_value?(number)
  DecimalEncoder.encode_value(number, @precision)
end
is_dynamic() click to toggle source
# File lib/ethereum-contract-abi/contract/abi_types/fixed.rb, line 18
def is_dynamic
  false
end
to_s() click to toggle source
# File lib/ethereum-contract-abi/contract/abi_types/fixed.rb, line 22
def to_s
  "fixed#{@bits}x#{@precision}"
end
valid_value?(number) click to toggle source
# File lib/ethereum-contract-abi/contract/abi_types/fixed.rb, line 26
def valid_value?(number)
  return false unless number.is_a? Numeric
  return false unless number.round(0).bit_length <= @bits
  true
end