Class: Trx::Abi::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/trx/abi/function.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Function

Returns a new instance of Function.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/trx/abi/function.rb', line 7

def initialize(data)
  @name = data["name"]
  @constant = data["constant"]
  @inputs = data.fetch("inputs", []).map do |input|
    FunctionInput.new(input)
  end
  @outputs = data.fetch("outputs", []).map do |output|
    FunctionOutput.new(output)
  end
  @function_string = self.class.calc_signature(@name, @inputs)
  @signature = self.class.calc_id(@function_string)
end

Instance Attribute Details

#constantObject

Returns the value of attribute constant.



5
6
7
# File 'lib/trx/abi/function.rb', line 5

def constant
  @constant
end

#function_stringObject

Returns the value of attribute function_string.



5
6
7
# File 'lib/trx/abi/function.rb', line 5

def function_string
  @function_string
end

#inputsObject

Returns the value of attribute inputs.



5
6
7
# File 'lib/trx/abi/function.rb', line 5

def inputs
  @inputs
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/trx/abi/function.rb', line 5

def name
  @name
end

#outputsObject

Returns the value of attribute outputs.



5
6
7
# File 'lib/trx/abi/function.rb', line 5

def outputs
  @outputs
end

#signatureObject

Returns the value of attribute signature.



5
6
7
# File 'lib/trx/abi/function.rb', line 5

def signature
  @signature
end

Class Method Details

.calc_id(signature) ⇒ Object



32
33
34
# File 'lib/trx/abi/function.rb', line 32

def self.calc_id(signature)
  Digest::Keccak.hexdigest(signature, 256)[0..7]
end

.calc_signature(name, inputs) ⇒ Object



28
29
30
# File 'lib/trx/abi/function.rb', line 28

def self.calc_signature(name, inputs)
  "#{name}(#{inputs.map {|x| self.to_canonical_type(x.type) }.join(",")})"
end

.to_canonical_type(type) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/trx/abi/function.rb', line 20

def self.to_canonical_type(type)
  type
  .gsub(/(int)(\z|\D)/, '\1256\2')
  .gsub(/(uint)(\z|\D)/, '\1256\2')
  .gsub(/(fixed)(\z|\D)/, '\1128x128\2')
  .gsub(/(ufixed)(\z|\D)/, '\1128x128\2')
end