⌘K

Running Scripts

Icon Link运行脚本

您可以使用脚本的 JSON-ABI 和其二进制文件的路径来运行脚本。您可以带参数运行脚本。为此,您必须使用之前见过的 abigen! 宏。

// The abigen is used for the same purpose as with contracts (Rust bindings)
abigen!(Script(
    name = "MyScript",
    abi = "e2e/sway/scripts/arguments/out/release/arguments-abi.json"
));
let wallet = launch_provider_and_get_wallet().await?;
let bin_path = "sway/scripts/arguments/out/release/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);

此外,如果出于任何原因需要将提交与值检索分开,您可以这样做:

let submitted_tx = script_instance.main(my_struct).submit().await?;
let value = submitted_tx.response().await?.value;

Icon Link使用事务策略运行脚本

传递事务策略的方法与合约 相同。作为提醒,工作流程如下:

let tx_policies = TxPolicies::default().with_script_gas_limit(1_000_000);
let result = script_instance
    .main(a, b)
    .with_tx_policies(tx_policies)
    .call()
    .await?;

Icon Link日志

脚本调用提供与合约调用相同的日志功能,decode_logs()decode_logs_with_type<T>()。作为提醒,工作流程如下:

abigen!(Script(
    name = "log_script",
    abi = "e2e/sway/logs/script_logs/out/release/script_logs-abi.json"
));
 
let wallet = launch_provider_and_get_wallet().await?;
let bin_path = "sway/logs/script_logs/out/release/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 Link从脚本调用合约

脚本使用与合约方法 相同的接口来设置外部合约。

以下是使用 with_contracts(&[&contract_instance, ...]) 的示例。

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

这是使用 with_contract_ids(&[&contract_id, ...]) 的示例。

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

Icon Link可配置的常量

与合约一样,您可以在 scripts 中定义可配置的常量,这些常量可以在脚本执行过程中进行更改。以下是定义常量的示例。

script;
 
#[allow(dead_code)]
enum EnumWithGeneric<D> {
    VariantOne: D,
    VariantTwo: (),
}
 
struct StructWithGeneric<D> {
    field_1: D,
    field_2: u64,
}
 
configurable {
    BOOL: bool = true,
    U8: u8 = 8,
    U16: u16 = 16,
    U32: u32 = 32,
    U64: u64 = 63,
    U256: u256 = 0x0000000000000000000000000000000000000000000000000000000000000008u256,
    B256: b256 = 0x0101010101010101010101010101010101010101010101010101010101010101,
    STR_4: str[4] = __to_str_array("fuel"),
    TUPLE: (u8, bool) = (8, true),
    ARRAY: [u32; 3] = [253, 254, 255],
    STRUCT: StructWithGeneric<u8> = StructWithGeneric {
        field_1: 8,
        field_2: 16,
    },
    ENUM: EnumWithGeneric<bool> = EnumWithGeneric::VariantOne(true),
}
//U128: u128 = 128, //TODO: add once https://github.com/FuelLabs/sway/issues/5356 is done
 
fn main() -> (bool, u8, u16, u32, u64, u256, b256, str[4], (u8, bool), [u32; 3], StructWithGeneric<u8>, EnumWithGeneric<bool>) {
    (BOOL, U8, U16, U32, U64, U256, B256, STR_4, TUPLE, ARRAY, STRUCT, ENUM)
}
 

每个可配置的常量都将在 SDK 中获得一个专用的 with 方法。例如,常量 STR_4 将获得 with_STR_4 方法,该方法接受与 sway 中定义的相同类型。以下是一个示例,其中我们链式调用了多个 with 方法,并使用新的常量执行了脚本。

abigen!(Script(
    name = "MyScript",
    abi = "e2e/sway/scripts/script_configurables/out/release/script_configurables-abi.json"
));
 
let wallet = launch_provider_and_get_wallet().await?;
let bin_path = "sway/scripts/script_configurables/out/release/script_configurables.bin";
let instance = MyScript::new(wallet, bin_path);
 
let str_4: SizedAsciiString<4> = "FUEL".try_into()?;
let new_struct = StructWithGeneric {
    field_1: 16u8,
    field_2: 32,
};
let new_enum = EnumWithGeneric::VariantTwo;
 
let configurables = MyScriptConfigurables::new(EncoderConfig {
    max_tokens: 5,
    ..Default::default()
})
.with_BOOL(false)?
.with_U8(7)?
.with_U16(15)?
.with_U32(31)?
.with_U64(63)?
.with_U256(U256::from(8))?
.with_B256(Bits256([2; 32]))?
.with_STR_4(str_4.clone())?
.with_TUPLE((7, false))?
.with_ARRAY([252, 253, 254])?
.with_STRUCT(new_struct.clone())?
.with_ENUM(new_enum.clone())?;
 
let response = instance
    .with_configurables(configurables)
    .main()
    .call()
    .await?;