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 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: