In order to optimize the cost of your recurring predicate executions, we recommend first deploying your predicate. This can be done using the Fuels CLI and running the deploy command .
By deploying the predicate, its bytecode is stored on chain as a blob. The SDK will then produce bytecode that can load the blob on demand to execute the original predicate. This far reduces the repeat execution cost of the predicate.
To deploy a predicate, we can use the Fuels CLI and execute the deploy command .
This will perform the following actions:
forc
version We can then utilize the above generated types like so:
// #import { Provider, Wallet };
import { WALLET_PVT_KEY } from 'path/to/my/env/file';
import { TypegenPredicateLoader } from 'path/to/typegen/outputs';
const provider = await Provider.create(providerUrl);
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider);
const baseAssetId = provider.getBaseAssetId();
// First, we will need to instantiate the script via it's loader bytecode.
// This can be imported from the typegen outputs that were created on `fuels deploy`.
// Then we can use the predicate as we would normally, such as overriding the configurables.
const predicate = new TypegenPredicateLoader({
data: [23],
provider,
configurableConstants: {
PIN: 23,
},
});
// Now, let's fund the predicate
const fundTx = await wallet.transfer(predicate.address, 100_000, baseAssetId);
await fundTx.waitForResult();
// Then we'll execute the transfer and validate the predicate
const transferTx = await predicate.transfer(receiver.address, 1000, baseAssetId);
const { gasUsed } = await transferTx.waitForResult();