|
| 1 | +import type * as viemT from "viem"; |
| 2 | +import type { Artifact } from "hardhat/types/artifacts"; |
| 3 | + |
| 4 | +import { |
| 5 | + AmbigousLibraryNameError, |
| 6 | + MissingLibraryAddressError, |
| 7 | + OverlappingLibraryNamesError, |
| 8 | + UnnecessaryLibraryLinkError, |
| 9 | +} from "./errors"; |
| 10 | + |
| 11 | +export interface Libraries<Address = string> { |
| 12 | + [libraryName: string]: Address; |
| 13 | +} |
| 14 | + |
| 15 | +export interface Link { |
| 16 | + sourceName: string; |
| 17 | + libraryName: string; |
| 18 | + address: string; |
| 19 | +} |
| 20 | + |
| 21 | +export async function linkBytecode( |
| 22 | + artifact: Artifact, |
| 23 | + libraries: Link[] |
| 24 | +): Promise<viemT.Hex> { |
| 25 | + const { isHex } = await import("viem"); |
| 26 | + let bytecode = artifact.bytecode; |
| 27 | + |
| 28 | + // TODO: measure performance impact |
| 29 | + for (const { sourceName, libraryName, address } of libraries) { |
| 30 | + const linkReferences = artifact.linkReferences[sourceName][libraryName]; |
| 31 | + for (const { start, length } of linkReferences) { |
| 32 | + bytecode = |
| 33 | + bytecode.substring(0, 2 + start * 2) + |
| 34 | + address.substring(2) + |
| 35 | + bytecode.substring(2 + (start + length) * 2); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return isHex(bytecode) ? bytecode : `0x${bytecode}`; |
| 40 | +} |
| 41 | + |
| 42 | +async function throwOnAmbigousLibraryNameOrUnnecessaryLink( |
| 43 | + contractName: string, |
| 44 | + libraries: Libraries<viemT.Address>, |
| 45 | + neededLibraries: Link[] |
| 46 | +) { |
| 47 | + for (const linkedLibraryName of Object.keys(libraries)) { |
| 48 | + const matchingLibraries = neededLibraries.filter( |
| 49 | + ({ sourceName, libraryName }) => |
| 50 | + libraryName === linkedLibraryName || |
| 51 | + `${sourceName}:${libraryName}` === linkedLibraryName |
| 52 | + ); |
| 53 | + |
| 54 | + if (matchingLibraries.length > 1) { |
| 55 | + throw new AmbigousLibraryNameError( |
| 56 | + contractName, |
| 57 | + linkedLibraryName, |
| 58 | + matchingLibraries.map( |
| 59 | + ({ sourceName, libraryName }) => `${sourceName}:${libraryName}` |
| 60 | + ) |
| 61 | + ); |
| 62 | + } else if (matchingLibraries.length === 0) { |
| 63 | + throw new UnnecessaryLibraryLinkError(contractName, linkedLibraryName); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +async function throwOnMissingLibrariesAddress( |
| 69 | + contractName: string, |
| 70 | + libraries: Libraries<viemT.Address>, |
| 71 | + neededLibraries: Link[] |
| 72 | +) { |
| 73 | + const missingLibraries = []; |
| 74 | + for (const { sourceName, libraryName } of neededLibraries) { |
| 75 | + const address = |
| 76 | + libraries[`${sourceName}:${libraryName}`] ?? libraries[libraryName]; |
| 77 | + |
| 78 | + if (address === undefined) { |
| 79 | + missingLibraries.push({ sourceName, libraryName }); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (missingLibraries.length > 0) { |
| 84 | + throw new MissingLibraryAddressError(contractName, missingLibraries); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +async function throwOnOverlappingLibraryNames( |
| 89 | + contractName: string, |
| 90 | + libraries: Libraries<viemT.Address>, |
| 91 | + neededLibraries: Link[] |
| 92 | +) { |
| 93 | + for (const { sourceName, libraryName } of neededLibraries) { |
| 94 | + if ( |
| 95 | + libraries[`${sourceName}:${libraryName}`] !== undefined && |
| 96 | + libraries[libraryName] !== undefined |
| 97 | + ) { |
| 98 | + throw new OverlappingLibraryNamesError(sourceName, libraryName); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +export async function resolveBytecodeWithLinkedLibraries( |
| 104 | + artifact: Artifact, |
| 105 | + libraries: Libraries<viemT.Address> |
| 106 | +): Promise<viemT.Hex> { |
| 107 | + const { linkReferences } = artifact; |
| 108 | + |
| 109 | + const neededLibraries: Link[] = []; |
| 110 | + for (const [sourceName, sourceLibraries] of Object.entries(linkReferences)) { |
| 111 | + for (const libraryName of Object.keys(sourceLibraries)) { |
| 112 | + neededLibraries.push({ |
| 113 | + sourceName, |
| 114 | + libraryName, |
| 115 | + address: |
| 116 | + libraries[`${sourceName}:${libraryName}`] ?? libraries[libraryName], |
| 117 | + }); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + await throwOnAmbigousLibraryNameOrUnnecessaryLink( |
| 122 | + artifact.contractName, |
| 123 | + libraries, |
| 124 | + neededLibraries |
| 125 | + ); |
| 126 | + await throwOnOverlappingLibraryNames( |
| 127 | + artifact.contractName, |
| 128 | + libraries, |
| 129 | + neededLibraries |
| 130 | + ); |
| 131 | + await throwOnMissingLibrariesAddress( |
| 132 | + artifact.contractName, |
| 133 | + libraries, |
| 134 | + neededLibraries |
| 135 | + ); |
| 136 | + |
| 137 | + return linkBytecode(artifact, neededLibraries); |
| 138 | +} |
0 commit comments