Skip to content

Commit

Permalink
Merge pull request #5398 from beepidibop/Add-ContractTypesMap
Browse files Browse the repository at this point in the history
Add contract types map to hardhat-viem
  • Loading branch information
schaable authored Jun 25, 2024
2 parents a0c1d90 + 890bdee commit 53c422c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
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
22 changes: 22 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,21 @@ 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"]>;
}
}

0 comments on commit 53c422c

Please sign in to comment.