Class: EthereumContractABI::ContractInterface::AbiTypes::Fixed

Inherits:
BaseType
  • Object
show all
Defined in:
lib/ethereum-contract-abi/contract/abi_types/fixed.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseType

#bytesize, #decode_value, #is_type_equal

Constructor Details

#initialize(bits = 128, precision = 18) ⇒ Fixed

Returns a new instance of Fixed.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
# 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

Class Method Details

.from_string(string_type) ⇒ Object



37
38
39
40
41
42
# 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

Instance Method Details

#encode_value(number) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
# 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_dynamicObject



18
19
20
# File 'lib/ethereum-contract-abi/contract/abi_types/fixed.rb', line 18

def is_dynamic
  false
end

#to_sObject



22
23
24
# File 'lib/ethereum-contract-abi/contract/abi_types/fixed.rb', line 22

def to_s
  "fixed#{@bits}x#{@precision}"
end

#valid_value?(number) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# 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