Skip to Content

Subscription NFTs on Aptos: User Guide

This guide explains how to create and manage subscription-based NFTs on the Aptos blockchain, designed to regulate access to a cryptocurrency trading bot. Each NFT has precise properties to control user subscriptions efficiently.

Overview


Purpose:

Subscription NFTs act as keys for accessing services, such as a trading bot. They include properties that allow fine-grained control over subscription terms.


Key Features:


  • Support for multiple collections with unique NFTs.
  • Precision-based properties that determine usage limitations, refill rates, and subscription impact. 

NFT Structure


Each NFT includes attributes to control the subscription model with 5-decimal precision (stored as u128).


Properties:

  • Energy: Core property, ranges from 0 to 100.00000.
  • Refill: Rate at which energy can be refilled (0 to 1.00000).
  • Profit, Volume, Time: Usage coefficients:
    • profit: A multiplier for energy consumption based on profit.
    • volume: A multiplier based on trade volume in USDT.
    • time: A multiplier based on time duration.
    • Note: If any of these are zero, they won’t impact the energy calculations.


Contract Functions


Function 1:

mint_nft


Creates NFTs with specific attributes and associates them with a collection.


public fun mint_nft(
    account: &signer,
    collection_name: &str,
    nft_name: &str,
    description: &str,
    energy: u128,
    refill: u128,
    profit: u128,
    volume: u128,
    time: u128
): TokenId

Description:
  • Takes energy, refill, profit, volume, and time as parameters with 5-decimal precision.
  • Creates an NFT with these properties in the specified collection.

Function 2:

use_energy

Applies energy usage based on provided values for profit, volume, and time.

moveCopy codepublic fun use_energy(
    account: &signer,
    nft_id: &TokenId,
    profit_val: u128,
    volume_val: u128,
    time_val: u128
) acquires SubscriptionNft

Description:

  • Uses non-zero coefficients to calculate energy usage.
  • Formula: (input_value * nft_property) / 100_000
  • Ensures energy isn’t deducted if the property value is zero.

Function 3:

refill_energy

Replenishes energy based on the refill rate and the current energy level.

moveCopy codepublic fun refill_energy(
​account: &signer,
​nft_id: &TokenId
) acquires SubscriptionNft

Description:

  • Calculates refill amount based on refill and available energy capacity.
  • Ensures that energy doesn’t exceed 100%.

Implementation Details


  • Data Types: Properties are stored as u128, with precision allowing 5 decimal places.
  • Calculation Conditions:
    • Properties with zero values are skipped in calculations to prevent unwanted deductions.
    • Refill amount is capped to ensure energy remains within allowed limits.

Testing and Validation


This contract includes comprehensive tests to validate functionality and property handling.

Tests:

  1. NFT Creation: Checks that NFT properties are stored and retrieved accurately.
  2. Energy Usage: Confirms that use_energy applies correct deductions based on the provided values.
  3. Energy Refill: Ensures refill limits are respected and energy caps at 100%.

Assertions:

  • Ensures signer access control.
  • Handles errors for invalid refill rates and insufficient energy.

Code Walkthrough


Each function is detailed below:

  1. Minting Function: mint_nft
    • Creates NFT with precise properties.
    • Ensures properties such as energy and refill are set accurately for future usage.
  2. Energy Consumption Function: use_energy
    • Deducts energy based on non-zero usage values.
    • Skips properties with zero values, preventing unintended deductions.
  3. Energy Refill Function: refill_energy
    • Calculates refill based on refill rate.
    • Enforces energy cap at 100% to avoid overflow.


Summary and Future Potential


  • Precision in Subscription Management:
    Fine-grained control over subscriptions with energy-based management tailored to usage patterns.

  • Future Enhancements:
    • Integration with the trading bot to monitor energy in real time.
    • Further customization of properties and usage formulas to support diverse subscription models.