繁体   English   中英

使用 Node.js 编译和部署以太坊智能合约时出错

[英]Getting error when compiling and deploying ethereum smart contract with Node.js

我正在尝试使用 Node.js 脚本编译和部署智能合约到 Rinkeby。 这是我在 Pluralsight 关注的课程内容。 它在那里工作,但对我不起作用。 错误是什么?

注意:我有一个 solc 编译器 0.7.0。 当我将pragma solidity更改为 0.7.0 并进行所有适当的更改时,错误不会改变。

node deploy.js
Compiling the contract
assert.js:350
    throw err;
    ^

AssertionError [ERR_ASSERTION]: Invalid callback object specified.
    at runWithCallbacks (C:\COMP_DEV\blockchain\module_5\node_modules\solc\wrapper.js:97:7)
    at compileStandard (C:\COMP_DEV\blockchain\module_5\node_modules\solc\wrapper.js:207:14)
    at Object.compileStandardWrapper [as compile] (C:\COMP_DEV\blockchain\module_5\node_modules\solc\wrapper.js:214:14)
    at compileContract (C:\COMP_DEV\blockchain\module_5\deploy.js:24:33)
    at Object.<anonymous> (C:\COMP_DEV\blockchain\module_5\deploy.js:5:16)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

合同是这样的:

pragma solidity ^0.4.0;

contract Voter {

    struct OptionPos {
        uint pos;
        bool exists;
    }

    uint[] public votes;
    mapping (address => bool) hasVoted;
    mapping (string => OptionPos) posOfOption;
    string[] public options;
    bool votingStarted;

    function addOption(string option) public {
        require(!votingStarted);
        options.push(option);
    }

    function startVoting() public {
        require(!votingStarted);
        votes.length = options.length;

        for (uint i = 0; i < options.length; i++) {
            OptionPos memory option = OptionPos(i, true);
            posOfOption[options[i]] = option;
        }
        votingStarted = true;
    }

    function vote(uint option) public {
        require(0 <= option && option < options.length);
        require(!hasVoted[msg.sender]);

        hasVoted[msg.sender] = true;
        votes[option] = votes[option] + 1;
    }

    function vote(string option) public {
        require(!hasVoted[msg.sender]);
        OptionPos memory optionPos = posOfOption[option];
        require(optionPos.exists);

        hasVoted[msg.sender] = true;
        votes[optionPos.pos] = votes[optionPos.pos]++;
    }

    function getVotes() public view returns (uint[]) {
        return votes;
    }
}

部署 js 脚本是这样的:

let fs = require('fs');
let solc = require('solc')
let Web3 = require('web3');

let contract = compileContract();
let web3 = createWeb3();
let sender = "0xc3884e4e8e8ee58a7262a6c4910794e55513616a";


deployContract(web3, contract, sender)
    .then(function () {
        console.log('Deployment finished')
    })
    .catch(function (error) {
        console.log(`Failed to deploy contract: ${error}`)
    })

function compileContract() {
    let compilerInput = {
        'Voter': fs.readFileSync('Voter.sol', 'utf8')
    };

    console.log('Compiling the contract')
    // Compile and optimize the contract
    let compiledContract = solc.compile({sources: compilerInput}, 1);
    // Get compiled contract
    let contract = compiledContract.contracts['Voter:Voter']

    // Save contract's ABI
    let abi = contract.interface;
    fs.writeFileSync('abi.json', abi);

    return contract;
}

function createWeb3() {
    let web3 = new Web3();
    web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));

    return web3;
}

async function deployContract(web3, contract, sender) {
    let Voter = new web3.eth.Contract(JSON.parse(contract.interface));
    let bytecode = '0x' + contract.bytecode;
    let gasEstimate = await web3.eth.estimateGas({data: bytecode});

    console.log('Deploying the contract');
    const contractInstance = await Voter.deploy({
        data: bytecode
    })
    .send({
        from: sender,
        gas: gasEstimate
    })
    .on('transactionHash', function(transactionHash) {
        console.log(`Transaction hash: ${transactionHash}`);
    })
    .on('confirmation', function(confirmationNumber, receipt) {
        console.log(`Confirmation number: ${confirmationNumber}`);
    })

    console.log(`Contract address: ${contractInstance.options.address}`);
}

我通过将编译器版本与代码版本匹配来修复它......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM