Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a93ff68
Switches BigNumber to BN.
Nov 6, 2019
6122cee
Adds dependency on bn.js types.
Nov 6, 2019
27e343a
Merge pull request #1 from MicahZoltu/patch-1
pash7ka Nov 27, 2019
ec34982
Rename targets according to @krzkaczor instruction: https://github.co…
pash7ka Nov 27, 2019
e52e2d1
Fix builds for truffle@4 and truffle@5 targets
pash7ka Nov 27, 2019
cd33c65
Update package names with new namings
pash7ka Nov 27, 2019
cdac699
Change naming scheme from typechain-target-truffle@5 to typechain-tar…
pash7ka Nov 27, 2019
71606fd
Change naming scheme from typechain-target-truffle@5 to typechain-tar…
pash7ka Nov 27, 2019
0ccbb44
fix tsc target
pash7ka Nov 27, 2019
f1552b3
Rename typechain-target-truffle-v4 back to typechain-target-truffle
pash7ka Nov 27, 2019
2f2e842
Merge upstream updates (web3 v2 target)
pash7ka Dec 15, 2019
4800418
Specify target truffle version
pash7ka Dec 18, 2019
f406c18
Merge pull request #196 from pash7ka/bignumber-to-bn
krzkaczor Dec 27, 2019
835f7d0
rename truffle v5 package to new @typechain namespace
krzkaczor Jan 21, 2020
484bc5d
0.9 version and release
krzkaczor Jan 21, 2020
f851c34
update prettier config
krzkaczor Jan 21, 2020
52faa70
add support for loading targets with @typechain namespace
krzkaczor Jan 21, 2020
514fe75
Merge branch 'master' into kk/truffle-v5
krzkaczor Feb 8, 2020
13e6dd7
rename old target, cleanup a little bit test setup
krzkaczor Feb 9, 2020
6846415
ensure truffle is installed
krzkaczor Feb 9, 2020
8c810d1
bump nodejs version
krzkaczor Feb 9, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2
jobs:
build:
docker:
- image: circleci/node:8.11.2-stretch
- image: circleci/node:10.19.0-stretch

