EthWorks/ethereum.rb

send_raw_transaction throws error when key is a string

Closed this issue · 2 comments

Hello, I'm trying call the transact_and_wait method, but I am getting an error

Here is the code for sending the transaction:

require 'eth'
require 'ethereum'
require 'concurrent'

class BlockChain
 def initialize
   @client = Ethereum::HttpClient.new("https://ropsten.infura.io/v3/*****")
   @crowdsaleInstance = Ethereum::Contract.create(name: "GNITokenCrowdsale", truffle: { paths: [ 'truffle' ] }, client: @client)
 end

 def distribute_votes (private_key, vote_transactions)
   set_key(private_key)
   transaction = vote_transactions.first
   debugger
   @crowdsaleInstance.transact_and_wait.vote_for_project
 end

 private

 def set_key(private_key)
   key = Eth::Key.new(priv: private_key)
   @crowdsaleInstance.key = key.private_hex
 end
end

and here is the error I'm getting from the rails server:

NoMethodError - undefined method `address' for #<String:0x007fd57884b688>:
 app/blockchain/BlockChain.rb:15:in `distribute_votes'
 app/controllers/api/block_chain_controller.rb:5:in `update'

I believe that this error is stemming from this function in the Contract class when `key.address' is called:

def send_raw_transaction(payload, to = nil)
      Eth.configure { |c| c.chain_id = @client.net_version["result"].to_i }
      @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

If I cannot pass a string, what is the correct way to pass an address?

Thanks!

figured out the issue. I should not be setting my key to the public_hex, but rather the entire key object.

@stvtortora
Thanks for the issue. Please migrate as we will maintain the eth.rb library here. ;)