I am doing a project for my Honours year at the University of Cape Town using solidity and openzeppelin for my NFTs. I have uploaded a folder of json/png for the metadata. I need to now use the tokenID .json to set the tokens correct uri when minting them. Below is the simple contract:
//SPDX-License-Identifier: MIT
pragma solidity ^0.8;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract ImpactCollection is ERC721URIStorage {
uint256 public tokenCounter;
constructor () ERC721 ("Impact Tokens", "COLLECTION_TICKER"){
tokenCounter = 0;
}
function concatenate(string memory a,uint256 memory b,string memory c) public pure returns (string memory){
return string(abi.encodePacked(a,b,c));
}
function createCollectible() public returns (uint256) {
uint256 newItemId = tokenCounter;
string urinumber = string(abi.encodePacked(newItemId.toString()))
tokenURI = "https://ipfs.io/ipfs/QmQh54Rb8ZFY33P9bWUzgonRvA7XeChVWaAWG3nMqQ19xW/" urinumber ".json";
_safeMint(msg.sender, newItemId);
_setTokenURI(newItemId, tokenURI);
tokenCounter = tokenCounter 1;
return newItemId;
}
}
I have the folder url above and i just need to add the token id and then add a .json. My C# brain says: "ipfsurl" newItemId.toString() ".json";
What is the remix (solidity) equivalent?
CodePudding user response:
From solidity version 0.8.12 you can use string.concat(s1,s2)
for concatenate the strings.
I adjusted and put some notes in your smart contract code:
//SPDX-License-Identifier: MIT
pragma solidity ^0.8;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract ImpactCollection is ERC721URIStorage {
uint256 public tokenCounter;
using Strings for *;
constructor () ERC721 ("Impact Tokens", "COLLECTION_TICKER"){
tokenCounter = 0;
}
function concatenate(string memory a,uint256 b,string memory c) public pure returns (string memory){
return string(abi.encodePacked(a,b,c));
}
function createCollectible() public returns (uint256) {
uint256 newItemId = tokenCounter;
// NOTE: Use Strings.toString for convert a uint to string datatype
string memory urinumber = Strings.toString(newItemId);
// NOTE: I declared a new variable for contain token URI
string memory tokenURI = "https://ipfs.io/ipfs/QmQh54Rb8ZFY33P9bWUzgonRvA7XeChVWaAWG3nMqQ19xW/";
// NOTE: I declare a new variable for contain tokenURI concatenated
string memory fullTokenURI = string.concat("https://ipfs.io/ipfs/QmQh54Rb8ZFY33P9bWUzgonRvA7XeChVWaAWG3nMqQ19xW/", urinumber, ".json");
_safeMint(msg.sender, newItemId);
_setTokenURI(newItemId, tokenURI);
tokenCounter = tokenCounter 1;
return newItemId;
}
}
CodePudding user response:
This will work!
_setTokenURI(newItemId, string(abi.encodePacked(_uri, '/', newItemId.toString(), '.json')));