@typechain/ethers-v4
Advanced tools
Comparing version 1.0.0 to 1.0.1
import { FunctionDeclaration } from 'typechain'; | ||
export declare function codegenFunctions(fns: FunctionDeclaration[]): string; | ||
export declare function codegenForOverloadedFunctions(fns: FunctionDeclaration[]): string; | ||
interface GenerateFunctionOptions { | ||
overrideOutput?: string; | ||
} | ||
export declare function codegenFunctions(options: GenerateFunctionOptions, fns: FunctionDeclaration[]): string; | ||
export declare function codegenForOverloadedFunctions(options: GenerateFunctionOptions, fns: FunctionDeclaration[]): string; | ||
export {}; |
@@ -6,19 +6,21 @@ "use strict"; | ||
const types_1 = require("./types"); | ||
function codegenFunctions(fns) { | ||
function codegenFunctions(options, fns) { | ||
if (fns.length === 1) { | ||
return generateFunction(fns[0]); | ||
return `${generateFunction(options, fns[0])}${codegenForOverloadedFunctions(options, fns)}`; | ||
} | ||
return codegenForOverloadedFunctions(fns); | ||
return `${generateFunction(options, fns[0])}${codegenForOverloadedFunctions(options, fns)}`; | ||
} | ||
exports.codegenFunctions = codegenFunctions; | ||
function codegenForOverloadedFunctions(fns) { | ||
return fns.map((fn) => generateFunction(fn, `"${typechain_1.getSignatureForFn(fn)}"`)).join('\n'); | ||
function codegenForOverloadedFunctions(options, fns) { | ||
return fns.map((fn) => generateFunction(options, fn, `"${typechain_1.getSignatureForFn(fn)}"`)).join('\n'); | ||
} | ||
exports.codegenForOverloadedFunctions = codegenForOverloadedFunctions; | ||
function generateFunction(fn, overloadedName) { | ||
function generateFunction(options, fn, overloadedName) { | ||
return ` | ||
${generateFunctionDocumentation(fn.documentation)} | ||
${overloadedName !== null && overloadedName !== void 0 ? overloadedName : fn.name}(${types_1.generateInputTypes(fn.inputs)}${!typechain_1.isConstant(fn) && !typechain_1.isConstantFn(fn) ? 'overrides?: TransactionOverrides' : ''}): Promise<${fn.stateMutability === 'pure' || fn.stateMutability === 'view' | ||
? types_1.generateOutputTypes(fn.outputs) | ||
: 'ContractTransaction'}>; | ||
${overloadedName !== null && overloadedName !== void 0 ? overloadedName : fn.name}(${types_1.generateInputTypes(fn.inputs)} overrides?: TransactionOverrides): ${options.overrideOutput | ||
? options.overrideOutput | ||
: `Promise<${fn.stateMutability === 'pure' || fn.stateMutability === 'view' | ||
? types_1.generateOutputTypes(fn.outputs) | ||
: 'ContractTransaction'}>`}; | ||
`; | ||
@@ -25,0 +27,0 @@ } |
@@ -44,6 +44,6 @@ "use strict"; | ||
functions: { | ||
${lodash_1.values(contract.functions).map(functions_1.codegenFunctions).join('\n')} | ||
${lodash_1.values(contract.functions).map(functions_1.codegenFunctions.bind(null, {})).join('\n')} | ||
}; | ||
${lodash_1.values(contract.functions).map(functions_1.codegenFunctions).join('\n')} | ||
${lodash_1.values(contract.functions).map(functions_1.codegenFunctions.bind(null, {})).join('\n')} | ||
@@ -59,4 +59,3 @@ filters: { | ||
${lodash_1.values(contract.functions) | ||
.map((v) => v[0]) | ||
.map(generateEstimateFunction) | ||
.map(functions_1.codegenFunctions.bind(null, { overrideOutput: 'Promise<BigNumber>' })) | ||
.join('\n')} | ||
@@ -178,7 +177,2 @@ }; | ||
} | ||
function generateEstimateFunction(fn) { | ||
return ` | ||
${fn.name}(${types_1.generateInputTypes(fn.inputs)}): Promise<BigNumber>; | ||
`; | ||
} | ||
function generateInterfaceFunctionDescription(fn) { | ||
@@ -185,0 +179,0 @@ return ` |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"license": "MIT", | ||
@@ -14,0 +14,0 @@ "repository": "https://github.com/ethereum-ts/Typechain", |
@@ -22,1 +22,47 @@ # Typechain target Ethers-v4 | ||
## [TypeChain readme](https://github.com/ethereum-ts/TypeChain) | ||
## Contract typings | ||
The main files generated by this target are `<contract-name>.d.ts`. They declare typesafe interfaces for your contracts on top of ethers `Contract` instances: | ||
* typed contract's methods, available both at `contract.someMethod(...)` and `contract.functions.someMethod(...)` | ||
* typed events in `contract.interface.events.AnEvent` and filters in `contract.filters.AnEvent` | ||
* typed method gas estimates in `contract.estimate.someMethod` | ||
* overrides for the event listener methods (`on`, `once`, etc) that return the same contract type. | ||
Note: these are just _type declarations_ to help you call the blockchain properly, so they're not available at runtime, and all of the contracts are still instances of the same `Contract` class. | ||
## Contract factories | ||
This target also generates a concrete factory class for each contract, to help you deploy or connect to contract instances. The factory classes are an extension of ethers' `ContractFactory`. They serve two main purposes: | ||
* wrap passing contract ABI and bytecode to the `ContractFactory` class, so you don't have to load and parse the JSON manually | ||
* provide a correctly typed interface to `ContractFactory` (since it returns plain `Contract` instances). | ||
Abstract contracts or solidity interfaces are handled a bit different, because they have no bytecode. For those, a simplified factory is generated that doesn't extends `ContractFactory`, and only includes the static `connect` method, so you can easily connect to a deployed instance without having to pass the ABI manually. | ||
## Basic example | ||
Suppose you have an `Erc20Token.sol` solidity interface and a `DummyToken.sol` contract implementing it. | ||
```typescript | ||
import { BigNumber } from 'ethers/utils'; | ||
import { Wallet } from 'ethers'; | ||
import { DummyTokenFactory } from 'typechain-out-dir/DummyTokenFactory'; | ||
import { DummyToken } from 'typechain-out-dir/DummyToken'; | ||
import { Erc20TokenFactory } from 'typechain-out-dir/Erc20TokenFactory'; | ||
const provider = getYourProvider(...); | ||
// use the concrete contract factory if you need to operate on the bytecode (ie. deploy) | ||
async function deployTestToken(ownerPK: string): Promise<DummyToken> { | ||
const owner = new Wallet(ownerPK, provider); | ||
return new DummyTokenFactory(owner).deploy(); | ||
} | ||
// to call existing contracts, a factory for both the concrete contract and for the interface | ||
// can be used since the ABI is the same | ||
async function getTokenBalance(walletAddress: string, tokenAddress: string): Promise<BigNumber> { | ||
const token = Erc20TokenFactory.connect(tokenAddress, provider); | ||
return token.functions.balanceOf(walletAddress); | ||
} | ||
``` |
import { BigNumberish, EventDescription, FunctionDescription } from 'ethers/utils' | ||
export class TransactionOverrides { | ||
nonce?: BigNumberish | Promise<BigNumberish> | ||
export interface TransactionOverrides { | ||
gasLimit?: BigNumberish | Promise<BigNumberish> | ||
gasPrice?: BigNumberish | Promise<BigNumberish> | ||
nonce?: BigNumberish | Promise<BigNumberish> | ||
value?: BigNumberish | Promise<BigNumberish> | ||
from?: string | Promise<string> | ||
chainId?: number | Promise<number> | ||
@@ -9,0 +10,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
40069
68
0