Chainlink VRF allows users to get a random number by calling requestRandomness and then waiting for the fulfillRandomness callback to be called by the Chainlink contract.
I have a function testWorstMint that mints some NFT's, given an amount. I want my NFT distribution to be random, so I'm simulating what would happen if the loop had to iterate over thousands of NFT's already assigned.
I am using 5000 for the MAX_NFT_SUPPLY, and I minted 1 token, which in this case would be the token with ID 4999. This costs roughly 600k in gas.
function testWorstMint(uint256 amount) public
{
testWorstMint(msg.sender, amount);
}
function testWorstMint(address owner, uint256 amount) public
{
uint256 total = totalSupply();
for (uint256 x = 0; x < (MAX_NFT_SUPPLY - total); x++)
{
if (x >= MAX_NFT_SUPPLY - total - amount)
{
_mint(owner, x);
}
}
}
The issue I'm having is that fulfillRandomness does not estimate gas properly (at least, that's what I think is happening).
function request(uint256 amount) public {
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - contract will be funded soon");
bytes32 requestId = requestRandomness(keyHash, fee, 0);
owners[requestId] = msg.sender;
amountToMint[requestId] = amount;
}
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
testWorstMint(owners[requestId], amountToMint[requestId]);
}
Calling request() does the exact same as what I described above, but it does it through a callback. It costs roughly 60k in gas for the request call, and increasing it is futile since it'll just be returned (and it's not like it goes to fulfillRandomness anyway).
fulfillRandomness does the exact same thing as the function above, but it fails, presumably because of gas. From the contract, it seems that gas limit for all VRF fulfillments is capped at 500k, well below what I need to mint one token at random in the worst-case scenario.