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

#initialize(path = nil) ⇒ Solidity

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

Parameters:

  • path (String) (defaults to: nil)

    optional override of the solidity compiler path.

Raises:

  • (SystemCallError)


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

def initialize(path = nil)

  # Currently only supports `solc`. Try to override with `path`.
  solc = path || 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)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/eth/solidity.rb', line 45

def compile(contract)
  raise Errno::ENOENT, "Contract file not found: #{contract}" unless File.exist? contract
  flag_opt = "--optimize"
  flag_ir = "--via-ir"
  flag_json = "--combined-json=bin,abi"
  path = File.realpath contract
  output, error, status = Open3.capture3 @compiler, flag_opt, flag_ir, flag_json, path
  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