Solidity

Solc 編譯器:不支持文件導入回調

  • October 2, 2021

作為上下文,我正在嘗試使用我自己的編譯器和部署文件來分叉 uniswap v2 合約。我沒有使用松露,因為我想在使用框架之前深入了解。

當我執行我的編譯器時,我得到這個錯誤:

 formattedMessage: 'Contract:3:1: ParserError: Source "interfaces/IUniswapV2Factory.sol" not found: File import callback not supported\n' +
   "import './interfaces/IUniswapV2Factory.sol';\r\n" +
   '^------------------------------------------^\n',
 message: 'Source "interfaces/IUniswapV2Factory.sol" not found: File import callback not supported',

我試圖將所有契約複製到我試圖部署的契約中,並且它有效。但我不能對每份契約都這樣做……

這是我的編譯器文件:`


const path = require("path");
const solc = require("solc"); //don't forget to install the right solc version !
const fs = require("fs-extra");

const buildPath = path.resolve(__dirname, "build");

fs.removeSync(buildPath);

function compileContract(Contract) {
   const contractPath = path.resolve(__dirname, "./", Contract);
   
   const contractSourceCode = fs.readFileSync(contractPath, "utf8");
   
   fs.ensureDirSync(buildPath);

   var input = {
       language: 'Solidity',
       sources: {
           Contract : {
               content: contractSourceCode
           }
       },
       settings: {
           optimizer: {
               enabled: true
           },
           outputSelection: {
               '*': {
                   '*': [ '*' ]
               }
           }
       }
   };

   const output = JSON.parse(solc.compile(JSON.stringify(input)));
   console.log(output);

   for(let contractName in output.contracts.Contract) {
       fs.outputJsonSync(
           path.resolve(buildPath, `${contractName}.json`),
           output.contracts.Contract[contractName]
       );
   }
   
}

// compileContract("UniswapV2Pair.sol");
compileContract("UniswapV2Factory.sol");
compileContract("UniswapV2ERC20.sol"); 

我正在使用 solc@0.5.16

這是我要部署的文件:https ://github.com/Uniswap/uniswap-v2-core/tree/master/contracts

這是我編寫的使用 solc@0.5.16 編譯帶有 import 語句的文件的程式碼。該腳本編譯文件並將它們作為 json 文件放入建構文件夾中。

謝謝你幫我做這個!

"use-strict";

const path = require("path");
const solc = require("solc"); //don"t forget to install the right solc version !
const fs = require("fs-extra");

// interfaces folder
const IERC20SourceCode = fs.readFileSync("./interfaces/IERC20.sol");
const IUniswapV2CalleeSourceCode = fs.readFileSync("./interfaces/IUniswapV2Callee.sol");
const IUniswapV2ERC20SourceCode = fs.readFileSync("./interfaces/IUniswapV2ERC20.sol");
const IUniswapV2FactorySourceCode = fs.readFileSync("./interfaces/IUniswapV2Factory.sol");
const IUniswapV2PairSourceCode = fs.readFileSync("./interfaces/IUniswapV2Pair.sol");

// libraries folder
const MathSourceCode = fs.readFileSync("./libraries/Math.sol");
const UQ112x112SourceCode = fs.readFileSync("./libraries/UQ112x112.sol");
const SafeMathSourceCode = fs.readFileSync("./libraries/SafeMath.sol");

// core folder
const UniswapV2PairSourceCode = fs.readFileSync("./UniswapV2Pair.sol");
const UniswapV2ERC20SourceCode = fs.readFileSync("./UniswapV2ERC20.sol");
const UniswapV2FactorySourceCode = fs.readFileSync("./UniswapV2Factory.sol");

const buildPath = path.resolve(__dirname, "build");

fs.removeSync(buildPath);

function compileContract(Contract) {
   const contractPath = path.resolve(__dirname, ...Contract);  
   
   const contractSourceCode = fs.readFileSync(contractPath, "utf8");
   
   fs.ensureDirSync(buildPath);

   var input = {
       language: "Solidity",
       sources: {
           Contract: {
               content: contractSourceCode
           }
       },
       settings: {
           optimizer: {
               enabled: true
           },
           outputSelection: {
               "*": {
                   "*": [ "*" ]
               }
           }
       }
   };

   function findImports(path) {
       if (path === "interfaces/IUniswapV2Factory.sol") return { contents: `${IUniswapV2FactorySourceCode}` };
       if (path === "interfaces/IERC20.sol") return { contents: `${IERC20SourceCode}` };
       if (path === "interfaces/IUniswapV2Callee.sol") return { contents: `${IUniswapV2CalleeSourceCode}` };
       if (path === "interfaces/IUniswapV2ERC20.sol") return { contents: `${IUniswapV2ERC20SourceCode}` };
       if (path === "interfaces/IUniswapV2Pair.sol") return { contents: `${IUniswapV2PairSourceCode}` };
       if (path === "libraries/Math.sol") return { contents: `${MathSourceCode}` };
       if (path === "libraries/UQ112x112.sol") return { contents: `${UQ112x112SourceCode}` };
       if (path === "libraries/SafeMath.sol") return { contents: `${SafeMathSourceCode}` };
       if (path === "UniswapV2Pair.sol") return { contents: `${UniswapV2PairSourceCode}` };
       if (path === "UniswapV2ERC20.sol") return { contents: `${UniswapV2ERC20SourceCode}` };
       if (path === "UniswapV2Factory.sol") return { contents: `${UniswapV2FactorySourceCode}` };
       else return { error: "File not found" };
     }

   let output = JSON.parse(solc.compile(JSON.stringify(input), { import: findImports }));

   for(let contractName in output.contracts.Contract) {
       fs.outputJsonSync(
           path.resolve(buildPath, `${contractName}.json`),
           output.contracts.Contract[contractName]
       );
   }    
}

compileContract(["./", "UniswapV2Factory.sol"]);
compileContract(["./", "UniswapV2Pair.sol"]);
compileContract(["./", "UniswapV2ERC20.sol"]);
compileContract(["./", "libraries", "Math.sol"]);
compileContract(["./", "libraries", "SafeMath.sol"]);
compileContract(["./", "libraries", "UQ112x112.sol"]);
compileContract(["./", "interfaces", "IUniswapV2Factory.sol"]);
compileContract(["./", "interfaces", "IERC20.sol"]);
compileContract(["./", "interfaces", "IUniswapV2Callee.sol"]);
compileContract(["./", "interfaces", "IUniswapV2ERC20.sol"]);
compileContract(["./", "interfaces", "IUniswapV2Pair.sol"]);

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