Giter Site home page Giter Site logo

nomicfoundation / truffle-flattener Goto Github PK

View Code? Open in Web Editor NEW
373.0 10.0 98.0 341 KB

Truffle Flattener concats solidity files from Truffle and Buidler projects with all of their dependencies

License: MIT License

JavaScript 98.16% Solidity 1.84%
ethereum solidity truffle smart-contracts buidler

truffle-flattener's Introduction

truffle-flattener

npm

Truffle Flattener concats solidity files from Truffle and Buidler projects with all of their dependencies.

This tool helps you to verify contracts developed with Truffle and Buidler on Etherscan, or debugging them on Remix, by merging your files and their dependencies in the right order.

If you are still using Truffle, we recommend you try Buidler, our Ethereum development environment, which is much faster and flexible.

Hardhat support

If you are using Hardhat, you don't need this project. Use npx hardhat flatten instead.

Installation

npm install truffle-flattener -g

Usage

Just install it with npm in your truffle project and run truffle-flattener <solidity-files>.

Limitations

Aliased imports (eg: import {symbol1 as alias, symbol2} from "filename";) are not supported by truffle-flattener.

truffle-flattener's People

Contributors

alcuadrado avatar arthcp avatar cedricwalter avatar dependabot[bot] avatar eordano avatar ezulkosk avatar frangio avatar gganebnyi avatar ihordiachenko avatar jbcarpanelli avatar jossef avatar kandrianov avatar maraoz avatar moejoe90 avatar nonnonnon avatar okwme avatar spalladino avatar tomlinton avatar vittominacori avatar webmaster128 avatar yaoding16 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

truffle-flattener's Issues

Include --output in the docs

Hello all together,

we are using your truffle-flattener and so far it works great. We saw in the code that there is the --output option to suppress the yarn comments in the flattend .sol files.

Maybe you guys can include this option in your docs? Would be better I think if one can directly get this from the README :)

Thank your very much.
Best regards.

Flattener doesn't parse "pragma experimental"

I have the following at the top of my contract:

pragma solidity ^0.4.19;
pragma experimental ABIEncoderV2;

truffle-flattener contracts/Whatever.sol fails with the following error:

Error: Could not parse contracts/Whatever.sol for extracting its imports.\n    at getDependencies (/Users/kriskelly/.config/yarn/global/node_modules/truffle-flattener/index.js:51

Digging further, it looks like the solidity parser throws this error:

SyntaxError: Expected "solidity", comment, end of line, or whitespace but "e" found. Line: 2, Column: 8

The code compiles and migrates fine with truffle. The flattener also works fine with my contracts that do not use pragma experimental.

I'm assuming that the solidity parser is out of date, not really sure how to proceed.

None of the sub-resolvers resolved "nft\node_modules\@openzeppelin\contracts\token\ERC721\ERC721Full.sol" location.

I m cant use truffle flattener because this error

(node:14296) 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:14296) Warning: Accessing non-existent property 'INVALID_ALT_NUMBER' of module exports inside circular dependency
Error: None of the sub-resolvers resolved "nft\node_modules@openzeppelin\contracts\token\ERC721\ERC721.sol" location.
at ResolverEngine. (C:\Users\im\data\nft\node_modules@resolver-engine\core\build\src\resolverengine.js:35:23)
at Generator.next ()
at fulfilled (C:\Users\im\data\nft\node_modules@resolver-engine\core\build\src\resolverengine.js:4:58)

add command line option to specify the truffle config file to be used

This is an enhancement request to add a command-line option to specify the truffle config file to be used on the compilation.

I would like to add like the below in the main function.

  let rootIndex = args.indexOf("--root");

[Example]

truffle-flattener contracts/XXX.sol --output build/contracts/FlattenerXXX.sol --dir ./subdir
/rootdir
    |--subdir
    |       |--/contracts
    |       |--/build
    |       |--truffle-config.js
    |--/nodemodules
    |--package.json

[Related]
trufflesuite/truffle#1879

Format is incorrect if import is separated into multiple lines

If import is separated into multiple lines like this,

import {
    ReentrancyGuardUpgradeSafe
} from "@openzeppelin/contracts-ethereum-package/contracts/utils/ReentrancyGuard.sol";

