Skip to content

Commit

Permalink
Improve event types (web3-1.0.0)
Browse files Browse the repository at this point in the history
* Parse event inputs in order to improve the typing of contract events for the `web3-1.0.0` target
* Update existing types to reflect updated `web3` (e.g. `EstimateGasOptions`)
  • Loading branch information
JamesLefrere committed May 4, 2019
1 parent 429061d commit 0581946
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
26 changes: 7 additions & 19 deletions lib/targets/web3/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import {

export function codegen(contract: Contract) {
const template = `
import { Contract, ContractOptions, Options } from "web3-eth-contract";
import { Block } from "web3-eth";
import BN from "bn.js";
import { Contract, ContractOptions, EventOptions } from "web3-eth-contract";
import { EventLog } from "web3-core";
import { EventEmitter } from "events";
import { Callback, TransactionObject } from "./types";
import { Callback, TransactionObject, ContractEvent } from "./types";
export class ${contract.name} extends Contract {
constructor(jsonInterface: any[], address?: string, options?: ContractOptions);
Expand All @@ -37,11 +37,7 @@ export function codegen(contract: Contract) {
events: {
${contract.events.map(generateEvents).join("\n")}
allEvents: (
options?: {
filter?: object;
fromBlock?: number | string;
topics?: (null|string)[];
},
options?: EventOptions,
cb?: Callback<EventLog>
) => EventEmitter;
};
Expand Down Expand Up @@ -86,15 +82,7 @@ function generateOutputTypes(outputs: Array<AbiParameter>): string {
}

function generateEvents(event: EventDeclaration) {
return `
${event.name}(
options?: {
filter?: object;
fromBlock?: number | string;
topics?: (null|string)[];
},
cb?: Callback<EventLog>): EventEmitter;
`;
return `${event.name}: ContractEvent<${generateOutputTypes(event.inputs)}>`;
}

function generateInputType(evmType: EvmType): string {
Expand Down Expand Up @@ -124,9 +112,9 @@ function generateInputType(evmType: EvmType): string {
function generateOutputType(evmType: EvmType): string {
switch (evmType.constructor) {
case IntegerType:
return "string";
return "BN";
case UnsignedIntegerType:
return "string";
return "BN";
case AddressType:
return "string";
case VoidType:
Expand Down
25 changes: 19 additions & 6 deletions lib/targets/web3/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Contract } from "../../parser/abiParser";
import { TsGeneratorPlugin, TContext, TFileDesc } from "ts-generator";
import { join } from "path";
import { extractAbi, parse } from "../../parser/abiParser";
Expand Down Expand Up @@ -46,17 +45,31 @@ export class Web3 extends TsGeneratorPlugin {
{
path: join(this.outDirAbs, "types.d.ts"),
contents: `
import { Transaction } from "web3-core";
import { EventLog } from "web3-core";
import BN from "bn.js";
import { EstimateGasOptions, EventOptions } from "web3-eth-contract";
import { EventEmitter } from "events";
// @ts-ignore
import PromiEvent from "web3-core-promievent";
export type Callback<T> = (error: Error, result: T) => void;
export interface TransactionObject<T> {
arguments: any[];
call(tx?: Transaction): Promise<T>;
send(tx?: Transaction): PromiEvent<T>;
estimateGas(tx?: Transaction): Promise<number>;
call(options?: EstimateGasOptions): Promise<T>;
send(options?: EstimateGasOptions): PromiEvent<T>;
estimateGas(options?: EstimateGasOptions): Promise<number>;
encodeABI(): string;
}`,
}
export interface ContractEventLog<T> extends EventLog {
returnValues: T;
}
export interface ContractEventEmitter<T> extends EventEmitter {
on(event: 'data' | 'changed', listener: (event: ContractEventLog<T>) => void): this;
on(event: 'error', listener: (error: Error) => void): this;
}
export type ContractEvent<T> = (
options?: EventOptions,
cb?: Callback<ContractEventLog<T>>
) => ContractEventEmitter<T>;`,
},
];
}
Expand Down

0 comments on commit 0581946

Please sign in to comment.