Truffle compile error importing OpenZeppelin Contracts

Hey, i just started using WSL on VS Code.
Before that, 5mins ago i was using normal windows environment.

In both the environments, i am unable to compile my truffle project and it looks like and error in the openzepplin .sol file’s import statements.

Can you help?

2 Likes

Hi @Rahul_Gupta,

Welcome to the community :wave:

I’m sorry that you are having this issue when compiling with Truffle.

I am using WSL2 and wasn’t able to reproduce what you are seeing.

GLDToken.sol

pragma solidity ^0.6.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract GLDToken is ERC20 {
    constructor(uint256 initialSupply) ERC20("Gold", "GLD") public {
        _mint(msg.sender, initialSupply);
    }
}

Truffle compile

$ npx truffle compile

Compiling your contracts...
===========================
> Compiling ./contracts/GLDToken.sol
> Compiling ./contracts/Migrations.sol
> Compiling @openzeppelin/contracts/GSN/Context.sol
> Compiling @openzeppelin/contracts/math/SafeMath.sol
> Compiling @openzeppelin/contracts/token/ERC20/ERC20.sol
> Compiling @openzeppelin/contracts/token/ERC20/IERC20.sol
> Compiling @openzeppelin/contracts/utils/Address.sol
> Artifacts written to /home/abcoathup/projects/forum/rahul_gupta/build/contracts
> Compiled successfully using:
   - solc: 0.6.7+commit.b8d736ae.Emscripten.clang

Project in VSCode

Would you mind running npx truffle version (if truffle installed locally) or truffle version if installed globally.

I have the following versions:

$ npx truffle version
Truffle v5.1.26 (core: 5.1.26)
Solidity - 0.6.7 (solc-js)
Node v10.19.0
Web3.js v1.2.1

How are you running the compile?
I use npx truffle compile.

Hi @Rahul_Gupta,

I wanted to check if you were able to resolve? Please ask all the questions that you need.

Having the same problem. Compiler isn't recognizing imports at all.
<
pragma solidity ^0.5.0;

pragma experimental ABIEncoderV2;

// Import money-legos contracts

import "@studydefi/money-legos/aave/contracts/ILendingPool.sol";

import "@studydefi/money-legos/aave/contracts/IFlashLoanReceiver.sol";

import "@studydefi/money-legos/aave/contracts/FlashloanReceiverBase.sol";

import "@studydefi/money-legos/uniswap/contracts/IUniswapFactory.sol";

import "@studydefi/money-legos/uniswap/contracts/IUniswapExchange.sol";

// Import Openzeppelin contracts

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract FlashloanMoneyLego is FlashLoanReceiverBase {

    address public constant AaveLendingPoolAddressProviderAddress = 0x506B0B2CF20FAA8f38a4E2B524EE43e1f4458Cc5;

    address public constant BAT_ADDRESS = 0x2d12186Fbb9f9a8C28B3FfdD4c42920f8539D738;

    address public constant UNISWAP_FACTORY_A = 0xECc6C0542710a0EF07966D7d1B10fA38bbb86523;

    address public constant UNISWAP_FACTORY_B = 0x54Ac34e5cE84C501165674782582ADce2FDdc8F4;

    IUniswapFactory public uniswapFactoryA;

    IUniswapFactory public uniswapFactoryB;

    IUniswapExchange public exchangeAforLoanAsset;

    IUniswapExchange public exchangeBforLoanAsset;

    IUniswapExchange public exchangeAforBAT;

    IUniswapExchange public exchangeBforBAT;

    constructor() public {

      // Override the addressesProvider in the base class to use Kovan for testing

      FlashLoanReceiverBase.addressesProvider = ILendingPoolAddressesProvider(

        AaveLendingPoolAddressProviderAddress

      );

      // Instantiate Uniswap Factory A & B

      uniswapFactoryA = IUniswapFactory(UNISWAP_FACTORY_A);

      uniswapFactoryB = IUniswapFactory(UNISWAP_FACTORY_B);

      // get Exchange B Address

      address addressForBATExchangeA = uniswapFactoryA.getExchange(BAT_ADDRESS);

      address addressForBATExchangeB = uniswapFactoryB.getExchange(BAT_ADDRESS);

      // Instantiate Exchange B for BAT Token swaps

      exchangeAforBAT = IUniswapExchange(addressForBATExchangeA);

      exchangeBforBAT = IUniswapExchange(addressForBATExchangeB);

    }

    function executeOperation(

        address _reserve,

        uint _amount,

        uint _fee,

        bytes calldata _params

    ) external {

        require(_amount <= getBalanceInternal(address(this), _reserve), "Invalid balance, was the flashLoan successful?");

        address RESERVE_ADDRESS = _reserve;

        uint256 deadline = now + 3000;

        // get Exchange Address for the reserve asset

        address addressForLoanAssetExchangeA = uniswapFactoryA.getExchange(RESERVE_ADDRESS);

        address addressForLoanAssetExchangeB = uniswapFactoryB.getExchange(RESERVE_ADDRESS);

        // Instantiate Exchange A

        exchangeAforLoanAsset = IUniswapExchange(addressForLoanAssetExchangeA);

        exchangeBforLoanAsset = IUniswapExchange(addressForLoanAssetExchangeB);

        IERC20 loan = IERC20(RESERVE_ADDRESS);

        IERC20 bat = IERC20(BAT_ADDRESS);

        // Swap the reserve asset (e.g. DAI) for BAT

        require(loan.approve(address(exchangeBforLoanAsset), _amount), "Could not approve reserve asset sell");

        uint256 batPurchased = exchangeBforLoanAsset.tokenToTokenSwapInput(

            _amount,

            1,

            1,

            deadline,

            BAT_ADDRESS

        );

        require(bat.approve(address(exchangeAforBAT), batPurchased), "Could not approve BAT asset sell");

        // Swap BAT back to the reserve asset (e.g. DAIs)

        uint256 reserveAssetPurchased = exchangeAforBAT.tokenToTokenSwapInput(

            batPurchased,

            1,

            1,

            deadline,

            RESERVE_ADDRESS

        );

        uint amount = _amount;

        uint totalDebt = amount.add(_fee);

        require(reserveAssetPurchased > totalDebt, "There is no profit! Reverting!");

        transferFundsBackToPoolInternal(RESERVE_ADDRESS, amount.add(_fee));

    }

    // Entry point for flashloan

    function initateFlashLoan(

        address assetToFlashLoan,

        uint amountToLoan

    ) external {

        bytes memory data = "";

        // Get Aave lending pool

        ILendingPool lendingPool = ILendingPool(addressesProvider.getLendingPool());

        IERC20 loan = IERC20(assetToFlashLoan);

        // Ask for a flashloan

        lendingPool.flashLoan(

            address(this),

            assetToFlashLoan,

            amountToLoan,

            data

        );

        // If there is still a balance of the loan asset then this is profit to be returned to sender!

        uint256 profit = loan.balanceOf(address(this));

        require(loan.transfer(msg.sender, profit), "Could not transfer back the profit");

    }

}

Fixed it with relative link:
‘import “…/node_modules/@studydefi/money-legos/aave/contracts/ILendingPool.sol”;’
Probably not best practice but it’ll do.

2 Likes