Typechain Polkadot Overview

Bohdan Ohorodnii
727.ventures
Published in
3 min readNov 10, 2022

Developing smart contracts is very responsible work. The contract should be audited and well-tested to be trusted by users, and it’s where Typechain-Polkadot comes into hands.

It provides many features to deploy, test, and interact with contracts. The main reasons for developing Typechain-Polkadot were to make development faster and to create a user-friendly contract interface, which includes (or will consist of) sending transactions, query methods, event subscribing, and much more. Also, it fully covers every type of argument and return type, so you will be sure that your scripts won’t have undefined behavior.

Technical Details

Let’s dive into more technical details. When a smart contract is written, the developer receives a file representation in the Application Binary Interface (ABI) format. One ABI per each contract new ABI for every update of a contract. Information about how to interact with a contract (methods names, arguments & returns types, etc.) is included in this ABI file. It is not relatively human-readable, so extracting that information becomes a challenge. We need to have the correct type definitions for each contract in TypeScript. Interaction with blockchain is done with the polkadot.js library, which only has abstract definitions for the assurance in use. Thus users’ code cannot be typesafe. And Typechain-Polkadot can change it.

To start using Typechain-Polkadot, you can go directly to its repo,

https://github.com/727-ventures/typechain-polkadot and read the docs.

Also, you can check our examples and flipper-contract setup

Installation:

To install Typechain-Polkadot into your project

  • Create an npm project
  • Install with
npm i -D @727-Ventures/typechain-polkadot@<VERSION<
  • Now you can use it like that
npx @727-Ventures/typechain-polkadot — input path/to/input — output path/to/output

In input, you should have ABI for each contract you want to interact with, and also, if you deploy them, you should provide a .contract file. After you run, you will have files generated by Typechain-Polkadot in the output directory.

Generated code

Let’s go through essential classes generated by the typechain you will use.

The first will be the Contract class. It’s our main class that consists of:

• .tx namespace — used to send transactions

• .query namespace — used to query the contract

• .mixed-methods — contains both .tx and .query

• .build-extrinsic — used to create unsigned transactions to be sent sooner

Also, it provides some utility functions like:

• .withSigner — to change the signer of the contract

• .withAddress — to change the address of the contract (it’s useful for proxies)

• .withApi — to change the API used in the contract

The constructor consumes the following arguments:

• address: string — address of the contract

• signer: KeyringPair — signer

• nativeAPI: ApiPromise — API

The second important class is Constructors. It’s used to deploy contracts. The constructor consumes the following arguments:

• nativeAPI: ApiPromise — API

• signer: KeyringPair — signer

And if you want to deploy a contract, you should create an instance of the Constructors class and call .<constucor-name> method!

Conclusion

To sum up, Typechain-Polkadot is a fast-growing project, and we have many plans for what to do. Web3 Foundation supported us with our first milestone, and we have two more milestones, which include very cool features like Typechain-Compiler — a tool that will help you to compile all contracts and call typechain in just one command, Typechain-Types — a utility package to avoid code re-usage. If you have suggestions or feedback, don’t be shy to share them in our GitHub repo or Element chat!

About us

727.ventures is venture studio that creates, builds, and funds the next generation of web3 companies.
Аre focused on companies that change the landscape of the markets and ecosystems on both small and big scales, those that drive the creation of web3, and those that are dedicated towards innovation done for people.

Website, Twitter

--

--