如何使用SOLC 0.5编译固体 [英] How to compile solidity using solc 0.5

查看:193
本文介绍了如何使用SOLC 0.5编译固体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

compile.js:

compile.js :

const path = require('path');
const solc = require('solc');
const fs = require('fs-extra');

const buildPath = path.resolve(__dirname, 'build');
fs.removeSync(buildPath);

const campaignPath = path.resolve(__dirname, 'contracts', 'Campaign.sol');
const source = fs.readFileSync(campaignPath, 'utf8');

var input = {
    language: 'Solidity',
    sources: {
        'Campaign.sol': {
            content: source
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': [ '*' ]
            }
        }
    }
}

const output = solc.compile(input, 1).contracts;

fs.ensureDirSync(buildPath);

for(let contract in output){
    fs.outputJSONSync(
        path.resolve(buildPath, contract+'.json')
    );
}

Campaign.sol:

Campaign.sol :

pragma solidity ^0.5.3;

contract FactoryCampaign {
    . . .
}

contract Campaign {
    . . . 
}

Solidity在混音编辑器中完美运行,而solc版本为0.5.3

The Solidity works perfectly in remix editor and the solc version is 0.5.3

SOLC版本0.4允许我直接在源上调用solc.compile,但后来的版本抛出此错误

The solc version 0.4 allowed me to call solc.compile on the 'source' directly but the later versions throw this error


AssertionError [ERR_ASSERTION]:指定了无效的回调。

AssertionError [ERR_ASSERTION]: Invalid callback specified.


推荐答案

使用Solidity编译器版本> = 0.5.0时,调用 solc.compile 的语法已更改。

With Solidity compiler version >= 0.5.0, the syntax has changed for calling solc.compile.

您将要使用类似这样的东西:

You'll want to use something like this:

const buildPath = path.resolve(__dirname, 'build');
const output = JSON.parse(solc.compile(JSON.stringify(input)));

if(output.errors) {
    output.errors.forEach(err => {
        console.log(err.formattedMessage);
    });
} else {
    const contracts = output.contracts["Campaign.sol"];
    for (let contractName in contracts) {
        const contract = contracts[contractName];
        fs.writeFileSync(path.resolve(buildPath, `${contractName}.json`), JSON.stringify(contract.abi, null, 2), 'utf8');
    }
}

这篇关于如何使用SOLC 0.5编译固体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