smartcontractkit / full-blockchain-solidity-course-js

Learn Blockchain, Solidity, and Full Stack Web3 Development with Javascript
12.25k stars 2.94k forks source link

R #4836

Closed NicolasMazzoleni closed 1 year ago

NicolasMazzoleni commented 1 year ago

Lesson

Lesson 14

Could you please leave a link to the timestamp in the video where this error occurs? (You can right click a video and "copy video URL at current time")

https://youtu.be/gyMwXuJrbJQ?t=84834

Operating System

macOS (Apple Silicon)

Describe the bug

Hello guys,

I am facing an issue regarding the deployment of the Lesson 14 DynamicSvgNft.sol contract. It seams like the override function tokenURI() is not working properly. When a tokenId = 0 is passed as a parameter (when called from the local nodes or when it's deployed on the Goerli testnest, the function is reverted with the custom error. Here 's a screenshot of the console below.

`// SPDX-License-Identifier: MIT pragma solidity ^0.8.8;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; import "base64-sol/base64.sol"; import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; import "hardhat/console.sol";

error DynamicSVGNFT__UnexistenceTokenId();

contract DynamicSVGNFT is ERC721, Ownable { // mint nft // store our SVG on the blockchain // logic to show X or Y image

uint256 private s_tokenCounter;
string private i_lowImageURI;
string private i_highImageURI;
string private c_base64EncodedSvgPrefix = "data:image/svg+xml;base64,";
AggregatorV3Interface internal immutable i_priceFeed;
mapping(uint256 => int256) public s_tokenIdToHighValue; // Is like declaring an array in JS

event createdNFT(uint256 tokenId, int256 highValue);

constructor(
    address priceFeedAddress,
    string memory lowSvg,
    string memory highSvg
) ERC721("Dynamic SVG NFT", "DSN") {
    s_tokenCounter = 0;
    i_lowImageURI = svgToImageURI(lowSvg);
    i_highImageURI = svgToImageURI(highSvg);
    i_priceFeed = AggregatorV3Interface(priceFeedAddress);
}

function svgToImageURI(string memory svg) public view returns (string memory) {
    string memory svgBase64Encoded = Base64.encode(bytes(string(abi.encodePacked(svg))));
    return string(abi.encodePacked(c_base64EncodedSvgPrefix, svgBase64Encoded));

    // string memory svgBase64Encoded = Base64.encode(bytes(string.concat(svg)));
    // return string.concat(base64EncodedSvgPrefix, svgBase64Encoded);
}

// publishing your token on the blockchain to make it purchasable
function mintNft(int256 highValue) public {
    s_tokenIdToHighValue[s_tokenCounter] = highValue;
    s_tokenCounter += 1;
    _safeMint(msg.sender, s_tokenCounter);
    emit createdNFT(s_tokenCounter, highValue);
}

function _baseURI() internal pure override returns (string memory) {
    return "data:application/json;base64,";
}

function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
    if (!_exists(tokenId)) {
        revert DynamicSVGNFT__UnexistenceTokenId();
    }
    (, int256 price, , , ) = i_priceFeed.latestRoundData();
    string memory imageURI = i_lowImageURI;
    if (price >= s_tokenIdToHighValue[tokenId]) {
        imageURI = i_highImageURI;
    }
    return
        string(
            abi.encodePacked(
                _baseURI(),
                Base64.encode(
                    bytes(
                        abi.encodePacked(
                            '{"name":"',
                            name(), // You can add whatever name here
                            '", "description":"An NFT that changes based on the Chainlink Feed", ',
                            '"attributes": [{"trait_type": "coolness", "value": 100}], "image":"',
                            imageURI,
                            '"}'
                        )
                    )
                )
            )
        );
}}`

If you guys have an idea on when I should look for this kind of error. Thank you ! Nicolas

Screenshot 2023-02-16 at 10 12 57 AM