0

suppose i have:

struct Features {
        uint256 _level;
        uint256 _strength;
    }

mapping(uint256 => Features) public tokenIdToFeatures;

Now i wanna set both feature from 2 different functions, like this:

 function getLevels(uint256 _tokenId) public returns (string memory) {
        level++;
       
        tokenToFeatures[_tokenId] = Features({_level: level})
        return level.toString();
    }
    
    function getStrength(uint256 _tokenId) public returns(string memory){
        strength++;
        tokenIdToFeatures[_tokenId] = Features({_strength: strength});
    }

Is it right approach? if not then what is the right approach? N.B: As i creating a NFT so i will have to set this 2 features in 2 places, like this:

'<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 350 350">',
            '<style>.base { fill: white; font-family: serif; font-size: 14px; }</style>',
            '<rect width="100%" height="100%" fill="black" />',
            '<text x="50%" y="40%" class="base" dominant-baseline="middle" text-anchor="middle">',
            "Warrior",
            "</text>",
            '<text x="50%" y="50%" class="base" dominant-baseline="middle" text-anchor="middle">',
            "Levels: ",
            getLevels(_tokenId),
            "</text>",
            '<text x="50%" y="50%" class="base" dominant-baseline="middle" text-anchor="middle">',
            "Levels: ",
            getStrength(_tokenId),
            "</text>",
            "</svg>"

That is why i created 2 different functions.

1 Answer 1

0

Your approach is close, but there are potential improvements you can make:

  1. I suppose there's a typo in getLevels it should be tokenIdToFeatures instead of tokenToFeatures

  2. I think it's better to update values like this:

    tokenIdToFeatures[_tokenId]._level++; // level++ isn't needed
    ...
    tokenIdToFeatures[_tokenId]._strength++; // strength++ isn't needed
    
  3. You could use event to log the feature updates as it's common practice

2
  • Right, thanks, will my approach give error?
    – Hacek
    Commented Apr 20, 2023 at 6:46
  • if you fix the typo, it wont give error Commented Apr 20, 2023 at 20:55

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.