|
1 | 1 | import { utils } from "ethers";
|
2 |
| -import { artifacts } from "hardhat"; |
3 |
| -import { Artifact } from "hardhat/types"; |
4 | 2 | import path from "path";
|
5 |
| -import globby from "globby"; |
6 | 3 |
|
7 |
| -// If libraryName corresponds to more than one artifact (e.g there are |
8 |
| -// duplicate contract names in the project), `readArtifactSync` |
9 |
| -// will throw. In such cases it"s necessary to pass this method the fully qualified |
10 |
| -// contract name. ex: `contracts/mocks/LibraryMock.sol:LibraryMock` |
| 4 | +// Converts a fully qualified contract name in a bytecode link id. |
| 5 | +// (A fully qualified name looks like: `contracts/mocks/LibraryMock.sol:LibraryMock`) |
11 | 6 | export function convertLibraryNameToLinkId(libraryName: string): string {
|
12 |
| - let artifact; |
13 |
| - let fullyQualifiedName; |
14 |
| - |
15 |
| - if (libraryName.includes(path.sep) && libraryName.includes(":")) { |
16 |
| - fullyQualifiedName = libraryName; |
17 |
| - } else { |
18 |
| - artifact = getArtifact(libraryName); |
19 |
| - fullyQualifiedName = `${artifact.sourceName}:${artifact.contractName}`; |
20 |
| - } |
21 |
| - |
22 |
| - const hashedName = utils.keccak256(utils.toUtf8Bytes(fullyQualifiedName)); |
23 |
| - return `__$${hashedName.slice(2).slice(0, 34)}$__`; |
24 |
| -} |
25 |
| - |
26 |
| -// Tries to resolve via hardhat artifacts helpers, then by searching for appropriately |
27 |
| -// named jsons in the root `external` folder |
28 |
| -function getArtifact(libraryName: string): Artifact { |
29 |
| - try { |
30 |
| - return artifacts.readArtifactSync(libraryName); |
31 |
| - } catch (e) { |
32 |
| - /* ignore */ |
33 |
| - } |
34 |
| - |
35 |
| - const files = globby.sync("external", { |
36 |
| - expandDirectories: { extensions: ["json"], }, |
37 |
| - }); |
38 |
| - |
39 |
| - const matches = files.filter(f => f.includes(`/${libraryName}.json`)); |
40 |
| - |
41 |
| - if (!matches.length) { |
42 |
| - throw new Error(`Unable to find artifact for '${libraryName}' while linking.`); |
43 |
| - } |
44 |
| - |
45 |
| - if (matches.length > 1) { |
| 7 | + if (!(libraryName.includes(path.sep) && libraryName.includes(":"))) { |
46 | 8 | throw new Error(
|
47 |
| - `Unable to resolve '${libraryName}' while linking. ` + |
48 |
| - `(More than one file name matches in 'external/')` |
| 9 | + "Converting library name to link id requires a fully qualified " + |
| 10 | + "contract name. Example: `contracts/mocks/LibraryMock.sol:LibraryMock`" |
49 | 11 | );
|
50 | 12 | }
|
51 | 13 |
|
52 |
| - const pathToArtifact = path.join(process.cwd(), matches[0]); |
53 |
| - return require(pathToArtifact); |
| 14 | + const hashedName = utils.keccak256(utils.toUtf8Bytes(libraryName)); |
| 15 | + return `__$${hashedName.slice(2).slice(0, 34)}$__`; |
54 | 16 | }
|
0 commit comments