Class: ABI::Contract

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constructor: nil, functions: [], events: [], has_receive: false, has_fallback: false) ⇒ Contract

Returns a new instance of Contract.



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
# File 'lib/abiparser/contract.rb', line 56

def initialize( constructor: nil,
                functions: [],
                events: [],
                has_receive: false,
                has_fallback: false )
    @ctor  =  constructor
    @funcs  = functions
    @events = events
    @has_receive = has_receive
    @has_fallback = has_fallback

    @selectors = {}

    ## auto-add selectors (hashed signatures)
    @funcs.each do |func|
       sighash = func.sighash
       puts "0x#{sighash} => #{func.sig}"

       ## assert - no duplicates allowed
       if @selectors[sighash]
          puts "!! ERROR - duplicate function signature #{func.sig}; already in use; sorry"
          exit 1
       end

       @selectors[sighash] = func
    end
end

Class Method Details

.parse(data) ⇒ Object



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
# File 'lib/abiparser/contract.rb', line 10

def self.parse( data )
  ## note: auto-convert (parse) from json if string passed-in
  data = JSON.parse( data )  if data.is_a?( String )

  ctor =  nil
  funcs = []
  has_fallback = false
  has_receive  = true

  data.each do |o|
     if o['type'] == 'function'
        funcs <<  Function.parse( o )
     elsif o['type'] == 'constructor'
        raise ArgumentError, "constructor already defined; only one declaration allowed"   if ctor
        ctor =  Constructor.parse( o )
     elsif o['type'] == 'event'
     elsif o['type'] == 'receive'
        ## skip for now
        ## e.g.
        ##  {"stateMutability": "payable",
        ##   "type": "receive"}
     elsif o['type'] == 'fallback'
        ## skip for now
        ## e.g.
        ##  {"stateMutability"=>"nonpayable",
        ##   "type"=>"fallback"}
     elsif o['type'] == 'error'
        ##  skip for now
        ## e.g.
        ##  {"inputs":[],
        ##    "name":"ApprovalCallerNotOwnerNorApproved",
        ##    "type":"error"}
     else
       pp o
       raise TypeError, "expected function or event; sorry: got #{o['type']}"
     end
   end
   new( constructor: ctor,
        functions: funcs,
        events: [],
        has_receive: has_receive,
        has_fallback: has_fallback  )
end

.read(path) ⇒ Object



4
5
6
7
8
# File 'lib/abiparser/contract.rb', line 4

def self.read( path )
  data = read_json( path )
  ## pp data
  parse( data )
end

Instance Method Details

#constructorObject



97
# File 'lib/abiparser/contract.rb', line 97

def constructor() @ctor; end

#functionsObject



98
# File 'lib/abiparser/contract.rb', line 98

def functions() @funcs; end

#helper_functionsObject

add pure ?? funcs alias - why? why not?



115
116
117
# File 'lib/abiparser/contract.rb', line 115

def helper_functions    ## add pure ?? funcs alias - why? why not?
  @funcs.select { |func| func.pure? }
end

#payable_functionsObject

how to name functions categories ???

- use  pay, writer, reader, helper  - why? why not?


103
104
105
# File 'lib/abiparser/contract.rb', line 103

def payable_functions
   @funcs.select { |func| func.payable? }
end

#query_functionsObject

add read funcs alias - why? why not?



111
112
113
# File 'lib/abiparser/contract.rb', line 111

def query_functions    ## add read funcs alias - why? why not?
  @funcs.select { |func| !func.payable? && !func.pure? && func.constant?  }
end

#selectorsObject

return hexstrings of sig(natures) - why? why not? rename to sighashes - why? why not?



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

def selectors()  @selectors.keys;  end

#support?(sig) ⇒ Boolean Also known as: supports?

Returns:

  • (Boolean)


90
91
92
# File 'lib/abiparser/contract.rb', line 90

def support?( sig )
  Utils.support?( @selectors.keys, sig )
end

#transact_functionsObject

add write funcs alias - why? why not?



107
108
109
# File 'lib/abiparser/contract.rb', line 107

def transact_functions    ## add write funcs alias - why? why not?
   @funcs.select { |func| !func.payable? && !func.constant? }
end