npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

Iโ€™ve always been into building performant and accessible sites, but lately Iโ€™ve been taking it extremely seriously. So much so that Iโ€™ve been building a tool to help me optimize and monitor the sites that I build to make sure that Iโ€™m making an attempt to offer the best experience to those who visit them. If youโ€™re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, ๐Ÿ‘‹, Iโ€™m Ryan Hefnerย  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If youโ€™re interested in other things Iโ€™m working on, follow me on Twitter or check out the open source projects Iโ€™ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soonโ€“ish.

Open Software & Tools

This site wouldnโ€™t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you ๐Ÿ™

ยฉ 2024 โ€“ย Pkg Stats / Ryan Hefner

@hovoh/typechain-ethers-multicall

v10.1.3

Published

๐Ÿ”Œ TypeChain target for ethers-multicall

Downloads

287

Readme

Typechain target Ethers-v5

This package requires TypeScript >= 4.0. If you need support for earlier TS versions check out: 1.0 version of this package.

TypeChain readme

Start here

  1. First install the package: yarn add --dev @hovoh/typechain-ethers-multicall
  2. Generate typings by specifying the full path to the module: typechain --target node_modules/@hovoh/typechain-ethers-multicall --out-dir ./generated './abis/**/*.json'

Then to make calls, make sure ethers and @hovoh/ethcall is installed. yarn add ethers @hovoh/ethcall

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 and Ethcall Contract:

  • typed contract's methods, available both at contract.someMethod(...) and contract.functions.someMethod(...)
  • typed events in contract.interface.events.AnEvent and filters in contract.filters.AnEvent
  • typed method gas estimates in contract.estimateGas.someMethod
  • overrides for the event listener methods (on, once, etc) that return the same contract type.
  • Prepare multicalls typings with the <contract-name>Multicall class

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).

The connect method returns the basic EthersJs Contract to make simple calls to your contract The multicall method returns an Ethcall Contract instance. To see how to use it please refer to their docs.

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. The test package contains a test which can be used as a reference.

import { BigNumber } from 'ethers';
import { Wallet } from 'ethers';
import {initMulticallProvider} from "@hovoh/ethcall";

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


const provider = getYourProvider(...);
const chainId = 1;

// 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<BigNumber> {
    const token = Erc20TokenFactory.connect(tokenAddress, provider);
    return token.balanceOf(walletAddress);
}

// You can query multiple balances with the same call by creating a multicall contract instance
// and running the query with a multicall provider.
async function getTokenBalance(walletAddresses: string[], tokenAddress: string): Promise<BigNumber[]> {
  const token = Erc20TokenFactory.multicall(tokenAddress, provider);
  const calls = walletAddresses.map(address => token.balanceOf(address));
  const multicall = initMulticallProvider(provider, chainId)
  return multicall.all(calls);
}