Build Reliable: Standards & Best Practices for Sway Developers

The purpose of this repository is to contain standards for the Sway Language which users can import and use.

Standards in this repository may be in various stages of development. Use of draft standards and feedback on the proposed standards is encouraged. To use a draft, search for a standard using the appropriate GitHub label and implement the standard abi into your contract.

If you don't find what you're looking for, feel free to create an issue and propose a new standard!

Note Sway is a language under heavy development therefore the standards may not be the most ergonomic. Over time they should receive updates / improvements in order to demonstrate how Sway can be used in real use cases.

Standards

Native Assets

  • SRC-20; Native Asset Standard defines the implementation of a standard API for Native Assets using the Sway Language.
  • SRC-3; Mint and Burn is used to enable mint and burn functionality for Native Assets.
  • SRC-7; Arbitrary Asset Metadata Standard is used to store metadata for Native Assets.
  • SRC-9; Metadata Keys Standard is used to store standardized metadata keys for Native Assets in combination with the SRC-7 standard.
  • SRC-6; Vault Standard defines the implementation of a standard API for asset vaults developed in Sway.

Access Control

  • SRC-5; Ownership Standard is used to restrict function calls to admin users in contracts.

Bridge

  • SRC-8; Bridged Asset defines the metadata required for an asset bridged to the Fuel Network.
  • SRC-10; Native Bridge Standard defines the standard API for the Native Bridge between the Fuel Chain and the canonical base chain.

Documentation

  • SRC-2; Inline Documentation defines how to document your Sway files.

Using a standard

To import a standard the following should be added to the project's Forc.toml file under [dependencies] with the most recent release:

standard = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.1.0" }

You may then import your desired standard in your Sway Smart Contract as so:

use standard::<standard_abi>;

For example, to import the SRC-20 Native Asset Standard use the following statements in your Forc.toml and Sway Smart Contract file respectively:

src20 = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.3.3" }
use src20::SRC20;

Examples of Standards

Minimal example implementations for every standard can be found in the examples/ folder.

SRC-20; Native Asset Standard Examples

- Single Native Assset

Example of the SRC-20 implementation where a contract contains a single asset with one SubId. This implementation is recommended for users that intend to deploy a single asset with their contract.

- Multi Native Asset

Example of the SRC-20 implementation where a contract contains multiple assets with differing SubIds. This implementation is recommended for users that intend to deploy multiple assets with their contract.

SRC-3; Mint and Burn Standard Examples

- Single Native Asset

Example of the SRC-3 implementation where a contract only mints a single asset with one SubId.

- Multi Native Asset

Example of the SRC-3 implementation where a contract mints multiple assets with differing SubIds.

SRC-5; Ownership Examples

- Uninitalized

Example of the SRC-5 implementation where a contract does not have an owner set at compile time with the intent to set it during runtime.

- Initialized

Example of the SRC-5 implementation where a contract has an owner set at compile time.

SRC-6; Vault Standard Examples

Multi Asset Vault

A basic implementation of the vault standard that supports any number of sub vaults being created for every AssetId.

Single Asset Vault

A basic implementation of the vault standard demonstrating how to restrict deposits and withdrawals to a single AssetId.

Single Asset Single Sub Vault

A basic implementation of the vault standard demonstrating how to restrict deposits and withdrawals to a single AssetId, and to a single Sub vault.

SRC-7; Arbitrary Asset Metadata Standard Examples

- Single Native Asset

Example of the SRC-7 implementation where metadata exists for only a single asset with one SubId.

- Mutli Native Asset

Example of the SRC-7 implementation where metadata exists for multiple assets with differing SubIds.


Download Details:

Author: FuelLabs

Official Github: https://github.com/FuelLabs/sway-standards 

License: Apache-2.0 license

#typescript #rust 

Build Reliable: Standards & Best Practices for Sway Developers
1.55 GEEK