Skip to content

Commit

Permalink
Merge pull request #300 from ethereum-ts/kk/0xGabi/ethers-target-fact…
Browse files Browse the repository at this point in the history
…ories-subdir
  • Loading branch information
krzkaczor authored Nov 10, 2020
2 parents 51923b3 + 785c15b commit 3f6cb73
Show file tree
Hide file tree
Showing 40 changed files with 454 additions and 81 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ You will also need to install a desired target for example `@typechain/ethers-v4

## Packages 📦

| Package | Version | Description | Examples |
| ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------- | --------------------- | -------------------------------- |
| [`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 | [example](./examples/ethers-v5) |
| [`@typechain/ethers-v4`](/packages/target-ethers-v4) | [![npm](https://img.shields.io/npm/v/@typechain/ethers-v4.svg)](https://www.npmjs.com/package/@typechain/ethers-v4) | Ethers ver 4 support | [example](./examples/ethers-v4) |
| [`@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/truffle-v4`](/packages/target-truffle-v4) | [![npm](https://img.shields.io/npm/v/@typechain/truffle-v4.svg)](https://www.npmjs.com/package/@typechain/truffle-v4) | Truffle ver 4 support | [example](./examples/truffle-v4) |
| [`@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) |
| Package | Version | Description | Examples |
| ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- | -------------------------------- |
| [`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 3.8 >=) | [example](./examples/ethers-v5) |
| [`@typechain/ethers-v4`](/packages/target-ethers-v4) | [![npm](https://img.shields.io/npm/v/@typechain/ethers-v4.svg)](https://www.npmjs.com/package/@typechain/ethers-v4) | Ethers ver 4 support | [example](./examples/ethers-v4) |
| [`@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/truffle-v4`](/packages/target-truffle-v4) | [![npm](https://img.shields.io/npm/v/@typechain/truffle-v4.svg)](https://www.npmjs.com/package/@typechain/truffle-v4) | Truffle ver 4 support | [example](./examples/truffle-v4) |
| [`@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) |

## Usage

Expand Down
9 changes: 9 additions & 0 deletions contracts/PayableFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pragma solidity ^0.6.4;

import "./Payable.sol";

contract PayableFactory {
function newPayable() public returns (Payable) {
return new Payable();
}
}
59 changes: 59 additions & 0 deletions packages/target-ethers-v4-test/types/PayableFactory.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */

import { Contract, ContractTransaction, EventFilter, Signer } from "ethers";
import { Listener, Provider } from "ethers/providers";
import { Arrayish, BigNumber, BigNumberish, Interface } from "ethers/utils";
import {
TransactionOverrides,
TypedEventDescription,
TypedFunctionDescription,
} from ".";

interface PayableFactoryInterface extends Interface {
functions: {
newPayable: TypedFunctionDescription<{ encode([]: []): string }>;
};

events: {};
}

export class PayableFactory extends Contract {
connect(signerOrProvider: Signer | Provider | string): PayableFactory;
attach(addressOrName: string): PayableFactory;
deployed(): Promise<PayableFactory>;

on(event: EventFilter | string, listener: Listener): PayableFactory;
once(event: EventFilter | string, listener: Listener): PayableFactory;
addListener(
eventName: EventFilter | string,
listener: Listener
): PayableFactory;
removeAllListeners(eventName: EventFilter | string): PayableFactory;
removeListener(eventName: any, listener: Listener): PayableFactory;

interface: PayableFactoryInterface;

functions: {
newPayable(overrides?: TransactionOverrides): Promise<ContractTransaction>;

"newPayable()"(
overrides?: TransactionOverrides
): Promise<ContractTransaction>;
};

newPayable(overrides?: TransactionOverrides): Promise<ContractTransaction>;

"newPayable()"(
overrides?: TransactionOverrides
): Promise<ContractTransaction>;

filters: {};

estimate: {
newPayable(overrides?: TransactionOverrides): Promise<BigNumber>;

"newPayable()"(overrides?: TransactionOverrides): Promise<BigNumber>;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { Contract, Signer } from "ethers";
import { Provider } from "ethers/providers";

import { DataTypesInput } from "./DataTypesInput";
import { DataTypesInput } from "../DataTypesInput";

export class DataTypesInputFactory {
export class DataTypesInput__factory {
static connect(
address: string,
signerOrProvider: Signer | Provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { Contract, Signer } from "ethers";
import { Provider } from "ethers/providers";

import { DataTypesPure } from "./DataTypesPure";
import { DataTypesPure } from "../DataTypesPure";

export class DataTypesPureFactory {
export class DataTypesPure__factory {
static connect(
address: string,
signerOrProvider: Signer | Provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { Contract, Signer } from "ethers";
import { Provider } from "ethers/providers";

import { DataTypesView } from "./DataTypesView";
import { DataTypesView } from "../DataTypesView";

export class DataTypesViewFactory {
export class DataTypesView__factory {
static connect(
address: string,
signerOrProvider: Signer | Provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { Contract, Signer } from "ethers";
import { Provider } from "ethers/providers";

import { Events } from "./Events";
import { Events } from "../Events";

export class EventsFactory {
export class Events__factory {
static connect(address: string, signerOrProvider: Signer | Provider): Events {
return new Contract(address, _abi, signerOrProvider) as Events;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { Contract, Signer } from "ethers";
import { Provider } from "ethers/providers";

import { LibraryConsumer } from "./LibraryConsumer";
import { LibraryConsumer } from "../LibraryConsumer";

export class LibraryConsumerFactory {
export class LibraryConsumer__factory {
static connect(
address: string,
signerOrProvider: Signer | Provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { Contract, Signer } from "ethers";
import { Provider } from "ethers/providers";

import { Library } from "./Library";
import { Library } from "../Library";

export class LibraryFactory {
export class Library__factory {
static connect(
address: string,
signerOrProvider: Signer | Provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { Contract, Signer } from "ethers";
import { Provider } from "ethers/providers";

import { NameMangling } from "./NameMangling";
import { NameMangling } from "../NameMangling";

export class NameManglingFactory {
export class NameMangling__factory {
static connect(
address: string,
signerOrProvider: Signer | Provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { Contract, Signer } from "ethers";
import { Provider } from "ethers/providers";

import { Overloads } from "./Overloads";
import { Overloads } from "../Overloads";

export class OverloadsFactory {
export class Overloads__factory {
static connect(
address: string,
signerOrProvider: Signer | Provider
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */

import { Contract, Signer } from "ethers";
import { Provider } from "ethers/providers";

import { PayableFactory } from "../PayableFactory";

export class PayableFactory__factory {
static connect(
address: string,
signerOrProvider: Signer | Provider
): PayableFactory {
return new Contract(address, _abi, signerOrProvider) as PayableFactory;
}
}

const _abi = [
{
inputs: [],
name: "newPayable",
outputs: [
{
internalType: "contract Payable",
name: "",
type: "address",
},
],
stateMutability: "nonpayable",
type: "function",
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { Contract, Signer } from "ethers";
import { Provider } from "ethers/providers";

import { Payable } from "./Payable";
import { Payable } from "../Payable";

export class PayableFactory {
export class Payable__factory {
static connect(
address: string,
signerOrProvider: Signer | Provider
Expand Down
2 changes: 1 addition & 1 deletion packages/target-ethers-v4/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"TypeChain",
"TypeScript"
],
"version": "2.0.0",
"version": "3.0.0",
"license": "MIT",
"repository": "https://github.com/ethereum-ts/Typechain",
"main": "./dist/index.js",
Expand Down
15 changes: 8 additions & 7 deletions packages/target-ethers-v4/src/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from 'typechain'
import { generateInputType, generateInputTypes } from './types'
import { codegenFunctions } from './functions'
import { FACTORY_POSTFIX } from '../common'

export function codegenContractTypings(contract: Contract) {
const template = `
Expand Down Expand Up @@ -94,9 +95,9 @@ export function codegenContractFactory(contract: Contract, abi: any, bytecode?:
${ethersUtilsImportLine}
import { TransactionOverrides } from ".";
import { ${contract.name} } from "./${contract.name}";
import { ${contract.name} } from "../${contract.name}";
export class ${contract.name}Factory extends ContractFactory {
export class ${contract.name}${FACTORY_POSTFIX} extends ContractFactory {
${generateFactoryConstructor(contract, bytecode)}
deploy(${constructorArgs}): Promise<${contract.name}> {
return super.deploy(${constructorArgNames}) as Promise<${contract.name}>;
Expand All @@ -107,8 +108,8 @@ export function codegenContractFactory(contract: Contract, abi: any, bytecode?:
attach(address: string): ${contract.name} {
return super.attach(address) as ${contract.name};
}
connect(signer: Signer): ${contract.name}Factory {
return super.connect(signer) as ${contract.name}Factory;
connect(signer: Signer): ${contract.name}${FACTORY_POSTFIX} {
return super.connect(signer) as ${contract.name}${FACTORY_POSTFIX};
}
static connect(address: string, signerOrProvider: Signer | Provider): ${contract.name} {
return new Contract(address, _abi, signerOrProvider) as ${contract.name};
Expand All @@ -128,9 +129,9 @@ export function codegenAbstractContractFactory(contract: Contract, abi: any): st
import { Contract, Signer } from "ethers";
import { Provider } from "ethers/providers";
import { ${contract.name} } from "./${contract.name}";
import { ${contract.name} } from "../${contract.name}";
export class ${contract.name}Factory {
export class ${contract.name}${FACTORY_POSTFIX} {
static connect(address: string, signerOrProvider: Signer | Provider): ${contract.name} {
return new Contract(address, _abi, signerOrProvider) as ${contract.name};
}
Expand Down Expand Up @@ -163,7 +164,7 @@ function generateFactoryConstructor(contract: Contract, bytecode: BytecodeWithLi

return `
constructor(linkLibraryAddresses: ${contract.name}LibraryAddresses, signer?: Signer) {
super(_abi, ${contract.name}Factory.linkBytecode(linkLibraryAddresses), signer);
super(_abi, ${contract.name}${FACTORY_POSTFIX}.linkBytecode(linkLibraryAddresses), signer);
}
static linkBytecode(linkLibraryAddresses: ${contract.name}LibraryAddresses): string {
Expand Down
1 change: 1 addition & 0 deletions packages/target-ethers-v4/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FACTORY_POSTFIX = '__factory'
5 changes: 3 additions & 2 deletions packages/target-ethers-v4/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'typechain'

import { codegenAbstractContractFactory, codegenContractFactory, codegenContractTypings } from './codegen'
import { FACTORY_POSTFIX } from './common'

export interface IEthersCfg {
outDir?: string
Expand Down Expand Up @@ -102,7 +103,7 @@ export default class Ethers extends TsGeneratorPlugin {

genContractFactoryFile(contract: Contract, abi: any, bytecode?: BytecodeWithLinkReferences) {
return {
path: join(this.outDirAbs, `${contract.name}Factory.ts`),
path: join(this.outDirAbs, 'factories', `${contract.name}${FACTORY_POSTFIX}.ts`),
contents: codegenContractFactory(contract, abi, bytecode),
}
}
Expand All @@ -113,7 +114,7 @@ export default class Ethers extends TsGeneratorPlugin {
const abstractFactoryFiles = Object.keys(this.contractCache).map((contractName) => {
const { contract, abi } = this.contractCache[contractName]
return {
path: join(this.outDirAbs, `${contract.name}Factory.ts`),
path: join(this.outDirAbs, 'factories', `${contract.name}${FACTORY_POSTFIX}.ts`),
contents: codegenAbstractContractFactory(contract, abi),
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/target-ethers-v4/test/generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ describe('Ethers generation edge cases', () => {
}

it('should generate simple factory when no bytecode available', () => {
expect(codegenContractFactory(emptyContract, 'abi', undefined)).to.match(/export class TestContractFactory \{/)
expect(codegenContractFactory(emptyContract, 'abi', undefined)).to.match(/export class TestContract__factory \{/)
})
})
Loading

0 comments on commit 3f6cb73

Please sign in to comment.