Skip to content

Commit

Permalink
Add helper functions to SRC7 standard's Metadata (#144)
Browse files Browse the repository at this point in the history
* Add helper functions to SRC7 standard

* Update CHANGELOG
  • Loading branch information
bitzoic authored Aug 30, 2024
1 parent d264041 commit 955061b
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 2 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ Description of the upcoming release here.

### Added Unreleased

- Something new here 1
- Something new here 2
- [#144](https://github.com/FuelLabs/sway-standards/pull/144) Adds the following helper functions for `Metadata` to check and return the underlying type: `as_string()`, `is_string()`, `as_bytes()`, `is_bytes()`, `as_b256()`, `is_u64()`, `as_u64()`.

### Changed Unreleased

Expand Down
214 changes: 214 additions & 0 deletions standards/src/src7.sw
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,217 @@ impl core::ops::Eq for Metadata {
}
}
}

impl Metadata {
/// Returns the underlying metadata as a `String`.
///
/// # Returns
///
/// * [Option<String>] - `Some` if the underlying type is a `String`, otherwise `None`.
///
/// # Examples
///
/// ```sway
/// use std::string::String;
/// use standards::src7::{SRC7, Metadata};
///
/// fn foo(contract_id: ContractId, asset: AssetId, key: String) {
/// let metadata_abi = abi(SRC7, contract_id);
/// let metadata: Option<Metadata> = metadata_abi.metadata(asset, key);
///
/// let string = metadata.unwrap().as_string();
/// assert(string.len() != 0);
/// }
/// ```
pub fn as_string(self) -> Option<String> {
match self {
Self::String(data) => Option::Some(data),
_ => Option::None,
}
}

/// Returns whether the underlying metadata is a `String`.
///
/// # Returns
///
/// * [bool] - `true` if the metadata is a `String`, otherwise `false`.
///
/// # Examples
///
/// ```sway
/// use std::string::String;
/// use standards::src7::{SRC7, Metadata};
///
/// fn foo(contract_id: ContractId, asset: AssetId, key: String) {
/// let metadata_abi = abi(SRC7, contract_id);
/// let metadata: Option<Metadata> = metadata_abi.metadata(asset, key);
///
/// assert(metadata.unwrap().is_string());
/// }
/// ```
pub fn is_string(self) -> bool {
match self {
Self::String(_) => true,
_ => false,
}
}

/// Returns the underlying metadata as a `u64`.
///
/// # Returns
///
/// * [Option<u64>] - `Some` if the underlying type is a `u64`, otherwise `None`.
///
/// # Examples
///
/// ```sway
/// use std::string::String;
/// use standards::src7::{SRC7, Metadata};
///
/// fn foo(contract_id: ContractId, asset: AssetId, key: String) {
/// let metadata_abi = abi(SRC7, contract_id);
/// let metadata: Option<Metadata> = metadata_abi.metadata(asset, key);
///
/// let int = metadata.unwrap().as_u64();
/// assert(int != 0);
/// }
/// ```
pub fn as_u64(self) -> Option<u64> {
match self {
Self::Int(data) => Option::Some(data),
_ => Option::None,
}
}

/// Returns whether the underlying metadata is a `u64`.
///
/// # Returns
///
/// * [bool] - `true` if the metadata is a `u64`, otherwise `false`.
///
/// # Examples
///
/// ```sway
/// use std::string::String;
/// use standards::src7::{SRC7, Metadata};
///
/// fn foo(contract_id: ContractId, asset: AssetId, key: String) {
/// let metadata_abi = abi(SRC7, contract_id);
/// let metadata: Option<Metadata> = metadata_abi.metadata(asset, key);
///
/// assert(metadata.unwrap().is_u64());
/// }
/// ```
pub fn is_u64(self) -> bool {
match self {
Self::Int(_) => true,
_ => false,
}
}

/// Returns the underlying metadata as `Bytes`.
///
/// # Returns
///
/// * [Option<Bytes>] - `Some` if the underlying type is `Bytes`, otherwise `None`.
///
/// # Examples
///
/// ```sway
/// use std::{bytes::Bytes, string::String};
/// use standards::src7::{SRC7, Metadata};
///
/// fn foo(contract_id: ContractId, asset: AssetId, key: String) {
/// let metadata_abi = abi(SRC7, contract_id);
/// let metadata: Option<Metadata> = metadata_abi.metadata(asset, key);
///
/// let bytes = metadata.unwrap().as_bytes();
/// assert(bytes.len() != 0);
/// }
/// ```
pub fn as_bytes(self) -> Option<Bytes> {
match self {
Self::Bytes(data) => Option::Some(data),
_ => Option::None,
}
}

/// Returns whether the underlying metadata is `Bytes`.
///
/// # Returns
///
/// * [bool] - `true` if the metadata is `Bytes`, otherwise `false`.
///
/// # Examples
///
/// ```sway
/// use std::{bytes::Bytes, string::String};
/// use standards::src7::{SRC7, Metadata};
///
/// fn foo(contract_id: ContractId, asset: AssetId, key: String) {
/// let metadata_abi = abi(SRC7, contract_id);
/// let metadata: Option<Metadata> = metadata_abi.metadata(asset, key);
///
/// assert(metadata.unwrap().is_bytes());
/// }
/// ```
pub fn is_bytes(self) -> bool {
match self {
Self::Bytes(_) => true,
_ => false,
}
}

/// Returns the underlying metadata as a `b256`.
///
/// # Returns
///
/// * [Option<u64>] - `Some` if the underlying type is a `b256`, otherwise `None`.
///
/// # Examples
///
/// ```sway
/// use std::string::String;
/// use standards::src7::{SRC7, Metadata};
///
/// fn foo(contract_id: ContractId, asset: AssetId, key: String) {
/// let metadata_abi = abi(SRC7, contract_id);
/// let metadata: Option<Metadata> = metadata_abi.metadata(asset, key);
///
/// let val = metadata.unwrap().as_b256();
/// assert(val != b256::zero());
/// }
/// ```
pub fn as_b256(self) -> Option<b256> {
match self {
Self::B256(data) => Option::Some(data),
_ => Option::None,
}
}

/// Returns whether the underlying metadata is a `b256`.
///
/// # Returns
///
/// * [bool] - `true` if the metadata is a `b256`, otherwise `false`.
///
/// # Examples
///
/// ```sway
/// use std::string::String;
/// use standards::src7::{SRC7, Metadata};
///
/// fn foo(contract_id: ContractId, asset: AssetId, key: String) {
/// let metadata_abi = abi(SRC7, contract_id);
/// let metadata: Option<Metadata> = metadata_abi.metadata(asset, key);
///
/// assert(metadata.unwrap().is_b256());
/// }
/// ```
pub fn is_b256(self) -> bool {
match self {
Self::B256(_) => true,
_ => false,
}
}
}

0 comments on commit 955061b

Please sign in to comment.