Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link

Icon LinkMetadata Functionality

For implementation details on the Asset Library metadata functionality please see the Sway Libs Docs Icon Link.

Icon LinkImporting the Asset Library Metadata Functionality

In order to use the Asset Library, Sway Libs and Sway Standards Icon Link must be added to the Forc.toml file and then imported into your Sway project. To add Sway Libs as a dependency to the Forc.toml file in your project please see the Getting Started . To add Sway Standards as a dependency please see the Sway Standards Book Icon Link.

To import the Asset Library Base Functionality and SRC-7 Icon Link Standard to your Sway Smart Contract, add the following to your Sway file:

use sway_libs::asset::metadata::{_metadata, _set_metadata, SetAssetMetadata, StorageMetadata};
use standards::src7::*;

Icon LinkIntegration with the SRC-7 Standard

The SRC-7 Icon Link definition states that the following abi implementation is required for any Native Asset on Fuel:

abi SRC7 {
    #[storage(read)]
    fn metadata(asset: AssetId, key: String) -> Option<Metadata>;
}

The Asset Library has the following complimentary data type for the SRC-7 Icon Link standard:

  • StorageMetadata

Icon LinkSetting Up Storage

Once imported, the Asset Library's metadata functionality should be available. To use them, be sure to add the storage block bellow to your contract which enables the SRC-7 Icon Link standard.

storage {
    metadata: StorageMetadata = StorageMetadata {},
}

Icon LinkUsing the StorageMetadata Type

Icon LinkSetting Metadata

To set some metadata for an Asset, use the SetAssetMetadata ABI provided by the Asset Library with the _set_metadata() function. Be sure to follow the SRC-9 Icon Link standard for your key. It is recommended that the Ownership Library is used in conjunction with the SetAssetMetadata ABI to ensure only a single user has permissions to set an Asset's metadata.

The _set_metadata() function follows the SRC-7 standard for logging and will emit the SetMetadataEvent when called.

use sway_libs::asset::metadata::*;
use standards::src7::Metadata;
 
storage {
    metadata: StorageMetadata = StorageMetadata {},
}
 
impl SetAssetMetadata for Contract {
    #[storage(read, write)]
    fn set_metadata(asset: AssetId, key: String, metadata: Metadata) {
        _set_metadata(storage.metadata, asset, key, metadata);
    }
}
Icon InfoCircle

NOTE The _set_metadata() function will set the metadata of an asset unconditionally. External checks should be applied to restrict the setting of metadata.

Icon LinkImplementing the SRC-7 Standard with StorageMetadata

To use the StorageMetadata type, simply get the stored metadata with the associated key and AssetId using the provided _metadata() convenience function. The example below shows the implementation of the SRC-7 Icon Link standard in combination with the Asset Library's StorageMetadata type and the _metadata() function with no user defined restrictions or custom functionality.

use sway_libs::asset::metadata::*;
use standards::src7::{Metadata, SRC7};
 
storage {
    metadata: StorageMetadata = StorageMetadata {},
}
 
// Implement the SRC-7 Standard for this contract
impl SRC7 for Contract {
    #[storage(read)]
    fn metadata(asset: AssetId, key: String) -> Option<Metadata> {
        // Return the stored metadata
        storage.metadata.get(asset, key)
    }
}