Contract 3: EvolutionNFT - The Living Characters

Purpose: ERC721 NFTs that serve as evolving game characters

Key Features:

  • Dynamic metadata generation based on current traits

  • Integration with TraitFusion for trait persistence

  • Base64 encoded JSON metadata (production should use IPFS)

struct BaseTraits {
    uint256 level;
    uint256 energy;
    uint256 strength;
    uint256 stamina;
    string zone;
    string elementAffinity;
}

What's happening here?

  • Defines the basic stats every NFT character has

  • These are stored locally in the NFT contract

  • But dynamic traits are stored in TraitFusion contract

function mint(address to) external onlyRole(GAME_ENGINE_ROLE) returns (uint256) {
    _tokenIds.increment();
    uint256 tokenId = _tokenIds.current();
    
    _mint(to, tokenId);
    
    // Initialize base traits
    BaseTraits memory base = BaseTraits({
        level: 1,
        energy: 100,
        strength: 10,
        stamina: 50,
        zone: "starter_zone",
        elementAffinity: "neutral"
    });
    
    baseTraits[tokenId] = base;

What's happening here?

  • Creates a new NFT with incremental ID

  • Sets up starting stats (level 1, 100 energy, etc.)

  • Stores these base traits in the NFT contract

// Set initial traits in TraitFusion
traitFusion.setTraitUint(address(this), tokenId, "level", base.level);
traitFusion.setTraitUint(address(this), tokenId, "energy", base.energy);
traitFusion.setTraitUint(address(this), tokenId, "strength", base.strength);

What's happening here?

  • Copies the same data to TraitFusion contract

  • Why? Because TraitFusion traits can be modified, base traits cannot

  • This creates a dynamic system where stats can evolve

function tokenURI(uint256 tokenId) public view override returns (string memory) {
    require(_exists(tokenId), "Token does not exist");
    
    // Get current traits from TraitFusion
    uint256 level = traitFusion.getTraitAsUint(address(this), tokenId, "level");
    uint256 energy = traitFusion.getTraitAsUint(address(this), tokenId, "energy");
    string memory zone = traitFusion.getTraitAsString(address(this), tokenId, "zone");

What's happening here?

  • When someone asks for NFT metadata, it gets current traits from TraitFusion

  • Not the original base traits - the evolved ones!

  • This makes the NFT metadata dynamic (changes as character evolves)

Last updated