Class: Abi::Tool

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

Class Method Summary collapse

Class Method Details

.do_download_abisObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/abibase.rb', line 154

def self.do_download_abis
   puts "==> download abis..."

   recs = read_csv( "./contracts.csv" )
   puts "   #{recs.size} record(s)"

   delay_in_s = 1

   recs.each_with_index do |rec,i|
     addr  = rec['address'].downcase
     names = rec['names']
     puts "==> [#{i+1}/#{recs.size}] #{names} @ #{addr}..."

     outpath = "./address/#{addr}/abi.json"

     if File.exist?( outpath )
        # already download / found in cache
     else
       puts "  sleeping in #{delay_in_s} sec(s)..."
       sleep( delay_in_s )
       data = Etherscan.getabi( address: addr )
       pp data   ## note: returns abi data as a json string (parse again!!)
       abi = JSON.parse( data )
       pp abi

       write_json( outpath, abi )
     end
   end
end

.do_download_codeObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/abibase.rb', line 187

def self.do_download_code
  puts "==> download (source) code..."

  recs = read_csv( "./contracts.csv" )
  puts "   #{recs.size} record(s)"

  delay_in_s = 1

  recs.each_with_index do |rec,i|
    addr  = rec['address'].downcase
    names = rec['names']
    puts "==> [#{i+1}/#{recs.size}] #{names} @ #{addr}..."

    outpath_code = "./address/#{addr}/contract.sol"
    outpath_meta = "./address/#{addr}/contract.yml"

    if File.exist?( outpath_code )
       # already download / found in cache
    else
      puts "  sleeping in #{delay_in_s} sec(s)..."
      sleep( delay_in_s )

      data = Etherscan.getsourcecode( address: addr )
      pp data   ## note: returns abi data as a json string (parse again!!)

      ## note: returns an array
      if data.size != 1
        puts "!! ERROR - expected array of size 1; got #{data.size}"
        exit 1
      end

      code = data[0]['SourceCode']

      ## note:  unroll multi-file format if present (starts with {{ .. }})
      code = format_code( code )   if code.start_with?( /[ \t\n\r]*\{\{/ )


      ## fix:  use "universal new line or such ?? - why lines get duplicated??"
      ##  hack: use write_blob
      write_blob( outpath_code, code )

      ## remove SourceCode & ABI entries
      data[0].delete('SourceCode')
      data[0].delete('ABI')
      puts "meta:"
      pp data[0]

      ##  save rest (remaining) as yml
      write_text( outpath_meta, YAML.dump( data[0] ))
    end
  end
end

.do_generate_docsObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/abibase.rb', line 242

def self.do_generate_docs
  puts "==> generate docs..."

  paths = Dir.glob( "./address/**/abi.json" )
  ## paths = paths[0..2]
  paths.each do |path|
     basename = File.basename( File.dirname( path ))

     abi = ABI.read( path )

     natspec =  if File.exist?( "./address/#{basename}/contract.md" )
                   Natspec.read( "./address/#{basename}/contract.md" )
                else
                  nil
                end

     buf = abi.generate_doc( title: "Contract ABI - #{basename}",
                             natspec: natspec )
     puts buf
     write_text( "./address/#{basename}/README.md", buf )

     buf =  abi.generate_interface( name: '' )    # solidity interface declarations (source code)
     write_text( "./address/#{basename}/interface.sol", buf )
  end
end

.do_generate_timelineObject



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/abibase.rb', line 269

def self.do_generate_timeline
  puts "==> generate timeline..."

  ## collection all addresses
  addresses = []
  paths = Dir.glob( "./address/**/abi.json" )
  ## paths = paths[0..2]
  paths.each do |path|
     basename = File.basename( File.dirname( path ))

     addresses <<  basename.downcase
  end

  pp addresses
  puts "   #{addresses.size} record(s)"

  ## update contractdetails.csv
  ## build cache
  cache = ContractDetailsCache.new( './contractdetails.csv' )

  recs = []
  addresses.each_with_index do |addr,i|
     rec = cache[ addr ]
     recs << rec
  end
  ### note:  save back contractdetails cache
  cache.save

  ############
  ## sort by blocknumber  (reverse chronological)
  recs = recs.sort do |l,r|
                l['blockNumber'].to_i(16) <=> r['blockNumber'].to_i(16)
              end

  pp recs

## create report / page
buf = <<TXT
#  Awesome (Ethereum) Contracts  / Blockchain Services

Cache of (ethereum) contract ABIs (application binary interfaces)
and  open source code (if verified / available)


TXT

meta = read_csv( './contracts.csv')
## build name lookup by address
contracts = meta.reduce( {} ) {|h,rec| h[rec['address']]=rec['names']; h }
pp contracts

recs.each_with_index do |rec,i|
addr        = rec['contractAddress'].downcase
timestamp   = rec['timestamp'].to_i(16)
date = Time.at( timestamp).utc

tooltip = date.strftime('%b %-d, %Y')

 names = contracts[addr]
 if names.nil?
  puts "!! ERROR: no name found for contract; sorry:"
  pp rec
  exit 1
 end

 names = names.split( '|' )
 names = names.map { |name| name.gsub( /[ \t]+/, ' ' ).strip }
 name  = names[0]

 buf << " ยท "  if i > 0
 buf << %Q{[#{name}](address/#{addr}  "#{tooltip}")}
 buf << "\n"
end


buf << "\n"
buf << "## Timeline\n\n"

recs.each do |rec|

addr        = rec['contractAddress']
creator     = rec['contractCreator']
txid        = rec['txHash']
blocknumber = rec['blockNumber'].to_i(16)
timestamp   = rec['timestamp'].to_i(16)
date = Time.at( timestamp).utc

names = contracts[addr]
if names.nil?
 puts "!! ERROR: no name found for contract; sorry:"
 pp rec
 exit 1
end

names = names.split( '|' )
names = names.map { |name| name.gsub( /[ \t]+/, ' ' ).strip }
name  = names[0]

buf << "###  #{names.join( ' | ')} - #{date.strftime('%b %-d, %Y')}\n\n"

buf << "contract @ [**#{addr}**](address/#{addr})"
buf << " - [Etherscan](https://etherscan.io/address/#{addr})"
buf << ", [Bloxy](https://bloxy.info/address/#{addr})"
 ## buf << ", [ABIDocs](https://abidocs.dev/contracts/#{addr})"
buf << "\n\n"

#  buf << "created by [#{creator}](https://etherscan.io/address/#{creator}))"
#  buf << " at block no. #{blocknumber} (#{date})"
#  buf << " - txid [#{txid}](https://etherscan.io/tx/#{txid})"
#  buf << "\n\n"
end


write_text( './README.md', buf )





end

.main(args = ARGV) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/abibase.rb', line 113

def self.main( args=ARGV )
  puts "==> welcome to abibase tool with args:"
  pp args

  options = {}

  parser = OptionParser.new do |opts|
    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end

  parser.parse!( args )
  puts "options:"
  pp options

  puts "args:"
  pp args

  command          = args[0] || 'download'

  if ['d', 'dl', 'download'].include?( command )
    do_download_abis
  elsif ['code'].include?( command )
    do_download_code
  elsif ['doc', 'docs'].include?( command )
    do_generate_docs
  elsif ['t', 'time', 'timeline'].include?( command )
    do_generate_timeline   # & download contract details (txid,timestamp,etc.)
  else
    puts "!! ERROR - unknown command >#{command}<, sorry"
  end

  puts "bye"
end