Skip to content

Commit

Permalink
extend hardhat config with compiler settings of cloned contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
Troublor committed Jul 7, 2024
1 parent 010ff90 commit ff8f48e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/clone/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ export async function cloneContract(
source_meta.sourceTree.dump(dumpDir);

if (!apiKey) {
opts.quiet || console.log("Wait for 5 second before next request to Etherscan to avoid rate limiting");
opts.quiet ||
console.log(
'Wait for 5 second before next request to Etherscan to avoid rate limiting',
);
await new Promise((resolve) => setTimeout(resolve, 5000));
}

Expand All @@ -102,12 +105,21 @@ export async function cloneContract(
let metas: CloneMetadata[] = [];
if (fs.existsSync(metaFile)) {
const metaRaw = JSON.parse(fs.readFileSync(metaFile, 'utf-8'));
assert.ok(metaRaw instanceof Array, 'Invalid metadata file, expected an array of CloneMetadata');
metas = metaRaw.map((meta: unknown) => plainToInstance(CloneMetadata, meta))
assert.ok(
metaRaw instanceof Array,
'Invalid metadata file, expected an array of CloneMetadata',
);
metas = metaRaw.map((meta: unknown) =>
plainToInstance(CloneMetadata, meta),
);
}
metas.push(cloneMetadata);
fs.writeFileSync(
path.join(hre.config.paths.root, CloneMetadata.META_FILE),
JSON.stringify(metas.map((meta) => instanceToPlain(meta)), null, 2),
JSON.stringify(
metas.map((meta) => instanceToPlain(meta)),
null,
2,
),
);
}
43 changes: 43 additions & 0 deletions src/config-extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import fs from 'node:fs';
import { CloneMetadata } from './clone';
import { extendConfig } from 'hardhat/config';
import { HardhatConfig } from 'hardhat/types';
import { plainToInstance } from 'class-transformer';
import assert from 'node:assert';

extendConfig((config: HardhatConfig) => {
// We apply our default config here. Any other kind of config resolution
// or normalization should be placed here.
//
// `config` is the resolved config, which will be used during runtime and
// you should modify.
// `userConfig` is the config as provided by the user. You should not modify
// it.
//
// If you extended the `HardhatConfig` type, you need to make sure that
// executing this function ensures that the `config` object is in a valid
// state for its type, including its extensions. For example, you may
// need to apply a default value, like in this example.

if (!fs.existsSync(config.paths.root + CloneMetadata)) {
// if there is no metadata file, there is no cloned contract, so we don't need to do anything
return;
}

// We need to load the metadata file
const metadataRaw = JSON.parse(
fs.readFileSync(config.paths.root + CloneMetadata, 'utf8'),
);
assert.ok(
metadataRaw instanceof Array,
'Invalid metadata file, expected an array of CloneMetadata',
);
const cloneMetas: CloneMetadata[] = metadataRaw.map((meta: unknown) =>
plainToInstance(CloneMetadata, meta),
);

// We need to override SolcConfig for the cloned contracts.
for (const cloneMeta of cloneMetas) {
config.solidity.overrides[cloneMeta.path] = cloneMeta.solcConfig;
}
});
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { task } from 'hardhat/config';
import * as types from './types';
import { cloneContract } from './clone';

// This import is needed to let the TypeScript compiler know that it should include your type
// extensions in your npm package's types file.
import './type-extensions';
// This import is needed to extend Hardhat config with compiler settings of cloned contracts.
import './config-extensions';

import { task } from 'hardhat/config';
import * as types from './types';
import { cloneContract } from './clone';
import { findChain, supportedChains } from './chain';

task('clone', 'Clone on-chain contract into current Hardhat project')
Expand Down

0 comments on commit ff8f48e

Please sign in to comment.