<commit_before>module Ethereum
  class Contract

    attr_reader :address
    attr_accessor :key
    attr_accessor :gas_limit, :gas_price
    attr_accessor :code, :name, :abi, :class_object, :sender, :deployment, :client
    attr_accessor :events, :functions, :constructor_inputs
    attr_accessor :call_raw_proxy, :call_proxy, :transact_proxy, :transact_and_wait_proxy
    attr_accessor :new_filter_proxy, :get_filter_logs_proxy, :get_filter_change_proxy

    def initialize(name, code, abi, client = Ethereum::Singleton.instance)
      @name = name
      @code = code
      @abi = abi
      @constructor_inputs, @functions, @events = Ethereum::Abi.parse_abi(abi)
      @formatter = Ethereum::Formatter.new
      @client = client
      @sender = client.default_account
      @encoder = Encoder.new
      @decoder = Decoder.new
      @gas_limit = @client.gas_limit
      @gas_price = @client.gas_price
    end

    def self.create(file: nil, client: Ethereum::Singleton.instance, code: nil, abi: nil, address: nil, name: nil)
      contract = nil
      if file.present?
        contracts = Ethereum::Initializer.new(file, client).build_all
        raise "No contracts complied" if contracts.empty?
        contract = contracts.first.class_object.new
      else
        abi = JSON.parse(abi) if abi.is_a? String
        contract = Ethereum::Contract.new(name, code, abi, client)
        contract.build
        contract = contract.class_object.new
      end
      contract.address = address
      contract
    end

    def address=(addr)
      @address = addr
      @events.each do |event|
        event.set_address(addr)
        event.set_client(@client)
      end
    end

    def deploy_payload(params)
      if @constructor_inputs.present?
        raise ArgumentError, "Wrong number of arguments in a constructor" and return if params.length != @constructor_inputs.length
      end
      deploy_arguments = @encoder.encode_arguments(@constructor_inputs, params)
      "0x" + @code + deploy_arguments
    end

    def deploy_args(params)
      add_gas_options_args({from: sender, data: deploy_payload(params)})
    end

    def send_transaction(tx_args)
        @client.eth_send_transaction(tx_args)["result"]
    end

    def send_raw_transaction(payload, to = nil)
      Eth.configure { |c| c.chain_id = @client.get_chain }
      args = { 
        from: key.address,
        value: 0,
        data: payload,
        nonce: @client.get_nonce(key.address),
        gas_limit: gas_limit,
        gas_price: gas_price
      }
      args[:to] = to if to
      tx = Eth::Tx.new(args)
      tx.sign key
      @client.eth_send_raw_transaction(tx.hex)["result"]
    end

    def deploy(*params)
      if key
        tx = send_raw_transaction(deploy_payload(params))
      else
        tx = send_transaction(deploy_args(params))<commit_msg>fix subtle bug with nonce (when emiting two transactions in a row<commit_after>module Ethereum
  class Contract

    attr_reader :address
    attr_accessor :key
    attr_accessor :gas_limit, :gas_price
    attr_accessor :code, :name, :abi, :class_object, :sender, :deployment, :client
    attr_accessor :events, :functions, :constructor_inputs
    attr_accessor :call_raw_proxy, :call_proxy, :transact_proxy, :transact_and_wait_proxy
    attr_accessor :new_filter_proxy, :get_filter_logs_proxy, :get_filter_change_proxy

    def initialize(name, code, abi, client = Ethereum::Singleton.instance)
      @name = name
      @code = code
      @abi = abi
      @constructor_inputs, @functions, @events = Ethereum::Abi.parse_abi(abi)
      @formatter = Ethereum::Formatter.new
      @client = client
      @sender = client.default_account
      @encoder = Encoder.new
      @decoder = Decoder.new
      @gas_limit = @client.gas_limit
      @gas_price = @client.gas_price
    end

    def self.create(file: nil, client: Ethereum::Singleton.instance, code: nil, abi: nil, address: nil, name: nil)
      contract = nil
      if file.present?
        contracts = Ethereum::Initializer.new(file, client).build_all
        raise "No contracts complied" if contracts.empty?
        contract = contracts.first.class_object.new
      else
        abi = JSON.parse(abi) if abi.is_a? String
        contract = Ethereum::Contract.new(name, code, abi, client)
        contract.build
        contract = contract.class_object.new
      end
      contract.address = address
      contract
    end

    def address=(addr)
      @address = addr
      @events.each do |event|
        event.set_address(addr)
        event.set_client(@client)
      end
    end

    def deploy_payload(params)
      if @constructor_inputs.present?
        raise ArgumentError, "Wrong number of arguments in a constructor" and return if params.length != @constructor_inputs.length
      end
      deploy_arguments = @encoder.encode_arguments(@constructor_inputs, params)
      "0x" + @code + deploy_arguments
    end

    def deploy_args(params)
      add_gas_options_args({from: sender, data: deploy_payload(params)})
    end

    def send_transaction(tx_args)
        @client.eth_send_transaction(tx_args)["result"]
    end

    def send_raw_transaction(payload, to = nil)
      Eth.configure { |c| c.chain_id = @client.get_chain }
      @nonce ||= @client.get_nonce(key.address) - 1
      @nonce += 1
      args = { 
        from: key.address,
        value: 0,
        data: payload,
        nonce: @nonce,
        gas_limit: gas_limit,
        gas_price: gas_price
      }
      args[:to] = to if to
      tx = Eth::Tx.new(args)
      tx.sign key
      @client.eth_send_raw_transaction(tx.hex)["result"]
    end

    def deploy(*params)
      if key
        tx = send_raw_transaction(deploy_payload(params))
      else
        tx = send_transaction(deploy_args(params))
{'fraction_non_alphanumeric': 0.09646079111727966, 'fraction_numerical': 0.001040943789035392, 'mean_word_length': 2.8613529805760214, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3677296', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1740, 'n_words': 411}