Deploying Predicates
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.
How to Deploy a Predicate
To deploy a predicate, we can use the Fuels CLI and execute the deploy command.
This will perform the following actions:
- Compile the predicate using your
forc
version - Deploy the built predicate binary to the chain as a blob
- Generate a new, smaller predicate that loads the deployed predicate's blob
- Generate types for both the predicate and the loader that you can use in your application
We can then utilize the above generated types like so:
ts
See code in contextimport { Provider, Wallet } from 'fuels';
import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env';
import { ConfigurablePin, ConfigurablePinLoader } from '../typegend/predicates';
const provider = await Provider.create(LOCAL_NETWORK_URL);
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider);
const receiver = Wallet.generate({ provider });
const baseAssetId = provider.getBaseAssetId();
// We can deploy dynamically or via `fuels deploy`
const originalPredicate = new ConfigurablePin({
provider,
});
const { waitForResult: waitForDeploy } = await originalPredicate.deploy(wallet);
await waitForDeploy();
// 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 loaderPredicate = new ConfigurablePinLoader({
data: [23],
provider,
configurableConstants: {
PIN: 23,
},
});
// Now, let's fund the predicate
const fundTx = await wallet.transfer(
loaderPredicate.address,
100_000,
baseAssetId
);
await fundTx.waitForResult();
// Then we'll execute the transfer and validate the predicate
const transferTx = await loaderPredicate.transfer(
receiver.address,
1000,
baseAssetId
);
const { isStatusSuccess } = await transferTx.waitForResult();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45