Skip to content

Commit

Permalink
[ethers-v6] Support node16 moduleSuffix in ethers-v6 target (#863)
Browse files Browse the repository at this point in the history
Co-authored-by: chmanie <code@chmanie.com>
  • Loading branch information
krzkaczor and chmanie authored Aug 20, 2023
1 parent ec6b860 commit 64d18f7
Show file tree
Hide file tree
Showing 14 changed files with 1,676 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-mayflies-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@typechain/ethers-v6': minor
---

Add support for ESM module resolutions using the `--node16-modules` flag in the `ethers-v6` target.
19 changes: 19 additions & 0 deletions examples/ethers-v6-nodenext/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# TypeChain x Ethers-v6 example

Note: examples in this dir require building monorepo first

```sh
# in the root of monorepo
pnpm i
pnpm build
```

## Running

```sh
pnpm install # it will automatically run TypeChain types generation

# pnpm generate-types to manually regenerate them

pnpm start
```
277 changes: 277 additions & 0 deletions examples/ethers-v6-nodenext/abi/dai.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
[

{
"inputs": [{ "internalType": "uint256", "name": "chainId_", "type": "uint256" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{ "indexed": true, "internalType": "address", "name": "src", "type": "address" },
{ "indexed": true, "internalType": "address", "name": "guy", "type": "address" },
{ "indexed": false, "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "Approval",
"type": "event"
},
{
"anonymous": true,
"inputs": [
{ "indexed": true, "internalType": "bytes4", "name": "sig", "type": "bytes4" },
{ "indexed": true, "internalType": "address", "name": "usr", "type": "address" },
{ "indexed": true, "internalType": "bytes32", "name": "arg1", "type": "bytes32" },
{ "indexed": true, "internalType": "bytes32", "name": "arg2", "type": "bytes32" },
{ "indexed": false, "internalType": "bytes", "name": "data", "type": "bytes" }
],
"name": "LogNote",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{ "indexed": true, "internalType": "address", "name": "src", "type": "address" },
{ "indexed": true, "internalType": "address", "name": "dst", "type": "address" },
{ "indexed": false, "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "Transfer",
"type": "event"
},
{
"constant": true,
"inputs": [],
"name": "DOMAIN_SEPARATOR",
"outputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "PERMIT_TYPEHASH",
"outputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{ "internalType": "address", "name": "", "type": "address" },
{ "internalType": "address", "name": "", "type": "address" }
],
"name": "allowance",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "usr", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "approve",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [{ "internalType": "address", "name": "", "type": "address" }],
"name": "balanceOf",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "usr", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "burn",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{ "internalType": "address", "name": "guy", "type": "address" }],
"name": "deny",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "usr", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "mint",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "src", "type": "address" },
{ "internalType": "address", "name": "dst", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "move",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [{ "internalType": "string", "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{ "internalType": "address", "name": "", "type": "address" }],
"name": "nonces",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "holder", "type": "address" },
{ "internalType": "address", "name": "spender", "type": "address" },
{ "internalType": "uint256", "name": "nonce", "type": "uint256" },
{ "internalType": "uint256", "name": "expiry", "type": "uint256" },
{ "internalType": "bool", "name": "allowed", "type": "bool" },
{ "internalType": "uint8", "name": "v", "type": "uint8" },
{ "internalType": "bytes32", "name": "r", "type": "bytes32" },
{ "internalType": "bytes32", "name": "s", "type": "bytes32" }
],
"name": "permit",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "usr", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "pull",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "usr", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "push",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{ "internalType": "address", "name": "guy", "type": "address" }],
"name": "rely",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [{ "internalType": "string", "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "dst", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "transfer",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "src", "type": "address" },
{ "internalType": "address", "name": "dst", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "transferFrom",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "version",
"outputs": [{ "internalType": "string", "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{ "internalType": "address", "name": "", "type": "address" }],
"name": "wards",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
23 changes: 23 additions & 0 deletions examples/ethers-v6-nodenext/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "example-ethers-v6-nodenext",
"private": true,
"version": "0.0.1",
"main": "index.js",
"type": "module",
"license": "MIT",
"scripts": {
"generate-types": "typechain --target=ethers-v6 --node16-modules 'abi/*.json'",
"start": "ts-node ./src/index.ts",
"typecheck": "pnpm generate-types && tsc --noEmit"
},
"devDependencies": {
"@typechain/ethers-v6": "workspace:^0.4.3",
"@types/bn.js": "^5.1.0",
"ts-node": "^10.7.0",
"typechain": "workspace:^8.3.1",
"typescript": "^4.7"
},
"dependencies": {
"ethers": "^6.1.0"
}
}
28 changes: 28 additions & 0 deletions examples/ethers-v6-nodenext/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ethers } from 'ethers'

import { Dai__factory } from '../types/ethers-contracts/factories/Dai__factory.js'

const RPC_HOST = 'https://mainnet.infura.io/v3/6d6c70e65c77429482df5b64a4d0c943'
const DAI_ADDRESS = '0x6B175474E89094C44Da98b954EedeAC495271d0F'
const BLOCK_NUMBER = 13730326

async function main() {
const provider = new ethers.JsonRpcProvider(RPC_HOST)
const dai = Dai__factory.connect(DAI_ADDRESS, provider)
const balance = await dai.balanceOf('0x70b144972C5Ef6CB941A5379240B74239c418CD4')

console.log(`Our DAI balance is: ${ethers.formatEther(balance)}`)

console.log(`Listing Transfer events for block ${BLOCK_NUMBER}`)
const eventsFilter = dai.filters.Transfer()
const events = await dai.queryFilter(eventsFilter, BLOCK_NUMBER, BLOCK_NUMBER)

for (const event of events) {
console.log(`${event.args.src} -> ${event.args.dst} | ${ethers.formatEther(event.args.wad)} DAI`)
}
}

main().catch((e) => {
console.error(e)
process.exit(1)
})
12 changes: 12 additions & 0 deletions examples/ethers-v6-nodenext/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"lib": ["ES2018", "DOM"],
"module": "NodeNext",
"moduleResolution": "node16",
"strict": true,
"target": "ES2018",
"sourceMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
}
}
Loading

0 comments on commit 64d18f7

Please sign in to comment.