Deploying Scripts
In order to optimize the cost of your recurring script executions, we recommend first deploying your script. This can be done using the Fuels CLI and running the deploy command.
By deploying the script, it's bytecode is stored on chain as a blob. The SDK will then produce bytecode that can load the blob on demand that can execute the script. This far reduces the repeat execution cost of the script.
How to Deploy a Script
To deploy a script, we can use the Fuels CLI and execute the deploy command.
This will perform the following actions:
- Compile the script using your
forc
version - Deploys the built script binary to the chain as a blob
- Generates a script that loads the blob that can be used to execute the script
- Generates types for both the script 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 { } from '../../../test/typegen';
import { WALLET_PVT_KEY } from 'path/to/my/env/file';
import { TypegenScriptLoader } from 'path/to/typegen/outputs';
const provider = await Provider.create(providerUrl);
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider);
// 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`
const script = new TypegenScriptLoader(wallet);
// Now we are free to interact with the script as we would normally, such as overriding the configurables
const configurable = {
AMOUNT: 20,
};
script.setConfigurableConstants(configurable);
const { waitForResult } = await script.functions.main(10).call();
const { value, gasUsed } = await waitForResult();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20