diff --git a/lib/targets/web3/generation.ts b/lib/targets/web3/generation.ts index 0e6347871..75d56e829 100644 --- a/lib/targets/web3/generation.ts +++ b/lib/targets/web3/generation.ts @@ -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); @@ -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 ) => EventEmitter; }; @@ -86,15 +82,7 @@ function generateOutputTypes(outputs: Array): string { } function generateEvents(event: EventDeclaration) { - return ` - ${event.name}( - options?: { - filter?: object; - fromBlock?: number | string; - topics?: (null|string)[]; - }, - cb?: Callback): EventEmitter; - `; + return `${event.name}: ContractEvent<${generateOutputTypes(event.inputs)}>`; } function generateInputType(evmType: EvmType): string { @@ -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: diff --git a/lib/targets/web3/index.ts b/lib/targets/web3/index.ts index 0656e2329..7f55c8bf9 100644 --- a/lib/targets/web3/index.ts +++ b/lib/targets/web3/index.ts @@ -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"; @@ -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 = (error: Error, result: T) => void; export interface TransactionObject { arguments: any[]; - call(tx?: Transaction): Promise; - send(tx?: Transaction): PromiEvent; - estimateGas(tx?: Transaction): Promise; + call(options?: EstimateGasOptions): Promise; + send(options?: EstimateGasOptions): PromiEvent; + estimateGas(options?: EstimateGasOptions): Promise; encodeABI(): string; - }`, + } + export interface ContractEventLog extends EventLog { + returnValues: T; + } + export interface ContractEventEmitter extends EventEmitter { + on(event: 'data' | 'changed', listener: (event: ContractEventLog) => void): this; + on(event: 'error', listener: (error: Error) => void): this; + } + export type ContractEvent = ( + options?: EventOptions, + cb?: Callback> + ) => ContractEventEmitter;`, }, ]; }