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: Allow renaming exported ABIs #25

Merged
merged 7 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Add configuration under the `abiExporter` key:
| `spacing` | number of spaces per indentation level of formatted output | `2` |
| `pretty` | whether to use interface-style formatting of output for better readability | `false` |
| `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` |

Note that the configuration formatted as either a single `Object`, or an `Array` of objects. An `Array` may be used to specify multiple outputs.

Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface AbiExporterUserConfig {
spacing?: number,
pretty?: boolean,
filter?: (abiElement: any, index: number, abi: any, fullyQualifiedName: string) => boolean,
rename?: (sourceName: string, contractName: string) => string,
}

declare module 'hardhat/types/config' {
Expand All @@ -28,6 +29,7 @@ declare module 'hardhat/types/config' {
spacing: number,
pretty: boolean,
filter: (abiElement: any, index: number, abi: any, fullyQualifiedName: string) => boolean,
rename: (sourceName: string, contractName: string) => string,
}[]
}
}
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path');
const { extendConfig } = require('hardhat/config');
const { HardhatPluginError } = require('hardhat/plugins');
const { name: PLUGIN_NAME } = require('./package.json')
const { name: PLUGIN_NAME } = require('./package.json');

require('./tasks/clear_abi.js');
require('./tasks/export_abi.js');
Expand All @@ -16,12 +17,13 @@ const DEFAULT_CONFIG = {
spacing: 2,
pretty: false,
filter: () => true,
// `rename` is not defaulted as it may depend on `flat` option
};

function validate(config, key, type) {
if (type === 'array') {
if (!Array.isArray(config[key])) {
throw new HardhatPluginError(PLUGIN_NAME, `\`${key}\` config must be an ${type}`)
throw new HardhatPluginError(PLUGIN_NAME, `\`${key}\` config must be an ${type}`);
}
} else {
if (typeof config[key] !== type) {
Expand All @@ -42,6 +44,20 @@ extendConfig(function (config, userConfig) {
validate(conf, 'spacing', 'number');
validate(conf, 'pretty', 'boolean');
validate(conf, 'filter', 'function');

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

if (conf.flat) {
conf.rename = (sourceName, contractName) => contractName;
}

if (!conf.rename) {
conf.rename = (sourceName, contractName) => path.join(sourceName, contractName);
}

validate(conf, 'rename', 'function');
return conf;
});
});
3 changes: 1 addition & 2 deletions tasks/export_abi.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ subtask(

const destination = path.resolve(
outputDirectory,
config.flat ? '' : sourceName,
contractName
config.rename(sourceName, contractName)
) + '.json';

outputData.push({ abi, destination });
Expand Down