Lesson 14 RandomIpfsNFT can't call requestRandomWords #1468
-
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
contract RandomIpfsNft is ERC721, Ownable, VRFConsumerBaseV2 {
using Counters for Counters.Counter;
Counters.Counter private _tokenIdCounter;
VRFCoordinatorV2Interface private immutable i_VRFCoordinatorV2Contract;
bytes32 private immutable i_keyHash;
uint32 private immutable i_callbackGasLimit;
uint64 private immutable i_subscriptionId;
uint16 private constant REQUEST_CONFIRMATIONS = 3;
uint32 private constant NUMWORDS = 1;
uint256 public s_requestId;
mapping(uint256 => address) public s_requestIdToSender;
constructor(
address vrfCoordinatorV2Address,
bytes32 keyHash,
uint32 callbackGasLimit,
uint64 subscriptionId
) ERC721("MyToken", "MTK") VRFConsumerBaseV2(vrfCoordinatorV2Address) {
i_VRFCoordinatorV2Contract = VRFCoordinatorV2Interface(
vrfCoordinatorV2Address
);
i_keyHash = keyHash;
i_callbackGasLimit = callbackGasLimit;
i_subscriptionId = subscriptionId;
}
function requestNft() public onlyOwner returns (uint256) {
s_requestId = i_VRFCoordinatorV2Contract.requestRandomWords(
i_keyHash,
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callbackGasLimit,
NUMWORDS
);
s_requestIdToSender[s_requestId] = msg.sender;
return s_requestId;
}
function fulfillRandomWords(uint256 requestId, uint256[] memory randomWords)
internal
override
{
address dogOwner = s_requestIdToSender[requestId];
}
function getVRFCoordinatorV2Contract()
public
view
returns (VRFCoordinatorV2Interface)
{
return i_VRFCoordinatorV2Contract;
}
function getKeyHash() public view returns (bytes32) {
return i_keyHash;
}
function getCallbackGasLimit() public view returns (uint32) {
return i_callbackGasLimit;
}
function getSubscriptionId() public view returns (uint64) {
return i_subscriptionId;
}
function getNumWords() public pure returns (uint32) {
return NUMWORDS;
}
function getRequestConfirmations() public pure returns (uint16) {
return REQUEST_CONFIRMATIONS;
}
} When I test my code on the local hardhat network on this function: function requestNft() public onlyOwner returns (uint256) {
s_requestId = i_VRFCoordinatorV2Contract.requestRandomWords(
i_keyHash,
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callbackGasLimit,
NUMWORDS
);
s_requestIdToSender[s_requestId] = msg.sender;
return s_requestId;
} I'm running into this error:
I checked the Invalid Consumer error and don't understand why I'm getting it, because when I deploy my contract, I set up the subscriptionId in the following manner:
I also ran a test to make sure that my subscriptionId was returning correctly, which it is:
I know my mock and contract are both deploying correctly because all my other tests are working correctly. For additional information, here is my mock: // SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@chainlink/contracts/src/v0.8/mocks/VRFCoordinatorV2Mock.sol"; here are my tests:
Here are my deploy scripts:
and here are my hh config and hh helper config
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
@WilliamGiammona add the older version of chainlink contracts |
Beta Was this translation helpful? Give feedback.
-
It worked, thank you so much! |
Beta Was this translation helpful? Give feedback.
-
@alymurtazamemon @WilliamGiammona Thank you so much for providing a solution. Just out of curiosity, do we know why the new version doesn't work? |
Beta Was this translation helpful? Give feedback.
@WilliamGiammona add the older version of chainlink contracts
npm i @chainlink/contracts@0.4.1