Asset creation and minting
Focus: one reward-token contract that creates a mintable asset once, then mints supply into the current contract balance.
reward_asset.slx
const ASSET_HASH_KEY: string = "reward_asset"
entry create_reward_asset(id: u64, max_supply: u64) -> Hash {
let storage: Storage = Storage::new()
require(!storage.has(ASSET_HASH_KEY), "asset already created")
let asset: Asset = Asset::create(
id,
"Example Reward",
"XREWARD",
8,
MaxSupplyMode::Mintable { max_supply }
).expect("asset creation failed")
let hash: Hash = asset.get_hash()
storage.store(ASSET_HASH_KEY, hash)
return hash
}
entry mint_to_contract(amount: u64) -> u64 {
let storage: Storage = Storage::new()
let hash: Hash = storage.load(ASSET_HASH_KEY).expect("create asset first")
let asset: Asset = Asset::get_by_hash(hash).expect("asset not found")
require(asset.mint(amount), "mint failed")
return get_balance_for_asset(hash).unwrap_or(0)
}How it works
ASSET_HASH_KEYstores the created asset hash so the contract mints the same asset later.create_reward_asset(id, max_supply)rejects duplicate creation, callsAsset::create, stores the hash, and returns it.MaxSupplyMode::Mintable { max_supply }makes the asset mintable up to a capped supply.mint_to_contract(amount)loads the stored hash, gets theAsset, and callsasset.mint(amount).- If minting succeeds, the new units are credited to the balance of the contract that called
mint. - From that contract balance, another entry can call
transfer(...)if the contract should distribute minted units to a user or another destination. - Asset creation has a protocol cost, so the contract needs enough XELIS balance before
Asset::create.
Last updated on