Using Generated Types

Icon Link使用生成的类型

通过以下方式生成类型:

pnpm fuels typegen -i ./abis/*-abi.json -o ./types

我们可以像这样使用这些文件:

import { DemoContractAbi__factory } from './types';
 
const contractInstance = DemoContractAbi__factory.connect(contractId, wallet);
const { value: v2 } = await contractInstance.functions.return_input(1337).call();

Icon Link合约

让我们使用 Contract 类来部署一个合约:

import { DemoContractAbi__factory } from './types';
import bytecode from './types/DemoContractAbi.hex';
 
// Deploy
const contract = await DemoContractAbi__factory.deployContract(bytecode, wallet);
 

Icon Link自动加载存储槽

Typegen 试图在 MyContract__factory 类中解析、自动加载并嵌入您的合约的存储槽 。但是,在调用 deployContract 方法时,您仍然可以覆盖它以及来自 DeployContractOptions Icon Link 的其他选项:

import storageSlots from './contract/out/debug/demo-contract-storage_slots.json';
 
const contract = await DemoContractAbi__factory.deployContract(bytecode, wallet, {
  storageSlots,
});

Icon Link脚本

通过以下方式生成类型:

pnpm fuels typegen -i ./abis/*-abi.json -o ./types --script

我们可以像这样使用这些文件:

import { ScriptAbi__factory } from './types';
 
const script = ScriptAbi__factory.createInstance(wallet);
const { value } = await script.functions.main().call();

Icon Link断言

通过以下方式生成类型:

pnpm fuels typegen -i ./abis/*-abi.json -o ./types --predicate

我们可以像这样使用这些文件:

import type { PredicateAbiInputs } from './types';
import { PredicateAbi__factory } from './types';
 
// In this exchange, we are first transferring some coins to the predicate
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
const receiver = Wallet.fromAddress(Address.fromRandom(), provider);
 
const predicateData: PredicateAbiInputs = [];
const predicate = PredicateAbi__factory.createInstance(provider, predicateData);
 
const tx = await wallet.transfer(predicate.address, 150_000, baseAssetId);
const { isStatusSuccess } = await tx.wait();
 
// Then we are transferring some coins from the predicate to a random address (receiver)
const tx2 = await predicate.transfer(receiver.address, 50_000, baseAssetId);
await tx2.wait();
 
expect((await receiver.getBalance()).toNumber()).toEqual(50_000);
expect(isStatusSuccess).toBeTruthy();

另请参阅: