Solidity Compiler Error – File Import Callback Not Supported

compilersolcsolidity

As a context I am trying to fork uniswap v2 contracts using my own compiler and deploy files. I am not using truffle, as I want to deeply understand before using a framework.

When I run my compiler, I get this error :

  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',

I tried to copy all contracts into the contract that I was trying to deploy, and it worked. But I can't do this for every contract …

Here is my compiler file :
`


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"); 

I am using solc@0.5.16

And here are the files I am trying to deploy : https://github.com/Uniswap/uniswap-v2-core/tree/master/contracts

Best Answer

Here is the code that I wrote to compile files with import statements with solc@0.5.16. The script compile the files and put them into a build folder as a json file.

Thanks for helping me making this !

"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"]);
Related Topic