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 contract types map to hardhat-viem #5398

Merged
merged 5 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-rabbits-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/hardhat-viem": patch
---

Added `ContractTypesMap` to simplify contract type imports (thanks @beepidibop!)
12 changes: 12 additions & 0 deletions packages/hardhat-viem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ This allows you to deploy a contract linked to the `ExampleLib` library at the a

To deploy a contract, all libraries must be linked. An error will be thrown if any libraries are missing.

#### Using `ContractTypesMap` for easier contract type imports

To simplify importing contract types in `hardhat-viem`, you can use the `ContractTypesMap`. This map contains the contract types of all contracts in your project, indexed by their names.

```typescript
import { ContractTypesMap } from "hardhat/types/artifacts";

const contract: ContractTypesMap["ContractName"];
```

This reduces the need for multiple imports and makes your code cleaner and easier to manage.

## Usage

There are no additional steps you need to take for this plugin to work.
Expand Down
16 changes: 16 additions & 0 deletions packages/hardhat-viem/src/internal/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ declare module "hardhat/types/artifacts" {
.map((name) => `${name}: never;`)
.join("\n ")}
}

interface ContractTypesMap {
${Array.from(duplicateContractNames)
.map((name) => `${name}: never;`)
.join("\n ")}
}
}
`;
}
Expand Down Expand Up @@ -260,6 +266,7 @@ function generateArtifactsDefinition(
return `${AUTOGENERATED_FILE_PREFACE}

import "hardhat/types/artifacts";
import type { GetContractReturnType } from "@nomicfoundation/hardhat-viem/types";

${contractTypeData
.map((ctd) => `import { ${ctd.typeName} } from "./${ctd.contractName}";`)
Expand All @@ -274,6 +281,15 @@ declare module "hardhat/types/artifacts" {
.map((ctd) => `["${ctd.fqn}"]: ${ctd.typeName};`)
.join("\n ")}
}

interface ContractTypesMap {
${contractTypeData
.map((ctd) => `["${ctd.contractName}"]: GetContractReturnType<${ctd.typeName}["abi"]>;`)
.join("\n ")}
${contractTypeData
.map((ctd) => `["${ctd.fqn}"]: GetContractReturnType<${ctd.typeName}["abi"]>;`)
.join("\n ")}
}
}
`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ declare module "hardhat/types/artifacts" {
interface ArtifactsMap {
B: never;
}

interface ContractTypesMap {
B: never;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// eslint-disable

import "hardhat/types/artifacts";
import type { GetContractReturnType } from "@nomicfoundation/hardhat-viem/types";

import { A$Type } from "./A";
import { B$Type } from "./B";
Expand All @@ -15,4 +16,11 @@ declare module "hardhat/types/artifacts" {
["contracts/A.sol:A"]: A$Type;
["contracts/A.sol:B"]: B$Type;
}

interface ContractTypesMap {
["A"]: GetContractReturnType<A$Type["abi"]>;
["B"]: GetContractReturnType<B$Type["abi"]>;
["contracts/A.sol:A"]: GetContractReturnType<A$Type["abi"]>;
["contracts/A.sol:B"]: GetContractReturnType<B$Type["abi"]>;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// eslint-disable

import "hardhat/types/artifacts";
import type { GetContractReturnType } from "@nomicfoundation/hardhat-viem/types";

import { B$Type } from "./B";
import { C$Type } from "./C";
Expand All @@ -15,4 +16,11 @@ declare module "hardhat/types/artifacts" {
["contracts/C.sol:B"]: B$Type;
["contracts/C.sol:C"]: C$Type;
}

interface ContractTypesMap {
["B"]: GetContractReturnType<B$Type["abi"]>;
["C"]: GetContractReturnType<C$Type["abi"]>;
["contracts/C.sol:B"]: GetContractReturnType<B$Type["abi"]>;
["contracts/C.sol:C"]: GetContractReturnType<C$Type["abi"]>;
}
}
Loading