使用ganache从已部署合同的nodejs脚本调用函数。给出未定义的地址错误

时间:2019-04-08 11:51:34

标签: node.js blockchain ethereum web3 truffle

我正在尝试在区块链中插入一个条目(createEntry)。我创建了一个名为BaseApp.sol的可靠性文件,并使用松露对其进行了部署。现在正在尝试使用节点try.js文件访问它,以在区块链中插入一个条目。我无法将其插入区块链。

这是我尝试访问的try.js文件 它给出了错误

const Web3 = require('web3');
const TruffleContract = require('truffle-contract');
const BaseApp = require('./build/contracts/BaseApp.json');
const web3Provider = new Web3.providers.HttpProvider("http://127.0.0.1:7545");


(async () => {

    const web3 = new Web3(web3Provider);
    account = web3.eth.accounts[0]
    console.log(web3.eth.accounts[0])
    const MetaCoin = TruffleContract(BaseApp);
    MetaCoin.setProvider(web3Provider);
    const instance = await MetaCoin.deployed();
    console.log('instance.address' + instance.address)

        const cst = "bce"
        const content = "gfdgdggdfgdg"
        const content2 = "sdfdsf"
        console.log(cst,content,content2)
        await instance.createEntry(cst,content,content2)

})();

输出: 0x907c2bb97af366a5ac5cca69decb62574707a01b instance.address0xa4E98bE9022c009d3d77f853fA402cBa9Af515D7 bce gfdgdggdfgdg sdfdsf (节点:34180)UnhandledPromiseRejectionWarning:错误:无效的地址     在inputAddressFormatter(D:\ project \ tryal \ node_modules \ truffle-contract \ node_modules \ web3 \ lib \ web3 \ formatters.js:274:11)

这是BaseApp.sol文件

pragma solidity ^0.5.0;

contract BaseApp {
  uint public taskCount = 0;

  struct Entry {
    uint id;
    string userid;
    string data1;
    string data2;
  }

  mapping(uint => Entry) public Entries;

  event EntryCreated(
    uint id,
    string userid,
    string data1,
    string data2
  );


  constructor() public {
    createEntry("abc","xyz","ddd");
  }

  function createEntry(string memory _userid,string memory _data1,string memory _data2) public {
    taskCount ++;
    Entries[taskCount] = Entry(taskCount, _userid, _data1, _data2);
    emit EntryCreated(taskCount, _userid, _data1, _data2);
  }

}

0 个答案:

没有答案