Class: ABI::Contract

Inherits:
Object
  • Object
show all
Defined in:
lib/abi2ruby/generate.rb

Instance Method Summary collapse

Instance Method Details

#_generate_func(func) ⇒ Object



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
# File 'lib/abi2ruby/generate.rb', line 6

def _generate_func( func )
  buf = String.new

  buf << "def #{func.name}("

  func.inputs.each_with_index do |param,i|
      buf << ", "  if i > 0
      if param.name
         buf << "#{param.name}"
      else
         buf << "arg#{i}"
      end
  end

  buf << ")\n"

  buf << "  do_call("
  buf << %Q{"#{func.name}"}

  func.inputs.each_with_index do |param,i|
    buf << ", "
    if param.name
       buf << "#{param.name}"
    else
       buf << "arg#{i}"
    end
  end

  buf << ")\n"
  buf << "end\n"

  ## note: quick hack - for rdoc/yard doc generation
  ##            move sig method below method
  buf << %Q{sig "#{func.name}"}

  if func.inputs.size > 0
      quoted_types = func.inputs.map {|param| %Q{"#{param.sig}"} }
      buf << ", inputs: [#{quoted_types.join(',')}]"
  end

  if func.outputs.size > 0
    quoted_types = func.outputs.map {|param| %Q{"#{param.sig}"} }
    buf << ", outputs: [#{quoted_types.join(',')}]"
  end
  buf
end

#generate_code(name: 'Contract', address: nil, natspec: nil) ⇒ Object



53
54
55
56
57
58
59
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/abi2ruby/generate.rb', line 53

def generate_code( name: 'Contract',
                   address: nil,
                   natspec: nil )
  buf = ''
  ## buf << "#########################\n"
  buf << "# #{name} contract / (blockchain) services / function calls\n"
  buf << "#\n"
  buf << "#  auto-generated via abi2ruby (see https://rubygems.org/gems/abi2ruby) on #{Time.now.utc}\n"
  buf << "#  - #{query_functions.size} query functions(s)\n"
  buf << "#  - #{helper_functions.size} helper functions(s)\n"

  if natspec && natspec.head.size > 0
     buf << "#\n#\n"
     natspec.head.each do |line|
        buf <<  (line.empty? ? "#\n" : "#  #{line}\n")
     end
  end
  buf << "\n\n"



  buf << "class  #{name} <  Ethlite::Contract\n\n"

  if address
    buf << "  address "
    buf << %Q{"#{address}"}
    buf << "\n"
  end


  if query_functions.size > 0
    buf << "\n"
    query_functions.each do |func|

      if natspec && (natspec.storage[ func.name] || natspec.functions[ func.name ])
         sect = natspec.storage[ func.name ] || natspec.functions[ func.name ]
         buf << "#  #{sect[0]}\n#\n"
         sect[1].each do |line|
               buf <<  (line.empty? ? "#\n" : "#  #{line}\n")
              end
      else
        buf << "#  #{func.doc} _readonly_\n"
      end

      buf << _generate_func( func )
      buf << "\n\n"
    end
  end


  if helper_functions.size > 0
    buf << "\n"
    helper_functions.each do |func|

      if natspec && (natspec.storage[ func.name] || natspec.functions[ func.name ])
         sect = natspec.storage[ func.name ] || natspec.functions[ func.name ]
         buf << "#  #{sect[0]}\n#\n"
         sect[1].each do |line|
               buf <<  (line.empty? ? "#\n" : "#  #{line}\n")
              end
      else
        buf << "#  #{func.doc} _readonly_\n"
      end

      buf << _generate_func( func )
      buf << "\n\n"
   end
  end

   buf << "end   ## class #{name}\n"
   buf << "\n"
   buf
end