以太坊部署智能合约报错如何解决?
How to solve the error when deploying a smart contract in Ethereum?
尝试使用 solc-js 编译智能合约时出现错误
Krishna:投票 krishnakankipati$ 节点 deploy.js
编译合约
assert.js:350
抛出错误; ^
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 docs 以确保您正在针对 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...
}
尝试使用 solc-js 编译智能合约时出现错误
Krishna:投票 krishnakankipati$ 节点 deploy.js
编译合约
assert.js:350
抛出错误; ^
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 docs 以确保您正在针对 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...
}