Skip to content

Commit

Permalink
patch(hardhat-core,hardhat-ethers): moved linkBytecode function & lib…
Browse files Browse the repository at this point in the history
…rary related typing to hardhat-core
  • Loading branch information
tenchleb committed Apr 16, 2024
1 parent 3b36d76 commit 18740ec
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 37 deletions.
1 change: 1 addition & 0 deletions packages/hardhat-core/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./provider";
export * from "./runtime";
export * from "./artifacts";
export * from "./builtin-tasks";
export * from "./libraries";
9 changes: 9 additions & 0 deletions packages/hardhat-core/src/types/libraries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface Libraries<Address = string> {
[libraryName: string]: Address;
}

export interface Link {
sourceName: string;
libraryName: string;
address: string;
}
18 changes: 18 additions & 0 deletions packages/hardhat-core/src/utils/link-bytecode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Artifact, Link } from "../types";

export function linkBytecode(artifact: Artifact, libraries: Link[]): string {
let bytecode = artifact.bytecode;

// TODO: measure performance impact
for (const { sourceName, libraryName, address } of libraries) {
const linkReferences = artifact.linkReferences[sourceName][libraryName];
for (const { start, length } of linkReferences) {
bytecode =
bytecode.substring(0, 2 + start * 2) +
address.substring(2) +
bytecode.substring(2 + (start + length) * 2);
}
}

return bytecode;
}
37 changes: 8 additions & 29 deletions packages/hardhat-ethers/src/internal/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import type { ethers as EthersT } from "ethers";
import type { HardhatEthersSigner } from "../signers";
import type {
DeployContractOptions,
FactoryOptions,
Libraries,
} from "../types";
import type { DeployContractOptions, FactoryOptions } from "../types";

import { Artifact, HardhatRuntimeEnvironment } from "hardhat/types";
import {
Artifact,
HardhatRuntimeEnvironment,
Libraries,
Link,
} from "hardhat/types";
import { linkBytecode } from "hardhat/utils/link-bytecode";
import { HardhatEthersError } from "./errors";

interface Link {
sourceName: string;
libraryName: string;
address: string;
}

function isArtifact(artifact: any): artifact is Artifact {
const {
contractName,
Expand Down Expand Up @@ -418,20 +414,3 @@ export async function getContractAtFromArtifact(

return contract;
}

function linkBytecode(artifact: Artifact, libraries: Link[]): string {
let bytecode = artifact.bytecode;

// TODO: measure performance impact
for (const { sourceName, libraryName, address } of libraries) {
const linkReferences = artifact.linkReferences[sourceName][libraryName];
for (const { start, length } of linkReferences) {
bytecode =
bytecode.substr(0, 2 + start * 2) +
address.substr(2) +
bytecode.substr(2 + (start + length) * 2);
}
}

return bytecode;
}
4 changes: 2 additions & 2 deletions packages/hardhat-ethers/src/internal/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import type {
FactoryOptions as FactoryOptionsT,
getContractFactory as getContractFactoryT,
HardhatEthersHelpers,
Libraries as LibrariesT,
} from "../types";

import "hardhat/types/runtime";
import { Libraries as LibrariesT } from "hardhat/types/libraries";

declare module "hardhat/types/runtime" {
interface HardhatRuntimeEnvironment {
Expand All @@ -17,7 +17,7 @@ declare module "hardhat/types/runtime" {
// Beware, adding new types to any hardhat type submodule is not a good practice in a Hardhat plugin.
// Doing so increases the risk of a type clash with another plugin.
// Removing any of these three types is a breaking change.
type Libraries = LibrariesT;
type Libraries = LibrariesT<ethers.Addressable | string>;
type FactoryOptions = FactoryOptionsT;
// eslint-disable-next-line @typescript-eslint/naming-convention
type getContractFactory = typeof getContractFactoryT;
Expand Down
9 changes: 3 additions & 6 deletions packages/hardhat-ethers/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type * as ethers from "ethers";
import type { Artifact } from "hardhat/types";
import type { Artifact } from "hardhat/types/artifacts";
import type { Libraries } from "hardhat/types/libraries";
import type { HardhatEthersProvider } from "../internal/hardhat-ethers-provider";
import type { HardhatEthersSigner } from "../signers";

export interface Libraries {
[libraryName: string]: string | ethers.Addressable;
}

export interface FactoryOptions {
signer?: ethers.Signer;
libraries?: Libraries;
libraries?: Libraries<ethers.Addressable | string>;
}

export type DeployContractOptions = FactoryOptions & ethers.Overrides;
Expand Down

0 comments on commit 18740ec

Please sign in to comment.