web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.18k stars 4.91k forks source link

Sending a transaction fails #1335

Closed woniesong92 closed 6 years ago

woniesong92 commented 6 years ago

x-posting from https://stackoverflow.com/questions/48498958/sending-a-transaction-fails

I am using web3.js v1.0 with solidity ^0.4.17 with Ganache v1.1.0. I am trying to call a send transaction and it fails with the error message below.

Returned error: Error: Error: [ethjs-query] while formatting outputs from RPC 'undefined' for method 'sendRawTransaction' Error: [ethjs-format] hex string 'undefined' must be an alphanumeric 66 utf8 byte hex (chars: a-fA-F) string, is 0 bytes

MyContract.sol

  function createStarCard(string name, uint price) public {
    require(msg.sender == owner);

    uint starCardId = starCards.push(StarCard(name, price));
    starCardIdToOwner[starCardId] = owner;
  }

App.js

  createStarCard = ({ name, price }) => {
    window.web3.eth.getAccounts().then((accounts) => {
      this.state.ContractInstance.methods.createStarCard(name, price).send({
        from: accounts[0],
        gas: 300000,
      }).then((receipt) => {
        console.log(receipt)
      }).catch((err) => {
        console.log(err.message) <-- Caught error message
      })
    })
  }

Google search results with the error message pointed me to the following issues, but they weren't helpful in my case:

Any help will be appreciated. Feel free to close the issue if it isn't relevant to web3. The contract's function works fine in truffle console so I thought I should try here for help. Thank you!

drewstone commented 6 years ago

I am getting this same error using truffle-contract with MetaMask and Ganache for a local network.

EDIT: In addition to this, I have gotten my logic working on Ropsten Testnet with MetaMask. This seems to be a problem with Ganache

woniesong92 commented 6 years ago

@drewstone It looks like the problem is with the Ganache mac app. I solved this by using ganache-cli instead

pakaplace commented 6 years ago

Shouldn't it default to the sendTransaction()method for payable truffle contract functions?

During testing (with Ganache) it sometimes defaults to sendTransaction() and othertimes to sendRawTransaction() :(

pakaplace commented 6 years ago

Resetting my Ganache accounts in Metamask worked for me https://github.com/trufflesuite/ganache/issues/333. Go to Metamask sidebar >> settings >> reset account