Truffle Flatten in Solidity 0.8.0

Hey - I'm having trouble using truffle flatten. It's throwing error

/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol for extracting its imports: ParserError: missing ';' at '{' (158:18) NOTE: Please make sure to run npm install in the truffle project dir.

Here's the contract I'm trying to flatten.

// contracts/Test.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

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

contract Test is ERC20, Ownable {
    address public admin;
    constructor() ERC20("Testcoin", "TST") {
        _mint(msg.sender, 100000000000 * 10 ** 18);
        admin = msg.sender;
    }

    function burn(uint amount) private {
        require(msg.sender == admin, 'only admin');
        _burn(msg.sender, amount);
        
    }
}

:computer: Environment

Truffle v5.4.3 (core: 5.4.3)
Solidity - 0.8.0 (solc-js)
Node v14.17.4
Web3.js v1.5.0
Solidity Contract flattener v0.0.10

I know there are other articles that explain other ways of doing this and getting verification. But I really wanted to flatten all the code for my website and also to test some of the functions in Remix :slight_smile:

Any help would be greatly appreciated - Thanks!

Hi, could you please show your file named package.json?

And if you want to test your code on the remix, I think there is no need to flatten the whole contracts, cause remix has support the OpenZeppelin repo, you can copy the code you show above and then paste to the remix to have a test.

Hello, I only see package-lock.json. Is this correct ?

So I had to run npm init -y in my root folder to create a package.json file. Apparently, truffle doesn't create one automaticaly. Tried to run the flattener again and still no luck.

Here is the Package.json file

{
  "name": "Test.sol",
  "version": "1.0.0",
  "description": "",
  "main": "truffle-config.js",
  "directories": {
    "test": "test"
  },
  "dependencies": {
    "bootstrap": "^5.1.0",
    "moralis": "^0.0.31",
    "opensea-js": "^1.1.11",
    "truffle-flatten": "^1.0.8",
    "truffle-flattener": "^1.5.1",
    "web3": "^1.5.1"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Yeah, so could you please search for a dependency named @openzeppelin, so what is the version of it?

It's possible that truffle-flattener is not compatible with Solidity 0.8.

I think you are right. The last upgrade was compatible with Solidity 0.7.0

I have successfully deployed and checked all functions using Remix. Here's the code I used.

It seems Hard Hat does have a flatten function available in the latest solidity language.

// contracts/Test.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";

contract Test is ERC20, Ownable {
    constructor() ERC20("Testcoin", "TST") {
        _mint(msg.sender, 100000000000 * 10 ** 18);
    
    }

    function burn(uint256 amount) external {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
        _burn(msg.sender, amount);
        
    }
}   

And one last thing to add to this. There is actually a flatten function in Remix. Right-click the sol file and the option to flatten is there.

1 Like