Skip to content

Commit f6e6153

Browse files
authored
Add the Contract Type Generation section
1 parent f6548ce commit f6e6153

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/src/content/hardhat-runner/docs/advanced/using-viem.md

+20
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,26 @@ Use the `hre.viem` object to get these helpers, similar to how clients are used.
124124

125125
4. Open your terminal and run `npx hardhat run scripts/contracts.ts`. This will deploy the `MyToken` contract, call the `increaseSupply()` function, and display the new supply in your terminal.
126126

127+
#### Contract Type Generation
128+
129+
The proper types for each contract are generated during compilation. These types are used to overload the `hardhat-viem` types and improve type checking and suggestions. For example, if you copy and paste the following code at the end of the `main()` function of `scripts/contracts.ts`, TypeScript would highlight it as an error:
130+
131+
```tsx
132+
// The amount is required as a parameter
133+
// TS Error: Expected 1-2 arguments, but got 0.
134+
await myToken.write.increaseSupply();
135+
136+
// There is no setSupply function in the MyToken contract
137+
// TS Error: Property 'setSupply' does not exist on type...
138+
const tokenPrice = await myToken.write.setSupply([5000000n]);
139+
140+
// The first argument of the constructor arguments is expected to be an bigint
141+
// TS Error: No overload matches this call.
142+
const myToken2 = await hre.viem.deployContract("MyToken", ["1000000"]);
143+
```
144+
145+
If you want to learn more about working with contracts, you can visit the [`hardhat-viem` plugin site](/hardhat-runner/plugins/nomicfoundation-hardhat-viem#contracts) and [Viem's official site](https://viem.sh/docs/contract/getContract.html).
146+
127147
### Testing
128148

129149
In this example, we’ll test the `MyToken` contract, covering scenarios like supply increase and expected operation reverts.

0 commit comments

Comments
 (0)