Skip to content

Commit

Permalink
preserve nested paths (#633)
Browse files Browse the repository at this point in the history
* Add nested library ABIs and copy them in compile-contracts

* Preserve directory paths in abiParser::parse

* Generate barrel files from paths

* Generate types to nested directories

* Stop emitting self-recursive import

* Stop replacing underscores in generated identifiers

* WIP: Multiple fixes

* Change example target to @typechain/ethers-v5

* Remove ethers-v5 target from README

* Remove ethers-v4 target

* Fix reexports

* Fix imports

* Fix corrupt barrel reexport

* Autofix

* Run autofix

* Fix input root detection with Hardhat

* Run autofix

* Add changeset

* move path utils to `typechain` core package (#639)

* Preserve paths web3 (#640)

* Emit nested directory structures in Web3 v1 target

* Update paths in web3-v1 tests

* Fix case in import path

* Remove merge conflict marker
  • Loading branch information
hasparus authored Mar 11, 2022
1 parent d86d048 commit 3a8a99a
Show file tree
Hide file tree
Showing 150 changed files with 1,161 additions and 394 deletions.
12 changes: 12 additions & 0 deletions .changeset/chatty-toys-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@typechain/hardhat': major
'@typechain/ethers-v5': major
'@typechain/web3-v1': major
'typechain': major
---

Directory tree in generated types now reflects the directory tree in the inputs.
Also, only the main contract type is reexported from each file.

This change solves a number of name clashing problems.
All generated code can still be imported after updating the import path.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ Use `ethers-v5` target to generate wrappers for [ethers.js](https://github.com/e
work great with Hardhat, use [Hardhat plugin](https://github.com/ethereum-ts/TypeChain/tree/master/packages/hardhat).

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 .
[npm](https://www.npmjs.com/package/@typechain/ethers-v4) and commit
[`db551b5`](https://github.com/dethcrypto/TypeChain/tree/db551b5c5f70e86f3ca342551e9e0369d099cfa2).

### Truffle v4 / v5
### Truffle v5

Truffle target is great when you use truffle contracts already. Check out
[truffle-typechain-example](https://github.com/ethereum-ts/truffle-typechain-example) for more details. It require
Expand Down
9 changes: 9 additions & 0 deletions contracts/v0.8.9/nested/a/NestedLibrary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"inputs": [],
"name": "getValue",
"outputs": [{ "internalType": "uint32", "name": "", "type": "uint32" }],
"stateMutability": "pure",
"type": "function"
}
]
9 changes: 9 additions & 0 deletions contracts/v0.8.9/nested/b/NestedLibrary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"inputs": [],
"name": "getValue",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"stateMutability": "pure",
"type": "function"
}
]
3 changes: 2 additions & 1 deletion examples/ethers-v5/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { utils, ethers } from 'ethers'
import { ethers, utils } from 'ethers'

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

const RPC_HOST = 'https://mainnet.infura.io/v3/6d6c70e65c77429482df5b64a4d0c943'
Expand Down
7 changes: 3 additions & 4 deletions examples/hardhat-truffe-v5/test/counter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { artifacts, contract } from 'hardhat'
import BN from 'bn.js'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'

import BN from 'bn.js'
import { artifacts, contract } from 'hardhat'

// chai.use(solidity);
chai.use(chaiAsPromised).should()
Expand Down Expand Up @@ -31,7 +30,7 @@ contract('Counter', (accounts: string[]) => {
describe('count up', async () => {
it('should count up', async () => {
await counter.countUp()
let count = await counter.getCount()
const count = await counter.getCount()
expect(count.eq(new BN(1)))
})
})
Expand Down
111 changes: 111 additions & 0 deletions examples/hardhat/Counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
import {
BaseContract,
BigNumber,
BytesLike,
CallOverrides,
ContractTransaction,
Overrides,
PopulatedTransaction,
Signer,
utils,
} from 'ethers'
import { FunctionFragment, Result, EventFragment } from '@ethersproject/abi'
import { Listener, Provider } from '@ethersproject/providers'
import { TypedEventFilter, TypedEvent, TypedListener, OnEvent } from '../common'

export interface CounterInterface extends utils.Interface {
contractName: 'Counter'
functions: {
'countDown()': FunctionFragment
'countUp()': FunctionFragment
'getCount()': FunctionFragment
}

encodeFunctionData(functionFragment: 'countDown', values?: undefined): string
encodeFunctionData(functionFragment: 'countUp', values?: undefined): string
encodeFunctionData(functionFragment: 'getCount', values?: undefined): string

decodeFunctionResult(functionFragment: 'countDown', data: BytesLike): Result
decodeFunctionResult(functionFragment: 'countUp', data: BytesLike): Result
decodeFunctionResult(functionFragment: 'getCount', data: BytesLike): Result

events: {
'CountedTo(uint256)': EventFragment
}

getEvent(nameOrSignatureOrTopic: 'CountedTo'): EventFragment
}

export type CountedToEvent = TypedEvent<[BigNumber], { number: BigNumber }>

export type CountedToEventFilter = TypedEventFilter<CountedToEvent>

export interface Counter extends BaseContract {
contractName: 'Counter'
connect(signerOrProvider: Signer | Provider | string): this
attach(addressOrName: string): this
deployed(): Promise<this>

interface: CounterInterface

queryFilter<TEvent extends TypedEvent>(
event: TypedEventFilter<TEvent>,
fromBlockOrBlockhash?: string | number | undefined,
toBlock?: string | number | undefined,
): Promise<Array<TEvent>>

listeners<TEvent extends TypedEvent>(eventFilter?: TypedEventFilter<TEvent>): Array<TypedListener<TEvent>>
listeners(eventName?: string): Array<Listener>
removeAllListeners<TEvent extends TypedEvent>(eventFilter: TypedEventFilter<TEvent>): this
removeAllListeners(eventName?: string): this
off: OnEvent<this>
on: OnEvent<this>
once: OnEvent<this>
removeListener: OnEvent<this>

functions: {
countDown(overrides?: Overrides & { from?: string | Promise<string> }): Promise<ContractTransaction>

countUp(overrides?: Overrides & { from?: string | Promise<string> }): Promise<ContractTransaction>

getCount(overrides?: CallOverrides): Promise<[BigNumber]>
}

countDown(overrides?: Overrides & { from?: string | Promise<string> }): Promise<ContractTransaction>

countUp(overrides?: Overrides & { from?: string | Promise<string> }): Promise<ContractTransaction>

getCount(overrides?: CallOverrides): Promise<BigNumber>

callStatic: {
countDown(overrides?: CallOverrides): Promise<BigNumber>

countUp(overrides?: CallOverrides): Promise<BigNumber>

getCount(overrides?: CallOverrides): Promise<BigNumber>
}

filters: {
'CountedTo(uint256)'(number?: null): CountedToEventFilter
CountedTo(number?: null): CountedToEventFilter
}

estimateGas: {
countDown(overrides?: Overrides & { from?: string | Promise<string> }): Promise<BigNumber>

countUp(overrides?: Overrides & { from?: string | Promise<string> }): Promise<BigNumber>

getCount(overrides?: CallOverrides): Promise<BigNumber>
}

populateTransaction: {
countDown(overrides?: Overrides & { from?: string | Promise<string> }): Promise<PopulatedTransaction>

countUp(overrides?: Overrides & { from?: string | Promise<string> }): Promise<PopulatedTransaction>

getCount(overrides?: CallOverrides): Promise<PopulatedTransaction>
}
}
3 changes: 2 additions & 1 deletion examples/web3-v1/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Web3 from 'web3'
import { Dai } from '../types/web3-v1-contracts/dai'

import { Dai } from '../types/web3-v1-contracts/Dai'

const abi = require('../abi/dai.json')

Expand Down
6 changes: 6 additions & 0 deletions packages/hardhat-test/typechain-types/factories/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
export { Counter__factory } from "./Counter__factory";
export { Demo__factory } from "./Demo__factory";
export { StructsInConstructor__factory } from "./StructsInConstructor__factory";
2 changes: 1 addition & 1 deletion packages/hardhat-test/typechain-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
export type { Counter } from "./Counter";
export type { Demo } from "./Demo";
export type { StructsInConstructor } from "./StructsInConstructor";

export * as factories from "./factories";
export { Counter__factory } from "./factories/Counter__factory";
export { Demo__factory } from "./factories/Demo__factory";
export { StructsInConstructor__factory } from "./factories/StructsInConstructor__factory";
8 changes: 5 additions & 3 deletions packages/hardhat/test/__snapshots__/project.test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ exports[`Typechain x Hardhat when recompiling generates typings only for changed
"/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
import type * as lib from \\"./lib\\";
export type { lib };
export type { EdgeCases } from \\"./EdgeCases\\";
export type { SafeMath } from \\"./SafeMath\\";
export type { TestContract } from \\"./TestContract\\";
export type { TestContract1 } from \\"./TestContract1\\";
export type { TestContract2 } from \\"./TestContract2\\";
export * as factories from \\"./factories\\";
export { EdgeCases__factory } from \\"./factories/EdgeCases__factory\\";
export { SafeMath__factory } from \\"./factories/SafeMath__factory\\";
export type { SafeMath } from \\"./lib/SafeMath\\";
export { SafeMath__factory } from \\"./factories/lib/SafeMath__factory\\";
export { TestContract__factory } from \\"./factories/TestContract__factory\\";
export { TestContract1__factory } from \\"./factories/TestContract1__factory\\";
export { TestContract2__factory } from \\"./factories/TestContract2__factory\\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
TypedEvent,
TypedListener,
OnEvent,
} from "./common";
} from "../../common";

export interface EdgeCasesInterface extends utils.Interface {
contractName: "EdgeCases";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
TypedEvent,
TypedListener,
OnEvent,
} from "./common";
} from "../../common";

export interface TestContractInterface extends utils.Interface {
contractName: "TestContract";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
TypedEvent,
TypedListener,
OnEvent,
} from "./common";
} from "../../common";

export interface TestContract1Interface extends utils.Interface {
contractName: "TestContract1";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
import type * as lib from "./lib";
export type { lib };
export type { EdgeCases } from "./EdgeCases";
export type { TestContract } from "./TestContract";
export type { TestContract1 } from "./TestContract1";
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
TypedEvent,
TypedListener,
OnEvent,
} from "./common";
} from "../../../common";

export interface SafeMathInterface extends utils.Interface {
contractName: "SafeMath";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
export type { SafeMath } from "./SafeMath";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
import type * as contracts from "./contracts";
export type { contracts };
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
TypedEvent,
TypedListener,
OnEvent,
} from "./common";
} from "../common";

export interface ERC20Interface extends utils.Interface {
contractName: "ERC20";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
export type { ERC20 } from "./ERC20";
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
Overrides,
} from "ethers";
import type { Provider, TransactionRequest } from "@ethersproject/providers";
import type { EdgeCases, EdgeCasesInterface } from "../EdgeCases";
import type {
EdgeCases,
EdgeCasesInterface,
} from "../../../artifacts/contracts/EdgeCases";

const _abi = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
Overrides,
} from "ethers";
import type { Provider, TransactionRequest } from "@ethersproject/providers";
import type { TestContract1, TestContract1Interface } from "../TestContract1";
import type {
TestContract1,
TestContract1Interface,
} from "../../../artifacts/contracts/TestContract1";

const _abi = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
Overrides,
} from "ethers";
import type { Provider, TransactionRequest } from "@ethersproject/providers";
import type { TestContract, TestContractInterface } from "../TestContract";
import type {
TestContract,
TestContractInterface,
} from "../../../artifacts/contracts/TestContract";

const _abi = [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
export * as lib from "./lib";
export { EdgeCases__factory } from "./EdgeCases__factory";
export { TestContract__factory } from "./TestContract__factory";
export { TestContract1__factory } from "./TestContract1__factory";
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
/* eslint-disable */
import { Signer, utils, Contract, ContractFactory, Overrides } from "ethers";
import type { Provider, TransactionRequest } from "@ethersproject/providers";
import type { SafeMath, SafeMathInterface } from "../SafeMath";
import type {
SafeMath,
SafeMathInterface,
} from "../../../../artifacts/contracts/lib/SafeMath";

const _abi = [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
export { SafeMath__factory } from "./SafeMath__factory";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
export * as contracts from "./contracts";
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable */
import { Signer, utils, Contract, ContractFactory, Overrides } from "ethers";
import type { Provider, TransactionRequest } from "@ethersproject/providers";
import type { ERC20, ERC20Interface } from "../ERC20";
import type { ERC20, ERC20Interface } from "../../externalArtifacts/ERC20";

const _abi = [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
export { ERC20__factory } from "./ERC20__factory";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
export * as artifacts from "./artifacts";
export * as externalArtifacts from "./externalArtifacts";
Loading

0 comments on commit 3a8a99a

Please sign in to comment.