-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from krzkaczor/kk/target-web3-1.0.0
Web3.js 1.0.0 support
- Loading branch information
Showing
18 changed files
with
2,665 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { | ||
Contract, | ||
AbiParameter, | ||
ConstantFunctionDeclaration, | ||
FunctionDeclaration, | ||
ConstantDeclaration, | ||
EventDeclaration, | ||
} from "../../parser/abiParser"; | ||
import { | ||
EvmType, | ||
IntegerType, | ||
UnsignedIntegerType, | ||
AddressType, | ||
VoidType, | ||
BytesType, | ||
BooleanType, | ||
ArrayType, | ||
StringType, | ||
} from "../../parser/typeParser"; | ||
|
||
export function codegen(contract: Contract) { | ||
const template = ` | ||
import Contract, { CustomOptions, contractOptions } from "web3/eth/contract"; | ||
import { TransactionObject, BlockType } from "web3/eth/types"; | ||
import { Callback, EventLog } from "web3/types"; | ||
import { EventEmitter } from "events"; | ||
import { Provider } from "web3/providers"; | ||
export class ${contract.name} { | ||
constructor( | ||
jsonInterface: any[], | ||
address?: string, | ||
options?: CustomOptions | ||
); | ||
options: contractOptions; | ||
methods: { | ||
${contract.constantFunctions.map(generateFunction).join("\n")} | ||
${contract.functions.map(generateFunction).join("\n")} | ||
${contract.constants.map(generateConstants).join("\n")} | ||
}; | ||
deploy(options: { | ||
data: string; | ||
arguments: any[]; | ||
}): TransactionObject<Contract>; | ||
events: { | ||
${contract.events.map(generateEvents).join("\n")} | ||
allEvents: ( | ||
options?: { | ||
filter?: object; | ||
fromBlock?: BlockType; | ||
topics?: string[]; | ||
}, | ||
cb?: Callback<EventLog> | ||
) => EventEmitter; | ||
}; | ||
getPastEvents( | ||
event: string, | ||
options?: { | ||
filter?: object; | ||
fromBlock?: BlockType; | ||
toBlock?: BlockType; | ||
topics?: string[]; | ||
}, | ||
cb?: Callback<EventLog[]> | ||
): Promise<EventLog[]>; | ||
setProvider(provider: Provider): void; | ||
} | ||
`; | ||
|
||
return template; | ||
} | ||
|
||
function generateFunction(fn: ConstantFunctionDeclaration | FunctionDeclaration): string { | ||
return ` | ||
${fn.name}(${generateInputTypes(fn.inputs)}): TransactionObject<${generateOutputTypes( | ||
fn.outputs, | ||
)}>; | ||
`; | ||
} | ||
|
||
function generateConstants(fn: ConstantDeclaration): string { | ||
return `${fn.name}(): TransactionObject<${generateOutputTypes([fn.output])}>;`; | ||
} | ||
|
||
function generateInputTypes(input: Array<AbiParameter>): string { | ||
if (input.length === 0) { | ||
return ""; | ||
} | ||
return ( | ||
input | ||
.map((input, index) => `${input.name || `arg${index}`}: ${generateInputType(input.type)}`) | ||
.join(", ") + ", " | ||
); | ||
} | ||
|
||
function generateOutputTypes(outputs: Array<EvmType>): string { | ||
if (outputs.length === 1) { | ||
return generateOutputType(outputs[0]); | ||
} else { | ||
return `{ ${outputs.map((t, i) => `${i}: ${generateOutputType(t)}`).join(", ")}}`; | ||
} | ||
} | ||
|
||
function generateEvents(event: EventDeclaration) { | ||
return ` | ||
${event.name}( | ||
options?: { | ||
filter?: object; | ||
fromBlock?: BlockType; | ||
topics?: string[]; | ||
}, | ||
cb?: Callback<EventLog>): EventEmitter; | ||
`; | ||
} | ||
|
||
function generateInputType(evmType: EvmType): string { | ||
switch (evmType.constructor) { | ||
case IntegerType: | ||
return "number | string"; | ||
case UnsignedIntegerType: | ||
return "number | string"; | ||
case AddressType: | ||
return "string"; | ||
case BytesType: | ||
return "string | number[]"; | ||
case ArrayType: | ||
return `(${generateInputType((evmType as ArrayType).itemType)})[]`; | ||
case BooleanType: | ||
return "boolean"; | ||
case StringType: | ||
return "string"; | ||
|
||
default: | ||
throw new Error(`Unrecognized type ${evmType}`); | ||
} | ||
} | ||
|
||
function generateOutputType(evmType: EvmType): string { | ||
switch (evmType.constructor) { | ||
case IntegerType: | ||
return "string"; | ||
case UnsignedIntegerType: | ||
return "string"; | ||
case AddressType: | ||
return "string"; | ||
case VoidType: | ||
return "void"; | ||
case BytesType: | ||
return "string"; | ||
case ArrayType: | ||
return `(${generateOutputType((evmType as ArrayType).itemType)})[]`; | ||
case BooleanType: | ||
return "boolean"; | ||
case StringType: | ||
return "string"; | ||
|
||
default: | ||
throw new Error(`Unrecognized type ${evmType}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Contract } from "../../parser/abiParser"; | ||
import { TsGeneratorPlugin, TContext, TFileDesc } from "ts-generator"; | ||
import { join } from "path"; | ||
import { extractAbi, parse } from "../../parser/abiParser"; | ||
import { getFilename } from "../shared"; | ||
import { codegen } from "./generation"; | ||
|
||
export interface IWeb3Cfg { | ||
outDir?: string; | ||
} | ||
|
||
const DEFAULT_OUT_PATH = "./types/truffle-contracts/"; | ||
|
||
export class Web3 extends TsGeneratorPlugin { | ||
name = "Web3"; | ||
|
||
private readonly outDirAbs: string; | ||
|
||
constructor(ctx: TContext<IWeb3Cfg>) { | ||
super(ctx); | ||
|
||
const { cwd, rawConfig } = ctx; | ||
|
||
this.outDirAbs = join(cwd, rawConfig.outDir || DEFAULT_OUT_PATH); | ||
} | ||
|
||
transformFile(file: TFileDesc): TFileDesc | void { | ||
const abi = extractAbi(file.contents); | ||
const isEmptyAbi = abi.length === 0; | ||
if (isEmptyAbi) { | ||
return; | ||
} | ||
|
||
const name = getFilename(file.path); | ||
|
||
const contract = parse(abi, name); | ||
|
||
return { | ||
path: join(this.outDirAbs, "index.d.ts"), | ||
contents: codegen(contract), | ||
}; | ||
} | ||
} |
Oops, something went wrong.