简体   繁体   中英

Smart contract deployment with node is not working

I have a problem like this. I am new to blockchain development and I have created a Smart contract using solidity. To compile it and to deploy it I have created a compile.js and deploy.js file.

This is my Solidity file.

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);
    }
}

This is my compile.js file.

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'];

This is my deploy.js file.

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

When I hit node deploy.js in the command prompt it gives me an error like this.

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

Can someone help me to solve this problem? I search a lot but I was unable to find a suitable solution for this. Thank you.

I got the same error. I used solc.compile(source, 1).errors as Adam suggested and found a typo in the contract. You should try that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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