Class: Eth::Contract

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

Overview

Provides classes to access smart contracts

Defined Under Namespace

Classes: Event, Function, FunctionInput, FunctionOutput, Initializer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, bin, abi) ⇒ Contract

Constructor of the Eth::Contract class.

Parameters:

  • name (String)

    contract name.

  • bin (String)

    contract bin string.

  • abi (String)

    contract abi string.



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

def initialize(name, bin, abi)
  @name = name
  @bin = bin
  @abi = abi
  @constructor_inputs, @functions, @events = parse_abi(abi)
end

Instance Attribute Details

#abiObject

Returns the value of attribute abi.



25
26
27
# File 'lib/eth/contract.rb', line 25

def abi
  @abi
end

#addressObject

Returns the value of attribute address.



22
23
24
# File 'lib/eth/contract.rb', line 22

def address
  @address
end

#binObject

Returns the value of attribute bin.



25
26
27
# File 'lib/eth/contract.rb', line 25

def bin
  @bin
end

#class_objectObject

Returns the value of attribute class_object.



25
26
27
# File 'lib/eth/contract.rb', line 25

def class_object
  @class_object
end

#constructor_inputsObject

Returns the value of attribute constructor_inputs.



26
27
28
# File 'lib/eth/contract.rb', line 26

def constructor_inputs
  @constructor_inputs
end

#eventsObject

Returns the value of attribute events.



26
27
28
# File 'lib/eth/contract.rb', line 26

def events
  @events
end

#functionsObject

Returns the value of attribute functions.



26
27
28
# File 'lib/eth/contract.rb', line 26

def functions
  @functions
end

#gas_limitObject

Returns the value of attribute gas_limit.



24
25
26
# File 'lib/eth/contract.rb', line 24

def gas_limit
  @gas_limit
end

#gas_priceObject

Returns the value of attribute gas_price.



24
25
26
# File 'lib/eth/contract.rb', line 24

def gas_price
  @gas_price
end

#keyObject

Returns the value of attribute key.



23
24
25
# File 'lib/eth/contract.rb', line 23

def key
  @key
end

#max_fee_per_gasObject

Returns the value of attribute max_fee_per_gas.



24
25
26
# File 'lib/eth/contract.rb', line 24

def max_fee_per_gas
  @max_fee_per_gas
end

#max_priority_fee_per_gasObject

Returns the value of attribute max_priority_fee_per_gas.



24
25
26
# File 'lib/eth/contract.rb', line 24

def max_priority_fee_per_gas
  @max_priority_fee_per_gas
end

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/eth/contract.rb', line 25

def name
  @name
end

#nonceObject

Returns the value of attribute nonce.



24
25
26
# File 'lib/eth/contract.rb', line 24

def nonce
  @nonce
end

Class Method Details

.from_abi(abi:, address:, name:) ⇒ Eth::Contract::Object

Creates a contract wrapper from ABI and address.

Parameters:

  • abi (String)

    contract abi string.

  • address (String)

    contract address.

  • name (String)

    name of contract.

Returns:

  • (Eth::Contract::Object)

    Returns the class of the smart contract.

Raises:

  • (JSON::ParserError)

    if the json format is wrong.

  • (ArgumentError)

    if ABI, address, or name is missing.



61
62
63
64
65
66
67
68
# File 'lib/eth/contract.rb', line 61

def self.from_abi(abi:, address:, name:)
  abi = abi.is_a?(Array) ? abi : JSON.parse(abi)
  contract = Eth::Contract.new(name, nil, abi)
  contract.build
  contract = contract.class_object.new
  contract.address = address
  contract
end

.from_bin(bin:, abi:, name:) ⇒ Eth::Contract::Object

Creates a contract wrapper from binary and ABI.

Parameters:

  • bin (String)

    contract bin string.

  • abi (String)

    contract abi string.

  • name (String)

    name of contract.

Returns:

  • (Eth::Contract::Object)

    Returns the class of the smart contract.

Raises:

  • (JSON::ParserError)

    if the json format is wrong.

  • (ArgumentError)

    if ABI, binary, or name is missing.



78
79
80
81
82
83
# File 'lib/eth/contract.rb', line 78

def self.from_bin(bin:, abi:, name:)
  abi = abi.is_a?(Array) ? abi : JSON.parse(abi)
  contract = Eth::Contract.new(name, bin, abi)
  contract.build
  contract.class_object.new
end

.from_file(file:, contract_index: 0) ⇒ Eth::Contract::Object

Creates a contract wrapper from a Solidity file.

Parameters:

  • file (String)

    solidity file path.

  • contract_index (Number) (defaults to: 0)

    specify contract.

Returns:

  • (Eth::Contract::Object)

    Returns the class of the smart contract.

Raises:

  • (ArgumentError)

    if the file path is empty or no contracts were compiled.



46
47
48
49
50
51
# File 'lib/eth/contract.rb', line 46

def self.from_file(file:, contract_index: 0)
  raise ArgumentError, "Cannot find the contract at #{file.to_s}!" if !File.exist?(file.to_s)
  contracts = Eth::Contract::Initializer.new(file).build_all
  raise ArgumentError, "No contracts compiled." if contracts.empty?
  contracts[contract_index].class_object.new
end

Instance Method Details

#buildObject

Create meta classes for smart contracts.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/eth/contract.rb', line 100

def build
  class_name = @name
  parent = self
  class_methods = Class.new do
    extend Forwardable
    def_delegators :parent, :key, :key=
    def_delegators :parent, :name, :abi, :bin
    def_delegators :parent, :gas_limit, :gas_price, :gas_limit=, :gas_price=, :nonce, :nonce=
    def_delegators :parent, :max_fee_per_gas, :max_fee_per_gas=, :max_priority_fee_per_gas, :max_priority_fee_per_gas=
    def_delegators :parent, :events
    def_delegators :parent, :address, :address=
    def_delegator :parent, :functions
    def_delegator :parent, :constructor_inputs
    define_method :parent do
      parent
    end
  end
  Eth::Contract.send(:remove_const, class_name) if Eth::Contract.const_defined?(class_name, false)
  Eth::Contract.const_set(class_name, class_methods)
  @class_object = class_methods
end