the result file would have something like this,


    ReentrancyGuardUpgradeSafe
} from "@openzeppelin/contracts-ethereum-package/contracts/utils/ReentrancyGuard.sol";

Crashes when pathname includes spaces.

Whenever the pathname includes spaces, the spaces are replaced by '%20', which, at least in powershell, leads the program to halt and return an error saying it can't open the file.

Powershell handles spaces in paths by putting quotes around it, like a string.

for example:

c:'Program Files'\something\helloWorld.sol

Could not parse contracts

Error: Could not parse contracts/Clovers.sol for extracting its imports.
    at getDependencies (/usr/local/lib/node_modules/truffle-flattener/index.js:50:11)
    at dependenciesDfs (/usr/local/lib/node_modules/truffle-flattener/index.js:69:24)
    at <anonymous> 'Error: Could not parse contracts/Clovers.sol for extracting its imports.\n    at getDependencies (/usr/local/lib/node_modules/truffle-flattener/index.js:50:11)\n    at dependenciesDfs (/usr/local/lib/node_modules/truffle-flattener/index.js:69:24)\n    at <anonymous>'

while trying w contracts located at https://github.com/clovers-network/clovers-contracts

works w contracts/ClubToken.sol and contracts/ClubTokenController.sol but not with contracts/Clovers.sol or contracts/CloversController.sol

Confused

When I use the command in my VS Code terminal, it returns the exact same contract.

Could not parse contract for extracting its imports.

Hello have you any idea about why it is throwing this error when I moved to solidity 0.6?

Error: Could not parse @openzeppelin/contracts/token/ERC20/ERC20Detailed.sol for extracting its imports.
    at getDependencies (/path/erc20-generator-smartcontracts/node_modules/truffle-flattener/index.js:58:11)
    at dependenciesDfs (/path/erc20-generator-smartcontracts/node_modules/truffle-flattener/index.js:78:24)

It works on master branch but not here.

Support for HardHat

Hi,

I'd like to know if you have any plans to add support for HardHat.
Would be a nice way to flatten contracts to verify on blockscout when deploying to sokol or xdai.

Just found related issue #65 and pr #66

What is the status on that?

Thanks!

Contract dependency not found (on windows)

We have this directory organization:

root
 |- contracts
      |- Test.sol
 |- node_modules
      |- zeppelin-solidity

Our contract Test.sol has

import "zeppelin-solidity/contracts/ownership/Ownable.sol";

Starting

truffle-flattener contracts/Test.sol

we got:

Could not find ..\ownership\Ownable.sol from any sources

Does Not Support Pragma Statement Including `||`

Declaring the pragma statement in the form of pragma solidity ^0.5.0 || ^0.6.0 ; causes truffle-flattener to fail, reporting the following error:

Error: Could not parse /tmp/t/ABDKMathQuad.sol for extracting its imports: ParserError: mismatched input '||' expecting ';' (5:23)
    at getDependencies (/home/solidity/.npm_modules/lib/node_modules/truffle-flattener/index.js:44:11)
    at dependenciesDfs (/home/solidity/.npm_modules/lib/node_modules/truffle-flattener/index.js:64:24)
    at async getSortedFilePaths (/home/solidity/.npm_modules/lib/node_modules/truffle-flattener/index.js:83:5)
    at async flatten (/home/solidity/.npm_modules/lib/node_modules/truffle-flattener/index.js:180:23)
    at async main (/home/solidity/.npm_modules/lib/node_modules/truffle-flattener/index.js:237:3)

To test you can use truffle-flattener on this contract

Could not find XXXXXXX.sol (BasicToken.sol) from any sources

Hello, I want to pack into a file for Verify Contract Code to use truffle-flattener, the following code all the following are said to be unable to find: Could not find BasicToken.sol from any sources

1.truffle-flattener./contracts/LhhToken.sol --output result.sol
2.truffle-flattener  $(pwd)/contracts/LhhToken.sol  --solc-paths=openzeppelin-solidity=$(pwd)/solidity/node_modules/openzeppelin-solidity/

token

npx github:fedeb95/truffle-flattener is error!!!

npm ERR! Error while executing:
npm ERR! X:\XX\xxx\bin\git.EXE ls-remote -h -t ssh://[email protected]/fedeb95/truffle-flattener.git
npm ERR!
npm ERR! ssh: Could not resolve hostname github.com: Name or service not known
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
npm ERR!
npm ERR! exited with error code: 128

