Class: Eth::Solidity

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

Overview

Class to create Solidity compiler bingings for Ruby.

Defined Under Namespace

Classes: CompilerError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSolidity

Instantiates a Solidity solc system compiler binding that can be used to compile Solidity contracts.

Raises:

  • (SystemCallError)


31
32
33
34
35
36
37
# File 'lib/eth/solidity.rb', line 31

def initialize

  # Currently only supports `solc`.
  solc = get_compiler_path
  raise SystemCallError, "Unable to find the solc compiler path!" if solc.nil?
  @compiler = solc
end

Instance Attribute Details

#compilerObject (readonly)

Solidity compiler binary path.



27
28
29
# File 'lib/eth/solidity.rb', line 27

def compiler
  @compiler
end

Instance Method Details

#compile(contract) ⇒ Array

Use the bound Solidity executable to compile the given contract.

Parameters:

  • contract (String)

    path of the contract to compile.

Returns:

  • (Array)

    JSON containing the compiled contract and ABI for all contracts.

Raises:

  • (Errno::ENOENT)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/eth/solidity.rb', line 43

def compile(contract)
  raise Errno::ENOENT, "Contract file not found: #{contract}" unless File.exist? contract
  command = "#{@compiler} --optimize --combined-json bin,abi #{contract}"
  output, error, status = Open3.capture3 command
  raise SystemCallError, "Unable to run solc compiler!" if status.exitstatus === 127
  raise CompilerError, error unless status.success?
  json = JSON.parse output
  result = {}
  json["contracts"].each do |key, value|
    _file, name = key.split ":"
    result[name] = {}
    result[name]["abi"] = value["abi"]
    result[name]["bin"] = value["bin"]
  end
  return result
end