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: add format parameter to hardhat-abi-exporter #42

Merged
merged 3 commits into from Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions README.md
Expand Up @@ -30,6 +30,7 @@ Add configuration under the `abiExporter` key:
| `except` | `Array` of `String` matchers used to exclude contracts | `[]` |
| `spacing` | number of spaces per indentation level of formatted output | `2` |
| `pretty` | whether to use interface-style formatting of output for better readability | `false` |
| `format` | format type ("json", "minimal", "fullName"). Alternative to `pretty` | `json` |
| `filter` | `Function` with signature `(abiElement: any, index: number, abi: any, fullyQualifiedName: string) => boolean` used to filter elements from each exported ABI | `() => true` |
| `rename` | `Function` with signature `(sourceName: string, contractName: string) => string` used to rename an exported ABI (incompatible with `flat` option) | `undefined` |

Expand All @@ -44,9 +45,10 @@ abiExporter: {
only: [':ERC20$'],
spacing: 2,
pretty: true,
format: "minimal",
}

// or
// or
ItsNickBarry marked this conversation as resolved.
Show resolved Hide resolved

abiExporter: [
{
Expand All @@ -55,7 +57,24 @@ abiExporter: [
},
{
path: './abi/ugly',
pretty: false,
pretty: false,
ItsNickBarry marked this conversation as resolved.
Show resolved Hide resolved
},
]

// or

abiExporter: [
{
path: './abi/json',
format: "json",
},
{
path: './abi/minimal',
format: "minimal",
},
{
path: './abi/fullName',
format: "fullName",
},
]
```
Expand Down
4 changes: 3 additions & 1 deletion index.d.ts
Expand Up @@ -9,6 +9,7 @@ interface AbiExporterUserConfig {
except?: string[],
spacing?: number,
pretty?: boolean,
format?: string,
filter?: (abiElement: any, index: number, abi: any, fullyQualifiedName: string) => boolean,
rename?: (sourceName: string, contractName: string) => string,
}
Expand All @@ -27,7 +28,8 @@ declare module 'hardhat/types/config' {
only: string[],
except: string[],
spacing: number,
pretty: boolean,
pretty?: boolean,
format?: string,
filter: (abiElement: any, index: number, abi: any, fullyQualifiedName: string) => boolean,
rename: (sourceName: string, contractName: string) => string,
}[]
Expand Down
12 changes: 12 additions & 0 deletions index.js
Expand Up @@ -18,6 +18,7 @@ const DEFAULT_CONFIG = {
pretty: false,
filter: () => true,
// `rename` is not defaulted as it may depend on `flat` option
// `format` is not defaulted as it may depend on `pretty` option
};

function validate(config, key, type) {
Expand Down Expand Up @@ -49,6 +50,10 @@ extendConfig(function (config, userConfig) {
throw new HardhatPluginError(PLUGIN_NAME, '`flat` & `rename` config cannot be specified together');
}

if (conf.pretty && typeof conf.format !== 'undefined') {
throw new HardhatPluginError(PLUGIN_NAME, '`pretty` & `format` config cannot be specified together');
}

if (conf.flat) {
conf.rename = (sourceName, contractName) => contractName;
}
Expand All @@ -58,6 +63,13 @@ extendConfig(function (config, userConfig) {
}

validate(conf, 'rename', 'function');

if (!config.format) {
conf.format = conf.pretty ? "minimal": "json";
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ItsNickBarry let me know if you are good with this approach.
pretty internally just uses format and that way code is less messier in export_abi.js file

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, this is the best way. Similar to flat/rename above.


validate(conf, 'format', 'string');

return conf;
});
});
10 changes: 7 additions & 3 deletions tasks/export_abi.js
Expand Up @@ -48,10 +48,14 @@ subtask(

if (!abi.length) return;

abi = abi.filter((element, index, array) => config.filter(element, index, array, fullName));

if (config.pretty) {
if (config.format == "json") {
abi = abi.filter((element, index, array) => config.filter(element, index, array, fullName));
} else if (config.format == "minimal") {
abi = new Interface(abi).format(FormatTypes.minimal);
} else if (config.format == "fullName") {
abi = new Interface(abi).format(FormatTypes.fullName);
} else {
throw new HardhatPluginError(`Unknown format: ${config.format}`);
thelostone-mc marked this conversation as resolved.
Show resolved Hide resolved
ItsNickBarry marked this conversation as resolved.
Show resolved Hide resolved
}

const destination = path.resolve(
Expand Down