npm ERR! A complete log of this run can be found in:
npm ERR! X:\npm-cache_logs\2021-11-22T02_46_24_545Z-debug.log

Flattener doesn't correctly handle import alias.

When an import is destructured and aliased the alias name is not defined. The following example includes DetailedERC20, but Token would not be defined.
import {DetailedERC20 as Token} from "openzeppelin-solidity/contracts/token/ERC20/DetailedERC20.sol";

I have worked around this while adding code to etherscan by manually adding:
contract Token is DetailedERC20 {}

Error: None of the sub-resolvers resolved "@openzeppelin/contracts/access/Ownable.sol" location.

Hi,
I'm trying to use truffle-flattener v1.4.4 to flatten my truffle project contracts, but I'm getting the following error:

Error: None of the sub-resolvers resolved "@openzeppelin/contracts/access/Ownable.sol" location.

when trying the following:

$ truffle-flattener MyContract.sol > FlattenMyContract.sol

Using the following dependences:
Ubuntu 18.04
npm v13.13.0
truffle v5.1.25
solc v0.6.7

Doesn't work with truffle-config.js

When running truffle-flattener, in a project with truffle-config.js, I get:

TypeError: Cannot read property 'substring' of null
    at getDirPath (/usr/local/lib/node_modules/truffle-flattener/index.js:38:19)
    at getTruffleRoot (/usr/local/lib/node_modules/truffle-flattener/index.js:213:10)
    at <anonymous> 'TypeError: Cannot read property \'substring\' of null\n    at getDirPath (/usr/local/lib/node_modules/truffle-flattener/index.js:38:19)\n    at getTruffleRoot (/usr/local/lib/node_modules/truffle-flattener/index.js:213:10)\n    at <anonymous>'

I'm able to get it to work with mv truffle-config.js truffle.js, but it seems something isn't working as intended.

Can not find library

Truffle Flattener can't find import packages, which I assume is part of the goal of this project! I'm happy to help sort this out as I think this would make the project even more valuable

The import:
import 'zeppelin-solidity/contracts/token/StandardToken.sol';

Error: Could not find zeppelin-solidity/contracts/token/StandardToken.sol from any sources

truffle-flattener output has multiple SPDX licenses

Hi,

I have a smart contract that imports multiple openzeppelin contracts. After running the truffle-flattener tool, it looks like the SPDX license on each of the files is being put into the file, which causes the following error:

Solc experienced a fatal error.\n\nParserError: Multiple SPDX license identifiers found in source file. 

Am I using the tool improperly?

Error thrown when using Truffle Flattener

I'm excited about this project, but when I and my co-worker try to use it we get this error:

truffle-flattener RareToken.sol
/Users/tommy/.nvm/versions/node/v6.0.0/lib/node_modules/truffle-flattener/index.js:65
async function dependenciesDfs(graph, visitedFiles, filePath) {
^^^^^^^^
SyntaxError: Unexpected token function
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:511:25)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Function.Module.runMain (module.js:575:10)
at startup (node.js:159:18)
at node.js:444:3

Even running "truffle-flattener -v" or any truffle-flattener code throws this error.

Any ideas?

Deduplicate or remove SPDX license identifier comments

Solidity 0.6.8 introduced // SPDX-License-Identifier comments that will now become quite common because their omission produces a compiler warning. These comments cannot be duplicated or they will produce a compiler error. They need to be combined into a single comment. This is easy if all comments are exactly the same (although hopefully the compiler can be changed so that this is no longer an error...) but it's not clear what should be done when there are multiple licenses across the file. I've asked in the Solidity repository for guidance. ethereum/solidity#8989

TypeError: Definition of base has to precede definition of derived contract abstract contract

Hi, when I use this plugin to flatten contracts on the Remix, I got this error:

TypeError: Definition of base has to precede definition of derived contract abstract contract

It looks like the files are in the wrong location.

My test code is:

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.2;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.4.0/contracts/presets/ERC20PresetMinterPauser.sol";

Could not parse smart contract with "unchecked block" syntax

When I flatten a certain smart contract that uses openzeppelin's SafeMath library I'm getting this error:
Error: Could not parse @openzeppelin/contracts/utils/math/SafeMath.sol for extracting its imports: ParserError: missing ';' at '{'

