Skip to content

Commit

Permalink
Merge pull request #42 from thelostone-mc/master
Browse files Browse the repository at this point in the history
feat: add format parameter to hardhat-abi-exporter
  • Loading branch information
ItsNickBarry committed Jul 3, 2022
2 parents 52d5adc + 846bbaa commit 90635b7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
19 changes: 19 additions & 0 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,6 +45,7 @@ abiExporter: {
only: [':ERC20$'],
spacing: 2,
pretty: true,
format: "minimal",
}

// or
Expand All @@ -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:
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";
}

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

return conf;
});
});
6 changes: 5 additions & 1 deletion tasks/export_abi.js
Expand Up @@ -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(
Expand Down

0 comments on commit 90635b7

Please sign in to comment.