Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference Sway-Libs, Sway-Standards, and Sway-Applications In Sway Book #5944

Merged
merged 11 commits into from
May 8, 2024
14 changes: 13 additions & 1 deletion docs/book/spell-check-custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,20 @@ unary
SRC
DEX
SubId
transpiler
Pausable
Libs
Reentrancy
reentrancy
mathematic
Soulbound
NFTs
NFT
dApps
fungible
TicTacToe
DAO
Timelock
transpiler
namespacing
unsafety
prioritizations
3 changes: 3 additions & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
- [The Fuel Toolchain](./introduction/fuel_toolchain.md)
- [A Forc Project](./introduction/forc_project.md)
- [Standard Library](./introduction/standard_library.md)
- [Sway Language Standards](./introduction/sway_standards.md)
- [Examples](./examples/index.md)
- [Counter](./examples/counter.md)
- [`FizzBuzz`](./examples/fizzbuzz.md)
- [Wallet Smart Contract](./examples/wallet_smart_contract.md)
- [Liquidity Pool](./examples/liquidity_pool.md)
- [Sway Applications](./examples/sway_applications.md)
- [Program Types](./sway-program-types/index.md)
- [Contracts](./sway-program-types/smart_contracts.md)
- [Libraries](./sway-program-types/libraries.md)
Expand Down Expand Up @@ -63,6 +65,7 @@
- [Features](./lsp/features.md)
- [Troubleshooting](./lsp/troubleshooting.md)
- [Sway Reference](./reference/index.md)
- [Sway Libraries](./reference/sway_libs.md)
- [Compiler Intrinsics](./reference/compiler_intrinsics.md)
- [Attributes](./reference/attributes.md)
- [Style Guide](./reference/style_guide.md)
Expand Down
11 changes: 10 additions & 1 deletion docs/book/src/blockchain-development/access_control.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The `msg_sender` function works as follows:

Many contracts require some form of ownership for access control. The [SRC-5 Ownership Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-5.md) has been defined to provide an interoperable interface for ownership within contracts.

