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

Support Settings (a.k.a. Input JSON Description) in @nomiclabs/hardhat-vyper #4872

Merged
merged 14 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fluffy-cups-clap.md
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'
15 changes: 13 additions & 2 deletions packages/hardhat-vyper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,23 @@ module.exports = {
};
```

You can also configure multiple versions of the Vyper compiler:
You can also configure multiple versions of the Vyper compiler, as well as the compiler settings evmVersion and optimize. See the [Vyper docs](https://docs.vyperlang.org/en/v0.3.10/compiling-a-contract.html) for more info.

```js
module.exports = {
vyper: {
compilers: [{ version: "0.2.1" }, { version: "0.3.0" }],
compilers: [
{
version: "0.2.1",
},
{
version: "0.3.10",
settings: {
evmVersion: "paris",
optimize: "gas",
},
},
],
},
};
```
Expand Down
84 changes: 82 additions & 2 deletions packages/hardhat-vyper/src/compiler.ts
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) {}
Expand All @@ -7,10 +10,18 @@ export class Compiler {
*
* @param inputPaths array of paths to contracts to be compiled
Copy link
Member

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.

*/
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,
},
Expand All @@ -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.lte(compilerVersion, "0.3.0")
) {
throw new VyperPluginError(
`The 'optimize' setting with value 'true' is not supported for versions of the Vyper compiler older than or equal to 0.3.0 or newer than or equal to 0.3.10. You are currently using version ${compilerVersion}.`
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it's just me, but I find that error message a bit too verbose, how about something like: "The 'optimize' setting with value 'true' is supported only for Vyper versions >0.3.0 and <0.3.10. You are currently using version ${compilerVersion}."
Feel free to ignore it if you think the current message is better 😁

Copy link
Member

Choose a reason for hiding this comment

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

(same for the rest of the error messages)

);
}

// The optimizer is enabled by default
return "";
} else {
if (semver.lte(compilerVersion, "0.3.0")) {
throw new VyperPluginError(
`The 'optimize' setting with value 'false' is not supported for versions of the Vyper compiler older than or equal to 0.3.0. 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
);
}
66 changes: 49 additions & 17 deletions packages/hardhat-vyper/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Artifacts as ArtifactsImpl } from "hardhat/internal/artifacts";
import type { Artifacts } from "hardhat/types/artifacts";
import type { VyperFilesCache as VyperFilesCacheT } from "./cache";
import type { VyperOutput, VyperBuild } from "./types";
import type { VyperOutput, VyperBuild, VyperSettings } from "./types";
import type { ResolvedFile } from "./resolver";

import * as os from "os";
Expand Down Expand Up @@ -188,13 +188,21 @@ subtask(TASK_COMPILE_VYPER_RUN_BINARY)
async ({
inputPaths,
vyperPath,
vyperVersion,
settings,
}: {
inputPaths: string[];
vyperPath: string;
vyperVersion?: string;
settings?: VyperSettings;
}): Promise<VyperOutput> => {
const compiler = new Compiler(vyperPath);

const { version, ...contracts } = await compiler.compile(inputPaths);
const { version, ...contracts } = await compiler.compile(
inputPaths,
vyperVersion,
settings
);

return {
version,
Expand Down Expand Up @@ -249,36 +257,53 @@ subtask(TASK_COMPILE_VYPER)
({ version }) => version
);

const versionGroups: Record<string, ResolvedFile[]> = {};
const versionsToSettings = Object.fromEntries(
config.vyper.compilers.map(({ version, settings }) => [
version,
settings,
])
);

const versionGroups: Record<
string,
{ files: ResolvedFile[]; settings: VyperSettings }
> = {};
const unmatchedFiles: ResolvedFile[] = [];

for (const file of resolvedFiles) {
const hasChanged = vyperFilesCache.hasFileChanged(
file.absolutePath,
file.contentHash,
{ version: file.content.versionPragma }
);

if (!hasChanged) continue;

const maxSatisfyingVersion = semver.maxSatisfying(
configuredVersions,
file.content.versionPragma
);

// check if there are files that don't match any configured compiler
// version
// check if there are files that don't match any configured compiler version
if (maxSatisfyingVersion === null) {
unmatchedFiles.push(file);
continue;
}

const settings = versionsToSettings[maxSatisfyingVersion] ?? {};

const hasChanged = vyperFilesCache.hasFileChanged(
file.absolutePath,
file.contentHash,
{
version: maxSatisfyingVersion,
settings,
}
);

if (!hasChanged) continue;

if (versionGroups[maxSatisfyingVersion] === undefined) {
versionGroups[maxSatisfyingVersion] = [file];
versionGroups[maxSatisfyingVersion] = {
files: [file],
settings,
};
continue;
}

versionGroups[maxSatisfyingVersion].push(file);
versionGroups[maxSatisfyingVersion].files.push(file);
}

if (unmatchedFiles.length > 0) {
Expand All @@ -297,7 +322,9 @@ ${list}`
);
}

for (const [vyperVersion, files] of Object.entries(versionGroups)) {
for (const [vyperVersion, { files, settings }] of Object.entries(
versionGroups
)) {
const vyperBuild: VyperBuild = await run(TASK_COMPILE_VYPER_GET_BUILD, {
quiet,
vyperVersion,
Expand All @@ -312,6 +339,8 @@ ${list}`
{
inputPaths: files.map(({ absolutePath }) => absolutePath),
vyperPath: vyperBuild.compilerPath,
vyperVersion,
settings,
}
);

Expand All @@ -329,7 +358,10 @@ ${list}`
lastModificationDate: file.lastModificationDate.valueOf(),
contentHash: file.contentHash,
sourceName: file.sourceName,
vyperConfig: { version },
vyperConfig: {
version,
settings,
},
versionPragma: file.content.versionPragma,
artifacts: [artifact.contractName],
});
Expand Down
6 changes: 6 additions & 0 deletions packages/hardhat-vyper/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
export type VyperUserConfig = string | VyperConfig | MultiVyperConfig;

export interface VyperSettings {
evmVersion?: string;
optimize?: string | boolean;
}

export interface VyperConfig {
version: string;
settings?: VyperSettings;
}

export interface MultiVyperConfig {
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-vyper/test/fixture-projects/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/*/cache
/*/artifacts
/**/cache
/**/artifacts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @version 0.3.9

@external
def test() -> int128:
return 42
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,
},
},
],
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @version 0.3.10

@external
def test() -> int128:
return 42
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,
},
},
],
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @version 0.3.9

@external
def test() -> int128:
return 42
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,
},
},
],
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @version 0.3.0

@external
def test() -> int128:
return 42
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,
},
},
],
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @version 0.3.9

@external
def test() -> int128:
return 42
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,
},
},
],
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @version 0.3.9

@external
def test() -> int128:
return 42
Loading
Loading