@typechain/ethers-v5
Advanced tools
Comparing version 6.0.5 to 7.0.0
# @typechain/ethers-v5 | ||
## 7.0.0 | ||
### Minor Changes | ||
- 021e959: Expose ABI, bytecode, and contract interface in factories | ||
### Patch Changes | ||
- d590f88: Make filter parameters optional | ||
- bc4539a: Generated types now extend new `BaseContract` not `Contract` from ethers. This removes all index signatures | ||
and makes calling a non-existing function a compile-time error. | ||
- Updated dependencies [d60a343] | ||
- Updated dependencies [5a60d00] | ||
- Updated dependencies [d60a343] | ||
- typechain@5.0.0 | ||
## 6.0.5 | ||
@@ -4,0 +20,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { FunctionDeclaration } from 'typechain'; | ||
import { FunctionDeclaration, CodegenConfig } from 'typechain'; | ||
interface GenerateFunctionOptions { | ||
@@ -6,2 +6,3 @@ returnResultObject?: boolean; | ||
overrideOutput?: string; | ||
codegenConfig: CodegenConfig; | ||
} | ||
@@ -8,0 +9,0 @@ export declare function codegenFunctions(options: GenerateFunctionOptions, fns: FunctionDeclaration[]): string; |
@@ -8,3 +8,8 @@ "use strict"; | ||
if (fns.length === 1) { | ||
return `${generateFunction(options, fns[0])}${codegenForOverloadedFunctions(options, fns)}`; | ||
if (options.codegenConfig.alwaysGenerateOverloads) { | ||
return generateFunction(options, fns[0]) + codegenForOverloadedFunctions(options, fns); | ||
} | ||
else { | ||
return generateFunction(options, fns[0]); | ||
} | ||
} | ||
@@ -11,0 +16,0 @@ return codegenForOverloadedFunctions(options, fns); |
@@ -1,4 +0,4 @@ | ||
import { BytecodeWithLinkReferences, Contract } from 'typechain'; | ||
export declare function codegenContractTypings(contract: Contract): string; | ||
import { BytecodeWithLinkReferences, CodegenConfig, Contract } from 'typechain'; | ||
export declare function codegenContractTypings(contract: Contract, codegenConfig: CodegenConfig): string; | ||
export declare function codegenContractFactory(contract: Contract, abi: any, bytecode?: BytecodeWithLinkReferences): string; | ||
export declare function codegenAbstractContractFactory(contract: Contract, abi: any): string; |
@@ -5,10 +5,11 @@ "use strict"; | ||
const lodash_1 = require("lodash"); | ||
const types_1 = require("./types"); | ||
const common_1 = require("../common"); | ||
const functions_1 = require("./functions"); | ||
const common_1 = require("../common"); | ||
const reserved_keywords_1 = require("./reserved-keywords"); | ||
function codegenContractTypings(contract) { | ||
const contractImports = ['Contract', 'ContractTransaction']; | ||
const types_1 = require("./types"); | ||
function codegenContractTypings(contract, codegenConfig) { | ||
const contractImports = ['BaseContract', 'ContractTransaction']; | ||
const allFunctions = lodash_1.values(contract.functions) | ||
.map((fn) => functions_1.codegenFunctions({ returnResultObject: true }, fn) + functions_1.codegenFunctions({ isStaticCall: true }, fn)) | ||
.map((fn) => functions_1.codegenFunctions({ returnResultObject: true, codegenConfig }, fn) + | ||
functions_1.codegenFunctions({ isStaticCall: true, codegenConfig }, fn)) | ||
.join(''); | ||
@@ -55,3 +56,3 @@ const optionalContractImports = ['Overrides', 'PayableOverrides', 'CallOverrides']; | ||
export class ${contract.name} extends Contract { | ||
export class ${contract.name} extends BaseContract { | ||
connect(signerOrProvider: Signer | Provider | string): this; | ||
@@ -85,3 +86,3 @@ attach(addressOrName: string): this; | ||
${lodash_1.values(contract.functions) | ||
.map(functions_1.codegenFunctions.bind(null, { returnResultObject: true })) | ||
.map(functions_1.codegenFunctions.bind(null, { returnResultObject: true, codegenConfig })) | ||
.join('\n')} | ||
@@ -92,3 +93,3 @@ }; | ||
.filter((f) => !reserved_keywords_1.reservedKeywords.has(f[0].name)) | ||
.map(functions_1.codegenFunctions.bind(null, {})) | ||
.map(functions_1.codegenFunctions.bind(null, { codegenConfig })) | ||
.join('\n')} | ||
@@ -98,3 +99,3 @@ | ||
${lodash_1.values(contract.functions) | ||
.map(functions_1.codegenFunctions.bind(null, { isStaticCall: true })) | ||
.map(functions_1.codegenFunctions.bind(null, { isStaticCall: true, codegenConfig })) | ||
.join('\n')} | ||
@@ -112,3 +113,3 @@ }; | ||
${lodash_1.values(contract.functions) | ||
.map(functions_1.codegenFunctions.bind(null, { overrideOutput: 'Promise<BigNumber>' })) | ||
.map(functions_1.codegenFunctions.bind(null, { overrideOutput: 'Promise<BigNumber>', codegenConfig })) | ||
.join('\n')} | ||
@@ -119,3 +120,3 @@ }; | ||
${lodash_1.values(contract.functions) | ||
.map(functions_1.codegenFunctions.bind(null, { overrideOutput: 'Promise<PopulatedTransaction>' })) | ||
.map(functions_1.codegenFunctions.bind(null, { overrideOutput: 'Promise<PopulatedTransaction>', codegenConfig })) | ||
.join('\n')} | ||
@@ -140,3 +141,3 @@ }; | ||
// tsc with noUnusedLocals would complain about unused imports | ||
const ethersImports = ['Signer']; | ||
const ethersImports = ['Signer', 'utils']; | ||
const optionalEthersImports = ['BytesLike', 'BigNumberish']; | ||
@@ -147,7 +148,9 @@ optionalEthersImports.forEach((importName) => pushImportIfUsed(importName, constructorArgs, ethersImports)); | ||
optionalContractImports.forEach((importName) => pushImportIfUsed(importName, constructorArgs, ethersContractImports)); | ||
const { body, header } = codegenCommonContractFactory(contract, abi); | ||
return ` | ||
import { ${[...ethersImports, ...ethersContractImports].join(', ')} } from "ethers"; | ||
import { Provider, TransactionRequest } from '@ethersproject/providers'; | ||
${header} | ||
import type { ${contract.name} } from "../${contract.name}"; | ||
const _bytecode = "${bytecode.bytecode}"; | ||
@@ -168,11 +171,6 @@ export class ${contract.name}${common_1.FACTORY_POSTFIX} extends ContractFactory { | ||
} | ||
static connect(address: string, signerOrProvider: Signer | Provider): ${contract.name} { | ||
return new Contract(address, _abi, signerOrProvider) as ${contract.name}; | ||
} | ||
static readonly bytecode = _bytecode; | ||
${body} | ||
} | ||
const _abi = ${JSON.stringify(abi, null, 2)}; | ||
const _bytecode = "${bytecode.bytecode}"; | ||
${generateLibraryAddressesInterface(contract, bytecode)} | ||
@@ -183,18 +181,31 @@ `; | ||
function codegenAbstractContractFactory(contract, abi) { | ||
const { body, header } = codegenCommonContractFactory(contract, abi); | ||
return ` | ||
import { Contract, Signer } from "ethers"; | ||
import { Contract, Signer, utils } from "ethers"; | ||
import { Provider } from "@ethersproject/providers"; | ||
${header} | ||
import type { ${contract.name} } from "../${contract.name}"; | ||
export class ${contract.name}${common_1.FACTORY_POSTFIX} { | ||
${body} | ||
} | ||
`; | ||
} | ||
exports.codegenAbstractContractFactory = codegenAbstractContractFactory; | ||
function codegenCommonContractFactory(contract, abi) { | ||
const header = ` | ||
import type { ${contract.name}, ${contract.name}Interface } from "../${contract.name}"; | ||
export class ${contract.name}${common_1.FACTORY_POSTFIX} { | ||
const _abi = ${JSON.stringify(abi, null, 2)}; | ||
`.trim(); | ||
const body = ` | ||
static readonly abi = _abi; | ||
static createInterface(): ${contract.name}Interface { | ||
return new utils.Interface(_abi) as ${contract.name}Interface; | ||
} | ||
static connect(address: string, signerOrProvider: Signer | Provider): ${contract.name} { | ||
return new Contract(address, _abi, signerOrProvider) as ${contract.name}; | ||
} | ||
} | ||
const _abi = ${JSON.stringify(abi, null, 2)}; | ||
`; | ||
`.trim(); | ||
return { header, body }; | ||
} | ||
exports.codegenAbstractContractFactory = codegenAbstractContractFactory; | ||
function generateFactoryConstructor(contract, bytecode) { | ||
@@ -283,3 +294,3 @@ if (!bytecode.linkReferences) { | ||
.map((arg) => { | ||
return `${arg.name}: ${generateEventArgType(arg)}`; | ||
return `${arg.name}?: ${generateEventArgType(arg)}`; | ||
}) | ||
@@ -286,0 +297,0 @@ .join(', ') + ', '); |
@@ -1,17 +0,16 @@ | ||
import { TContext, TFileDesc, TsGeneratorPlugin } from 'ts-generator'; | ||
import { BytecodeWithLinkReferences, Contract } from 'typechain'; | ||
import { BytecodeWithLinkReferences, Config, Contract, FileDescription, TypeChainTarget, CodegenConfig } from 'typechain'; | ||
export interface IEthersCfg { | ||
outDir?: string; | ||
} | ||
export default class Ethers extends TsGeneratorPlugin { | ||
export default class Ethers extends TypeChainTarget { | ||
name: string; | ||
allContracts: string[]; | ||
private readonly allContracts; | ||
private readonly outDirAbs; | ||
private readonly contractCache; | ||
private readonly bytecodeCache; | ||
constructor(ctx: TContext<IEthersCfg>); | ||
transformFile(file: TFileDesc): TFileDesc[] | void; | ||
transformBinFile(file: TFileDesc): TFileDesc[] | void; | ||
transformAbiOrFullJsonFile(file: TFileDesc): TFileDesc[] | void; | ||
genContractTypingsFile(contract: Contract): TFileDesc; | ||
constructor(config: Config); | ||
transformFile(file: FileDescription): FileDescription[] | void; | ||
transformBinFile(file: FileDescription): FileDescription[] | void; | ||
transformAbiOrFullJsonFile(file: FileDescription): FileDescription[] | void; | ||
genContractTypingsFile(contract: Contract, codegenConfig: CodegenConfig): FileDescription; | ||
genContractFactoryFile(contract: Contract, abi: any, bytecode?: BytecodeWithLinkReferences): { | ||
@@ -21,5 +20,5 @@ path: string; | ||
}; | ||
afterRun(): TFileDesc[]; | ||
afterRun(): FileDescription[]; | ||
private genCommons; | ||
private genReExports; | ||
} |
@@ -5,16 +5,16 @@ "use strict"; | ||
const lodash_1 = require("lodash"); | ||
const ts_generator_1 = require("ts-generator"); | ||
const typechain_1 = require("typechain"); | ||
const codegen_1 = require("./codegen"); | ||
const common_1 = require("./common"); | ||
const hardhat_1 = require("./codegen/hardhat"); | ||
const DEFAULT_OUT_PATH = './types/ethers-contracts/'; | ||
class Ethers extends ts_generator_1.TsGeneratorPlugin { | ||
constructor(ctx) { | ||
super(ctx); | ||
class Ethers extends typechain_1.TypeChainTarget { | ||
constructor(config) { | ||
super(config); | ||
this.name = 'Ethers'; | ||
this.allContracts = []; | ||
this.contractCache = {}; | ||
this.bytecodeCache = {}; | ||
const { cwd, rawConfig } = ctx; | ||
this.outDirAbs = path_1.resolve(cwd, rawConfig.outDir || DEFAULT_OUT_PATH); | ||
const { cwd, outDir, allFiles } = config; | ||
this.outDirAbs = path_1.resolve(cwd, outDir || DEFAULT_OUT_PATH); | ||
this.allContracts = allFiles.map((fp) => typechain_1.normalizeName(typechain_1.getFilename(fp))); | ||
} | ||
@@ -57,14 +57,16 @@ transformFile(file) { | ||
if (bytecode) { | ||
return [this.genContractTypingsFile(contract), this.genContractFactoryFile(contract, abi, bytecode)]; | ||
return [ | ||
this.genContractTypingsFile(contract, this.cfg.flags), | ||
this.genContractFactoryFile(contract, abi, bytecode), | ||
]; | ||
} | ||
else { | ||
this.contractCache[name] = { abi, contract }; | ||
return [this.genContractTypingsFile(contract)]; | ||
return [this.genContractTypingsFile(contract, this.cfg.flags)]; | ||
} | ||
} | ||
genContractTypingsFile(contract) { | ||
this.allContracts.push(contract.name); | ||
genContractTypingsFile(contract, codegenConfig) { | ||
return { | ||
path: path_1.join(this.outDirAbs, `${contract.name}.d.ts`), | ||
contents: codegen_1.codegenContractTypings(contract), | ||
contents: codegen_1.codegenContractTypings(contract, codegenConfig), | ||
}; | ||
@@ -88,3 +90,6 @@ } | ||
}); | ||
const allFiles = [ | ||
const hardhatHelper = this.cfg.flags.environment === 'hardhat' | ||
? { path: path_1.join(this.outDirAbs, 'hardhat.d.ts'), contents: hardhat_1.generateHardhatHelper(this.allContracts) } | ||
: undefined; | ||
const allFiles = lodash_1.compact([ | ||
...abstractFactoryFiles, | ||
@@ -99,3 +104,4 @@ { | ||
}, | ||
]; | ||
hardhatHelper, | ||
]); | ||
return allFiles; | ||
@@ -105,13 +111,21 @@ } | ||
return ` | ||
import { EventFilter, Event } from 'ethers' | ||
import { Result } from '@ethersproject/abi' | ||
import { EventFilter, Event } from 'ethers' | ||
import { Result } from '@ethersproject/abi' | ||
export interface TypedEventFilter<_EventArgsArray, _EventArgsObject> extends EventFilter {} | ||
export interface TypedEventFilter<_EventArgsArray, _EventArgsObject> extends EventFilter {} | ||
export interface TypedEvent<EventArgs extends Result> extends Event { | ||
args: EventArgs; | ||
export interface TypedEvent<EventArgs extends Result> extends Event { | ||
args: EventArgs; | ||
} | ||
export type TypedListener<EventArgsArray extends Array<any>, EventArgsObject> = (...listenerArg: [...EventArgsArray, TypedEvent<EventArgsArray & EventArgsObject>]) => void; | ||
export type MinEthersFactory<C, ARGS> = { | ||
deploy(...a: ARGS[]): Promise<C> | ||
} | ||
export type GetContractTypeFromFactory<F> = F extends MinEthersFactory<infer C, any> ? C : never | ||
export type GetARGsTypeFromFactory<F> = F extends MinEthersFactory<any, any> ? Parameters<F['deploy']> : never | ||
`; | ||
} | ||
export type TypedListener<EventArgsArray extends Array<any>, EventArgsObject> = (...listenerArg: [...EventArgsArray, TypedEvent<EventArgsArray & EventArgsObject>]) => void;`; | ||
} | ||
genReExports() { | ||
@@ -118,0 +132,0 @@ const codegen = []; |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "6.0.5", | ||
"version": "7.0.0", | ||
"license": "MIT", | ||
@@ -30,3 +30,3 @@ "repository": "https://github.com/ethereum-ts/Typechain", | ||
"lint:fix": "yarn lint --fix", | ||
"typecheck": "tsc --noEmit --incremental false --composite false && tsc --noEmit --incremental false --composite false -p tsconfig.types.json", | ||
"typecheck": "tsc --noEmit --incremental false --composite false", | ||
"clean": "rm -rf dist && rm -f tsconfig.build.tsbuildinfo", | ||
@@ -37,5 +37,8 @@ "test": "mocha --config ../../.mocharc.js", | ||
"peerDependencies": { | ||
"typechain": "^4.0.0", | ||
"typechain": "^5.0.0", | ||
"typescript": ">=4.0.0", | ||
"ethers": "^5.0.0" | ||
"ethers": "^5.1.3", | ||
"@ethersproject/bytes": "^5.0.0", | ||
"@ethersproject/providers": "^5.0.0", | ||
"@ethersproject/abi": "^5.0.0" | ||
}, | ||
@@ -42,0 +45,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58078
25
666
6