To accomplish this, use the [Ownership Library](https://github.com/FuelLabs/sway-libs/tree/master/libs/src/ownership) to keep track of the owner. This allows setting and revoking ownership using the variants `Some(..)` and `None` respectively. This is better, safer, and more readable than using the `Identity` type directly where revoking ownership has to be done using some magic value such as `std::constants::ZERO_B256` or otherwise.
To accomplish this, use the [Ownership Library](https://fuellabs.github.io/sway-libs/book/ownership/index.html) to keep track of the owner. This allows setting and revoking ownership using the variants `Some(..)` and `None` respectively. This is better, safer, and more readable than using the `Identity` type directly where revoking ownership has to be done using some magic value such as `std::constants::ZERO_B256` or otherwise.

- The following is an example of how to properly lock a function such that only the owner may call a function:

Expand Down Expand Up @@ -62,3 +62,12 @@ Setting ownership can be done in one of two ways; During compile time or run tim
```sway
{{#include ../../../../examples/ownership/src/main.sw:get_owner_example}}
```

## Access Control Libraries

[Sway-Libs](../reference/sway_libs.md) provides the following libraries to enable further access control.

- [Ownership Library](https://fuellabs.github.io/sway-libs/book/ownership/index.html); used to apply restrictions on functions such that only a **single** user may call them. This library provides helper functions for the [SRC-5; Ownership Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-5.md).
- [Admin Library](https://fuellabs.github.io/sway-libs/book/admin/index.html); used to apply restrictions on functions such that only a select few users may call them like a whitelist.
- [Pausable Library](https://fuellabs.github.io/sway-libs/book/pausable/index.html); allows contracts to implement an emergency stop mechanism.
- [Reentrancy Guard Library](https://fuellabs.github.io/sway-libs/book/reentrancy/index.html); used to detect and prevent reentrancy attacks.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn main() {

A common attack vector for smart contracts is [re-entrancy](https://docs.soliditylang.org/en/v0.8.4/security-considerations.html#re-entrancy). Similar to the EVM, the FuelVM allows for re-entrancy.

A _stateless_ re-entrancy guard is included in the [`sway-libs`](https://github.com/FuelLabs/sway-libs) library. The guard will panic (revert) at run time if re-entrancy is detected.
A _stateless_ re-entrancy guard is included in the [`sway-libs`](https://fuellabs.github.io/sway-libs/book/reentrancy/index.html) library. The guard will panic (revert) at run time if re-entrancy is detected.

```sway
contract;
Expand Down
25 changes: 23 additions & 2 deletions docs/book/src/blockchain-development/native_assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ On the FuelVM, _all_ assets are native and the process for sending _any_ native

While you would still need a smart contract to handle the minting and burning of assets, the sending and receiving of these assets can be done independently of the asset contract.

Just like the EVM however, Fuel has a standard that describes a standard API for Native Assets using the Sway Language. The ERC-20 equivalent for the Sway Language is the [SRC-20; Native Asset Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-20.md).

> **NOTE** It is important to note that Fuel does not have tokens.

### ERC-721 vs Native Asset

On the EVM, an ERC-721 token or NFT is a contract that contains multiple tokens which are non-fungible with one another.

On the FuelVM, the ERC-721 equivalent is a Native Asset where each asset has a supply of one. This is defined in the [SRC-20; Native Asset Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-20.md#non-fungible-asset-restrictions) under the Non-Fungible Asset Restrictions.

In practice, this means all NFTs are treated the same as any other Native Asset on Fuel. When writing Sway code, no additional cases for handling non-fungible and fungible assets are required.

### No Token Approvals

An advantage Native Assets bring is that there is no need for token approvals; as with Ether on the EVM. With millions of dollars hacked every year due to misused token approvals, the FuelVM eliminates this attack vector.
Expand Down Expand Up @@ -96,6 +106,8 @@ You may also mint an asset to a specific entity with the `std::asset::mint_to()`
{{#include ../../../../examples/native_asset/src/main.sw:mint_to_asset}}
```

If you intend to allow external users to mint assets using your contract, the [SRC-3; Mint and Burn Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-3.md#fn-mintrecipient-identity-vault_sub_id-subid-amount-u64) defines a standard API for minting assets. The [Sway-Libs Asset Library](https://fuellabs.github.io/sway-libs/book/asset/supply.html) also provides an additional library to support implementations of the SRC-3 Standard into your contract.

### Burning a Native Asset

To burn an asset, the `std::asset::burn()` function must be called internally from the contract which minted them. The `SubId` used to mint the coins and amount must be provided. The burned coins must be owned by the contract. When an asset is burned it doesn't exist anymore.
Expand All @@ -104,6 +116,8 @@ To burn an asset, the `std::asset::burn()` function must be called internally fr
{{#include ../../../../examples/native_asset/src/main.sw:burn_asset}}
```

If you intend to allow external users to burn assets using your contract, the [SRC-3; Mint and Burn Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-3.md#fn-mintrecipient-identity-vault_sub_id-subid-amount-u64) defines a standard API for burning assets. The [Sway-Libs Asset Library](https://fuellabs.github.io/sway-libs/book/asset/supply.html) also provides an additional library to support implementations of the SRC-3 Standard into your contract.

### Transfer a Native Asset

To internally transfer a Native Asset, the `std::asset::transfer()` function must be called. A target `Identity` or user must be provided as well as the `AssetId` of the asset and an amount.
Expand Down Expand Up @@ -166,13 +180,20 @@ We currently have the following standards for Native Assets:
- [SRC-3; Mint and Burn Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-3.md) is used to enable mint and burn functionality for Native Assets.
- [SRC-7; Arbitrary Asset Metadata Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-7.md) is used to store metadata for Native Assets.
- [SRC-6; Vault Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-6.md) defines the implementation of a standard API for asset vaults developed in Sway.

## Native Asset Libraries

Additional Libraries have been developed to allow you to quickly create an deploy dApps that follow the [Sway Standards](https://github.com/FuelLabs/sway-standards).

- [Asset Library](https://fuellabs.github.io/sway-libs/book/asset/index.html) provides functionality to implement the [SRC-20; Native Asset Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-20.md), [SRC-3; Mint and Burn Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-3.md), and [SRC-7; Arbitrary Asset Metadata Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-7.md) standards.

<!-- native_assets:example:end -->

## Single Native Asset Example

In this fully fleshed out example, we show a native asset contract which mints a single asset. This is the equivalent to the ERC-20 Standard use in Ethereum. Note there are no token approval functions.

It implements the [SRC-20; Native Asset](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-20.md), [SRC-3; Mint and Burn](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-3.md), and [SRC-5; Ownership](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-5.md) standards.
It implements the [SRC-20; Native Asset](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-20.md), [SRC-3; Mint and Burn](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-3.md), and [SRC-5; Ownership](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-5.md) standards. It does not use any external libraries.

```sway
// ERC20 equivalent in Sway.
Expand Down Expand Up @@ -314,7 +335,7 @@ fn require_access_owner() {

In this fully fleshed out example, we show a native asset contract which mints multiple assets. This is the equivalent to the ERC-1155 Standard use in Ethereum. Note there are no token approval functions.

It implements the [SRC-20; Native Asset](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-20.md), [SRC-3; Mint and Burn](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-3.md), and [SRC-5; Ownership](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-5.md) standards.
It implements the [SRC-20; Native Asset](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-20.md), [SRC-3; Mint and Burn](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-3.md), and [SRC-5; Ownership](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-5.md) standards. It does not use any external libraries.

```sway
// ERC1155 equivalent in Sway.
Expand Down
1 change: 1 addition & 0 deletions docs/book/src/examples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ Some basic example contracts to see how Sway and Forc work.
- [Counter](./counter.md)
- [`FizzBuzz`](./fizzbuzz.md)
- [Wallet Smart Contract](./wallet_smart_contract.md)
- [Liquidity Pool](./wallet_smart_contract.md)

Additional examples can be found in the [Sway Applications](https://github.com/FuelLabs/sway-applications/tree/master) repository.
33 changes: 33 additions & 0 deletions docs/book/src/examples/sway_applications.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Sway Applications

The [Sway-Applications](https://github.com/FuelLabs/sway-applications) Repository contains end-to-end example applications that are written in Sway in order to demonstrate what can be built.

## Asset Management

- [Airdrop](https://github.com/FuelLabs/sway-applications/tree/master/airdrop) is an asset distribution program where users are able to claim assets given a valid merkle proof.
- [Escrow](https://github.com/FuelLabs/sway-applications/tree/master/escrow) is a third party that keeps an asset on behalf of multiple parties.
- [Non-Fungible Native Asset (NFT)](https://github.com/FuelLabs/sway-applications/tree/master/NFT) is an asset contract which provides unique collectibles, identified and differentiated by IDs, where assets contain metadata giving them distinctive characteristics.
- [Fractional Non-Fungible Token (F-NFT)](https://github.com/FuelLabs/sway-applications/tree/master/fractional-NFT) is a token contract which issues shares or partial ownership upon locking an NFT into a vault.
- [Timelock](https://github.com/FuelLabs/sway-applications/tree/master/timelock) is a contract which restricts the execution of a transaction to a specified time range.
- [Native Asset](https://github.com/FuelLabs/sway-applications/tree/master/native-asset) is a basic asset contract that enables the use of Native Assets on Fuel using existing standards and libraries.

## Decentralized Finance

- [English Auction](https://github.com/FuelLabs/sway-applications/tree/master/english-auction) is an auction where users bid up the price of an asset until the bidding period has ended or a reserve has been met.
- [Fundraiser](https://github.com/FuelLabs/sway-applications/tree/master/fundraiser) is a program allowing users to pledge towards a goal.
- [OTC Swap Predicate](https://github.com/FuelLabs/sway-applications/tree/master/OTC-swap-predicate) is a predicate that can be used to propose and execute an atomic swap between two parties without requiring any on-chain state.

## Governance

- [Decentralized Autonomous Organization (DAO)](https://github.com/FuelLabs/sway-applications/tree/master/DAO) is an organization where users get to vote on governance proposals using governance assets.
- [Multi-Signature Wallet](https://github.com/FuelLabs/sway-applications/tree/master/multisig-wallet) is a wallet that requires multiple signatures to execute a transaction.

## Games

- [TicTacToe](https://github.com/FuelLabs/sway-applications/tree/master/TicTacToe) is a game where two players compete to align three markers in a row.

## Other

- [Counter-Script](https://github.com/FuelLabs/sway-applications/tree/master/counter-script) is a script that calls a contract to increment a counter.
- [Name-Registry](https://github.com/FuelLabs/sway-applications/tree/master/name-registry) allows users to perform transactions with human readable names instead of addresses.
- [Oracle](https://github.com/FuelLabs/sway-applications/tree/master/oracle) is a smart contract that provides off-chain data to on-chain applications.
1 change: 1 addition & 0 deletions docs/book/src/introduction/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ To get started with Forc and Sway smart contract development, install the Fuel t
- [The Fuel Toolchain](./fuel_toolchain.md)
- [A Forc Project](./forc_project.md)
- [Standard Library](./standard_library.md)
- [Sway Language Standards](./sway_standards.md)
40 changes: 40 additions & 0 deletions docs/book/src/introduction/sway_standards.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Sway Standards

Just like many other smart contract languages, usage standards have been developed to enable cross compatibility between smart contracts.

For more information on using a Sway Standard, please refer to the [Sway-Standards Repository](https://github.com/FuelLabs/sway-standards).

## Standards

### Native Asset Standards

- [SRC-20; Native Asset Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-20.md) defines the implementation of a standard API for [Native Assets](../blockchain-development/native_assets.md) using the Sway Language.
- [SRC-3; Mint and Burn](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-3.md) is used to enable mint and burn functionality for Native Assets.
- [SRC-7; Arbitrary Asset Metadata Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-7.md) is used to store metadata for [Native Assets](../blockchain-development/native_assets.md), usually as NFTs.
- [SRC-9; Metadata Keys Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-9.md) is used to store standardized metadata keys for [Native Assets](../blockchain-development/native_assets.md) in combination with the SRC-7 standard.
- [SRC-6; Vault Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-6.md) defines the implementation of a standard API for asset vaults developed in Sway.

### Predicate Standards

- [SRC-13; Soulbound Address Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-13.md) defines a specific `Address` as a Soulbound Address for Soulbound Assets to become non-transferable.

### Access Control Standards

- [SRC-5; Ownership Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-5.md) is used to restrict function calls to admin users in contracts.

### Contract Standards

- [SRC-12; Contract Factory](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-12.md) defines the implementation of a standard API for contract factories.

### Bridge Standards

- [SRC-8; Bridged Asset](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-8.md) defines the metadata required for an asset bridged to the Fuel Network.
- [SRC-10; Native Bridge Standard](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-10.md) defines the standard API for the Native Bridge between the Fuel Chain and the canonical base chain.

### Documentation Standards

- [SRC-2; Inline Documentation](https://github.com/FuelLabs/sway-standards/blob/master/SRCs/src-2.md) defines how to document your Sway files.

## Standards Support

Libraries have also been developed to support Sway Standards. These can be in [Sway-Libs](../reference/sway_libs.md).
Loading
Loading