-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Support Settings (a.k.a. Input JSON Description) in @nomiclabs/hardhat-vyper #4872
Merged
ChristopherDedominici
merged 14 commits into
main
from
feature/4566-support-settings-in-vyper
Apr 8, 2024
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
89b5571
add settings in the compiler options
ChristopherDedominici e09dfad
fix readme
ChristopherDedominici 0bbfd00
fix test
ChristopherDedominici 5f078bc
fix VyperSettings type
ChristopherDedominici bb9a402
Create fluffy-cups-clap.md
ChristopherDedominici 593d897
Merge branch 'feature/4566-support-settings-in-vyper' of github.com:N…
ChristopherDedominici e7ddc3e
improve handling of optimize setting
ChristopherDedominici da8b0ec
Use VyperPluginError instead of Error
fvictorio de57883
Merge branch 'main' of github.com:NomicFoundation/hardhat into featur…
ChristopherDedominici 87e2516
fix optimize flag for versions < 0.3.1
ChristopherDedominici d466a56
small PR's comments fixes
ChristopherDedominici 2f1dafe
smaller fixes based on PR's comments
ChristopherDedominici b85ff41
increase mocha timeout
ChristopherDedominici 598acb2
add params in doc
ChristopherDedominici File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@nomiclabs/hardhat-vyper": patch | ||
--- | ||
|
||
Added support for vyper settings 'evmVersion' and 'optimize' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { exec } from "child_process"; | ||
import semver from "semver"; | ||
import { VyperSettings } from "./types"; | ||
import { VyperPluginError } from "./util"; | ||
|
||
export class Compiler { | ||
constructor(private _pathToVyper: string) {} | ||
|
@@ -7,10 +10,18 @@ export class Compiler { | |
* | ||
* @param inputPaths array of paths to contracts to be compiled | ||
*/ | ||
public async compile(inputPaths: string[]) { | ||
public async compile( | ||
inputPaths: string[], | ||
compilerVersion: string = "", | ||
settings: VyperSettings = {} | ||
) { | ||
const output: string = await new Promise((resolve, reject) => { | ||
const settingsCmd = getSettingsCmd(compilerVersion, settings); | ||
|
||
const process = exec( | ||
`${this._pathToVyper} -f combined_json ${inputPaths.join(" ")}`, | ||
`${this._pathToVyper} ${settingsCmd} -f combined_json ${inputPaths.join( | ||
" " | ||
)}`, | ||
{ | ||
maxBuffer: 1024 * 1024 * 500, | ||
}, | ||
|
@@ -28,3 +39,72 @@ export class Compiler { | |
return JSON.parse(output); | ||
} | ||
} | ||
|
||
function getSettingsCmd( | ||
compilerVersion: string, | ||
settings: VyperSettings | ||
): string { | ||
let settingsStr = | ||
settings.evmVersion !== undefined | ||
? `--evm-version ${settings.evmVersion} ` | ||
: ""; | ||
|
||
settingsStr += getOptimize(compilerVersion, settings.optimize); | ||
|
||
return settingsStr; | ||
} | ||
|
||
function getOptimize( | ||
compilerVersion: string, | ||
optimize: string | boolean | undefined | ||
): string { | ||
if (compilerVersion === "" && optimize !== undefined) { | ||
throw new VyperPluginError( | ||
"The 'compilerVersion' parameter must be set when the setting 'optimize' is set." | ||
); | ||
} | ||
|
||
if (optimize === undefined) { | ||
return ""; | ||
} | ||
schaable marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (typeof optimize === "boolean") { | ||
if (optimize) { | ||
if ( | ||
semver.gte(compilerVersion, "0.3.10") || | ||
semver.lt(compilerVersion, "0.3.1") | ||
) { | ||
throw new VyperPluginError( | ||
`The 'optimize' setting with value 'true' is not supported for versions of the Vyper compiler older than 0.3.1 or newer than 0.3.10. You are currently using version ${compilerVersion}.` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This message has an off-by-one error, right? You should either say |
||
); | ||
} | ||
|
||
// The optimizer is enabled by default | ||
return ""; | ||
} else { | ||
if (semver.lt(compilerVersion, "0.3.1")) { | ||
throw new VyperPluginError( | ||
`The 'optimize' setting with value 'false' is not supported for versions of the Vyper compiler older than 0.3.1. You are currently using version ${compilerVersion}.` | ||
); | ||
} | ||
|
||
return semver.lt(compilerVersion, "0.3.10") | ||
? "--no-optimize" | ||
: "--optimize none"; | ||
} | ||
} | ||
|
||
if (typeof optimize === "string") { | ||
if (semver.gte(compilerVersion, "0.3.10")) { | ||
return `--optimize ${optimize}`; | ||
} | ||
|
||
throw new VyperPluginError( | ||
`The 'optimize' setting, when specified as a string value, is available only starting from the Vyper compiler version 0.3.10. You are currently using version ${compilerVersion}.` | ||
); | ||
} | ||
|
||
throw new VyperPluginError( | ||
`The 'optimize' setting has an invalid type value. Type is: ${typeof optimize}.` | ||
schaable marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
/*/cache | ||
/*/artifacts | ||
/**/cache | ||
/**/artifacts |
5 changes: 5 additions & 0 deletions
5
...e-projects/compilation-with-settings-option-variants/optimize-invalid-type/contracts/A.vy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# @version 0.3.9 | ||
|
||
@external | ||
def test() -> int128: | ||
return 42 |
15 changes: 15 additions & 0 deletions
15
...rojects/compilation-with-settings-option-variants/optimize-invalid-type/hardhat.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require("../../../../src/index"); | ||
|
||
module.exports = { | ||
vyper: { | ||
compilers: [ | ||
{ | ||
version: "0.3.9", | ||
settings: { | ||
evmVersion: "paris", | ||
optimize: 12, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
5 changes: 5 additions & 0 deletions
5
...ttings-option-variants/optimize-set-to-false-always-available-new-versions/contracts/A.vy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# @version 0.3.10 | ||
|
||
@external | ||
def test() -> int128: | ||
return 42 |
15 changes: 15 additions & 0 deletions
15
...ngs-option-variants/optimize-set-to-false-always-available-new-versions/hardhat.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require("../../../../src/index"); | ||
|
||
module.exports = { | ||
vyper: { | ||
compilers: [ | ||
{ | ||
version: "0.3.10", | ||
settings: { | ||
evmVersion: "paris", | ||
optimize: false, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
5 changes: 5 additions & 0 deletions
5
...ttings-option-variants/optimize-set-to-false-always-available-old-versions/contracts/A.vy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# @version 0.3.9 | ||
|
||
@external | ||
def test() -> int128: | ||
return 42 |
15 changes: 15 additions & 0 deletions
15
...ngs-option-variants/optimize-set-to-false-always-available-old-versions/hardhat.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require("../../../../src/index"); | ||
|
||
module.exports = { | ||
vyper: { | ||
compilers: [ | ||
{ | ||
version: "0.3.9", | ||
settings: { | ||
evmVersion: "paris", | ||
optimize: false, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
5 changes: 5 additions & 0 deletions
5
...-settings-option-variants/optimize-set-to-false-not-available-old-versions/contracts/A.vy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# @version 0.3.0 | ||
|
||
@external | ||
def test() -> int128: | ||
return 42 |
15 changes: 15 additions & 0 deletions
15
...ttings-option-variants/optimize-set-to-false-not-available-old-versions/hardhat.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require("../../../../src/index"); | ||
|
||
module.exports = { | ||
vyper: { | ||
compilers: [ | ||
{ | ||
version: "0.3.0", | ||
settings: { | ||
evmVersion: "istanbul", | ||
optimize: false, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
5 changes: 5 additions & 0 deletions
5
...re-projects/compilation-with-settings-option-variants/optimize-set-to-true/contracts/A.vy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# @version 0.3.9 | ||
|
||
@external | ||
def test() -> int128: | ||
return 42 |
15 changes: 15 additions & 0 deletions
15
...projects/compilation-with-settings-option-variants/optimize-set-to-true/hardhat.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require("../../../../src/index"); | ||
|
||
module.exports = { | ||
vyper: { | ||
compilers: [ | ||
{ | ||
version: "0.3.9", | ||
settings: { | ||
evmVersion: "paris", | ||
optimize: true, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
5 changes: 5 additions & 0 deletions
5
...n-with-settings-option-variants/optimize-string-not-available-old-versions/contracts/A.vy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# @version 0.3.9 | ||
|
||
@external | ||
def test() -> int128: | ||
return 42 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add the new parameters.