NomicFoundation / hardhat

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software.
https://hardhat.org
Other
7.22k stars 1.38k forks source link

Add an Easier Way to Import Contract Types for hardhat-viem #5375

Closed beepidibop closed 3 months ago

beepidibop commented 3 months ago

Describe the feature

Currently, in order to import contract types with hardhat-viem users need to import GetContractReturnType<ContractName$Type> from two sources (@nomicfoundation/hardhat-viem/types and the artifact path for the contract), the amount of imports grow as well if you need multiple contracts since you need one for each artifact, or you can use the ArtifactsMap but it's a handful to type GetContractReturnType<ArtifactsMap["ContractName"]> each time.

Adding a ContractTypesMap that returns GetContractReturnType<${ctd.typeName}["abi"]> to artifacts.d.ts by modifying generateArtifactsDefinition() and generateDuplicateArtifactsDefinition() can solve this problem. Users will only need to do import { ContractTypesMap } from "hardhat/types/artifacts"; and the contract type can be used like this

const contract: ContractTypesMap["ContractName"];

I have a fork ready for this as well, I've run the tests and updated the snapshots. Let me know if anything else needs to be done before I submit a PR.

Search terms

viem contract type

kanej commented 3 months ago

Hey @beepidibop, we would be open to a PR along these lines. Feel free to submit it.

beepidibop commented 3 months ago

Awesome, I've submitted the PR here.