Upon investigation, flattening fails when encountering an unchecked block.
unchecked block is a valid solidity syntax introduced since 0.8.0, check reference here.

Sample code to reproduce the error:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

library SafeMath {
    . . .
    function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a / b);
        }
    }
   . . .
}

SyntaxError: Unexpected token function

Using the suggested install approach and got the following error when trying to flatten a single sol file:

/usr/lib/node_modules/truffle-flattener/index.js:65
async function dependenciesDfs(graph, visitedFiles, filePath) {
      ^^^^^^^^

SyntaxError: Unexpected token function
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:383:7)
    at startup (bootstrap_node.js:149:9)

⚠️ WARNING ⚠️ tar.gz module has been deprecated and your application is vulnerable. Please use tar module instead: https://npmjs.com/tar

I just added truffle-flattener to my dev dependencies and got these two warnings:

$ yarn install
yarn install v1.12.3
[1/4] Resolving packages...
warning truffle-flattener > truffle-config > truffle-provider > web3 > web3-bzz > swarm-js > [email protected]: Use mz or fs-extra^3.0 with Promise Support
warning truffle-flattener > truffle-config > truffle-provider > web3 > web3-bzz > swarm-js > [email protected]: ⚠️  WARNING ⚠️ tar.gz module has been deprecated and your application is vulnerable. Please use tar module instead: https://npmjs.com/tar

It looks truffle-config ^1.1 has fixed both these warnings.

Add support for Solidity 0.6 new syntax, such as "receive"

MyContract has been compiled, deployed, and tested successfully on both Ganache as well as on Rinkeby. However, truffle-flattener fails to parse it --

$ truffle-flattener MyContract.sol
Error: Could not parse MyContract.sol for extracting its imports: ParserError: mismatched input '(' expecting {'from', 'calldata', 'constant', 'internal', 'private', 'public', Identifier} (79:11)
    at getDependencies (/usr/lib/node_modules/truffle-flattener/index.js:44:11)
    at dependenciesDfs (/usr/lib/node_modules/truffle-flattener/index.js:64:24)
    at async getSortedFilePaths (/usr/lib/node_modules/truffle-flattener/index.js:83:5)
    at async flatten (/usr/lib/node_modules/truffle-flattener/index.js:170:23)
    at async main (/usr/lib/node_modules/truffle-flattener/index.js:227:3)

$ cat -n MyContract.sol
   1     pragma solidity ^0.6.1;
 ...
 79     receive() external payable { // solidity 0.6
 80         emit FallbackDeposit(msg.sender, uint40(now), msg.value);
 81     }

Please add Solidity 0.6 support, many thanks in advance.

Pragma header should be added only once

While repeating pragma header does not make code invalid, it can look much better if only one pragma will be inserted at the top of the file. It may be taken from the target contract only, ignoring pragma on it's dependencies.

Will support hardhat?

Will support hardhat?

Error:
Truffle Flattener must be run inside a Truffle or Buidler project:
truffle.js, truffle-config.js, buidler.config.js, nor buidler.config.ts found

Support cyclic dependencies

Currently truffle-flattener doesn't support cyclic dependencies. It just fails when such dependencies are found. This happens because it just concats the graph of dependencies in the right (any topological) order.

An alternative to this is to actually parse the files and merge their AST. This way declarations can be written in order in the output.

This has two drawbacks:

  1. It's far more complex, but anyone with a basic understanding of parsing or willing to learn about it can tackle it.
  2. The 1-to-1 relationship between contiguos sections of the output and dependencies is lost, which makes it less useful for debugging.

I'm not really interested in implementing this (I think having cyclic dependencies should be avoided), but I can provide guidance for anyone who wants to do it.

The only condition for merging a PR for this is that it should make its best effort to only alter the output for the cyclic parts of the dependencies graph, avoiding drawback (2) as much as possible.

Solidity 6 'immutable' breaks parser

When a contract includes an immutable storage variable, the parser fails.

Error:
Error: Could not parse MyContract.sol for extracting its imports: ParserError: mismatched input 'private' expecting {';', '='} (17:20)

Invalid Path Problem

If the path for contracts contains space in the folder name, it gives an error.

