diff --git a/README.md b/README.md index 3bf66aa..80a1c30 100644 --- a/README.md +++ b/README.md @@ -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` | @@ -44,6 +45,7 @@ abiExporter: { only: [':ERC20$'], spacing: 2, pretty: true, + format: "minimal", } // or @@ -58,6 +60,23 @@ abiExporter: [ pretty: false, }, ] + +// or + +abiExporter: [ + { + path: './abi/json', + format: "json", + }, + { + path: './abi/minimal', + format: "minimal", + }, + { + path: './abi/fullName', + format: "fullName", + }, +] ``` The included Hardhat tasks may be run manually: diff --git a/index.d.ts b/index.d.ts index 074655d..7b44cdf 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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, } @@ -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, }[] diff --git a/index.js b/index.js index 78b2e71..5c8730e 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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; } @@ -58,6 +63,13 @@ extendConfig(function (config, userConfig) { } validate(conf, 'rename', 'function'); + + if (!config.format) { + conf.format = conf.pretty ? "minimal": "json"; + } + + validate(conf, 'format', 'string'); + return conf; }); }); diff --git a/tasks/export_abi.js b/tasks/export_abi.js index bb1a316..a841497 100644 --- a/tasks/export_abi.js +++ b/tasks/export_abi.js @@ -50,8 +50,12 @@ subtask( abi = abi.filter((element, index, array) => config.filter(element, index, array, fullName)); - if (config.pretty) { + if (config.format == "minimal") { abi = new Interface(abi).format(FormatTypes.minimal); + } else if (config.format == "fullName") { + abi = new Interface(abi).format(FormatTypes.fullName); + } else if (config.format != "json") { + throw new HardhatPluginError(`Unknown format: ${config.format}`); } const destination = path.resolve(