working_directory: ~/repo

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ test/integration/abis
test-tmp
.nyc_output
coverage
test/integration/targets/truffle/build
test/integration/targets/truffle*/build
test/package-test/types
packages/core/README.md
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dist/
**/*.d.ts
test/integration/targets/**/*.d.ts
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
"license": "MIT",
"scripts": {
"prepare": "patch-package",
"build": "rm -rf ./dist && tsc -p ./tsconfig.prod.json && (cd packages/core/ && yarn build) && (cd packages/typechain-target-ethers/ && yarn build) && (cd packages/typechain-target-truffle/ && yarn build) && (cd packages/typechain-target-web3-v1/ && yarn build) && (cd packages/typechain-target-web3-v2/ && yarn build)",
"build": "rm -rf ./dist && tsc -p ./tsconfig.prod.json && (cd packages/core/ && yarn build) && (cd packages/typechain-target-ethers/ && yarn build) && (cd packages/typechain-target-truffle-v4/ && yarn build) && (cd packages/typechain-target-truffle-v5/ && yarn build) && (cd packages/typechain-target-web3-v1/ && yarn build) && (cd packages/typechain-target-web3-v2/ && yarn build)",
"tslint": "tslint -p ./tsconfig.json -e 'node_modules/**/*' -e '**/node_modules/**/*' 'packages/**/*.ts'",
"tslint:fix": "tslint --fix --format stylish -p ./tsconfig.json -e 'node_modules/**/*' -e '**/node_modules/**/*' 'packages/**/*.ts'",
"format": "prettier --list-different 'packages/**/*.{ts,tsx,json,md,gql}' README.md 'test/integration/**/*.{ts,tsx,json,md,gql}'",
"format:fix": "prettier --write 'packages/**/*.{ts,tsx,json,md,gql}' README.md 'test/integration/**/*.{ts,tsx,json,md,gql}'",
"tsc": "tsc --noEmit",
"tsc:truffle": "tsc --noEmit --project ./test/integration/targets/truffle/tsconfig.json",
"prepublishOnly": "yarn test && yarn build",
"lint": "yarn format && yarn tslint",
"lint:fix": "yarn tslint:fix && yarn format:fix",
Expand Down
1 change: 1 addition & 0 deletions packages/core/lib/TypeChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class TypeChain extends TsGeneratorPlugin {
const possiblePaths = [
process.env.NODE_ENV === "test" && `../../typechain-target-${target}/lib/index`, // only for tests
`typechain-target-${target}`, //external module
`@typechain/${target}`, //external module
ensureAbsPath(target), // path
];

Expand Down
4 changes: 1 addition & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
"coveralls": "^3.0.2",
"ethers": "^4.0.7",
"ganache-cli": "^6.1.8",
"glob": "^7.1.2",
"truffle": "^4.1.14",
"truffle-typings": "^1.0.4"
"glob": "^7.1.2"
},
"dependencies": {
"command-line-args": "^4.0.7",
Expand Down
53 changes: 53 additions & 0 deletions packages/typechain-target-truffle-v4/lib/index.ts
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 TruffleV4 extends TsGeneratorPlugin {
name = "truffle-v4";

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),
},
];
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typechain-target-truffle",
"description": "TypeChain target with truffle support",
"description": "TypeChain target with truffle v4 support",
"keywords": [
"ethereum",
"typescript",
Expand All @@ -21,7 +21,7 @@
"typechain": "^1.0.0"
},
"scripts": {
"build": "rm -rf ./dist && cp -R '../../dist/typechain-target-truffle/lib' ./dist/",
"build": "rm -rf ./dist && cp -R '../../dist/typechain-target-truffle-v4/lib' ./dist/",
"prepublishOnly": "cd .. && yarn prepublishOnly"
},
"main": "./dist/index.js"
Expand Down
21 changes: 21 additions & 0 deletions packages/typechain-target-truffle-v5/README.md
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)
175 changes: 175 additions & 0 deletions packages/typechain-target-truffle-v5/lib/generation.ts
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(", ") +
"}"
);
}
29 changes: 29 additions & 0 deletions packages/typechain-target-truffle-v5/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@typechain/truffle-v5",
"description": "TypeChain target with truffle v5 support",
"keywords": [
"ethereum",
"typescript",
"bindings",
"truffle"
],
"version": "0.9.0",
"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"
}
9 changes: 4 additions & 5 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ rm -rf $ABI_DIR
echo "Generating ABIs for sample contracts"
../../../node_modules/krzkaczor-solc/solcjs --abi ./* --bin -o $ABI_DIR
echo "Compiling truffle project"
(cd ../targets/truffle && ../../../../node_modules/.bin/truffle compile)
(cd ../targets/truffle-v4 && yarn && ./node_modules/.bin/truffle compile)

echo "Building"
yarn build

echo "Running tests"
# removing any generated files because tests will regenerate them
rm -rf "test/integration/targets/truffle/@types"
rm -rf "test/integration/targets/truffle-v4/@types"
rm -rf "test/integration/targets/web3-v1/types/web3-v1-contracts"
rm -rf "test/integration/targets/web3-v2/types/web3-v2-contracts"
rm -rf "test/integration/targets/ethers/types/ethers-contracts"
Expand All @@ -35,10 +35,9 @@ else
fi

echo "Type checking generated wrappers"
yarn tsc --noUnusedParameters
yarn tsc --noUnusedParameters --noEmit
echo "--truffle"
yarn tsc:truffle
(cd ../targets/truffle && TS_NODE_FILES=true ../../../../node_modules/.bin/truffle test)
(cd ../targets/truffle-v4 && yarn test)
echo "--web3-v1"
(cd ../targets/web3-v1 && yarn && yarn test)
echo "--web3-v2"
Expand Down
Loading