Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add artifact functions in ethers getContractFactoryFromArtifact and getContractAtFromArtifact #2116

Merged
merged 4 commits into from
Jan 6, 2022

Conversation

kanej
Copy link
Member

@kanej kanej commented Nov 30, 2021

In hardhat-ethers, allow passing of artifacts (resolved via custom code) into an equivalent of the getContractFactory call, this is instead of resolving by contract name.

The intention here is to support custom contract artifact resolution but still allow the leveraging of hardhat-ethers.

Two new functions have been added to support this without altering the existing signature:

  • getContractFactoryFromArtifact
  • getContractAtFromArtifact

Relates to #1716

Example Usage

import { ethers } from "hardhat";
import { Artifacts } from "hardhat/internal/artifacts";
import { Artifact } from "hardhat/types";

export async function getArtifact(contract: string): Promise<Artifact> {
  const artifactsPath: string = "./artifacts";

  const artifacts = new Artifacts(artifactsPath);
  return artifacts.readArtifact(contract);
}

async function main() {
  const artifact = await getArtifact("Greeter");

  const Factory = await ethers.getContractFactoryFromArtifact(artifact);

  const greeter = await Factory.deploy("Hello, Hardhat!");

  await greeter.deployed();

  console.log("Greeter deployed to:", greeter.address);

  const response = await greeter.greet();

  console.log("First Response", response);

  const alternateGreeter = await ethers.getContractAtFromArtifact(
    artifact,
    greeter.address
  );

  const secondResponse = await alternateGreeter.greet();

  console.log("Second Response", secondResponse);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

TODO

  • support artifact in signature of getContractFactoryFromArtifact
  • support artifact in signature of getContractAtFromArtifact
  • resolve type issue in consuming lib
  • determine and update any docs affected

@changeset-bot
Copy link

changeset-bot bot commented Nov 30, 2021

🦋 Changeset detected

Latest commit: 1f796dc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@nomiclabs/hardhat-ethers Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@kanej kanej self-assigned this Dec 1, 2021
@kanej kanej added the type:feature Feature request label Dec 1, 2021
@kanej kanej force-pushed the add-contract-factory-overload-for-artifact branch from da3dd8c to c81c52f Compare December 1, 2021 14:08
@kanej kanej changed the base branch from development to master December 1, 2021 14:09
@kanej kanej force-pushed the add-contract-factory-overload-for-artifact branch from 0733aa5 to 22fbd89 Compare December 1, 2021 16:11
@kanej kanej marked this pull request as ready for review December 1, 2021 16:29
@kanej kanej requested a review from fvictorio December 1, 2021 16:29
@kanej kanej force-pushed the add-contract-factory-overload-for-artifact branch from 49d00f6 to 6fd363a Compare December 7, 2021 11:12
@kanej kanej changed the title Add overloads for artifact in ethers getContractFactory and getContractAt Add artifact functions in ethers getContractFactoryFromArtifact and getContractAtFromArtifact Dec 7, 2021
…romArtifact`

Allow passing of artifacts (resolved via custom code) into equivalents of `getContractFactory` and `getContractAt` calls, this is to support custom artifact resolution.

Relates to #1716.
@kanej kanej force-pushed the add-contract-factory-overload-for-artifact branch from 6fd363a to 6afeeff Compare December 7, 2021 11:30
@kanej
Copy link
Member Author

kanej commented Dec 7, 2021

I have test locally with newly created advanced typescript project, and we no longer get the type clash with typechain overriding the method signatures - getContractFactoryFromArtifact, getContractAtFromArtifact

@kanej kanej requested a review from fvictorio January 3, 2022 09:37
@kanej kanej merged commit 6ce1951 into master Jan 6, 2022
@kanej kanej deleted the add-contract-factory-overload-for-artifact branch January 6, 2022 14:50
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type:feature Feature request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[hardhat-ethers] getContractFactory and getContractAt should have an overload that take an Artifact
2 participants