Fuelnetwork Sway: FuelVM Program Types and Storage Choices
Fuelnetwork Sway supports contracts for persistent application state, scripts for transaction logic and predicates for programmable spending conditions. Libraries provide reusable code for those programs. Choosing a type starts with the work that must persist and the calls that the program must make through FuelVM.
Program Choice Before the First Build
Program selection begins with whether the code must retain state, coordinate calls, validate spending or supply reusable definitions. Each source file opens with
contract;
,
script;
,
predicate;
or
library;
to declare its type. A successful
forc build
confirms that the selected toolchain accepts the program; behavior tests still need to exercise its actual requirements.
| Program Parameter | Supported Value | Execution Limit |
|---|---|---|
| Contract storage | Persistent storage | Contract execution uses declared storage access. |
| Script storage | No persistent storage | Called contracts can retain changes. |
| Predicate return type |
bool
|
The matching predicate must return
true
to permit spending.
|
| Predicate contract calls | Unavailable | Spending conditions cannot consult contract storage. |
| Script entry point |
main()
|
Runs as transaction logic without owning persistent state. |
| Library deployment | No independent deployment | Imported definitions do not create a standalone contract. |
| Program type sets the execution boundaries for persistent state, transaction logic and spending validation. | ||
When Does Sway Need Persistent Contract Storage?
A Sway contract fits an application that must retain values between transactions and expose functions that other code can call. Contracts provide persistent storage in FuelVM. Temporary variables disappear after execution; contract storage keeps the values that later calls need. Sway combines familiar Rust-style syntax with blockchain concepts such as contract storage. Its compile-time analysis checks language rules, while application logic determines which changes the contract permits.
The contract's application binary interface, or ABI, declares its callable methods and their parameter and return types. An implementation supplies each method's behavior. A storage block declares stored fields with their types and initial values. Contracts can also call other contracts, so their methods may participate in a larger transaction.
Direct storage reads and writes require the appropriate storage annotations. Those annotations belong beside the affected functions, including ABI declarations when their methods access storage.
How Do Scripts Coordinate Contract Calls?
A Sway script coordinates calls during a transaction without owning persistent storage, so it fits logic that combines contract operations and reacts to their returned values. Its
main()
function supplies the entry point. The first contract call occurs through the script; the called contract can then call other contracts. This arrangement supports multiple calls within a transaction without requiring the script itself to become a deployed, stateful service.
A script can invoke a method that updates contract storage, even though the script has none of its own. Fuel's declared inputs and outputs also help identify transaction dependencies, which determine which transactions can execute in parallel.
Can Predicates Read or Change Contract Storage?
A Sway predicate cannot read or change contract storage; it decides whether a transaction satisfies the spending conditions for coins that its address owns. FuelVM predicates cannot call smart contracts. Their code can inspect transaction information, including inputs and outputs. That allows a condition to depend on the proposed movement of native assets without keeping a separate, persistent contract record.
Spending predicate-owned coins requires predicate bytecode whose root matches the coins' owner and an evaluation that returns
true
. Fuel verifies predicates before executing the transaction script. A false result invalidates that transaction. The chosen outputs can therefore form part of its authorization conditions.
The predicate's conditions must restrict the spending that it authorizes. A predicate that always returns
true
permits anyone satisfying the remaining transaction rules to spend its coins.
Library Reuse Without a Separate Deployment
Reusable definitions belong in Sway libraries, which supply common code without an independent on-chain deployment. Internal libraries live within a project's source tree. External libraries require a dependency entry in
Forc.toml
before their public items can be imported. The standard library,
std
, is implicitly available to Forc projects. A library organizes shared implementation or type definitions; it does not create a separately callable service. Library organization and program type answer different questions: where code lives and what it does during execution.
Network Toolchains and Behavior Tests
Toolchain selection must match the target network before build or test results can inform a deployment decision. Fuelup manages compatible component bundles, while Forc creates, builds and tests Sway projects.
Fuelup's
latest
channel is an alias for
mainnet
.
It does not select the newest upstream compiler or the testnet channel. Mainnet documentation also pins component versions, which can lag upstream releases.
The explicit channel names
mainnet
and
testnet
make that distinction clear in project setup.
fuelup show
reports the active toolchain and its component versions. That output describes the environment performing the build. It does not demonstrate compatibility with another network, and
nightly
supplies development tooling without a network compatibility promise.
Behavior tests should follow the chosen program's responsibility. Contract tests need assertions about returned values and stored changes. Script tests need to cover the calls and results that their orchestration depends on. Predicate tests need both permitted and rejected transaction conditions.
forc test
runs Sway unit tests; Rust and TypeScript SDKs support tests that interact with Sway programs. A passing test establishes the result of its assertions in its test environment.
The counter contract's
increment()
method changes its stored count, while
count()
reads the value.
Fuelnetwork Sway FAQs
Can a Sway Contract Call a Script?
A Sway contract cannot call a script. Scripts provide transaction entry logic and can call contracts; contracts can then call other contracts. Code that needs a reusable callable interface belongs in a contract, with shared definitions in a library where appropriate.
Does a Sway Predicate Need a Deployment Transaction?
A Sway predicate does not require a contract-style deployment transaction. Coins can name the predicate's bytecode root as their owner. The spending transaction supplies the predicate code for evaluation, so the address can own coins even though the predicate code runs only during a transaction.
Are a Contract and a Predicate Allowed in the Same Sway Project?
A Sway project can contain only one contract, script or predicate, alongside its libraries. An application that needs multiple executable program types therefore uses separate projects for those programs. Shared library code can still support the application without merging its distinct entry points.
Where Should an ABI Shared by a Sway Contract and Script Live?
A shared ABI can live in a Sway library that both programs import. The contract implements the interface, while the script uses it to describe contract calls. This keeps the callable method definitions reusable without turning the library into a deployed contract.
Is a Sway Storage Annotation a Caller Permission Check?
A storage annotation declares the storage access that a function needs; it does not identify an authorized caller. Permission rules belong in the implementation, using caller checks or other conditions appropriate to that contract. Marking a method for storage access does not make that method private.
How Can a Sway Unit Test Expect a Revert?
A test marked #[test(should_revert)] passes when its execution reverts. Sway also supports specifying an expected revert code. That narrower expectation helps distinguish the intended rejection from another failure that merely caused a revert. The forc test --reverts option displays revert information for these passing tests.
Why Does Forc Test --Locked Reject a Project?
The --locked option requires an existing, up-to-date Forc.lock file. Forc exits with an error if the file is missing or needs an update. This flag enforces the recorded dependency resolution; it does not select the target network or replace toolchain selection.
Must Every Sway Contract Call Use a Handwritten Script?
The Rust and TypeScript SDKs can construct the script that wraps a contract call. Their interfaces let application code invoke contract methods without manually writing that transaction script. The underlying execution still uses a script, and custom orchestration can require its own script logic.