Class: Ethereum::ContractInitializer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contract_name, contract, project_initializer) ⇒ ContractInitializer

Returns a new instance of ContractInitializer.


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ethereum/contract_initializer.rb', line 7

def initialize(contract_name, contract, project_initializer)
  @abi = JSON.parse(contract["abi"]) unless contract.nil?
  @binary = contract["bin"] unless contract.nil?
  @name = contract_name
  @project_initializer = project_initializer
  matchdata = @binary.scan(/_+[a-zA-Z]+_+/).uniq
  @needs_linking = matchdata.present?
  if @needs_linking
    @libraries = matchdata.collect do |libname|
      {name: libname.gsub(/_+/,''), sigil: libname}
    end
  end
end

Instance Attribute Details

#abiObject

Returns the value of attribute abi


5
6
7
# File 'lib/ethereum/contract_initializer.rb', line 5

def abi
  @abi
end

#binaryObject

Returns the value of attribute binary


5
6
7
# File 'lib/ethereum/contract_initializer.rb', line 5

def binary
  @binary
end

#contractObject

Returns the value of attribute contract


5
6
7
# File 'lib/ethereum/contract_initializer.rb', line 5

def contract
  @contract
end

#librariesObject

Returns the value of attribute libraries


5
6
7
# File 'lib/ethereum/contract_initializer.rb', line 5

def libraries
  @libraries
end

#nameObject

Returns the value of attribute name


5
6
7
# File 'lib/ethereum/contract_initializer.rb', line 5

def name
  @name
end

#needs_linkingObject

Returns the value of attribute needs_linking


5
6
7
# File 'lib/ethereum/contract_initializer.rb', line 5

def needs_linking
  @needs_linking
end

#project_initializerObject

Returns the value of attribute project_initializer


5
6
7
# File 'lib/ethereum/contract_initializer.rb', line 5

def project_initializer
  @project_initializer
end

Instance Method Details

#build(connection) ⇒ Object


42
43
44
45
# File 'lib/ethereum/contract_initializer.rb', line 42

def build(connection)
  @contract = Ethereum::Contract.new(@name, @binary, @abi) 
  @contract.build(connection)
end

#generate_javascripts(path) ⇒ Object


47
48
49
50
# File 'lib/ethereum/contract_initializer.rb', line 47

def generate_javascripts(path)
  data = {name: @name, abi: @abi, binary: @binary}
  File.open(File.join(path, "#{@name}.json"), 'w') {|f| f.puts data.to_json}
end

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ethereum/contract_initializer.rb', line 21

def link_libraries
  if @needs_linking
    @libraries.each do |library|
      name = library[:name]
      if @project_initializer.libraries[name].nil?
        ENV['ETHEREUM_DEPLOYER_WAIT_TIME'] ||= "120"
        wait_time = ENV['ETHEREUM_DEPLOYER_WAIT_TIME'].to_i
        library_instance = library[:name].constantize.new
        puts "Deploying library #{name}"
        library_instance.deploy_and_wait(wait_time)
        puts "Library deployed at #{library_instance.address}"
        @project_initializer.libraries[name] = library_instance.address
        @binary.gsub!(library[:sigil], library_instance.address.gsub(/^0x/,''))
      else
        address = @project_initializer.libraries[name]
        @binary.gsub!(library[:sigil], address.gsub(/^0x/,''))
      end
    end
  end
end