⌘K

Icon SunFilledIcon MoonStars
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)
	.txParams({ gasPrice, gasLimit: 10_000 })
	.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, { gasPrice });

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

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()
.txParams({
	gasPrice: provider.getGasConfig().minGasPrice,
	gasLimit: 10_000,
})
.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 { 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 predicate = PredicateAbi__factory.createInstance(provider);
 
	const tx = await wallet.transfer(predicate.address, 100_000, BaseAssetId, {
gasPrice: provider.getGasConfig().minGasPrice,
gasLimit: 50,
	});
	await tx.wait();
 
	const initialPredicateBalance = await predicate.getBalance();
 
	// Then we are transferring some coins from the predicate to a random address (receiver)
	const tx2 = await predicate.transfer(receiver.address, 50_000, BaseAssetId, {
gasPrice: provider.getGasConfig().minGasPrice,
gasLimit: 50,
	});
	await tx2.wait();
 
	expect((await receiver.getBalance()).toNumber()).toEqual(50_000);
	expect((await predicate.getBalance()).toNumber()).toBeLessThan(
initialPredicateBalance.toNumber()
	);

See also: