Sha256: f52e65168ebc4abf2a91647c9cbb91205c858d832e6fece91cb2258530b643ad

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

module Etherlite::Account
  class Base
    include Etherlite::Api::Address

    attr_reader :connection, :normalized_address

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

    def transfer_to(_target, _options = {})
      send_transaction _options.merge(to: _target, value: _options.fetch(:amount, 0))
    end

    def call(_target, _function, *_params)
      _function = parse_function(_function) if _function.is_a? String
      options = _params.last.is_a?(Hash) ? _params.pop.clone : {}

      if _function.constant?
        call_constant _target, _function, _params, options
      else
        value = options.fetch(:pay, 0)
        raise 'function is not payable' if value > 0 && !_function.payable?

        send_transaction options.merge(
          to: _target, data: _function.encode(_params), value: value
        )
      end
    end

    def send_transaction(_options = {})
      raise NotSupportedError, 'transactions are not supported by this kind of account'
    end

    private

    def call_constant(_target, _function, _params, _options)
      params = {
        from: json_encoded_address,
        to: Etherlite::Utils.encode_address_param(_target),
        data: _function.encode(_params)
      }

      params[:from] = json_encoded_address if @normalized_address.present?

      block = Etherlite::Utils.encode_block_param _options.fetch(:block, :latest)

      _function.decode @connection, @connection.eth_call(params, block)
    end

    def parse_function(_signature)
      Abi::LoadFunction.for signature: _signature
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
etherlite-0.2.0 lib/etherlite/account/base.rb
etherlite-0.1.8 lib/etherlite/account/base.rb
etherlite-0.1.7 lib/etherlite/account/base.rb
etherlite-0.1.6 lib/etherlite/account/base.rb
etherlite-0.1.5 lib/etherlite/account/base.rb