module Etherlite::Contract ## # The base class for etherlite contract classes # class Base include Etherlite::Api::Address # The contract's registered functions def self.functions @functions ||= [] end # The contract's registered events def self.events @events ||= [] end # The contract's compiled bytecode in binary format (stored as hex) def self.binary @binary ||= begin if /__[^_]+_+/ === unlinked_binary raise UnlinkedContractError, 'compiled contract contains unresolved library references' end unlinked_binary end end ## # Deploys the contract and waits for the creation transaction to be mined. # # This method can be given a source account or client. If an account is given, the it will be # used to send the creation transaction. If instead a client is given, the client # `default_account` will be used to send the transaction. If no account nor client is given, # then the default_account from the default client will be used (if configured). # # @param as (Object) The source account # @param client (Object) The source client (no effect if :as is given) # @param timeout (Integer) The transaction mining timeout in seconds, defaults to 120 seconds. # # This method also takes any parameters the underlying account.send_transaction accepts, like # :gas, :gas_price, etc. # # @return [Object] instance of the contract class pointing the newly deployed contract address. # def self.deploy(_options = {}) as = _options[:as] || _options[:client].try(:default_account) || Etherlite.default_account tx = as.send_transaction({ data: binary }.merge(_options)) if tx.wait_for_block(timeout: _options.fetch(:timeout, 120)) at tx.contract_address, as: as end end ## # Creates a new instance of the contract class that points to a given address. # # # 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.default_account) end end attr_reader :connection def initialize(_connection, _normalized_address, _default_account) @connection = _connection @normalized_address = _normalized_address @default_account = _default_account end ## # Searches for event logs emitted by this contract # # # 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 private attr_reader :default_account, :normalized_address def event_topic(_event) '0x' + Etherlite::Utils.sha3(_event.signature) end end end