![](/img/trans.png)
[英]Getting error when compiling and deploying ethereum smart contract with Node.js
[英]How to solve the error when deploying a smart contract in Ethereum?
當嘗試使用solc-js編譯智能合約時,我收到了錯誤
Krishna:投票krishnakankipati $ node deploy.js編譯合同assert.js:350 throw err; ^
AssertionError [ERR_ASSERTION]:指定了無效的回調。
let compilerInput = {
'Voter': fs.readFileSync('Voter.sol', 'utf8')
};
console.log('Compiling the contract')
// Compile and optimize the contract
let compiledContract = solc.compile(compilerInput, 1);
// Get compiled contract
let contract = compiledContract.contracts['Voter:Voter'] // Voter contract from Voter file.
// Save contract's ABI
let abi = contract.interface;
fs.writeFileSync('abi.json', abi);
您沒有正確使用solc-js。 您需要對輸入進行字符串化,然后傳遞1而不是導入回調。 請在發布問題之前閱讀文檔: https : //github.com/ethereum/solc-js
考慮使用etherjs,更好的文檔和比web3更強大。
請務必閱讀solc v0.5.0 +的solc文檔 ,以確保您正在調整Solidity編譯器的更改。
這樣的東西應該與最新版本的solc兼容:
// Note: You should be defining your contract sources as objects now.
// Note: You must also provide the compiler output selection as well.
const compilerInput = {
language: "Solidity",
sources: {
'Voter': { content: fs.readFileSync('Voter.sol', 'utf8') }
},
settings: {
outputSelection: {
"*": {
"*": [ "abi", "evm.bytecode" ]
}
}
}
};
console.log('Compiling the contract')
// Note: You have to pass the input in with JSON.stringify now.
const compiledContract = JSON.parse(solc.compile(JSON.stringify(compilerInput)));
if(compiledContract.errors) {
compiledContract.errors.forEach(err => console.log(err.formattedMessage));
}
// Note: This changed slightly since I'm using JSON.parse above.
const contract = compiledContract.contracts['Voter'].Voter; // Voter contract from Voter file.
// Note: This is now called 'abi' and not 'interface'
const abi = contract.abi;
fs.writeFileSync('abi.json', JSON.stringify(abi, null, 2));
您還需要更新deployContract
函數以與solc v0.5.0 +同步
async function deployContract(web3, contract, sender) {
let Voter = new web3.eth.Contract(JSON.parse(JSON.stringify(abi)));
let bytecode = '0x' + contract.evm.bytecode.object;
let gasEstimate = await web3.eth.estimateGas({data: bytecode});
// The rest should work fine...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.