PatrickAlphaC / hardhat-nft-fcc

100 stars 139 forks source link

Error: VM Exception while processing transaction: reverted with custom error 'InvalidConsumer()' #69

Closed aindriu80 closed 1 year ago

aindriu80 commented 1 year ago

Hi, I'm following along but I'm getting the following error Error: VM Exception while processing transaction: reverted with custom error 'InvalidConsumer()'

`

yarn hardhat deploy



deploying "BasicNFT" (tx: 0x856f4762a665e306d42f86075296104169d1cd0d7c899762a2ba14c4bc5e6ee8)...: deployed at 0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0 with 2020849 gas


deploying "RandomIpfsNft" (tx: 0x426e79fa73661c50e53209695233001c01acdc1485ab97206329d592c97a391e)...: deployed at 0x5FC8d32690cc91D4c39d9d3abcBD16989F875707 with 3553625 gas


deploying "DynamicSvgNft" (tx: 0xc6858035bf7e7c76e7b7110a2cec7e8df81a24e243bbbbd72b61553096d1d127)...: deployed at 0x0165878A594ca255338adfa4d48449f69242Eb8F with 4276707 gas Basic NFT index 0 has tokenURI: ipfs://bafybeig37ioir76s7mg5oobetncojcm3c3hxasyd4rvid4jqhy4gkaheg4/?filename=0-PUG.json Error: VM Exception while processing transaction: reverted with custom error 'InvalidConsumer()' at VRFCoordinatorV2Mock.onlyValidConsumer (@chainlink/contracts/src/v0.8/mocks/VRFCoordinatorV2Mock.sol:72) at VRFCoordinatorV2Mock.requestRandomWords (@chainlink/contracts/src/v0.8/mocks/VRFCoordinatorV2Mock.sol:147) at RandomIpfsNft.requestNft (contracts/RandomIpfsNft.sol:73) at processTicksAndRejections (node:internal/process/task_queues:95:5) at runNextTicks (node:internal/process/task_queues:64:3) at listOnTimeout (node:internal/timers:533:9) at processTimers (node:internal/timers:507:7) at HardhatNode._mineBlockWithPendingTxs (/home/aindriu/Cáipéisí/Programming/projects/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:1802:23) at HardhatNode.mineBlock (/home/aindriu/Cáipéisí/Programming/projects/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:491:16) at EthModule._sendTransactionAndReturnHash (/home/aindriu/Cáipéisí/Programming/projects/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:1522:18) error Command failed with exit code 1.

......`

My code base is here https://github.com/aindriu80/fcc-hardhat-nft-app

Here is RandomIpfsNft.sol

