Node.js 节点的智能协定部署不工作
我有这样的问题。我是区块链开发新手,我使用solidity创建了一个智能合约。为了编译和部署它,我创建了compile.js和deploy.js文件 这是我的档案Node.js 节点的智能协定部署不工作,node.js,solidity,Node.js,Solidity,我有这样的问题。我是区块链开发新手,我使用solidity创建了一个智能合约。为了编译和部署它,我创建了compile.js和deploy.js文件 这是我的档案 pragma solidity 0.4.20; contract Election { // Model a Candidate struct Candidate { uint id; string name; uint voteCount; } //
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);
}
}
这是我的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
有人能帮我解决这个问题吗?我搜索了很多,但找不到合适的解决方案。谢谢。我也犯了同样的错误。我使用了solc.compilesource,1.Adam建议的错误,并在合同中发现了一个拼写错误。你应该试试。你也可以发布你的election.sol文件吗?看起来问题可能出在一个旁注中,我建议您开始使用Truffle framework和Infura。@Paul我已经用Election.sol文件更新了问题。我猜您遇到了编译错误,但没有意识到。契约本身没有问题,但您可能试图使用不匹配的solc编译器版本。不要直接导出编译结果,而是执行solc.compilesource,1.errors的调试输出。@AdamKipnis是的,存在版本不匹配错误。谢谢
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