Class: ABI::Function

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, inputs: [], outputs: [], payable: false, constant: false, pure: false) ⇒ Function

:input_types, :output_types



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/abiparser/function.rb', line 60

def initialize( name,
                inputs:  [],
                outputs: [],
                payable: false,
                constant: false,
                pure: false )
   @name    = name
   @inputs  = inputs
   @outputs = outputs
   @payable = payable
   @constant = constant
   @pure = pure

   ##  parse inputs & outputs into types
   ##    note: use "calculated" sig(nature) and NOT the type
   ##        (differs for tuples, that is, types with components !!!)
   ## @input_types  = @inputs.map do |param|
   ##                                Type.parse( param.sig )
   ##                            end
   ## @output_types = @outputs.map do |param|
   ##                                  ## pp param
   ##                                  ## puts "sig: #{param.sig}"
   ##                                 Type.parse( param.sig )
   ##                             end
end

Instance Attribute Details

#inputsObject (readonly)

Returns the value of attribute inputs.



56
57
58
# File 'lib/abiparser/function.rb', line 56

def inputs
  @inputs
end

#nameObject (readonly)

Returns the value of attribute name.



56
57
58
# File 'lib/abiparser/function.rb', line 56

def name
  @name
end

#outputsObject (readonly)

Returns the value of attribute outputs.



56
57
58
# File 'lib/abiparser/function.rb', line 56

def outputs
  @outputs
end

Class Method Details

.parse(o) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/abiparser/function.rb', line 4

def self.parse( o )
  ## todo/fix: assert type  function (or contructor) ??
  name    = o['name']
  inputs  = o['inputs'].map {|param| Param.parse( param ) }
  outputs = o['outputs'].map {|param| Param.parse( param ) }

  payable  = nil
  constant = nil
  pure     = nil

  ## old soliditity before v0.6
  ##  newer version uses stateMutability
  if o.has_key?( 'stateMutability' )
    case o[ 'stateMutability' ]
    when 'nonpayable'    ## default
      payable  = false
      constant = false
    when 'payable'
      payable  = true
      constant = false
    when 'view'
      payable  = false
      constant = true
    when 'pure'
      payable  = false
      constant = true
      pure     = true
    else
       pp o
       ## todo/fix: change to ParseError
       raise ArgumentError, "unexpected stateMutability value; got: #{ o[ 'stateMutability' ]}"
    end
  end

  ## check - assert "strict" abi version keys - why? why not?
  if o.has_key?( 'stateMutability' ) && (o.has_key?( 'payable') || o.has_key?( 'constant'))
     pp o
     puts "!! WARN:  ABI version mismatch? got stateMutability AND payable OR constant"
     exit 1
  end

  payable = o['payable']    if o.has_key?( 'payable')
  constant = o['constant']  if o.has_key?( 'constant')


  new( name, inputs: inputs, outputs: outputs,
       payable: payable,
       constant: constant,
       pure: pure )
end

Instance Method Details

#constant?Boolean Also known as: readonly?

Returns:

  • (Boolean)


87
# File 'lib/abiparser/function.rb', line 87

def constant?() @constant; end

#declObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/abiparser/function.rb', line 133

def decl
  buf = "function #{@name}"
  if @inputs.empty?
    buf << "()"
  else
    buf2 = @inputs.map {|param| param.decl }
    buf << "(#{buf2.join(', ')})"
  end
  buf << " payable "  if @payable
  buf << " view "     if @constant && !@pure
  buf << " pure "     if @constant && @pure

  if @outputs.empty?
     ## do nothing
  else
    buf << " returns "
    buf2 = @outputs.map {|param| param.decl }
    buf << "(#{buf2.join(', ')})"
  end
  buf << ";"
  buf
end

#docObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/abiparser/function.rb', line 114

def doc
    ## note: text with markdown formatting
    buf = "function **#{@name}**"
    if @inputs.empty?
      buf << "()"
    else
      buf2 = @inputs.map {|param| param.doc }
      buf << "(#{buf2.join(', ')})"
    end
    if @outputs.empty?
       ## do nothing
    else
      buf << ""
      buf2 = @outputs.map {|param| param.doc }
      buf << "(#{buf2.join(', ')})"
    end
    buf
end

#payable?Boolean

Returns:

  • (Boolean)


90
# File 'lib/abiparser/function.rb', line 90

def payable?() @payable; end

#pure?Boolean

Returns:

  • (Boolean)


91
# File 'lib/abiparser/function.rb', line 91

def pure?() @pure; end

#sigObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/abiparser/function.rb', line 94

def sig
  ## note: signature
  ##  only includes name and inputs
  ##   excludes / drops outputs!!!

  buf = "#{@name}"
  if @inputs.empty?
    buf << "()"
  else
    buf2 = @inputs.map {|param| param.sig }
    buf << "(#{buf2.join(',')})"
  end
  buf
end

#sighashObject



109
110
111
# File 'lib/abiparser/function.rb', line 109

def sighash
  keccak256( sig )[0,4].hexdigest
end

#typesObject



157
158
159
160
161
# File 'lib/abiparser/function.rb', line 157

def types
    ## for debugging / analytics return all used types (input+output)
    @inputs.map {|param| param.type } +
    @outputs.map {|param| param.type }
end