Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link
Using Generated Types

Icon LinkUsing Generated Types

After generating types via:

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

We can use these files like so:

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

Icon LinkContract

Let's use the Contract class to deploy a contract:

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

Icon LinkAutoloading of Storage Slots

Typegen tries to resolve, auto-load, and embed the Storage Slots for your Contract within the MyContract__factory class. Still, you can override it alongside other options from DeployContractOptions Icon Link, when calling the deployContract method:

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

Icon LinkScript

After generating types via:

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

We can use these files like so:

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

Icon LinkPredicate

After generating types via:

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

We can use these files like so:

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

See also: