首页 > 解决方案 > 使用节点的智能合约部署不起作用

问题描述

我有这样的问题。我是区块链开发的新手,我使用solidity 创建了一个智能合约。为了编译和部署它,我创建了一个 compile.js 和 deploy.js 文件。

这是我的 Solidity 文件。

pragma solidity 0.4.20;

contract Election {
    // Model a Candidate
    struct Candidate {
        uint id;
        string name;
        uint voteCount;
    }

    // Store accounts that have voted
    mapping(address => bool) public voters;
    // Store Candidates
    // Fetch Candidate
    mapping(uint => Candidate) public candidates;
    // Store Candidates Count
    uint public candidatesCount;

    // voted event
    event votedEvent (
        uint indexed _candidateId
    );

    function Election () public {
        addCandidate("Candidate 1");
        addCandidate("Candidate 2");
    }

    function addCandidate (string _name) private {
        candidatesCount ++;
        candidates[candidatesCount] = Candidate(candidatesCount, _name, 0);
    }

    function vote (uint _candidateId) public {
        // require that they haven't voted before
        require(!voters[msg.sender]);

        // require a valid candidate
        require(_candidateId > 0 && _candidateId <= candidatesCount);

        // record that voter has voted
        voters[msg.sender] = true;

        // update candidate vote Count
        candidates[_candidateId].voteCount ++;

        // trigger voted event
        votedEvent(_candidateId);
    }
}

这是我的 compile.js 文件。

const path = require('path');
const fs =require('fs');
const solc = require('solc');

const electionPath= path.resolve(__dirname,'contracts','Election.sol');
const source = fs.readFileSync(electionPath,'utf8');
module.exports = solc.compile(source,1).contracts[':Election'];

这是我的 deploy.js 文件。

const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require('web3');
const { interface , bytecode } = require('./compile');

const provider = new HDWalletProvider(
    'retire embark gravity flight also ceiling dinr wine example slender armor rate',
    'https://rinkeby.infura.io/v3/mykey'
);

const web3 = new Web3(provider);

const deploy = async () => {
const accounts = await web3.eth.getAccounts();

console.log('Attempting to deploy from account',accounts[0]);
const result = await new web3.eth.Contract(JSON.parse(interface))
.deploy({data:bytecode})
.send({gas:'1000000',from :accounts[0]});

console.log( interface );
console.log( 'contract deploy to', result.options.address);

};

deploy();

当我在命令提示符下点击 node deploy.js 时,它给了我这样的错误。

TypeError: Cannot destructure property `interface` of 'undefined' or 'null'.
    at Object.<anonymous> (C:\Users\tharindusa\Desktop\election\deploy.js:3:34)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

有人可以帮我解决这个问题吗?我搜索了很多,但找不到合适的解决方案。谢谢你。

标签: node.jssolidity

解决方案


我得到了同样的错误。我solc.compile(source, 1).errors按照亚当的建议使用,发现合同中有错字。你应该试试。


推荐阅读