Icon Link基本功能

有关资产库基本功能的实现细节,请参阅Sway Libs 文档 Icon Link

Icon Link导入资产库基本功能

要使用资产库,必须将 Sway Libs 和Sway 标准 Icon Link添加到Forc.toml文件中,然后导入到您的 Sway 项目中。要将 Sway Libs 作为项目的依赖项添加到Forc.toml文件中,请参阅入门指南 。要将 Sway 标准添加为依赖项,请参阅Sway 标准书 Icon Link

要将资产库基本功能和SRC-20 Icon Link标准导入到您的 Sway 智能合约中,请将以下内容添加到您的 Sway 文件中:

use sway_libs::asset::base::*;
use standards::src20::*;

Icon Link与 SRC-20 标准的集成

SR-20 Icon Link定义了 Fuel 上的任何原生资产所需的 ABI 实现如下:

abi SRC20 {
    #[storage(read)]
    fn total_assets() -> u64;
    #[storage(read)]
    fn total_supply(asset: AssetId) -> Option<u64>;
    #[storage(read)]
    fn name(asset: AssetId) -> Option<String>;
    #[storage(read)]
    fn symbol(asset: AssetId) -> Option<String>;
    #[storage(read)]
    fn decimals(asset: AssetId) -> Option<u8>;
}

资产库为SRC20 abi 中的每个函数提供了以下补充功能:

  • _total_assets()
  • _total_supply()
  • _name()
  • _symbol()
  • _decimals()

还提供了以下 ABI 和函数来设置您的SRC-20 Icon Link标准存储值:

abi SetAssetAttributes {
    #[storage(write)]
    fn set_name(asset: AssetId, name: String);
    #[storage(write)]
    fn set_symbol(asset: AssetId, symbol: String);
    #[storage(write)]
    fn set_decimals(asset: AssetId, decimals: u8);
}
  • _set_name()
  • _set_symbol()
  • _set_decimals()
Icon InfoCircle

注意 _set_name()_set_symbol()_set_decimals() 函数将无条件设置资产的属性。应用外部检查以限制属性的设置。

Icon Link设置存储

导入后,应该可以使用资产库的基本功能。为了使用它们,请确保将下面的存储块添加到您的合约中,以启用SRC-20 Icon Link标准。

storage {
    total_assets: u64 = 0,
    total_supply: StorageMap<AssetId, u64> = StorageMap {},
    name: StorageMap<AssetId, StorageString> = StorageMap {},
    symbol: StorageMap<AssetId, StorageString> = StorageMap {},
    decimals: StorageMap<AssetId, u8> = StorageMap {},
}

Icon Link使用资产库实现 SRC-20 标准

要使用资产库的基本功能,只需传递预定义存储块中的 StorageKey。下面的示例显示了在没有用户定义的限制或自定义功能的情况下,与资产库结合使用的SRC-20 Icon Link标准的实现。

use sway_libs::asset::base::{_decimals, _name, _symbol, _total_assets, _total_supply};
use standards::src20::SRC20;
use std::{hash::Hash, storage::storage_string::*, string::String};
 
// The SRC-20 storage block
storage {
    total_assets: u64 = 0,
    total_supply: StorageMap<AssetId, u64> = StorageMap {},
    name: StorageMap<AssetId, StorageString> = StorageMap {},
    symbol: StorageMap<AssetId, StorageString> = StorageMap {},
    decimals: StorageMap<AssetId, u8> = StorageMap {},
}
 
// Implement the SRC-20 Standard for this contract
impl SRC20 for Contract {
    #[storage(read)]
    fn total_assets() -> u64 {
        // Pass the `total_assets` StorageKey to `_total_assets()` from the Asset Library.
        _total_assets(storage.total_assets)
    }
 
    #[storage(read)]
    fn total_supply(asset: AssetId) -> Option<u64> {
        // Pass the `total_supply` StorageKey to `_total_supply()` from the Asset Library.
        _total_supply(storage.total_supply, asset)
    }
 
    #[storage(read)]
    fn name(asset: AssetId) -> Option<String> {
        // Pass the `name` StorageKey to `_name_()` from the Asset Library.
        _name(storage.name, asset)
    }
 
    #[storage(read)]
    fn symbol(asset: AssetId) -> Option<String> {
        // Pass the `symbol` StorageKey to `_symbol_()` function from the Asset Library.
        _symbol(storage.symbol, asset)
    }
 
    #[storage(read)]
    fn decimals(asset: AssetId) -> Option<u8> {
        // Pass the `decimals` StorageKey to `_decimals_()` function from the Asset Library.
        _decimals(storage.decimals, asset)
    }
}

Icon Link设置资产的 SRC-20 属性

要为资产设置一些属性,请使用资产库提供的 SetAssetAttributes ABI。下面的示例显示了在没有用户定义的限制或自定义功能的情况下,使用 SetAssetAttributes ABI 的实现。建议结合使用Ownership 库 SetAssetAttributes ABI,以确保只有一个用户有权限设置资产的属性。

use sway_libs::asset::base::*;
use std::{hash::Hash, storage::storage_string::*, string::String};
 
storage {
    name: StorageMap<AssetId, StorageString> = StorageMap {},
    symbol: StorageMap<AssetId, StorageString> = StorageMap {},
    decimals: StorageMap<AssetId, u8> = StorageMap {},
}
 
impl SetAssetAttributes for Contract {
    #[storage(write)]
    fn set_name(asset: AssetId, name: String) {
        _set_name(storage.name, asset, name);
    }
 
    #[storage(write)]
    fn set_symbol(asset: AssetId, symbol: String) {
        _set_symbol(storage.symbol, asset, symbol);
    }
 
    #[storage(write)]
    fn set_decimals(asset: AssetId, decimals: u8) {
        _set_decimals(storage.decimals, asset, decimals);
    }
}
Icon InfoCircle

注意 _set_name()_set_symbol()_set_decimals() 函数将无条件设置资产的属性。应用外部检查以限制属性的设置。