Truffle cannot flatten openzeppelin/contracts

Having installed npm and truffle, I wish to use
import “@openzeppelin/contracts/token/ERC20/ERC20.sol”;

to import openzeppelin packages for deploying using Remix. However, I cannot truffle-flatten the main .sol and get an error referring to invalid ALT.

:computer: Environment

MacOS Big Sur, Truffle v5.1.567, Remix IDE

:memo:Details

users-mbp:STK user$ truffle-flattener contracts/SimpleToken.sol > contracts/FlattenedSimpleToken.sol
(node:7324) Warning: Accessing non-existent property 'INVALID_ALT_NUMBER' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:7324) Warning: Accessing non-existent property 'INVALID_ALT_NUMBER' of module exports inside circular dependency
Error: None of the sub-resolvers resolved "@openzeppelin/contracts/token/ERC20/StandardToken.sol" location.
    at ResolverEngine.<anonymous> (/usr/local/lib/node_modules/truffle-flattener/node_modules/@resolver-engine/core/build/src/resolverengine.js:35:23)
    at Generator.next (<anonymous>)
    at fulfilled (/usr/local/lib/node_modules/truffle-flattener/node_modules/@resolver-engine/core/build/src/resolverengine.js:4:58)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
users-mbp:STK user$ truffle-flattener contracts/SimpleToken.sol > contracts/FlattenedSimpleToken.sol
(node:7746) Warning: Accessing non-existent property 'INVALID_ALT_NUMBER' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:7746) Warning: Accessing non-existent property 'INVALID_ALT_NUMBER' of module exports inside circular dependency
Error: None of the sub-resolvers resolved "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol" location.
    at ResolverEngine.<anonymous> (/usr/local/lib/node_modules/truffle-flattener/node_modules/@resolver-engine/core/build/src/resolverengine.js:35:23)
    at Generator.next (<anonymous>)
    at fulfilled (/usr/local/lib/node_modules/truffle-flattener/node_modules/@resolver-engine/core/build/src/resolverengine.js:4:58)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

:1234: Code to reproduce

pragma solidity ^0.6.2;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";


contract SimpleToken is ERC20, ERC20Detailed {

    constructor () public ERC20Detailed("SimpleToken", "STt", 18) {
        _mint(msg.sender, 25000000000 * (10 ** uint256(decimals())));
    }
}
1 Like

You should try not to flatten your contracts when submitting to etherscan.

Truffle / Hardhat have verification plugins that allow you to create a combined-json format file that can be uploaded instead.

This makes your life easier because it contains version and optimisation data.

If you still need to flatten (e.g. for remix as you mentioned) try https://github.com/DaveAppleton/SolidityFlattery

1 Like

Hi @KL5616,

Welcome to the community :wave:

As @DaveAppleton said, you should avoid flattening your contract for verification.

See the following for how to use Hardhat to verify: Verify smart contract inheriting from OpenZeppelin Contracts

Many thanks for the suggestions; I am indeed deploying via Remix.

Remix is also able to inherit from OpenZeppelin through a github url (import “https://github.com/…”.
Can I separately verify the Remix deployed contract through hardhat? Or what is in general the recommended way to deploy to the Ethereum network?

Thanks again!

KL5616

1 Like

Hi @KL5616,

It depends on your use case. Remix is great for experimenting.

You can deploy using Remix using GitHub imports for OpenZeppelin and verify using Hardhat converting GitHub imports to npm imports. Verify smart contract inheriting from OpenZeppelin Contracts.

If your contract is going to have value, then I suggest appropriate testing (including automated unit tests) and auditing. See: Points to consider when creating a fungible token (ERC20, ERC777).