Skip to content

Instantly share code, notes, and snippets.

@korrio
Created November 11, 2021 00:24
Show Gist options
  • Save korrio/229ed06ccfca283aa8618fd22d8019fe to your computer and use it in GitHub Desktop.
Save korrio/229ed06ccfca283aa8618fd22d8019fe to your computer and use it in GitHub Desktop.
tokenURI.sol
function tokenURI(uint256 _tokenId)
public
view
override
returns (string memory)
{
CharacterAttributes memory charAttributes = nftHolderAttributes[
_tokenId
];
string memory strHp = Strings.toString(charAttributes.hp);
string memory strMaxHp = Strings.toString(charAttributes.maxHp);
string memory strAttackDamage = Strings.toString(
charAttributes.attackDamage
);
string memory json = Base64.encode(
bytes(
string(
abi.encodePacked(
'{"name": "',
charAttributes.name,
" -- NFT #: ",
Strings.toString(_tokenId),
'", "description": "This is an NFT that lets people play in the game Metaverse Slayer!", "image": "',
charAttributes.imageURI,
'", "attributes": [ { "trait_type": "Health Points", "value": ',
strHp,
', "max_value":',
strMaxHp,
'}, { "trait_type": "Attack Damage", "value": ',
strAttackDamage,
"} ]}"
)
)
)
);
string memory output = string(
abi.encodePacked("data:application/json;base64,", json)
);
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment