[英]Smart contract deployment with node is not working
我有這樣的問題。 我是區塊鏈開發的新手,我已經通過使用穩定性創建了智能合約。 為了進行編譯和部署,我創建了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
有人可以幫我解決這個問題嗎? 我進行了很多搜索,但找不到適合的解決方案。 謝謝。
我遇到了同樣的錯誤。 我按照亞當的建議使用了solc.compile(source, 1).errors
,並在合同中發現了一個錯字。 您應該嘗試一下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.