Sha256: 2212730eed5192f5579075b77757f210951ba892d73380c582101eb7d741f47a

Contents?: true

Size: 1.2 KB

Versions: 18

Compression:

Stored size: 1.2 KB

Contents

module Etherlite::Contract
  class Function
    attr_reader :name, :inputs, :outputs

    def initialize(_name, _inputs, _outputs, _payable, _constant)
      @name = _name
      @inputs = _inputs
      @outputs = _outputs

      @payable = _payable
      @constant = _constant
    end

    def constant?
      @constant
    end

    def payable?
      @payable
    end

    def signature
      @signature ||= begin
        arg_signatures = @inputs.map &:signature
        "#{@name}(#{arg_signatures.join(',')})"
      end
    end

    def encode(_values)
      if _values.length != @inputs.count
        raise ArgumentError, "Expected #{@inputs.count} arguments, got #{_values.length} "
      end

      encoded_inputs = Etherlite::Support::Array.encode(@inputs.map(&:type), _values)

      if @name
        signature_hash = Etherlite::Utils.sha3 signature
        '0x' + signature_hash[0..7] + encoded_inputs
      else
        encoded_inputs # if no name is provided, just render arguments
      end
    end

    def decode(_connection, _data)
      return nil if @outputs.empty?

      result = Etherlite::Support::Array.decode(_connection, @outputs, _data[2..-1])
      result.length > 1 ? result : result[0]
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
etherlite-0.6.0 lib/etherlite/contract/function.rb
etherlite-0.5.3 lib/etherlite/contract/function.rb
etherlite-0.5.2 lib/etherlite/contract/function.rb
etherlite-0.5.1 lib/etherlite/contract/function.rb
etherlite-0.5.0 lib/etherlite/contract/function.rb
etherlite-0.4.2 lib/etherlite/contract/function.rb
etherlite-0.4.1 lib/etherlite/contract/function.rb
etherlite-0.4.0 lib/etherlite/contract/function.rb
etherlite-0.3.0 lib/etherlite/contract/function.rb
etherlite-0.2.6 lib/etherlite/contract/function.rb
etherlite-0.2.5 lib/etherlite/contract/function.rb
etherlite-0.2.4 lib/etherlite/contract/function.rb
etherlite-0.2.3 lib/etherlite/contract/function.rb
etherlite-0.2.2 lib/etherlite/contract/function.rb
etherlite-0.2.1 lib/etherlite/contract/function.rb
etherlite-0.2.0 lib/etherlite/contract/function.rb
etherlite-0.1.8 lib/etherlite/contract/function.rb
etherlite-0.1.7 lib/etherlite/contract/function.rb