发送交易失败

Sending a transaction fails

我正在使用 web3.js v1.0 with solidity ^0.4.17 和 Ganache v1.1.0。我正在尝试调用发送事务,但失败并显示以下错误消息。

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 带有错误消息的搜索结果向我指出了以下问题,但它们对我的情况没有帮助:

更新:分享我的 App.js

的构造函数
  constructor(props) {
    super(props)

    if (typeof window.web3 !== 'undefined') {
      this.web3Provider = window.web3.currentProvider;
    } else {
      console.log("Use Ganache web3")
      this.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
    }

    window.web3 = new Web3(this.web3Provider);

    const contractAddress = "0x1bdaf0cd259887258bc13a92c0a6da92698644c0"
    const ContractInstance = new window.web3.eth.Contract(Abi, contractAddress);

    this.state = {
      ContractInstance,
    }
  }

问题似乎出在 Ganache mac 应用程序上。我通过使用 ganache-cli 解决了这个问题。

我通过重新安装 Metamask 解决了这个问题。