`

// SPDX-License-Identifier: MIT pragma solidity ^0.8.7;

import '@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol'; import '@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol'; import '@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol'; import '@openzeppelin/contracts/access/Ownable.sol';

error RandomIpfsNftRangeOutOfBounds(); error RandomIpfsNftNeedMoreETHSent(); error RandomIpfsNft__TransferFailed();

contract RandomIpfsNft is VRFConsumerBaseV2, ERC721URIStorage, Ownable { // When we mint an NFT, we will trigger a Chainlink VRF call to get us a random number // using that number, we will get a random NFT // Pug, Shiba Inu, St. Bernard // Pug super rare // Shiba sort of rare // St. Bernard very common // users have to pay to min an NFT // the owner of the contract can withdrawn the ETH

// Type Declaration
enum Breed {
    PUG,
    SHIBA_INU,
    ST_BERNARD
}

VRFCoordinatorV2Interface private immutable i_vrfCoordinator;
uint64 private immutable i_subscriptionId;
bytes32 private immutable i_gasLane;
uint32 private immutable i_callbackGasLimit;
uint16 private constant REQUEST_CONFIRMATIONS = 3;
uint32 private constant NUM_WORDS = 1;

// VRF Helpers
mapping(uint256 => address) public s_requestIdToSender;

// NFT Variables
uint256 public s_tokenCounter;
uint256 internal constant MAX_CHANCE_VALUE = 100;
string[] internal s_dogTokenUris;
uint256 internal immutable i_mintFee;

// Events
event NftRequested(uint256 indexed requestId, address requester);
event NftMinted(Breed dogBreed, address minter);

constructor(
    address vrfCoordinatorV2,
    uint64 subscriptionId,
    bytes32 gasLane,
    uint32 callbackGasLimit,
    string[3] memory dogTokenUris,
    uint256 mintFee
) VRFConsumerBaseV2(vrfCoordinatorV2) ERC721('Random IPFS NFT', 'RIN') {
    i_vrfCoordinator = VRFCoordinatorV2Interface(vrfCoordinatorV2);
    i_subscriptionId = subscriptionId;
    i_gasLane = gasLane;
    i_callbackGasLimit = callbackGasLimit;
    s_dogTokenUris = dogTokenUris;
    i_mintFee = mintFee;

}

function requestNft() public payable returns (uint256 requestId) {
    if (msg.value < i_mintFee) {
        revert RandomIpfsNft__NeedMoreETHSent();
    }
    requestId = i_vrfCoordinator.requestRandomWords(
        i_gasLane,
        i_subscriptionId,
        REQUEST_CONFIRMATIONS,
        i_callbackGasLimit,
        NUM_WORDS
    );
    s_requestIdToSender[requestId] = msg.sender;
    emit NftRequested(requestId, msg.sender);
}

function fulfillRandomWords(uint256 requestId, uint256[] memory randomWords) internal override {
    address dogOwner = s_requestIdToSender[requestId];
    uint256 newTokenId = s_tokenCounter;
    // What does this token look like?
    uint256 moddedRng = randomWords[0] % MAX_CHANCE_VALUE;
    // 0 - 99
    // 7 -> PUG
    // 88 -> St. Bernard
    // 45 -> St. Bernard
    // 12 -> Shiba Inu

    Breed dogBreed = getBreedFromModdedRng(moddedRng);
    s_tokenCounter += s_tokenCounter;
    _safeMint(dogOwner, newTokenId);
    _setTokenURI(newTokenId, s_dogTokenUris[uint256(dogBreed)]);
    emit NftMinted(dogBreed, dogOwner);
}

function withdraw() public onlyOwner {
    uint256 amount = address(this).balance;
    (bool success, ) = payable(msg.sender).call{value: amount}('');
    if (!success) {
        revert RandomIpfsNft__TransferFailed();
    }
}

function getBreedFromModdedRng(uint256 moddedRng) public pure returns (Breed) {
    uint256 cumulativeSum = 0;
    uint256[3] memory chanceArray = getChanceArray();

    for (uint256 i = 0; i < chanceArray.length; i++) {
        if (moddedRng >= cumulativeSum && moddedRng < cumulativeSum + chanceArray[i]) {
            return Breed(i);
        }
        cumulativeSum += chanceArray[i];
    }
    revert RandomIpfsNft__RangeOutOfBounds();
}

function getChanceArray() public pure returns (uint256[3] memory) {
    return [10, 30, MAX_CHANCE_VALUE];
}

function getMintFee() public view returns(uint256){
    return i_mintFee;

}

function getDogTokenUris(uint256 index) public view returns(string memory){
    return s_dogTokenUris[index];

}

function getTokenCounter() public view returns(uint256){
    return s_tokenCounter;
}

}

`

I'm comparing Patricks code and I've already fixed a few bugs but I don't understand the following error

Error: VM Exception while processing transaction: reverted with custom error 'InvalidConsumer()'

aindriu80 commented 1 year ago

I got it resolved. It seems to be something similar related to this thread:-

https://github.com/smartcontractkit/full-blockchain-solidity-course-js/discussions/1565

The steps were:

yarn add @chainlink/contracts

Then:

npm install --save-dev @chainlink/contracts@0.4.1

The above step didn't work so I tried:

npm install --save-dev @chainlink/contracts@0.4.1 --force

After that it was just a matter of fixing typos.