-
Notifications
You must be signed in to change notification settings - Fork 361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add truffle v5 target, switch from BigNumber to BN #196
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a93ff68
Switches BigNumber to BN.
MicahZoltu 6122cee
Adds dependency on bn.js types.
MicahZoltu 27e343a
Merge pull request #1 from MicahZoltu/patch-1
pash7ka ec34982
Rename targets according to @krzkaczor instruction: https://github.co…
pash7ka e52e2d1
Fix builds for truffle@4 and truffle@5 targets
pash7ka cd33c65
Update package names with new namings
pash7ka cdac699
Change naming scheme from typechain-target-truffle@5 to typechain-tar…
pash7ka 71606fd
Change naming scheme from typechain-target-truffle@5 to typechain-tar…
pash7ka 0ccbb44
fix tsc target
pash7ka f1552b3
Rename typechain-target-truffle-v4 back to typechain-target-truffle
pash7ka 2f2e842
Merge upstream updates (web3 v2 target)
pash7ka 4800418
Specify target truffle version
pash7ka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Typechain target Truffle | ||
|
||
<p align="center"> | ||
<img src="https://github.com/Neufund/TypeChain/blob/d82f3cc644a11e22ca8e42505c16f035e2f2555d/docs/images/typechain-logo.png?raw=true" width="300" alt="TypeChain"> | ||
<h3 align="center">TypeChain target Truffle v5</h3> | ||
<p align="center">🔌 TypeScript bindings for Truffle v5 smartcontracts</p> | ||
|
||
<p align="center"> | ||
<a href="https://circleci.com/gh/ethereum-ts/TypeChain"><img alt="Build Status" src="https://circleci.com/gh/ethereum-ts/TypeChain/tree/master.svg?style=svg"></a> | ||
<a href="https://coveralls.io/github/ethereum-ts/TypeChain?branch=master"><img alt="Coverage" src="https://coveralls.io/repos/github/ethereum-ts/TypeChain/badge.svg?branch=master"></a> | ||
<img alt="Downloads" src="https://img.shields.io/npm/dm/typechain.svg"> | ||
<a href="https://github.com/prettier/prettier"><img alt="Prettier" src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg"></a> | ||
<a href="/package.json"><img alt="Software License" src="https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square"></a> | ||
</p> | ||
|
||
<p align="center"> | ||
<a href="https://blog.neufund.org/introducing-typechain-typescript-bindings-for-ethereum-smart-contracts-839fc2becf22">Medium post</a> | <a href="https://www.youtube.com/watch?v=9x6AkShGkwU">DappCon Video</a> | ||
</p> | ||
</p> | ||
|
||
## [TypeChain readme](https://github.com/ethereum-ts/TypeChain) |
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,175 @@ | ||
import { | ||
Contract, | ||
AbiParameter, | ||
FunctionDeclaration, | ||
isConstant, | ||
isConstantFn, | ||
AbiOutputParameter, | ||
EvmType, | ||
TupleType, | ||
EvmOutputType, | ||
} from "typechain"; | ||
import { values } from "lodash"; | ||
|
||
export function codegen(contracts: Contract[]) { | ||
const template = ` | ||
/// <reference types="truffle-typings" /> | ||
import BN from "bn.js"; | ||
|
||
${contracts.map(generateContractInterface).join("\n")} | ||
|
||
${contracts.map(generateContractInstanceInterface).join("\n")} | ||
`; | ||
|
||
return template; | ||
} | ||
|
||
export function generateArtifactHeaders(contracts: Contract[]): string { | ||
return ` | ||
/// <reference types="truffle-typings" /> | ||
|
||
import * as TruffleContracts from "."; | ||
|
||
declare global { | ||
namespace Truffle { | ||
interface Artifacts { | ||
${contracts | ||
.map(f => `require(name: "${f.name}"): TruffleContracts.${f.name}Contract;`) | ||
.join("\n")} | ||
} | ||
} | ||
} | ||
`; | ||
} | ||
|
||
function generateContractInterface(c: Contract): string { | ||
return ` | ||
export interface ${c.name}Contract extends Truffle.Contract<${c.name}Instance> { | ||
${ | ||
c.constructor && c.constructor[0] | ||
? `"new"(${generateInputTypes( | ||
c.constructor[0].inputs, | ||
)} meta?: Truffle.TransactionDetails): Promise<${c.name}Instance>;` | ||
: `"new"(meta?: Truffle.TransactionDetails): Promise<${c.name}Instance>;` | ||
} | ||
} | ||
`; | ||
} | ||
|
||
function generateContractInstanceInterface(c: Contract): string { | ||
return ` | ||
export interface ${c.name}Instance extends Truffle.ContractInstance { | ||
${values(c.functions) | ||
.map(v => v[0]) | ||
.map(generateFunction) | ||
.join("\n")} | ||
} | ||
`; | ||
} | ||
|
||
function generateFunction(fn: FunctionDeclaration): string { | ||
if (isConstant(fn) || isConstantFn(fn)) { | ||
return generateConstantFunction(fn); | ||
} | ||
|
||
return ` | ||
${fn.name}: { | ||
(${generateInputTypes( | ||
fn.inputs, | ||
)} txDetails?: Truffle.TransactionDetails): Promise<Truffle.TransactionResponse>; | ||
call(${generateInputTypes( | ||
fn.inputs, | ||
)} txDetails?: Truffle.TransactionDetails): Promise<${generateOutputTypes(fn.outputs)}>; | ||
sendTransaction(${generateInputTypes( | ||
fn.inputs, | ||
)} txDetails?: Truffle.TransactionDetails): Promise<string>; | ||
estimateGas(${generateInputTypes( | ||
fn.inputs, | ||
)} txDetails?: Truffle.TransactionDetails): Promise<number>; | ||
} | ||
`; | ||
} | ||
|
||
function generateConstantFunction(fn: FunctionDeclaration): string { | ||
return ` | ||
${fn.name}(${generateInputTypes( | ||
fn.inputs, | ||
)} txDetails?: Truffle.TransactionDetails): Promise<${generateOutputTypes(fn.outputs)}>; | ||
`; | ||
} | ||
|
||
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<AbiOutputParameter>): string { | ||
if (outputs.length === 1) { | ||
return generateOutputType(outputs[0].type); | ||
} else { | ||
return `[${outputs.map(param => generateOutputType(param.type)).join(", ")}]`; | ||
} | ||
} | ||
|
||
function generateInputType(evmType: EvmType): string { | ||
switch (evmType.type) { | ||
case "integer": | ||
return "number | BN | string"; | ||
case "uinteger": | ||
return "number | BN | string"; | ||
case "address": | ||
return "string | BN"; | ||
case "bytes": | ||
return "string | BN"; | ||
case "dynamic-bytes": | ||
return "string"; | ||
case "array": | ||
return `(${generateInputType(evmType.itemType)})[]`; | ||
case "boolean": | ||
return "boolean"; | ||
case "string": | ||
return "string"; | ||
case "tuple": | ||
return generateTupleType(evmType, generateInputType); | ||
} | ||
} | ||
|
||
function generateOutputType(evmType: EvmOutputType): string { | ||
switch (evmType.type) { | ||
case "integer": | ||
return "BN"; | ||
case "uinteger": | ||
return "BN"; | ||
case "address": | ||
return "string"; | ||
case "void": | ||
return "void"; | ||
case "bytes": | ||
case "dynamic-bytes": | ||
return "string"; | ||
case "array": | ||
return `(${generateOutputType(evmType.itemType)})[]`; | ||
case "boolean": | ||
return "boolean"; | ||
case "string": | ||
return "string"; | ||
case "tuple": | ||
return generateTupleType(evmType, generateOutputType); | ||
} | ||
} | ||
|
||
function generateTupleType(tuple: TupleType, generator: (evmType: EvmType) => string) { | ||
return ( | ||
"{" + | ||
tuple.components | ||
.map(component => `${component.name}: ${generator(component.type)}`) | ||
.join(", ") + | ||
"}" | ||
); | ||
} |
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,53 @@ | ||
import { Contract, getFilename, extractAbi, parse } from "typechain"; | ||
import { TsGeneratorPlugin, TContext, TFileDesc } from "ts-generator"; | ||
import { join, resolve } from "path"; | ||
|
||
import { codegen, generateArtifactHeaders } from "./generation"; | ||
|
||
export interface ITruffleCfg { | ||
outDir?: string; | ||
} | ||
|
||
const DEFAULT_OUT_PATH = "./types/truffle-contracts/"; | ||
|
||
export default class Truffle extends TsGeneratorPlugin { | ||
name = "Truffle"; | ||
|
||
private readonly outDirAbs: string; | ||
private contracts: Contract[] = []; | ||
|
||
constructor(ctx: TContext<ITruffleCfg>) { | ||
super(ctx); | ||
|
||
const { cwd, rawConfig } = ctx; | ||
|
||
this.outDirAbs = resolve(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); | ||
|
||
this.contracts.push(contract); | ||
} | ||
|
||
afterRun(): TFileDesc[] { | ||
return [ | ||
{ | ||
path: join(this.outDirAbs, "index.d.ts"), | ||
contents: codegen(this.contracts), | ||
}, | ||
{ | ||
path: join(this.outDirAbs, "merge.d.ts"), | ||
contents: generateArtifactHeaders(this.contracts), | ||
}, | ||
]; | ||
} | ||
} |
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,29 @@ | ||
{ | ||
"name": "typechain-target-truffle-v5", | ||
"description": "TypeChain target with truffle v5 support", | ||
"keywords": [ | ||
"ethereum", | ||
"typescript", | ||
"bindings", | ||
"truffle" | ||
], | ||
"version": "1.0.2", | ||
"license": "MIT", | ||
"repository": "https://github.com/ethereum-ts/Typechain", | ||
"devDependencies": { | ||
"truffle": "^5.1.1", | ||
"truffle-typings": "^1.0.8" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.17.15", | ||
"@types/bn.js": "4.11.5" | ||
}, | ||
"peerDependencies": { | ||
"typechain": "^1.0.0" | ||
}, | ||
"scripts": { | ||
"build": "rm -rf ./dist && cp -R '../../dist/typechain-target-truffle-v5/lib' ./dist/", | ||
"prepublishOnly": "cd .. && yarn prepublishOnly" | ||
}, | ||
"main": "./dist/index.js" | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can u add here
Truffle@5
or something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done