类型链目标 Ethers-v6
TypeChain 目标以太-v6
QQ TypeScript 为以太 6.x.x 智能合同绑定
此软件包需要 TypeScript QQ 4.7 和 moduleResolution 将设为 node16 或者说 nodenext。 。 。 .
TypeChain 读取器
合同打字
此目标生成的主要文件是 <contract-name>.ts。他们为您的合同声明类型安全接口
醚的顶端 Contract 实例 :
- 类型合同的方法,在
contract.someMethod(...), (中文(简体) ),contract.someMethod.call(...), (中文(简体) ),contract.someMethod.staticcall(...)财务报告和审定财务报表contract.someMethod.send(...) - 输入的事件
contract.interface.events.AnEvent过滤器contract.filters.AnEvent - 排入法气体估计数
contract.someMethod.estimateGas(...) - 事件倾听器方法的覆盖( r)
on, (中文(简体) ),once等),返回同一合同类型. - 地址参数类型为
AddressLike,意思是你可以通过签或合同.
注:这些只是 类型声明 帮助你正确拨打区块链, 所以它们不能在运行时使用,
所有合同都一样 Contract 课.
合同工厂
这个目标还会为每个合同生成一个混凝土厂级,帮助您部署或连接到合同中
实例。 工厂是乙醚的延伸 ContractFactory它们有两个主要目的:
- 将合同 ABI 和字节代码包装到
ContractFactory班级,所以你不用装填和剖析JSON 手动 - 提供正确打入的接口到
ContractFactory(因为它返回平地Contract页:1.
抽象合同或固态接口的处理有些不同,因为它们没有字节码. 对于这些,a
简化工厂是不会延伸的 ContractFactory,仅包括静态 connect 方法,
这样您就可以轻松地连接到一个已部署的实例,而无需手动通过ABI.
基本实例
如果你有一个 Erc20Token.sol 固态接口和 a DummyToken.sol 执行合同.
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);
}