Non-Fungible Enumerable
Enumerable extension for Non-Fungible Token allows for enumeration of all the token IDs in the contract as well as all the token IDs owned by each account. This is useful for applications that need to list or iterate over tokens, such as marketplaces or wallets.
Usage
We’ll build on the example from Non-Fungible Token
and modify the contract so that all tokens an address own can be listed. Please note any account
can call award_item
and we might want to implement access control to restrict who can mint.
use soroban_sdk::{contract, contractimpl, Address, Env, String};
use stellar_default_impl_macro::default_impl;
use stellar_non_fungible::{
enumerable::{Enumerable, NonFungibleEnumerable},
Balance, Base, ContractOverrides, NonFungibleToken, TokenId,
};
#[contract]
pub struct GameItem;
#[contractimpl]
impl GameItem {
pub fn __constructor(e: &Env) {
Base::set_metadata(
e,
String::from_str(e, "www.mygame.com"),
String::from_str(e, "My Game Items Collection"),
String::from_str(e, "MGMC"),
);
}
pub fn award_item(e: &Env, to: Address) -> TokenId {
// access control might be needed
Enumerable::sequential_mint(e, &to)
}
pub fn burn(e: &Env, from: Address, token_id: TokenId) {
Enumerable::sequential_burn(e, &from, token_id);
}
}
#[default_impl]
#[contractimpl]
impl NonFungibleToken for GameItem {
type ContractType = Enumerable;
}
#[default_impl]
#[contractimpl]
impl NonFungibleEnumerable for GameItem {}
The extension exposes additionally the following entry-point functions, automatically implemented by #[default_impl]
:
fn total_supply(e: &Env) -> Balance;
fn get_owner_token_id(e: &Env, owner: Address, index: TokenId) -> TokenId;
fn get_token_id(e: &Env, index: TokenId) -> TokenId;