1

...as in one NFT is a parent one and the others are its children. Think of "object" and its "attributes" relation. Or a soldier and weapons, clothes and other things he could possess.

Is this possible in the NFT world? If so, how would I work with it on the programming level? E.g.

  • create/mint a parent NFT
  • establish a possible set of its children/attributes
  • bind together parent and children add/remove those

1 Answer 1

1

Yeah, for sure. NFT can contain many properties and attributes. Even mappings to other NFT's and such.

You can also create functions that allow you to modify/delete those attributes.

However, something like that might be quite expensive to mint on a public chain.

Here is example of SmartContract similar to what you requested:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

contract SoldierItems is ERC1155 {
    uint256 public constant SOLDIER = 0;
    uint256 public constant WEAPON = 1;
    uint256 public constant ARMOR = 2;

    struct SoldierAttributes {
        uint256[] weapons;
        uint256[] armors;
    }

    mapping (uint256 => SoldierAttributes) private _soldierAttributes;

    constructor() public ERC1155("https://game.example/api/item/{id}.json") {
        _mint(msg.sender, SOLDIER, 1, "");
        _mint(msg.sender, WEAPON, 10, "");
        _mint(msg.sender, ARMOR, 5, "");
    }

    // This function can be called to assign a weapon to a soldier
    function assignWeapon(uint256 soldierId, uint256 weaponId) public {
        // Check that the caller owns the soldier and the weapon NFT
        require(balanceOf(msg.sender, SOLDIER) > 0, "Caller does not own the soldier NFT");
        require(balanceOf(msg.sender, WEAPON) >= weaponId, "Caller does not own the weapon NFT");

        // Assign the weapon to the soldier
        _soldierAttributes[soldierId].weapons.push(weaponId);
    }

    // This function can be called to assign an armor to a soldier
    function assignArmor(uint256 soldierId, uint256 armorId) public {
        // Check that the caller owns the soldier and the armor NFT
        require(balanceOf(msg.sender, SOLDIER) > 0, "Caller does not own the soldier NFT");
        require(balanceOf(msg.sender, ARMOR) >= armorId, "Caller does not own the armor NFT");

        // Assign the armor to the soldier
        _soldierAttributes[soldierId].armors.push(armorId);
    }

    // Call to get the weapons of a soldier
    function getWeaponsOfSoldier(uint256 soldierId) public view returns (uint256[] memory) {
        return _soldierAttributes[soldierId].weapons;
    }

    // Call to get the armors of a soldier
    function getArmorsOfSoldier(uint256 soldierId) public view returns (uint256[] memory) {
        return _soldierAttributes[soldierId].armors;
    }
}

NOTE: I used ERC-1155 (NFT standard that allows you to use handle many different NFT's in same contract)

In short: It's surely possible. But do consider the cost of it.

3
  • Ok, how would I do it, in a simple way?
    – Camila326
    May 27, 2023 at 15:13
  • @Camila326 do you need some simple code example? What example would you like? Solder and attributes, weapons, etc?
    – Sky
    May 29, 2023 at 20:17
  • @Camila326 I added code example.
    – Sky
    May 30, 2023 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.