Class: Etherlite::Contract::Base

Inherits:
Object
  • Object
show all
Includes:
Api::Address
Defined in:
lib/etherlite/contract/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Api::Address

#address, #get_balance

Constructor Details

#initialize(_connection, _normalized_address, _default_account) ⇒ Base

Returns a new instance of Base.



57
58
59
60
61
# File 'lib/etherlite/contract/base.rb', line 57

def initialize(_connection, _normalized_address, )
  @connection = _connection
  @normalized_address = _normalized_address
   = 
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



55
56
57
# File 'lib/etherlite/contract/base.rb', line 55

def connection
  @connection
end

Class Method Details

.at(_address, client: nil, as: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/etherlite/contract/base.rb', line 44

def self.at(_address, client: nil, as: nil)
  _address = Etherlite::Utils.normalize_address_param _address

  if as
    new(as.connection, _address, as)
  else
    client ||= ::Etherlite
    new(client.connection, _address, client.)
  end
end

.binaryObject



21
22
23
24
25
26
27
28
29
# File 'lib/etherlite/contract/base.rb', line 21

def self.binary
  @binary ||= begin
    if /__[^_]+_+/ === unlinked_binary
      raise UnlinkedContractError, 'compiled contract contains unresolved library references'
    end

    unlinked_binary
  end
end

.constructorObject



17
18
19
# File 'lib/etherlite/contract/base.rb', line 17

def self.constructor
  nil
end

.deploy(*_args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/etherlite/contract/base.rb', line 31

def self.deploy(*_args)
  options = _args.last.is_a?(Hash) ? _args.pop : {}
  as = options[:as] || options[:client].try(:default_account) || Etherlite.

  tx_data = binary
  tx_data += constructor.encode(_args) unless constructor.nil?

  tx = as.send_transaction({ data: tx_data }.merge(options))
  if tx.wait_for_block(timeout: options.fetch(:timeout, 120))
    at tx.contract_address, as: as
  end
end

.eventsObject



9
10
11
# File 'lib/etherlite/contract/base.rb', line 9

def self.events
  @events ||= []
end

.functionsObject



5
6
7
# File 'lib/etherlite/contract/base.rb', line 5

def self.functions
  @functions ||= []
end

.unlinked_binaryObject



13
14
15
# File 'lib/etherlite/contract/base.rb', line 13

def self.unlinked_binary
  '0x0'
end

Instance Method Details

#get_logs(events: nil, from_block: :earliest, to_block: :latest) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/etherlite/contract/base.rb', line 63

def get_logs(events: nil, from_block: :earliest, to_block: :latest)
  params = {
    address: json_encoded_address,
    fromBlock: Etherlite::Utils.encode_block_param(from_block),
    toBlock: Etherlite::Utils.encode_block_param(to_block)
  }

  params[:topics] = [events.map { |e| event_topic e }] unless events.nil?

  event_map = Hash[(events || self.class.events).map { |e| [event_topic(e), e] }]

  logs = @connection.ipc_call(:eth_getLogs, params)
  logs.map do |log|
    event = event_map[log["topics"].first]
    # TODO: support anonymous events!
    event.decode(@connection, log) unless event.nil?
  end
end