Icon LinkRunning scripts

You can run a script using its JSON-ABI and the path to its binary file. You can run the scripts with arguments. For this, you have to use the abigen! macro seen previously .

    // The abigen is used for the same purpose as with contracts (Rust bindings)
    abigen!(Script(
name = "MyScript",
abi = "packages/fuels/tests/scripts/arguments/out/debug/arguments-abi.json"
    ));
    let wallet = launch_provider_and_get_wallet().await;
    let bin_path = "../fuels/tests/scripts/arguments/out/debug/arguments.bin";
    let script_instance = MyScript::new(wallet, bin_path);
 
    let bim = Bimbam { val: 90 };
    let bam = SugarySnack {
twix: 100,
mars: 1000,
    };
 
    let result = script_instance.main(bim, bam).call().await?;
 
    let expected = Bimbam { val: 2190 };
    assert_eq!(result.value, expected);

Icon LinkRunning scripts with transaction parameters

The method for passing transaction parameters is the same as with contracts . As a reminder, the workflow would look like this:

    let parameters = TxParameters::default()
.with_gas_price(1)
.with_gas_limit(1_000_000);
    let result = script_instance
.main(a, b)
.tx_params(parameters)
.call()
.await?;

Icon LinkLogs

Script calls provide the same logging functions, decode_logs() and decode_logs_with_type<T>(), as contract calls. As a reminder, the workflow looks like this:

    abigen!(Script(
name = "log_script",
abi = "packages/fuels/tests/logs/script_logs/out/debug/script_logs-abi.json"
    ));
 
    let wallet = launch_provider_and_get_wallet().await;
    let bin_path = "../fuels/tests/logs/script_logs/out/debug/script_logs.bin";
    let instance = log_script::new(wallet.clone(), bin_path);
 
    let response = instance.main().call().await?;
 
    let logs = response.decode_logs();
    let log_u64 = response.decode_logs_with_type::<u64>()?;

Icon LinkCalling contracts from scripts

Scripts use the same interfaces for setting external contracts as contract methods .

Below is an example that uses set_contracts(&[&contract_instance, ...]).

    let response = instance
.main(contract_id)
.with_contracts(&[&contract_instance])
.call()
.await?;

And this is an example that uses set_contract_ids(&[&contract_id, ...]).

    let response = instance
.main(contract_id)
.with_contract_ids(&[contract_id.into()])
.call()
.await?;

Icon LinkConfigurable constants

Same as contracts, you can define configurable constants in scripts which can be changed during the script execution. Here is an example how the constants are defined.

script;
 
#[allow(dead_code)]
enum EnumWithGeneric<D> {
    VariantOne: D,
    VariantTwo: (),
}
 
#[allow(dead_code)]
struct StructWithGeneric<D> {
    field_1: D,
    field_2: u64,
}
 
configurable {
    U8: u8 = 8u8,
    BOOL: bool = true,
    ARRAY: [u32; 3] = [253u32, 254u32, 255u32],
    STR_4: str[4] = "fuel",
    STRUCT: StructWithGeneric<u8> = StructWithGeneric {
field_1: 8u8,
field_2: 16,
    },
    ENUM: EnumWithGeneric<bool> = EnumWithGeneric::VariantOne(true),
}
 
fn main() -> (u8, bool, [u32; 3], str[4], StructWithGeneric<u8>, EnumWithGeneric<bool>) {
    (U8, BOOL, ARRAY, STR_4, STRUCT, ENUM)
}
 

Each configurable constant will get a dedicated with method in the SDK. For example, the constant STR_4 will get the with_STR_4 method which accepts the same type defined in sway. Below is an example where we chain several with methods and execute the script with the new constants.

    abigen!(Script(name="MyScript", abi="packages/fuels/tests/scripts/script_configurables/out/debug/script_configurables-abi.json"));
 
    let wallet = launch_provider_and_get_wallet().await;
    let bin_path = "../fuels/tests/scripts/script_configurables/out/debug/script_configurables.bin";
    let instance = MyScript::new(wallet, bin_path);
 
    let new_str: SizedAsciiString<4> = "FUEL".try_into()?;
    let new_struct = StructWithGeneric {
field_1: 16u8,
field_2: 32,
    };
    let new_enum = EnumWithGeneric::VariantTwo;
 
    let configurables = MyScriptConfigurables::new()
.with_STR_4(new_str.clone())
.with_STRUCT(new_struct.clone())
.with_ENUM(new_enum.clone());
 
    let response = instance
.with_configurables(configurables)
.main()
.call()
.await?;

Was this page helpful?