GithubHelp home page GithubHelp logo

jbelke / typechain Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dethcrypto/typechain

0.0 3.0 0.0 232 KB

๐Ÿ”Œ Typescript bindings for Ethereum smartcontracts

License: Other

TypeScript 97.44% JavaScript 1.07% Shell 1.49%

typechain's Introduction

TypeChain

TypeChain

๐Ÿ”Œ Typescript bindings for Ethereum smartcontracts

Build Status Prettier Software License

Medium post

Features โšก

  • static typing - you will never call not existing method again
  • 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
  • frictionless - works with simple, JSON ABI files as well as with Truffle style ABIs

Installation

npm install --save-dev typechain
yarn add --dev typechain

Note: TypeChain requires web3 in version: 0.20.x.

Usage

typechain [--force] [glob]
  • glob - pattern that will be used to find ABIs, remember about adding quotes: typechain "**/*.json"
  • --force - force overwrite existing files
  • --outDir - put all generated files to a specific dir

Example:

typechain --force --outDir app/contracts './node_modules/neufund-contracts/build/contracts/*.json'

Demo ๐ŸŽ๏ธ

Demo

Example usage

Getting started ๐Ÿ“š

Motivation

Interacting with blockchain in Javascript is a pain. Web3 interface is sluggish and when using it with Typescript it gets even worse. Often, you can't be sure what given method call will actually do without looking at ABI file. TypeChain is here to solve these problems (as long as you use Typescript).

How does it work?

TypeChain is code generator - provide ABI file and you will get Typescript class with flexible interface for interacting with blockchain.

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" issue.

Step by step guide

Install typechain with yarn add --dev typechain.

Run typechain (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.

That's it! Now, just import contract bindings as any other file import { MyAwesomeContract } from './contracts/MyAwesomeContract' and start interacting with it. We use named exports because of this.

API

Let's take a look at typings generated for simple smartcontract:

import { BigNumber } from "bignumber.js";
import {
  TypeChainContract,
  promisify,
  ITxParams,
  IPayableTxParams,
  DeferredTransactionWrapper
} from "./typechain-runtime";

export class DumbContract extends TypeChainContract {
  public readonly rawWeb3Contract: any;

  public constructor(web3: any, address: string) {
    const abi = [
      // ... ABI
    ];
    super(web3, address, abi);
  }

  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;
  }

  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:

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 ๐Ÿค”

Q: Should I commit generated files to my repository?

A: NO โ€” we believe that no generated files should go to git repository. You should git ignore them and make typechain run automatically for example in post install hook in package.json:

"postinstall":"typechain"

When you update ABI, just regenerate files with TypeChain and Typescript compiler will find any breaking changes for you.

Q: How do I customize generated classes?

A: You can create your own class that extends generated one and adds additional methods etc.

Currently we discuss various ideas around extendibility including APIs files (adding semantics to ABIs for covering popular cases like working with dates) or using user-defined templates for generated code.

Q: Generated files won't match current codestyle of my project :(

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.

Use as API

You might also use TypeChain as api:

import { generateTypeChainWrappers } from "../lib/generateTypeChainWrappers";

async function main() {
  await generateTypeChainWrappers({
    glob: "**/*.abi",
    force: true,
  });
}

main().catch(console.error);

Roadmap ๐Ÿ›ฃ๏ธ

  • improve generated code (more checks, wiring contracts together)

Running tests

yarn        # install all dependencies
yarn test   # runs tests + linting

Debugging ๐Ÿž

DEBUG=typechain typechain

Original author:

Krzysztof Kaczor | Github | Twitter

typechain's People

Contributors

krzkaczor avatar sdesmond46 avatar linusnorton avatar lukeautry avatar

Watchers

James Cloos avatar Joshua Belke avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.