@gnosis-guild/typechain-ethers-v6
TypeScript icon, indicating that this package has built-in type declarations

0.5.5Β β€’Β PublicΒ β€’Β Published

Typechain target Ethers-v6

TypeChain

TypeChain target Ethers-v6

πŸ”Œ TypeScript bindings for Ethers 6.x.x smartcontracts

Build Status Downloads Prettier Software License

Medium post | DappCon Video

This package requires TypeScript >= 4.7 and moduleResolution to be set as node16 or nodenext.

Contract typings

The main files generated by this target are <contract-name>.ts. They declare typesafe interfaces for your contracts on top of ethers Contract instances:

  • typed contract's methods, available both at contract.someMethod(...), contract.someMethod.call(...), contract.someMethod.staticcall(...) and contract.someMethod.send(...)
  • typed events in contract.interface.events.AnEvent and filters in contract.filters.AnEvent
  • typed method gas estimates in contract.someMethod.estimateGas(...)
  • overrides for the event listener methods (on, once, etc) that return the same contract type.
  • address argument types are AddressLike, meaning you can pass a signer or contract.

Note: these are just type declarations to help you call the blockchain properly, so they're not available at runtime, and all of the contracts are still instances of the same Contract class.

Contract factories

This target also generates a concrete factory class for each contract, to help you deploy or connect to contract instances. The factory classes are an extension of ethers' ContractFactory. They serve two main purposes:

  • wrap passing contract ABI and bytecode to the ContractFactory class, so you don't have to load and parse the JSON manually
  • provide a correctly typed interface to ContractFactory (since it returns plain Contract instances).

Abstract contracts or solidity interfaces are handled a bit different, because they have no bytecode. For those, a simplified factory is generated that doesn't extends ContractFactory, and only includes the static connect method, so you can easily connect to a deployed instance without having to pass the ABI manually.

Basic example

Suppose you have an Erc20Token.sol solidity interface and a DummyToken.sol contract implementing it.

import { Wallet } from 'ethers';

import { DummyTokenFactory } from 'typechain-out-dir/DummyTokenFactory';
import { DummyToken } from 'typechain-out-dir/DummyToken';
import { Erc20TokenFactory } from 'typechain-out-dir/Erc20TokenFactory';

const provider = getYourProvider(...);

// use the concrete contract factory if you need to operate on the bytecode (ie. deploy)
async function deployTestToken(ownerPK: string): Promise<DummyToken> {
    const owner = new Wallet(ownerPK, provider);
    return new DummyTokenFactory(owner).deploy();
}

// to call existing contracts, a factory for both the concrete contract and for the interface
// can be used since the ABI is the same
async function getTokenBalance(walletAddress: string, tokenAddress: string): Promise<bigint> {
    const token = Erc20TokenFactory.connect(tokenAddress, provider);
    return token.balanceOf(walletAddress);
}

Package Sidebar

Install

npm i @gnosis-guild/typechain-ethers-v6

Weekly Downloads

125

Version

0.5.5

License

MIT

Unpacked Size

97.6 kB

Total Files

32

Last publish

Collaborators

  • auryn