Solidity

如何使用 Solidity solc 編譯導入 B.sol 文件的 A.sol?

  • January 31, 2022

我知道如何通過“npm install –save solc”安裝 solc,然後使用 solc.compile 編譯一個 sol 文件。但是現在我有一個導入 B.sol 文件的 A.sol 文件,當我執行 node compile.js 時,我的終端沒有錯誤,但沒有生成 json 文件!

但是當我使用相同的 compile.js 編譯 B.sol 時,它可以工作。為什麼???如何在 Solidity sol 文件中修復此導入功能???謝謝你。

const pathA = path.resolve(__dirname, 'contracts', 'A.sol');
//to find the path of A.sol inside the folder 'contract' in your project
const pathB = path.resolve(__dirname, 'contracts', 'B.sol');
const solA = fs.readFileSync(pathA, 'utf8');
const solB = fs.readFileSync(pathB, 'utf8');

const input = {
 sources: {
   'A.sol': solA,
   'B.sol': solB
 }
};
console.log(solc.compile(input, 1));

可以將它們添加為源,但如果使用 solcjs,還要添加導入回調。

https://github.com/ethereum/solc-js/issues/114#issuecomment-354752466

引用自:https://ethereum.stackexchange.com/questions/46043