Using Generated Types
After generating types via:
console
pnpm fuels typegen -i ./abis/*-abi.json -o ./types
We can use these files like so:
ts
See code in contextimport { DemoContractAbi__factory } from './types';
const contractInstance = DemoContractAbi__factory.connect(contractId, wallet);
const call2 = await contractInstance.functions.return_input(1337).call();
const { value: v2 } = await call2.waitForResult();
Contract
Let's use the Contract class to deploy a contract:
ts
See code in contextimport { DemoContractAbi__factory } from './types';
import bytecode from './types/DemoContractAbi.hex';
// Deploy
const deploy = await DemoContractAbi__factory.deployContract(bytecode, wallet);
const { contract } = await deploy.waitForResult();
Autoloading 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
, when calling the deployContract
method:
ts
See code in contextimport storageSlots from './contract/out/debug/demo-contract-storage_slots.json';
const { waitForResult } = await DemoContractAbi__factory.deployContract(bytecode, wallet, {
storageSlots,
});
const { contract } = await waitForResult();
Script
After generating types via:
console
pnpm fuels typegen -i ./abis/*-abi.json -o ./types --script
We can use these files like so:
ts
See code in contextimport { ScriptAbi__factory } from './types';
const script = ScriptAbi__factory.createInstance(wallet);
const { waitForResult } = await script.functions.main().call();
const { value } = await waitForResult();
Predicate
After generating types via:
console
pnpm fuels typegen -i ./abis/*-abi.json -o ./types --predicate
We can use these files like so:
ts
See code in contextimport 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: