lib/etherlite/contract/base.rb in etherlite-0.1.4 vs lib/etherlite/contract/base.rb in etherlite-0.1.5
- old
+ new
@@ -1,25 +1,72 @@
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.first_account)
+ new(client.connection, _address, client.default_account)
end
end
attr_reader :connection
@@ -27,9 +74,14 @@
@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)