The command below doesn't work it gives the following error because of the space in my Software Development folder name, when I rename the folder and remove the space it works fine

F:\Software Development\Dapps\monte-finance>truffle-flattener contracts/Monte.sol Error: ENOENT: no such file or directory, open 'f:/Software%20Development/Dapps/monte-finance/node_modules/@openzeppelin/contracts/utils/Context.sol' at Object.openSync (fs.js:457:3) at Object.readFileSync (fs.js:359:35) at resolve (C:\Users\Dell\AppData\Roaming\nvm\v12.16.1\node_modules\truffle-flattener\index.js:23:27) at async dependenciesDfs (C:\Users\Dell\AppData\Roaming\nvm\v12.16.1\node_modules\truffle-flattener\index.js:62:20) at async dependenciesDfs (C:\Users\Dell\AppData\Roaming\nvm\v12.16.1\node_modules\truffle-flattener\index.js:73:7) at async dependenciesDfs (C:\Users\Dell\AppData\Roaming\nvm\v12.16.1\node_modules\truffle-flattener\index.js:73:7) at async getSortedFilePaths (C:\Users\Dell\AppData\Roaming\nvm\v12.16.1\node_modules\truffle-flattener\index.js:83:5) at async flatten (C:\Users\Dell\AppData\Roaming\nvm\v12.16.1\node_modules\truffle-flattener\index.js:180:23) at async main (C:\Users\Dell\AppData\Roaming\nvm\v12.16.1\node_modules\truffle-flattener\index.js:237:3) { errno: -4058, syscall: 'open', code: 'ENOENT', path: 'f:/Software%20Development/Dapps/monte-finance/node_modules/@openzeppelin/contracts/utils/Context.sol' }

This path works F:\Software-Development\Dapps\monte-finance

Normalizing version pragmas is unnecessary

truffle-flattener currently replaces all individual version pragmas with a single "normalized" pragma at the top of the result. This normalization doesn't support version specifiers other than ^, such as >= which is being used a bit lately in the ecosystem.

This isn't necessary though. The compiler supports files with multiple version pragmas throughout, so truffle-flattener could just leave them where they are.

Flat file contains multiple pragma experimental ABIEncoderV2;

This leads to an error:

Account.full.sol:393:1: Warning: Experimental features are turned on. Do not use experimental features on live deployments.
pragma experimental ABIEncoderV2;
^-------------------------------^

Account.full.sol:427:1: SyntaxError: Duplicate experimental feature name.
pragma experimental ABIEncoderV2;
^-------------------------------^

error Command failed with exit code 1.

Having a variable named "calldata" makes things explode.

Here's a simple contract that uses a variable named "calldata" that is unable to be flattened. Renaming "calldata" to "potatodata" makes everything work as expected.

pierce403@penguin:~/fun/potato/contracts$ cat Potato.sol 
pragma solidity ^0.4.24;

contract Potato {
  function yay() public pure {
    string calldata="hello";
  }
}
pierce403@penguin:~/fun/potato/contracts$ truffle-flattener Potato.sol 
Error: Could not parse contracts/Potato.sol for extracting its imports.
    at getDependencies (/home/pierce403/node/lib/node_modules/truffle-flattener/index.js:56:11)
    at dependenciesDfs (/home/pierce403/node/lib/node_modules/truffle-flattener/index.js:75:24)
    at <anonymous> 'Error: Could not parse contracts/Potato.sol for extracting its imports.\n    at getDependencies (/home/pierce403/node/lib/node_modules/truffle-flattener/index.js:56:11)\n    at dependenciesDfs (/home/pierce403/node/lib/node_modules/truffle-flattener/index.js:75:24)\n    at <anonymous>'
pierce403@penguin:~/fun/potato/contracts$ sed -i 's/calldata/potatodata/g' Potato.sol 
pierce403@penguin:~/fun/potato/contracts$ cat Potato.sol 
pragma solidity ^0.4.24;

contract Potato {
  function yay() public pure {
    string potatodata="hello";
  }
}
pierce403@penguin:~/fun/potato/contracts$ truffle-flattener Potato.sol 
pragma solidity ^0.4.24;

// File: contracts/Potato.sol

contract Potato {
  function yay() public pure {
    string potatodata="hello";
  }
}
pierce403@penguin:~/fun/potato/contracts$ 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.