@gnosis-guild/typechain-ethers-v6

中文文档
0.5.5发布于 2024/10/29MITTypeScriptESM + CJS近 30 天下载 4184

🔌 TypeChain target for ethers-v6

原始使用文档
文档目录5 节
  1. TypeChain target Ethers-v6
  2. TypeChain readme
  3. Contract typings
  4. Contract factories
  5. Basic example

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.

TypeChain readme

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

版本与发布记录

@gnosis-guild/typechain-ethers-v6 共发布 4 个版本,首次发布于 2024/08/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.5.5latest2024/10/292 年前
0.5.42024/08/282 年前
0.5.32024/08/282 年前
0.5.22024/08/272 年前

关于 @gnosis-guild/typechain-ethers-v6 的常见问题

@gnosis-guild/typechain-ethers-v6 是什么?

🔌 TypeChain target for ethers-v6

如何安装 @gnosis-guild/typechain-ethers-v6?

在项目目录运行 npm install @gnosis-guild/typechain-ethers-v6,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:ethers(6.x)、typechain(^8.3.2)、typescript(>=4.7.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@gnosis-guild/typechain-ethers-v6 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@gnosis-guild/typechain-ethers-v6 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@gnosis-guild/typechain-ethers-v6 可以商用吗?

@gnosis-guild/typechain-ethers-v6 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gnosis-guild/typechain-ethers-v6 的下载量有多少?

根据 npm 官方下载统计,@gnosis-guild/typechain-ethers-v6 在 2026/10/17 – 2026/11/15 这 30 天内被下载 4,184 次,日均约 139 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gnosis-guild/typechain-ethers-v6 还在维护吗?

最新版本 0.5.5 发布于 2024/10/29(2 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/08/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。