Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
geraldb committed Feb 9, 2023
1 parent a89df2b commit 3984103
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 52 deletions.
2 changes: 2 additions & 0 deletions abi2sol/lib/abi2sol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@


## our own code
require_relative 'abi2sol/model'
require_relative 'abi2sol/generate'




module Abi2Sol
class Tool

Expand Down
10 changes: 10 additions & 0 deletions abi2sol/lib/abi2sol/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ def generate_interface( name: ) ## interface declarations
# buf << "#{@ctor.decl}\n"
# end


if events.size > 0
buf << "\n"
buf << "// #{events.size} Event(s)\n"
events.each do |event|
buf << "#{event.decl}\n"
end
end


if payable_functions.size > 0
buf << "\n"
buf << "// #{payable_functions.size} Payable Function(s)\n"
Expand Down
76 changes: 76 additions & 0 deletions abi2sol/lib/abi2sol/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

module ABI

class Param
def decl
buf = String.new('')
buf << "#{sig} "
buf << "indexed " if @indexed
buf << (@name ? @name : '_')
## use inline comment - why? why not?
if @internal_type && @internal_type != sig
buf << " /* #{@internal_type} */"
end
buf
end
end ## class Param


class Constructor
def decl
buf = "constructor"
if @inputs.empty?
buf << "()"
else
params = @inputs.map {|param| param.decl }
buf << "(#{params.join(', ')})"
end
buf << ";"
buf
end
end ## class Constructor

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

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


class Event
def decl
buf = "event #{@name}"
if @inputs.empty?
buf << "()"
else
params = @inputs.map {|param| param.decl }
buf << "(#{params.join(', ')})"
end
buf << ";"
buf
end
end ## class Event




end ## module ABI
1 change: 1 addition & 0 deletions abi2sol/sandbox/test_parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# to run use
# ruby -I ./lib sandbox/test_parse.rb

$LOAD_PATH.unshift( "../abiparser/lib" )
require 'abi2sol'


Expand Down
23 changes: 23 additions & 0 deletions abi2sol/sandbox/test_parse2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
###
# to run use
# ruby -I ./lib sandbox/test_parse2.rb

$LOAD_PATH.unshift( "../abiparser/lib" )
require 'abi2sol'

names = [
'AirSwap',
'BunchaStructs',
]

names.each do |name|
abi = ABI.read( "./abis//#{name}.abi.json" )
pp abi

buf = abi.generate_interface( name: name )
puts buf
write_text( "./tmp/#{name}.sol", buf )
end


puts "bye"
1 change: 1 addition & 0 deletions abiparser/lib/abiparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def sig( bin )
require_relative 'abiparser/param'
require_relative 'abiparser/constructor'
require_relative 'abiparser/function'
require_relative 'abiparser/event'
require_relative 'abiparser/utils'
require_relative 'abiparser/contract'
require_relative 'abiparser/interface'
Expand Down
15 changes: 1 addition & 14 deletions abiparser/lib/abiparser/constructor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.parse( o )
pp o
end

if o.has_key?( 'constant')
if o.has_key?( 'constant' )
puts "!! WARN: constant for constructor possible?"
pp o
exit 1
Expand Down Expand Up @@ -95,19 +95,6 @@ def doc
buf
end

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


end # class Constructor
end # module ABI

Expand Down
6 changes: 5 additions & 1 deletion abiparser/lib/abiparser/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def self.parse( data )
funcs = []
has_fallback = false
has_receive = true
events = []

data.each do |o|
if o['type'] == 'function'
Expand All @@ -23,6 +24,7 @@ def self.parse( data )
raise ArgumentError, "constructor already defined; only one declaration allowed" if ctor
ctor = Constructor.parse( o )
elsif o['type'] == 'event'
events << Event.parse( o )
elsif o['type'] == 'receive'
## skip for now
## e.g.
Expand All @@ -46,7 +48,7 @@ def self.parse( data )
end
new( constructor: ctor,
functions: funcs,
events: [],
events: events,
has_receive: has_receive,
has_fallback: has_fallback )
end
Expand Down Expand Up @@ -96,6 +98,8 @@ def support?( sig )

def constructor() @ctor; end
def functions() @funcs; end
def events() @events; end


###
## how to name functions categories ???
Expand Down
56 changes: 56 additions & 0 deletions abiparser/lib/abiparser/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module ABI
class Event

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

new( name, inputs: inputs )
end


attr_reader :name,
:inputs
## :input_types

def initialize( name,
inputs: [] )
@name = name
@inputs = inputs
end


def sig
## note: signature
## only includes name and inputs

##
## todo/fix: check if event sig includes indexed or/and
## special prefix or such!!!!

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

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




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

end ## class Event
end ## module ABI

23 changes: 0 additions & 23 deletions abiparser/lib/abiparser/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,29 +134,6 @@ def doc
buf
end

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


def types
## for debugging / analytics return all used types (input+output)
Expand Down
21 changes: 7 additions & 14 deletions abiparser/lib/abiparser/param.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ def self.parse( o )
internal_type = o['internalType']
name = o['name']
components = o['components'] ? o['components'].map { |c| parse( c ) } : nil
indexed = o['indexed']

new( type, name,
internal_type: internal_type,
components: components )
components: components,
indexed: indexed )
end

### check - find a "better" name for internal_type
## use a keyword param - why? why not?
def initialize( type, name=nil,
internal_type: nil,
components: nil ) ## note: type goes first!!!
components: nil,
indexed: nil ) ## note: type goes first!!!
@type = type
## note: convert empty string "" to nil - why? why not?
@name = if name && name.empty?
Expand All @@ -33,9 +36,11 @@ def initialize( type, name=nil,
internal_type
end
@components = components
@indexed = indexed ## note: only used for event params (create a subparam type - why? why not??)
end



def sig
@sig ||= begin
if @components
Expand All @@ -62,17 +67,5 @@ def doc
buf
end

def decl
buf = ''
buf << "#{sig} "
buf << (@name ? @name : '_')
## use inline comment - why? why not?
if @internal_type && @internal_type != sig
buf << " /* #{@internal_type} */"
end
buf
end


end ## class Param
end ## module ABI

0 comments on commit 3984103

Please sign in to comment.