我有这样的问题。我是区块链开发的新手,我已经通过使用稳定性创建了智能合约。为了进行编译和部署,我创建了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
有人可以帮我解决这个问题吗?我进行了很多搜索,但找不到适合的解决方案。谢谢。
答案 0 :(得分:1)
我遇到了同样的错误。我按照亚当的建议使用solc.compile(source, 1).errors
,并在合同中发现了错字。您应该尝试一下。