简单的连接代酮与Tokenuri的方法

发布于 2025-02-11 15:25:03 字数 1265 浏览 2 评论 0原文

我正在开普敦大学(University of Cape Town),使用坚固的NFT和Openzeppelin在开普敦大学做一个项目。我已将JSON/PNG的文件夹用于元数据。我现在需要使用代币 + .json在铸造时设置代币正确的URI。以下是简单的合同:

    //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;
    }

}

我上面有文件夹URL,我只需要添加令牌ID,然后添加一个.json即可。我的C#Brain说:“ ipfsurl” + newitemid.tostring() +“ .json”;

混音(固体)等效的是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衾梦 2025-02-18 15:25:03

从固体版本 0.8.12 可以使用string.concat(s1,s2)进行连接字符串。
我调整了您的智能合同代码中的一些笔记:

//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;
  }

}

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;
  }

}
尘曦 2025-02-18 15:25:03

这将起作用!

_setTokenURI(newItemId, string(abi.encodePacked(_uri, '/', newItemId.toString(), '.json')));

This will work!

_setTokenURI(newItemId, string(abi.encodePacked(_uri, '/', newItemId.toString(), '.json')));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文