Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typechain

Package Overview
Dependencies
Maintainers
1
Versions
85
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typechain - npm Package Compare versions

Comparing version 0.2.7 to 0.3.1

__snapshots__/ContractWithOverloads.spec.ts.js

39

package.json

@@ -11,17 +11,17 @@ {

],
"version": "0.2.7",
"version": "0.3.1",
"license": "MIT",
"repository": "https://github.com/Neufund/Typechain",
"devDependencies": {
"@types/bignumber.js": "^5.0.0",
"@types/bluebird": "^3.5.18",
"@types/chai": "^4.0.6",
"@types/chai-as-promised": "^7.1.0",
"@types/chalk": "^0.4.31",
"@types/command-line-args": "^4.0.2",
"@types/debug": "^0.0.30",
"@types/fs-extra": "^4.0.2",
"@types/glob": "^5.0.32",
"@types/fs-extra": "^5.0.4",
"@types/glob": "^5.0.35",
"@types/mocha": "^2.2.44",
"@types/node": "^8.0.25",
"@types/prettier": "^1.8.1",
"@types/prettier": "^1.13.2",
"bignumber.js": "^5.0.0",

@@ -32,3 +32,4 @@ "bluebird": "^3.5.1",

"coveralls": "^3.0.2",
"ganache-cli": "^6.0.3",
"ganache-cli": "^6.1.8",
"glob": "^7.1.2",
"krzkaczor-solc": "^0.4.24",

@@ -38,7 +39,12 @@ "mocha": "^4.0.1",

"nyc": "^12.0.2",
"patch-package": "^5.1.1",
"prettier": "^1.14.2",
"snap-shot-it": "^6.1.1",
"source-map-support": "^0.5.8",
"truffle": "^4.1.14",
"truffle-typings": "^1.0.1",
"ts-node": "^3.3.0",
"tslint": "^5.8.0",
"tslint": "^5.11.0",
"tslint-language-service": "^0.9.7",
"typescript": "^2.6.1",
"typescript": "^3.0.1",
"typestrict": "^0.0.6",

@@ -49,8 +55,6 @@ "web3": "0.20.2",

"dependencies": {
"chalk": "^2.3.0",
"command-line-args": "^4.0.7",
"debug": "^3.0.1",
"fs-extra": "^4.0.2",
"glob": "^7.1.2",
"prettier": "^1.9.1"
"fs-extra": "^7.0.0",
"ts-generator": "^0.0.5"
},

@@ -62,5 +66,5 @@ "peerDependencies": {

"scripts": {
"prepublish": "yarn build",
"prepare": "patch-package && yarn build",
"build": "rm -rf ./dist && tsc -p ./tsconfig.production.json",
"postbuild": "chmod +x ./dist/cli.js",
"postbuild": "chmod +x ./dist/cli/cli.js",
"lint": "yarn formatting && yarn tslint",

@@ -71,2 +75,3 @@ "lint:fix": "yarn tslint:fix && yarn formatting:fix",

"tsc": "tsc --noEmit",
"tsc:truffle": "tsc --noEmit --project ./test/integration/targets/truffle/tsconfig.json",
"formatting": "prettier --list-different '**/*.ts'",

@@ -77,8 +82,8 @@ "formatting:fix": "prettier --write '**/*.ts'",

"test:fix": "yarn lint:fix && ./scripts/test.sh",
"test:mocha": "NODE_ENV=test mocha --require ts-node/register.js --require test/setup-chai.ts --require 'test/integration/before.ts' 'test/integration/web3.ts' 'test/**/*.spec.ts'",
"test:mocha:coverage": "NODE_ENV=test nyc mocha --require ts-node/register.js --require source-map-support/register --require test/setup-chai.ts --require 'test/integration/before.ts' 'test/integration/web3.ts' 'test/**/*.spec.ts'",
"test:mocha": "NODE_ENV=test mocha --require ts-node/register.js --require test/setup-chai.ts --require 'test/integration/before.ts' 'test/integration/web3.ts' './**/*.spec.ts'",
"test:mocha:coverage": "NODE_ENV=test nyc mocha --require ts-node/register.js --require source-map-support/register --require test/setup-chai.ts --require 'test/integration/before.ts' 'test/integration/web3.ts' './**/*.spec.ts'",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"main": "./dist/generateTypeChainWrappers.js",
"main": "./dist/index.js",
"bin": "./dist/cli.js"
}

@@ -27,4 +27,3 @@ <p align="center">

* IDE support - works with any IDE supporting Typescript
* revamped API - native promises, safety checks and more!
* compatibility - under the hood it uses web3 so it's 100% compatible
* works with multiple libraries - use `truffle` or `Web3.js 0.20.x`, `Web3.js 1.0` support coming soon
* frictionless - works with simple, JSON ABI files as well as with Truffle style ABIs

@@ -42,8 +41,8 @@

Note: TypeChain requires web3 in version: `0.20.x`.
## Usage
### CLI
```
typechain [--force] [glob]
typechain --target=(truffle|legacy) [glob]
```

@@ -53,11 +52,33 @@

"**/*.json"`
* `--force` - force overwrite existing files
* `--target` - `truffle` or `legacy`
* `--outDir` - put all generated files to a specific dir
Typechain always will rewrite existing files. You should not commit them. Read more in FAQ section.
Example:
```
typechain --force --outDir app/contracts './node_modules/neufund-contracts/build/contracts/*.json'
typechain --target --outDir app/contracts './node_modules/neufund-contracts/build/contracts/*.json'
```
### ts-generator unified config
Typechain is a plugin to [ts-generator](https://github.com/krzkaczor/ts-generator) meaning that you can as well create unified config.
`ts-generator.json` file:
```
[
{
"generator": "typechain",
"files": "./build/**/*.json",
"target": "truffle"
}
]
```
then just run `ts-generator` (you need to have `ts-generator` installed in your project).
This can be a great way to run more code generation plugins (css modules types, graphql types etc. ) at the same time.
## Demo 🏎️

@@ -81,8 +102,5 @@

TypeChain is code generator - provide ABI file and you will get Typescript class with flexible
interface for interacting with blockchain.
interface for interacting with blockchain. Depending on the target parameter it can generate typings for
truffle or web3.js 0.20.x.
In future we plan to leverage something like tsc plugin system to come up with much more elegant
solution. You can keep track of
[Allow "Compiler Plugins"](https://github.com/Microsoft/TypeScript/issues/16607) issue.
### Step by step guide

@@ -92,7 +110,6 @@

Run `typechain` (you might need to make sure that it's available in your path if you installed it
Run `typechain --target=your_target` (you might need to make sure that it's available in your path if you installed it
only locally), it will automatically find all `.abi` files in your project and generate Typescript
classes based on them. You can specify your glob pattern: `typechain "**/*.abi.json"`.
`node_modules` are always ignored. Use `--force` to overwrite already existing files. We recommend
git ignoring these generated files and making typechain part of your build process.
classes based on them. You can specify your glob pattern: `typechain --target=your_target "**/*.abi.json"`.
`node_modules` are always ignored. We recommend git ignoring these generated files and making typechain part of your build process.

@@ -103,95 +120,15 @@ That's it! Now, just import contract bindings as any other file `import { MyAwesomeContract } from

### API
## Targets 🎯
Let's take a look at typings generated for simple
[smartcontract](https://github.com/Neufund/TypeChain/blob/master/test/integration/contracts/DumbContract.sol):
### Truffle
```typescript
import { BigNumber } from "bignumber.js";
import {
TypeChainContract,
promisify,
ITxParams,
IPayableTxParams,
DeferredTransactionWrapper
} from "./typechain-runtime";
Truffle target is great when you use truffle contracts already. Check out [truffle-typechain-example](https://github.com/ethereum-ts/truffle-typechain-example) for more details. It require installing [typings](https://www.npmjs.com/package/truffle-typings) for truffle library itself.
export class DumbContract extends TypeChainContract {
public readonly rawWeb3Contract: any;
Now you can simply use your contracts as you did before and get full type safety, yay!
public constructor(web3: any, address: string) {
const abi = [
// ... ABI
];
super(web3, address, abi);
}
### Legacy
static async createAndValidate(web3: any, address: string): Promise<DumbContract> {
const contract = new DumbContract(web3, address);
const code = await promisify(web3.eth.getCode, [address]);
if (code === "0x0") {
throw new Error(`Contract at ${address} doesn't exist!`);
}
return contract;
}
This was default and only target for typechain 0.2.x. It requires `Web3.js 0.20.x` to be installed in your project and it generates promise based wrappers. It's nice upgrade comparing to raw callbacks but in the near future Typechain will support `Web3js 1.0` target.
public get counter(): Promise<BigNumber> {
return promisify(this.rawWeb3Contract.counter, []);
}
public get SOME_VALUE(): Promise<boolean> {
return promisify(this.rawWeb3Contract.SOME_VALUE, []);
}
public counterArray(index: BigNumber | number): Promise<BigNumber> {
return promisify(this.rawWeb3Contract.counterArray, [index]);
}
public returnAll(): Promise<[BigNumber, BigNumber]> {
return promisify(this.rawWeb3Contract.returnAll, []);
}
public counterWithOffset(offset: BigNumber | number): Promise<BigNumber> {
return promisify(this.rawWeb3Contract.counterWithOffset, [offset]);
}
public countupForEtherTx(): DeferredTransactionWrapper<IPayableTxParams> {
return new DeferredTransactionWrapper<IPayableTxParams>(this, "countupForEther", []);
}
public countupTx(offset: BigNumber | number): DeferredTransactionWrapper<ITxParams> {
return new DeferredTransactionWrapper<ITxParams>(this, "countup", [offset]);
}
}
```
Using it can be as simple as:
```typescript
const dumbContract = await DumbContract.createAndValidate(web3, contractAddress);
console.log(`Current counter value is: ${await dumbContract.counter}`);
console.log("Lets increase it by 2... This results in state change so we need to create tx.");
await dumbContract.countupTx(2).send({ from: accounts[0], gas: GAS_LIMIT_STANDARD });
console.log(`Current counter value is: ${await dumbContract.counter}`);
console.log("We can also get signed tx data: ");
console.log(await dumbContract.countupTx(2).getData());
console.log("When calling payable txs, TypeChain will make sure that you provide ether value:");
await dumbContract
.countupForEtherTx()
.send({ from: accounts[0], gas: GAS_LIMIT_STANDARD, value: 10 });
console.log(`Current counter value is: ${await dumbContract.counter}`);
```
which gives following output:
```
Current counter value is: 0
Lets increase it by 2... This results in state change so we need to create tx.
Current counter value is: 2
We can also get signed tx data:
0x7916df080000000000000000000000000000000000000000000000000000000000000002
When calling payable txs, TypeChain will make sure that you provide ether value:
Current counter value is: 12
```
## FAQ 🤔

@@ -211,3 +148,3 @@

### Q: How do I customize generated classes?
### Q: How do I customize generated classes? (legacy target only)

@@ -222,26 +159,22 @@ A: You can create your own class that extends generated one and adds additional methods etc.

A: We will automatically format generated classes with `prettier` to match your coding preferences (just make sure to use `.prettierrc` file). Furthermore, we will silent tslint for generated files with `/* tslint:disable */` comments.
A: We will automatically format generated classes with `prettier` to match your coding preferences (just make sure to use `.prettierrc` file).
Furthermore, we will silent tslint for generated files with `/* tslint:disable */` comments.
### Usage as API
You might also use TypeChain as api:
You may want to use `ts-generator` api to kick off whole process by api:
```typescript
import { generateTypeChainWrappers } from "../lib/generateTypeChainWrappers";
import { tsGen } from "ts-generator";
import { Typechain } from "typechain";
async function main() {
await generateTypeChainWrappers({
glob: "**/*.abi",
force: true,
});
await tsGen({ cwd }, new Typechain({ cwd, rawConfig: { files: "your-glob-here", outDir: "optional out dir path" } }));
}
main().catch(console.error);
```
other interesting methods are: `abiToWrapper` and `copyRuntime`.
## Roadmap 🛣️
* improve generated code (more checks, wiring contracts together)
* web3 1.0 target

@@ -262,4 +195,4 @@ ### Running tests

Original author:
# Licence
Krzysztof Kaczor | [Github](https://github.com/krzkaczor) | [Twitter](https://twitter.com/krzkaczor)
Krzysztof Kaczor (krzkaczor) MIT | [Github](https://github.com/krzkaczor) | [Twitter](https://twitter.com/krzkaczor)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc