gpt4 book ai didi

javascript - 如何使用 solc 0.5 编译 Solidity

转载 作者:行者123 更新时间:2023-11-30 14:11:19 26 4
gpt4 key购买 nike

编译.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')
);
}

事件.sol:

pragma solidity ^0.5.3;

contract FactoryCampaign {
. . .
}

contract Campaign {
. . .
}

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

solc 0.4 版本允许我直接在“源”上调用 solc.compile,但更高版本会抛出此错误

AssertionError [ERR_ASSERTION]: Invalid callback specified.

最佳答案

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

你会想要使用这样的东西:

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');
}
}

关于javascript - 如何使用 solc 0.5 编译 Solidity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54412333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com