How to integrate Chainlink VRF for random NFT owner rewards during minting?

I’m working on an NFT project and want to give a chunk of the mint price to a random NFT owner each time someone mints. The tricky part is using Chainlink VRF for this. I’m stuck on how to make the randomness function work with sending the reward to the randomly picked owner after the VRF response comes back.

Here’s a simplified version of what I’m trying to do:

contract RandomRewardNFT is ERC721, VRFConsumerBase {
    uint256 public mintPrice = 0.1 ether;
    uint256 public rewardPercentage = 10;
    
    function mint() public payable {
        require(msg.value >= mintPrice, "Not enough ETH sent");
        
        uint256 tokenId = _mintNFT(msg.sender);
        
        // How do I pick a random owner and send them a reward?
        // requestRandomness() from VRF doesn't return immediately
    }
    
    function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
        // This is called by Chainlink VRF later
        // How do I use this to send the reward?
    }
}

Any ideas on how to structure this to make it work? Thanks!

yo, have u considered using a queue system? like, each mint adds to a queue, then when VRF callback hits, u pick from that queue randomly. could batch rewards too, save on gas. just make sure ur contract has enough ETH to cover rewards. might need some tweaks but could work

I implemented a similar mechanism in one of my projects. In that case, I separated the minting process from the reward distribution entirely. In the mint function, I emitted an event recording the minter and token ID, then maintained state to track necessary information such as the total minted NFTs and reward amount. Later, when fulfillRandomness is triggered by Chainlink, the randomness is applied to select a winner and execute the reward transfer. This asynchronous design helps keep the mint function gas-efficient while enabling reward batching and handling of edge cases.

hey there ClimbingMountain! that’s a pretty cool idea for ur NFT project. i’ve been tinkering with chainlink VRF too and it can be a bit tricky to wrap ur head around at first. have u thought about using a two-step process? like, u could have the mint function kick off the VRF request, then use the fulfillRandomness callback to actually dish out the reward. something like this maybe:

function mint() public payable {
    // do ur regular minting stuff here
    // then kick off the VRF request
    bytes32 requestId = requestRandomness(keyHash, fee);
    // store the requestId somewhere so u can use it later
}

function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
    // here's where u could pick the winner and send the reward
    uint256 winnerIndex = randomness % totalSupply();
    address winner = ownerOf(winnerIndex);
    uint256 rewardAmount = mintPrice * rewardPercentage / 100;
    payable(winner).transfer(rewardAmount);
}

just a rough idea, but might help u get started? let me know if u want me to explain anything more!