3.3 脚本(Scripts)

A script is runnable bytecode on the chain which executes once to perform some task. It does not represent ownership of any resources and it cannot be called by a contract. A script can return a single value of any type.

脚本是链上可运行的字节码,运行一次来执行某些任务。它不代表任何资源的所有权,也不能被合约调用。脚本可以返回任何类型的单个值。

Scripts are state-aware in that while they have no persistent storage (because they only exist during the transaction) they can call contracts and act based upon the returned values and results.

脚本是状态可感知的,因为虽然它们没有持久存储(因为它们只在交易期间存在),但它们可以调用合约并根据返回的值和结果采取行动。

This example script calls a contract:

脚本调用合约的示例:

script;

use std::constants::ZERO_B256;
use wallet_abi::Wallet;

fn main() {
    let contract_address = 0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b;
    let caller = abi(Wallet, contract_address);
    let amount_to_send = 200;
    let recipient_address = Address::from(0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b);
    caller.send_funds {
        gas: 10000,
        coins: 0,
        asset_id: ZERO_B256,
    }(amount_to_send, recipient_address);
}

Scripts, similar to predicates, rely on a main() function as an entry point. You can call other functions defined in a script from the main() function or call another contract via an abi cast.

脚本与谓词类似,依靠一个main()函数作为入口。你可以从main()函数中调用脚本中定义的其他函数,或者通过abi cast调用另一个合约。

An example use case for a script would be a router that trades funds through multiple DEXes to get the price for the input asset, or a script to re-adjust a Collateralized Debt Position via a flashloan.

脚本的一个用例是通过多个DEX进行资金交易,成为获得输入资产的价格的路由器,或者通过闪电贷重新调整抵押债务头寸。

脚本和SDK (Scripts and the SDKs)

Unlike EVM transactions which can call a contract directly (but can only call a single contract), Fuel transactions execute a script, which may call zero or more contracts. The Rust and TypeScript SDKs provide functions to call contract methods as if they were calling contracts directly. Under the hood, the SDKs wrap all contract calls with scripts that contain minimal code to simply make the call and forward script data as call parameters.

与EVM交易可以直接调用合约(但只能调用一个合约)不同,Fuel交易执行一个脚本,它可以调用零个或多个合约。Rust和TypeScript SDKs提供了调用合约方法的功能,就像他们直接调用合约一样。在这种情况下,SDK用脚本包装所有的合约调用,这些脚本包含最小的代码来简单地进行调用并转发脚本数据作为调用参数。

Last updated