Icon SunFilledIcon MoonStars

Icon LinkUsing Generated Types

After generating types via:

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

We can use these files like so:

import { Wallet } from "fuels";
import { MyContract__factory } from "./types";
 
const contractId = "0x...";
const wallet = Wallet.fromAddress("...");
const contract = MyContract__factory.connect(contractId, wallet);
 
// All contract methods are available under functions with the correct types
const { transactionId, value } = await contract.functions.my_fn(1).call();
 
console.log(transactionId, value);

Icon LinkUsing the Generated Contract Factory to Deploy a Contract

import { Wallet } from "fuels";
import { MyContract__factory } from "./types";
import bytecode from "./types/MyContract.hex.ts";
 
const wallet = Wallet.fromAddress("...");
 
const contract = await MyContract__factory.deployContract(bytecode, wallet);
 
console.log(contract.id);

You can also pass in DeployContractOptions Icon Link like storage slots and configurable constants to the deployContract method:

import storageSlots from "../contract/out/debug/storage-slots.json";
 
const contract = await MyContract__factory.deployContract(bytecode, wallet, {
	storageSlots,
});

Icon LinkUsing Generated Script Types

After generating types via:

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

We can use these files like so:

import { Wallet } from "fuels";
import { MyScript__factory } from "./types";
 
const wallet = Wallet.fromAddress("...");
const script = ScriptAbi__factory.createInstance(wallet);
 
const { value, logs } = await script.functions.main(1).call();
 
console.log({ value, logs });

Icon LinkUsing Generated Predicate Types

Consider the following predicate:

predicate;
 
struct Validation {
has_account: bool,
total_complete: u64,
}
 
fn main(received: Validation) -> bool {
let expected_has_account: bool = true;
let expected_total_complete: u64 = 100;
 
received.has_account == expected_has_account && received.total_complete == expected_total_complete
}

Now, after generating types via:

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

We can use these files like so:

import { Wallet } from "fuels";
import { MyPredicate__factory } from "./types";
 
const wallet = Wallet.fromAddress("...");
const predicate = MyPredicate__factory.createInstance();
 
await predicate
	.setData({
has_account: true,
total_complete: 100,
	})
	.transfer(wallet.address, <amount>);
 
const walletBalance = await wallet.getBalance();
const predicateBalance = await predicate.getBalance();
 
console.log({
	walletBalance,
	predicateBalance,
});

See also:

Was this page helpful?