@0xgabi/hardhat-typechain 中文文档教程
hardhat-typechain
零配置 Typechain 支持
针对 Hardhat 进行了更新!
现在支持 Ethers v5 和 Truffle v5!
针对 TypeChain v3 进行了更新!
将 Typechain 任务添加到您的安全帽项目中!
What
TypeChain 为您的智能合约提供 Typescript 绑定。 现在,您的测试和前端代码可以是类型安全的,并且可以神奇地自动完成智能合约函数名称!
Installation
npm i hardhat-typechain typechain ts-generator
# choose plugin for required target, only need to install one of these
npm i @typechain/ethers-v5 @typechain/truffle-v5 @typechain/web3-v1
并将以下语句添加到您的 hardhat.config.js
:
require("hardhat-typechain");
或者,如果您使用的是 TypeScript,请将其添加到您的 hardhat.config.ts
:
import "hardhat-typechain";
Zero Config Usage
运行 像往常一样编译 任务,Typechain 工件将自动生成在名为typechain
的根目录中。 更多配置选项详述如下。
Tasks
此插件会覆盖编译 任务并在每次编译时自动生成新的 Typechain 工件。
有一个可选标志 --no-typechain
可以传入以跳过 Typechain 编译。
此插件将 typechain 任务添加到 hardhat:
Generate Typechain typings for compiled contracts
Configuration
此插件扩展了 hardhatConfig
可选 typechain
对象。 该对象包含两个字段,outDir
和 target
。 outDir
是 TypeChain 创建的工件的输出目录(默认为 typechain
)。 target
是 TypeChain docs 指定的目标之一(默认为 醚
).
这是如何设置它的示例:
module.exports = {
typechain: {
outDir: "src/types",
target: "ethers-v5",
},
};
Usage
npx hardhat compile
- 为您的合约编译和生成 Typescript 类型。
使用 typedef 进行合约的 Waffle + Ethers 测试示例:
import { ethers, waffle } from "@nomiclabs/hardhat";
import chai from "chai";
import { Wallet } from "ethers";
import CounterArtifact from "../artifacts/Counter.json";
import { Counter } from "../typechain/Counter";
const { deployContract } = waffle;
const { expect } = chai;
describe("Counter", () => {
let counter: Counter;
beforeEach(async () => {
// 1
const signers = await ethers.signers();
// 2
counter = (await deployContract(
<Wallet>signers[0],
CounterArtifact
)) as Counter;
const initialCount = await counter.getCount();
// 3
expect(initialCount).to.eq(0);
expect(counter.address).to.properAddress;
});
// 4
describe("count up", async () => {
it("should count up", async () => {
await counter.countUp();
let count = await counter.getCount();
expect(count).to.eq(1);
});
});
describe("count down", async () => {
// 5
it("should fail", async () => {
await counter.countDown();
});
it("should count down", async () => {
await counter.countUp();
await counter.countDown();
const count = await counter.getCount();
expect(count).to.eq(0);
});
});
});
有关完整示例,请参阅此入门工具包!
hardhat-typechain
Zero-config Typechain support
Updated for Hardhat!
Now supports Ethers v5 and Truffle v5!
Updated for TypeChain v3!
Add Typechain tasks to your hardhat project!
What
TypeChain gives you Typescript bindings for your smart contracts. Now, your tests and frontend code can be typesafe and magically autocomplete smart contract function names!
Installation
npm i hardhat-typechain typechain ts-generator
# choose plugin for required target, only need to install one of these
npm i @typechain/ethers-v5 @typechain/truffle-v5 @typechain/web3-v1
And add the following statement to your hardhat.config.js
:
require("hardhat-typechain");
Or, if you are using TypeScript, add this to your hardhat.config.ts
:
import "hardhat-typechain";
Zero Config Usage
Run the compile task as normal, and Typechain artifacts will automatically be generated in a root directory called typechain
. Further configuration options are detailed below.
Tasks
This plugin overrides the compile task and automatically generates new Typechain artifacts on each compilation.
There is an optional flag --no-typechain
which can be passed in to skip Typechain compilation.
This plugin adds the typechain task to hardhat:
Generate Typechain typings for compiled contracts
Configuration
This plugin extends the hardhatConfig
optional typechain
object. The object contains two fields, outDir
and target
. outDir
is the output directory of the artifacts that TypeChain creates (defaults to typechain
). target
is one of the targets specified by the TypeChain docs (defaults to ethers
).
This is an example of how to set it:
module.exports = {
typechain: {
outDir: "src/types",
target: "ethers-v5",
},
};
Usage
npx hardhat compile
- Compiles and generates Typescript typings for your contracts.
Example Waffle + Ethers test that uses typedefs for contracts:
import { ethers, waffle } from "@nomiclabs/hardhat";
import chai from "chai";
import { Wallet } from "ethers";
import CounterArtifact from "../artifacts/Counter.json";
import { Counter } from "../typechain/Counter";
const { deployContract } = waffle;
const { expect } = chai;
describe("Counter", () => {
let counter: Counter;
beforeEach(async () => {
// 1
const signers = await ethers.signers();
// 2
counter = (await deployContract(
<Wallet>signers[0],
CounterArtifact
)) as Counter;
const initialCount = await counter.getCount();
// 3
expect(initialCount).to.eq(0);
expect(counter.address).to.properAddress;
});
// 4
describe("count up", async () => {
it("should count up", async () => {
await counter.countUp();
let count = await counter.getCount();
expect(count).to.eq(1);
});
});
describe("count down", async () => {
// 5
it("should fail", async () => {
await counter.countDown();
});
it("should count down", async () => {
await counter.countUp();
await counter.countDown();
const count = await counter.getCount();
expect(count).to.eq(0);
});
});
});
See this starter kit for a full example!
你可能也喜欢
- @0x0006e/babel-preset-react-test 中文文档教程
- @1ky/pg 中文文档教程
- @4geit/rct-account-store 中文文档教程
- @4geit/rct-todo-store 中文文档教程
- @4tunaio/4tuna-iot-node-red 中文文档教程
- @aardvarkxr/aardvark-shared 中文文档教程
- @abandonware/bluetooth-hci-socket 中文文档教程
- @abhinavzspace/redux-pouchdb-plus 中文文档教程
- @acoustic-content-sdk/module-loader 中文文档教程
- @acoustic-content-sdk/utils 中文文档教程