Skip to content

Commit

Permalink
Add support for ethers v6 in @typechain/hardhat #843 (#845)
Browse files Browse the repository at this point in the history
Co-authored-by: Franco Victorio <victorio.franco@gmail.com>
  • Loading branch information
krzkaczor and fvictorio authored Jun 5, 2023
1 parent f6b3b0b commit 2380a8f
Show file tree
Hide file tree
Showing 67 changed files with 1,497 additions and 1,597 deletions.
6 changes: 6 additions & 0 deletions .changeset/tough-radios-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@typechain/hardhat': major
'@typechain/ethers-v6': minor
---

Added support for ethers v6 in @typechain/hardhat
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
npm install --save-dev typechain
```

You will also need to install a desired target for example `@typechain/ethers-v5`. [Learn more about targets](#targets-)
You will also need to install a desired target for example `@typechain/ethers-v6`. [Learn more about targets](#targets-)

_Take note, that code generated by TypeChain requires TypeScript version 4.3 or newer._

Expand All @@ -50,6 +50,7 @@ _Take note, that code generated by TypeChain requires TypeScript version 4.3 or
| ------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| [`typechain`](/packages/typechain) | [![npm](https://img.shields.io/npm/v/typechain.svg)](https://www.npmjs.com/package/typechain) | Core package | - |
| [`@typechain/ethers-v5`](/packages/target-ethers-v5) | [![npm](https://img.shields.io/npm/v/@typechain/ethers-v5.svg)](https://www.npmjs.com/package/@typechain/ethers-v5) | Ethers ver 5 support (⚠️ requires TS 4.0 >=) | [example](./examples/ethers-v5) |
| [`@typechain/ethers-v6`](/packages/target-ethers-v6) | [![npm](https://img.shields.io/npm/v/@typechain/ethers-v6.svg)](https://www.npmjs.com/package/@typechain/ethers-v6) | Ethers ver 6 support (⚠️ requires TS 4.0 >=) | [example](./examples/ethers-v6) |
| [`@typechain/starknet.js`](/packages/target-starknet) | [![npm](https://img.shields.io/npm/v/@typechain/starknet.svg)](https://www.npmjs.com/package/@typechain/starknet) | Starknet.js ver 3.9 | |
| [`@typechain/truffle-v5`](/packages/target-truffle-v5) | [![npm](https://img.shields.io/npm/v/@typechain/truffle-v5.svg)](https://www.npmjs.com/package/@typechain/truffle-v5) | Truffle ver 5 support | [example](./examples/truffle-v5) |
| [`@typechain/web3-v1`](/packages/target-web3-v1) | [![npm](https://img.shields.io/npm/v/@typechain/web3-v1.svg)](https://www.npmjs.com/package/@typechain/web3-v1) | Web3 ver 1 support | [example](./examples/web3-v1) |
Expand All @@ -72,25 +73,25 @@ _Note: If you use hardhat just use
[hardhat plugin](https://github.com/ethereum-ts/TypeChain/tree/master/packages/hardhat)._

```
typechain --target=(ethers-v5|truffle-v4|truffle-v5|web3-v1|path-to-custom-target) [glob]
typechain --target=(ethers-v5|ethers-v6|truffle-v4|truffle-v5|web3-v1|path-to-custom-target) [glob]
```

- `glob` - pattern that will be used to find ABIs, remember about adding quotes: `typechain "**/*.json"`, examples:
`./abis/**/*.abi`, `./abis/?(Oasis.abi|OasisHelper.abi)`.
- `--target` - ethers-v5, truffle-v4, truffle-v5, web3-v1 or path to your custom target. Typechain will try to load
package named: `@typechain/${target}`, so make sure that desired package is installed.
- `--target` - ethers-v5, ethers-v6, truffle-v4, truffle-v5, web3-v1 or path to your custom target. Typechain will try
to load package named: `@typechain/${target}`, so make sure that desired package is installed.
- `--out-dir` (optional) - put all generated files to a specific dir.
- `--always-generate-overloads` (optional) - some targets won't generate unnecessary types for overloaded functions by
default, this option forces to always generate them
- `--discriminate-types` (optional) - ethers-v5 will add an artificial field `contractName` that helps discriminate
between contracts
- `--discriminate-types` (optional) - ethers-v5 and ethers-v6 will add an artificial field `contractName` that helps
discriminate between contracts

TypeChain always will rewrite existing files. You should not commit them. Read more in FAQ section.

Example:

```
typechain --target ethers-v5 --out-dir app/contracts './node_modules/neufund-contracts/build/contracts/*.json'
typechain --target ethers-v6 --out-dir app/contracts './node_modules/neufund-contracts/build/contracts/*.json'
```

## Videos
Expand Down Expand Up @@ -123,13 +124,15 @@ That's it! Now, you can simply import typings, check out our examples for more d

## Targets 🎯

### Ethers.js v5
### Ethers.js v6

Use `ethers-v5` target to generate wrappers for [ethers.js](https://github.com/ethers-io/ethers.js/) lib. To make it
Use `ethers-v6` target to generate wrappers for [ethers.js](https://github.com/ethers-io/ethers.js/) lib. To make it
work great with Hardhat, use [Hardhat plugin](https://github.com/ethereum-ts/TypeChain/tree/master/packages/hardhat).

If you use `nodenext` aka `node16modules` flip the flag `--node16-modules` to generate compatible typings.

If you are using Ethers.js v5, use the `@typechain/ethers-v5` target.

If you're using Ethers.js v4, you can find legacy `@typechain/ethers-v4` target on
[npm](https://www.npmjs.com/package/@typechain/ethers-v4) and commit
[`db551b5`](https://github.com/dethcrypto/TypeChain/tree/db551b5c5f70e86f3ca342551e9e0369d099cfa2).
Expand Down
5 changes: 2 additions & 3 deletions examples/ethers-v6/types/ethers-contracts/Dai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,8 @@ export namespace TransferEvent {
}

export interface Dai extends BaseContract {
connect(runner?: ContractRunner | null): BaseContract;
attach(addressOrName: AddressLike): this;
deployed(): Promise<this>;
connect(runner?: ContractRunner | null): Dai;
waitForDeployment(): Promise<this>;

interface: DaiInterface;

Expand Down
3 changes: 0 additions & 3 deletions examples/ethers-v6/types/ethers-contracts/common.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
import type {
FunctionFragment,
Typed,
Expand Down
4 changes: 3 additions & 1 deletion examples/hardhat/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import '@nomiclabs/hardhat-waffle'
import '@typechain/hardhat'
import '@nomicfoundation/hardhat-ethers'
import '@nomicfoundation/hardhat-chai-matchers'

import { HardhatUserConfig } from 'hardhat/types'

const config: HardhatUserConfig = {
Expand Down
8 changes: 4 additions & 4 deletions examples/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"typecheck": "pnpm compile && pnpm test && pnpm tsc --noEmit"
},
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@typechain/ethers-v5": "workspace:^11.0.0",
"@nomicfoundation/hardhat-ethers": "^3.0.0",
"@nomicfoundation/hardhat-chai-matchers": "^2.0.0",
"@typechain/ethers-v6": "workspace:^0.3.0",
"@typechain/hardhat": "workspace:^7.0.0",
"@types/chai": "^4.2.15",
"@types/chai-as-promised": "^7.1.3",
Expand All @@ -23,7 +23,7 @@
"chai-as-promised": "^7.1.1",
"dotenv": "^8.2.0",
"ethereum-waffle": "^3.2.2",
"ethers": "5.4.0",
"ethers": "6.3.0",
"@ethersproject/providers": "5.4.0",
"@ethersproject/contracts": "5.4.0",
"@ethersproject/abi": "5.4.0",
Expand Down
6 changes: 2 additions & 4 deletions examples/hardhat/test/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ describe('Counter', () => {
const signers = await ethers.getSigners()

// 2
const counterFactory = await ethers.getContractFactory('Counter', signers[0])
counter = await ethers.deployContract('Counter')

counter = await counterFactory.deploy()
await counter.deployed()
const initialCount = await counter.getCount()

// 3
expect(initialCount).to.eq(0)
expect(counter.address).to.properAddress
expect(await counter.getAddress()).to.properAddress
})

// 4
Expand Down
5 changes: 3 additions & 2 deletions examples/hardhat/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"compilerOptions": {
"target": "es5",
"target": "ES2015",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"outDir": "dist",
"resolveJsonModule": true
"resolveJsonModule": true,
"skipLibCheck": true
},
"include": ["./scripts", "./test", "./typechain-types"],
"files": ["./hardhat.config.ts"]
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-test/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// We load the plugin here.
import '@typechain/hardhat'
import '@nomiclabs/hardhat-ethers'
import '@nomicfoundation/hardhat-ethers'

import type { HardhatUserConfig } from 'hardhat/types'

Expand Down
6 changes: 3 additions & 3 deletions packages/hardhat-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"devDependencies": {
"@ethersproject/providers": "^5.4.7",
"@ethersproject/abi": "^5.4.7",
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@typechain/ethers-v5": "workspace:^11.0.0",
"@nomicfoundation/hardhat-ethers": "^3.0.0",
"@typechain/ethers-v6": "workspace:^0.3.0",
"@typechain/hardhat": "workspace:^7.0.0",
"ethers": "^5.4.7",
"ethers": "~6.3.0",
"hardhat": "^2.9.9",
"test-utils": "1.0.0",
"typechain": "workspace:^8.2.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-test/test/hardhat.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Interface } from '@ethersproject/abi'
import type { Interface } from 'ethers'
import { ethers } from 'hardhat'
import type { AssertTrue, IsExact } from 'test-utils'

Expand Down
Loading

0 comments on commit 2380a8f

Please sign in to comment.