Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: flatten licenses flag #1223

Closed

Conversation

nachomazzara
Copy link

@nachomazzara nachomazzara commented Feb 3, 2021

Nowadays when trying to validate the code at Etherscan or other block explorers, and also trying to copy/paste the flattened code on Remix, you will get an error saying that you must have one license per file.

Screen Shot 2021-02-03 at 14 57 15

Screen Shot 2021-02-03 at 14 56 54

I propose two flags to extend hardhat flatten:

unifyABIEncoderV2: Remove all occurrences of pragma experimental ABIEncoderV2 except the first one.
removeLicences: Remove all licenses of the files to be flattened.
license: Add a unique license at the beginning of the file flattened. --license MIT will add // SPDX-License-Identifier: MIT.

Usage:

npx hardhat flatten --unifyABIEncoderV2
npx hardhat flatten --removeLicenses
npx hardhat flatten --license MIT
npx hardhat flatten --license MIT --unifyABIEncoderV2

@nachomazzara nachomazzara changed the title feat: flatten licences flag feat: flatten licenses flag Feb 3, 2021
@nachomazzara
Copy link
Author

@fvictorio @alcuadrado any thoughts on why CI is not passing?

packages/hardhat-core/test/builtin-tasks/flatten.ts Outdated Show resolved Hide resolved
@@ -115,6 +117,11 @@ subtask(
flattened = `// SPDX-License-Identifier: ${license}\n\n${flattened}`;
}

if (unifyABIEncoderV2) {
// Remove every line started with "pragma experimental ABIEncoderV2;" except the first one
flattened = flattened.replace(/pragma experimental ABIEncoderV2;\n/gm, (i => (m: string) => !i++ ? m : '')(0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is very hard to understand, is there a way to make it more readable?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I just want to keep it as functional as I can. It was a time ago the last time I've used an IIFE.

Do you think that with better variables names works?

flattened = flattened.replace(/pragma experimental ABIEncoderV2;\n/gm, (index => (match: string) => !index++ ? match: '')(0));

If not, alternative:

let index = 0
flattened = flattened.replace(/pragma experimental ABIEncoderV2;\n/gm, (match: string) => !index++ ? match : '');

Other solutions will include maybe a complex regex or the usage of split & replace.

Any other idea?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed a change for this to make it more like the code we tend to write. We usually avoid compact one-liners and err on the "verbose but clear" side.

.addOptionalParam("removeLicenses", undefined, false, types.boolean)
.addOptionalParam("license", undefined, undefined, types.string)
.setAction(
async (
{ files, removeLicenses = false, license }: FlattenInput,
{ files, unifyABIEncoderV2 = false, removeLicenses = false, license }: FlattenInput,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This option seems very ad hoc. I understand that this is what was required in the issue, but it doesn't feel right.

Maybe it could be called unifyPragmas, even if it only does this for the ABI encoder v2 one. If it happens that there's another pragma that needs this, we can add it in the future.

@alcuadrado wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also: #1050 (comment)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fvictorio I have added it cause I forgot to add it when I made the PR.

From Remix, you get the following errors when you flattened contracts with ABIEncoder V2. I would like to keep that flag.
Screen Shot 2021-05-11 at 08 32 32

About the naming, unifyPragmas can be confused because you can still have different pragma solidity versions. Maybe unifyExperimentalPragmas works but we can't predict if we will have more experimental pragmas in the future.

@fvictorio
Copy link
Member

@nachomazzara are tests passing for you locally? I'm getting 4 failures with the latest changes.

@nachomazzara
Copy link
Author

nachomazzara commented May 22, 2021

@fvictorio We should be good to go!

@alcuadrado
Copy link
Member

I'm closing this PR. There's a spec about how we should tackle this in this issue: #1499

@alcuadrado alcuadrado closed this Jun 8, 2021
@jtakalai
Copy link
Contributor

jtakalai commented Sep 7, 2021

Any reason why this PR was closed? There is no "competing" PR open that also fixes this, only an open issue.

@alcuadrado
Copy link
Member

We decided to take a different direction which is described in #1499. We haven't executed on it yet, but mergint this PR would prevent us from doing it.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants