Sha256: 3a15310afd644101bae5c69552e99d53cd401992dc7df0c8df25bef3f9a0a77e

Contents?: true

Size: 1.93 KB

Versions: 6

Compression:

Stored size: 1.93 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 next_nonce
      if @connection.use_parity
        @connection.parity_next_nonce(address)
      else
        # https://github.com/ethereum/go-ethereum/issues/2736
        @connection.eth_get_transaction_count(address, 'pending')
      end
    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 Etherlite::NotSupportedError, 'transactions are not supported by this kind of account'
    end

    def ==(_other)
      normalized_address == _other.normalized_address
    end

    private

    def call_constant(_target, _function, _params, _options)
      params = {
        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

6 entries across 6 versions & 1 rubygems

Version Path
etherlite-0.6.0 lib/etherlite/account/base.rb
etherlite-0.5.3 lib/etherlite/account/base.rb
etherlite-0.5.2 lib/etherlite/account/base.rb
etherlite-0.5.1 lib/etherlite/account/base.rb
etherlite-0.5.0 lib/etherlite/account/base.rb
etherlite-0.4.2 lib/etherlite/account/base.rb