首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决在Ethereum中部署智能契约时出现的错误?

如何解决在Ethereum中部署智能契约时出现的错误?
EN

Stack Overflow用户
提问于 2018-12-28 03:09:32
回答 2查看 891关注 0票数 1

当试图用solc-js编译智能契约时,我得到了错误。

Krishna:投票给krishnakankipati$ node deploy.js,编译契约assert.js:350抛出错误;

AssertionError ERR_ASSERTION:指定的无效回调。

代码语言:javascript
复制
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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-28 14:58:51

请务必阅读solc v0.5.0+的v0.5.0+,以确保您正在针对Solidity编译器的更改进行调整。

这样的内容应该与solc的最新版本兼容:

代码语言:javascript
复制
// 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+同步

代码语言:javascript
复制
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...
}
票数 1
EN

Stack Overflow用户

发布于 2018-12-28 13:54:13

你没有正确地使用solc。您需要压缩输入,并且传递一个1而不是一个导入回调。在发帖前请阅读文档:https://github.com/ethereum/solc-js

考虑使用etherjs,比web3更好的文档和更强大的功能。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53953260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档