Web3.js: Sending a transaction fails

Created on 29 Jan 2018  路  4Comments  路  Source: ChainSafe/web3.js

_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!

Most helpful comment

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

All 4 comments

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

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

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() :(

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

Was this page helpful?
0 / 5 - 0 ratings