From 0ddd17503095c24c993be3360fbe30823364b850 Mon Sep 17 00:00:00 2001 From: Gerald Bauer Date: Thu, 9 Feb 2023 17:20:22 +0100 Subject: [PATCH] up --- abidoc/Manifest.txt | 2 + abidoc/README.md | 2 +- abidoc/lib/abidoc.rb | 78 +---------------------- abidoc/lib/abidoc/generate.rb | 85 ++++++++++++++++++++++++++ abidoc/lib/abidoc/model.rb | 67 ++++++++++++++++++++ abidoc/lib/abidoc/version.rb | 2 +- abidoc/sandbox/test_gen.rb | 1 + abiparser/lib/abiparser/constructor.rb | 12 ---- abiparser/lib/abiparser/function.rb | 19 ------ abiparser/lib/abiparser/param.rb | 12 ---- 10 files changed, 159 insertions(+), 121 deletions(-) create mode 100644 abidoc/lib/abidoc/generate.rb create mode 100644 abidoc/lib/abidoc/model.rb diff --git a/abidoc/Manifest.txt b/abidoc/Manifest.txt index f1b958c..6d5bb01 100644 --- a/abidoc/Manifest.txt +++ b/abidoc/Manifest.txt @@ -3,4 +3,6 @@ Manifest.txt README.md Rakefile lib/abidoc.rb +lib/abidoc/generate.rb +lib/abidoc/model.rb lib/abidoc/version.rb diff --git a/abidoc/README.md b/abidoc/README.md index c4a1eca..60eed99 100644 --- a/abidoc/README.md +++ b/abidoc/README.md @@ -119,7 +119,7 @@ and so on. ## Bonus - Awesome Contracts -See the [**Awesome (Ethereum) Contracts / Blockchain Services**](https://github.com/rubycocos/awesome-contracts) repo. +See the [**Awesome (Ethereum) Contracts / Blockchain Services @ Open Blockchains**](https://github.com/openblockchains/awesome-contracts) repo. that is a cache of (ethereum) contract ABIs (application binary interfaces) and open source code (if verified / available) with auto-generated docs via abidoc & friends. diff --git a/abidoc/lib/abidoc.rb b/abidoc/lib/abidoc.rb index edda2f6..7fa5438 100644 --- a/abidoc/lib/abidoc.rb +++ b/abidoc/lib/abidoc.rb @@ -4,82 +4,8 @@ ## our own code require_relative 'abidoc/version' # note: let version always go first +require_relative 'abidoc/model' +require_relative 'abidoc/generate' -module ABI - class Contract - - -def generate_doc( title: 'Contract ABI', - natspec: nil ) - buf = '' - buf << "# #{title}\n\n" - - if natspec && natspec.head.size > 0 - natspec.head.each do |line| - buf << (line.empty? ? "\n" : "#{line}\n") - end - end - buf << "\n\n" - - - - if @ctor - buf << "\n" - buf << "**Constructor**\n\n" - buf << "- #{@ctor.doc}\n" - ## buf << " - sig #{@ctor.sig} => 0x#{sig(@ctor.sig).hexdigest}\n" - end - - if payable_functions.size > 0 - buf << "\n" - buf << "**#{payable_functions.size} Payable Function(s)**\n\n" - payable_functions.each do |func| - buf << "- #{func.doc} _payable_\n" - ## buf << " - sig #{func.sig} => 0x#{sig(func.sig).hexdigest}\n" - end - end - - if transact_functions.size > 0 - buf << "\n" - buf << "**#{transact_functions.size} Transact Functions(s)**\n\n" - transact_functions.each do |func| - buf << "- #{func.doc}\n" - ## buf << " - sig #{func.sig} => 0x#{sig(func.sig).hexdigest}\n" - end - end - - - if query_functions.size > 0 - buf << "\n" - buf << "**#{query_functions.size} Query Functions(s)**\n\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]}" - sect[1].each do |line| - buf << (line.empty? ? "
" : "
#{line}") - end - buf << "\n" - else - buf << "- #{func.doc} _readonly_\n" - ## buf << " - sig #{func.sig} => 0x#{sig(func.sig).hexdigest}\n" - end - end - end - - if helper_functions.size > 0 - buf << "\n" - buf << "**#{helper_functions.size} Helper Functions(s)**\n\n" - helper_functions.each do |func| - buf << "- #{func.doc}\n" - ## buf << " - sig #{func.sig} => 0x#{sig(func.sig).hexdigest}\n" - end - end - - buf -end - -end ## class Contract -end ## module ABI diff --git a/abidoc/lib/abidoc/generate.rb b/abidoc/lib/abidoc/generate.rb new file mode 100644 index 0000000..8423216 --- /dev/null +++ b/abidoc/lib/abidoc/generate.rb @@ -0,0 +1,85 @@ +module ABI + class Contract + + +def generate_doc( title: 'Contract ABI', + natspec: nil ) + buf = '' + buf << "# #{title}\n\n" + + if natspec && natspec.head.size > 0 + natspec.head.each do |line| + buf << (line.empty? ? "\n" : "#{line}\n") + end + end + buf << "\n\n" + + + if events.size > 0 + buf << "\n" + buf << "**#{events.size} Event Log Type(s)**\n\n" + events.each do |event| + buf << "- #{event.doc}\n" + end + end + + + + if @ctor + buf << "\n" + buf << "**Constructor**\n\n" + buf << "- #{@ctor.doc}\n" + ## buf << " - sig #{@ctor.sig} => 0x#{sig(@ctor.sig).hexdigest}\n" + end + + if payable_functions.size > 0 + buf << "\n" + buf << "**#{payable_functions.size} Payable Function(s)**\n\n" + payable_functions.each do |func| + buf << "- #{func.doc} _payable_\n" + ## buf << " - sig #{func.sig} => 0x#{sig(func.sig).hexdigest}\n" + end + end + + if transact_functions.size > 0 + buf << "\n" + buf << "**#{transact_functions.size} Transact Functions(s)**\n\n" + transact_functions.each do |func| + buf << "- #{func.doc}\n" + ## buf << " - sig #{func.sig} => 0x#{sig(func.sig).hexdigest}\n" + end + end + + + if query_functions.size > 0 + buf << "\n" + buf << "**#{query_functions.size} Query Functions(s)**\n\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]}" + sect[1].each do |line| + buf << (line.empty? ? "
" : "
#{line}") + end + buf << "\n" + else + buf << "- #{func.doc} _readonly_\n" + ## buf << " - sig #{func.sig} => 0x#{sig(func.sig).hexdigest}\n" + end + end + end + + if helper_functions.size > 0 + buf << "\n" + buf << "**#{helper_functions.size} Helper Functions(s)**\n\n" + helper_functions.each do |func| + buf << "- #{func.doc}\n" + ## buf << " - sig #{func.sig} => 0x#{sig(func.sig).hexdigest}\n" + end + end + + buf +end + +end ## class Contract +end ## module ABI diff --git a/abidoc/lib/abidoc/model.rb b/abidoc/lib/abidoc/model.rb new file mode 100644 index 0000000..ade22b1 --- /dev/null +++ b/abidoc/lib/abidoc/model.rb @@ -0,0 +1,67 @@ +module ABI + +class Param + def doc + buf = '' + if @internal_type && @internal_type != sig + buf << "#{@internal_type} " + else + buf << "#{sig} " + end + buf << (@name ? @name : '_') + buf + end +end + + +class Constructor + def doc + buf = "constructor" + if @inputs.empty? + buf << "()" + else + params = @inputs.map {|param| param.doc } + buf << "(#{params.join(', ')})" + end + buf + end +end ## class Constructor + +class Function + def doc + ## note: text with markdown formatting + buf = "function **#{@name}**" + if @inputs.empty? + buf << "()" + else + params = @inputs.map {|param| param.doc } + buf << "(#{params.join(', ')})" + end + if @outputs.empty? + ## do nothing + else + buf << " ⇒ " + params = @outputs.map {|param| param.doc } + buf << "(#{params.join(', ')})" + end + buf + end +end ## class Function + + +class Event + def doc + ## note: text with markdown formatting + buf = "event **#{@name}**" + if @inputs.empty? + buf << "()" + else + params = @inputs.map {|param| param.doc } + buf << "(#{params.join(', ')})" + end + buf + end +end ## class Event + + +end ## module ABI diff --git a/abidoc/lib/abidoc/version.rb b/abidoc/lib/abidoc/version.rb index 7258663..4147607 100644 --- a/abidoc/lib/abidoc/version.rb +++ b/abidoc/lib/abidoc/version.rb @@ -3,7 +3,7 @@ module ABIDoc MAJOR = 0 MINOR = 1 - PATCH = 0 + PATCH = 1 VERSION = [MAJOR,MINOR,PATCH].join('.') def self.version diff --git a/abidoc/sandbox/test_gen.rb b/abidoc/sandbox/test_gen.rb index d4de59e..6951551 100644 --- a/abidoc/sandbox/test_gen.rb +++ b/abidoc/sandbox/test_gen.rb @@ -2,6 +2,7 @@ # to run use # ruby -I ./lib sandbox/test_gen.rb +$LOAD_PATH.unshift( "../abiparser/lib" ) require 'abidoc' punks_v1 = '0x6ba6f2207e343923ba692e5cae646fb0f566db8d' diff --git a/abiparser/lib/abiparser/constructor.rb b/abiparser/lib/abiparser/constructor.rb index a8f037a..61bf5ca 100644 --- a/abiparser/lib/abiparser/constructor.rb +++ b/abiparser/lib/abiparser/constructor.rb @@ -83,18 +83,6 @@ def sig buf end - - def doc - buf = "constructor" - if @inputs.empty? - buf << "()" - else - buf2 = @inputs.map {|param| param.doc } - buf << "(#{buf2.join(', ')})" - end - buf - end - end # class Constructor end # module ABI diff --git a/abiparser/lib/abiparser/function.rb b/abiparser/lib/abiparser/function.rb index 554a9cb..456a6c3 100644 --- a/abiparser/lib/abiparser/function.rb +++ b/abiparser/lib/abiparser/function.rb @@ -115,25 +115,6 @@ def sighash end - 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 - def types ## for debugging / analytics return all used types (input+output) diff --git a/abiparser/lib/abiparser/param.rb b/abiparser/lib/abiparser/param.rb index ad3f060..6e1d1bd 100644 --- a/abiparser/lib/abiparser/param.rb +++ b/abiparser/lib/abiparser/param.rb @@ -55,17 +55,5 @@ def sig @sig end - - def doc - buf = '' - if @internal_type && @internal_type != sig - buf << "#{@internal_type} " - else - buf << "#{sig} " - end - buf << (@name ? @name : '_') - buf - end - end ## class Param end ## module ABI