Skip to content

Commit

Permalink
Merge pull request #33 from FuelLabs/bitzoic-src5-example
Browse files Browse the repository at this point in the history
Add SRC-5 Basic examples
  • Loading branch information
bitzoic authored Nov 6, 2023
2 parents 3befaf6 + 851bcc6 commit e228918
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
7 changes: 6 additions & 1 deletion examples/Forc.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
[workspace]
members = ["src_20/single_asset", "src_20/multi_asset"]
members = [
"src_5/initialized_example",
"src_5/uninitialized_example",
"src_20/single_asset",
"src_20/multi_asset",
]
8 changes: 8 additions & 0 deletions examples/src_5/initialized_example/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "initialized_example.sw"
license = "Apache-2.0"
name = "initialized_src5_example"

[dependencies]
src_5 = { path = "../../../standards/src_5" }
47 changes: 47 additions & 0 deletions examples/src_5/initialized_example/src/initialized_example.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
contract;

use src_5::{Ownership, SRC_5, State};
use std::constants::ZERO_B256;

configurable {
/// The owner of this contract at deployment.
INITIAL_OWNER: Identity = Identity::Address(Address::from(ZERO_B256)),
}

storage {
/// The owner in storage.
owner: Ownership = Ownership {
state: State::Initialized(INITIAL_OWNER),
},
}

impl SRC_5 for Contract {
/// Returns the owner.
///
/// # Return Values
///
/// * [State] - Represents the state of ownership for this contract.
///
/// # Number of Storage Accesses
///
/// * Reads: `1`
///
/// # Examples
///
/// ```sway
/// use src_5::SRC_5;
///
/// fn foo(contract_id: ContractId) {
/// let ownership_abi = abi(contract_id, SRC_5);
///
/// match ownership_abi.owner() {
/// State::Initialized(owner) => log("The ownership is initalized"),
/// _ => log("This example will never reach this statement"),
/// }
/// }
/// ```
#[storage(read)]
fn owner() -> State {
storage.owner.read().state
}
}
8 changes: 8 additions & 0 deletions examples/src_5/uninitialized_example/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "uninitialized_example.sw"
license = "Apache-2.0"
name = "uninitialized_src5_example"

[dependencies]
src_5 = { path = "../../../standards/src_5" }
41 changes: 41 additions & 0 deletions examples/src_5/uninitialized_example/src/uninitialized_example.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
contract;

use src_5::{Ownership, SRC_5, State};

storage {
/// The owner in storage.
owner: Ownership = Ownership {
state: State::Uninitialized,
},
}

impl SRC_5 for Contract {
/// Returns the owner.
///
/// # Return Values
///
/// * [State] - Represents the state of ownership for this contract.
///
/// # Number of Storage Accesses
///
/// * Reads: `1`
///
/// # Examples
///
/// ```sway
/// use src_5::SRC_5;
///
/// fn foo(contract_id: ContractId) {
/// let ownership_abi = abi(contract_id, SRC_5);
///
/// match ownership_abi.owner() {
/// State::Uninitalized => log("The ownership is uninitalized"),
/// _ => log("This example will never reach this statement"),
/// }
/// }
/// ```
#[storage(read)]
fn owner() -> State {
storage.owner.read().state
}
}

0 comments on commit e228918

Please sign in to comment.