Sha256: 8de1314a0933ececbf3e2fc4f6ede54b6447d3cb207450ce44cc6e0403607f28

Contents?: true

Size: 983 Bytes

Versions: 6

Compression:

Stored size: 983 Bytes

Contents

# frozen_string_literal: true

module Silkey # :nodoc: all
  class Contract < SimpleDelegator
    def initialize(contract, client)
      super(contract)
      @contract = contract
      @client = client
      _, @functions, = Ethereum::Abi.parse_abi(contract.abi)
    end

    def call_func_for_block(func_name, default_block = 'latest', *args)
      fun = @functions.find { |f| f.name == func_name.to_s }

      raise 'FunctionNotFoundError', func_name unless fun

      raw_result = @client.send_command('eth_call',
                                        FrozenArray.new(@contract.call_args(fun, args),
                                                        default_block))['result']

      output = Ethereum::Decoder.new.decode_arguments(fun.outputs, raw_result)

      return output[0] if output.length == 1

      output
    end

    class FrozenArray < Array
      def initialize(*args)
        super(args)
      end

      def <<(_)
        self
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
silkey-sdk-0.1.0 lib/silkey/contract.rb
silkey-sdk-0.0.5 lib/silkey/contract.rb
silkey-sdk-0.0.4 lib/silkey/contract.rb
silkey-sdk-0.0.3 lib/silkey/contract.rb
silkey-sdk-0.0.2 lib/silkey/contract.rb
silkey-sdk-0.0.1 lib/silkey/contract.rb