简体   繁体   English

在以太坊中部署智能合约时如何解决错误?

[英]How to solve the error when deploying a smart contract in Ethereum?

When trying to compile the smart contract with solc-js I was getting the error 当尝试使用solc-js编译智能合约时,我收到了错误

Krishna:Voting krishnakankipati$ node deploy.js Compiling the contract assert.js:350 throw err; Krishna:投票krishnakankipati $ node deploy.js编译合同assert.js:350 throw err; ^ ^
AssertionError [ERR_ASSERTION]: Invalid callback specified. 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);

You aren't using solc-js correctly. 您没有正确使用solc-js。 You need to stringify the input, and you're passing a 1 instead of an import callback. 您需要对输入进行字符串化,然后传递1而不是导入回调。 Please read the docs before posting questions: https://github.com/ethereum/solc-js 请在发布问题之前阅读文档: https//github.com/ethereum/solc-js

Consider using etherjs, much better documentation and more robust than web3. 考虑使用etherjs,更好的文档和比web3更强大。

Please be sure to read the solc docs for solc v0.5.0+ to ensure you're adjusting for the changes to the Solidity compiler. 请务必阅读solc v0.5.0 +的solc文档 ,以确保您正在调整Solidity编译器的更改。

Something like this should be compatible with the latest version of solc: 这样的东西应该与最新版本的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));

You'll also need to update your deployContract function to be in sync with solc v0.5.0+ 您还需要更新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.

相关问题 使用 Node.js 编译和部署以太坊智能合约时出错 - Getting error when compiling and deploying ethereum smart contract with Node.js 使用eosjs部署EOS智能合约时如何解决'missing setcode.vmtype (type=uint8)'错误? - How to solve 'missing setcode.vmtype (type=uint8)' error when deploying EOS Smart Contract using eosjs? ethereum remix 中的简单智能合约 - ParserError: Expected ';' - Simple smart contract in ethereum remix - ParserError: Expected ';' Node JS 与以太坊智能合约交互 - Node JS Interacting with Ethereum Smart Contract 通过 Hardhat 将智能合约部署到孟买 tes.net 时出错 - Error while deploying a smart contract to Mumbai testnet through Hardhat 部署智能合约时获取“未定义”的参数数量无效 - Getting Invalid number of parameters for “undefined” when deploying smart contract 如何使用 nodejs 上的 Web3js 解码在以太坊上调用智能合约的响应 - How to decode response from calling smart contract on Ethereum using Web3js on nodejs 调用智能合约方法时如何摆脱 web3 上的 504 超时错误? - How can I get rid of 504 timeout error on web3 when calling a smart contract method? 部署到 heroku 时如何解决 Node 和 reactapp 错误? - How to solve Node and reactapp error when deploying to heroku? 使用web3.js部署时如何获取智能合约地址 - How to get Smart Contract address when it is deployed with web3.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM