How to implement Chainlink VRF for random NFT owner rewards on minting?

Hey everyone! I’m working on an NFT project and I’m stuck on something. I want to give a part of the mint price to a random NFT owner each time someone mints. The problem is I’m using Chainlink VRF for randomness, and I can’t figure out how to integrate it with my reward mechanism. I’m looking for advice on how to link the VRF callback to a reward function that distributes a percentage of the mint fee to a randomly selected NFT holder.

Here’s a simplified version of my contract:

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");
        uint256 tokenId = _mint(msg.sender);
        requestRandomness();
    }

    function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
        uint256 winnerTokenId = randomness % totalSupply();
        address winner = ownerOf(winnerTokenId);
        uint256 reward = mintPrice * rewardPercentage / 100;
        payable(winner).transfer(reward);
    }
}

Any suggestions or corrections to improve this approach?
Thanks!

hey ethan, interesting project you’ve got there! i’m curious, have you considered using a mapping to keep track of pending rewards instead of sending them right away? it could help with gas costs and make things smoother. also, what’s your plan for handling scenarios where the contract doesn’t have enough ETH to pay out rewards? that could be a tricky situation.

have you thought about adding some extra randomness to the reward amount? like, maybe make it fluctuate a bit based on the VRF result? could add some excitement for the NFT holders.

btw, how are you planning to market this feature to potential buyers? the random reward thing sounds pretty cool, might be a good selling point. anyways, keep us updated on how it goes!

I’ve implemented something similar in one of my projects. Your approach is on the right track, but there are a few improvements you could make. First, consider using a two-step process: request randomness in the mint function, then handle the reward distribution in a separate function that’s called by fulfillRandomness. This separation of concerns can make your contract more gas-efficient and easier to maintain.

Also, be aware that transferring ETH directly can be risky. It’s generally better to use a ‘pull’ pattern where winners claim their rewards. This protects against potential issues with transfer failures.

Lastly, don’t forget to handle edge cases. What happens if there’s only one NFT minted? Or if the contract doesn’t have enough balance to pay the reward? Addressing these scenarios will make your contract more robust and secure.

yo, good question! i’ve messed with chainlink vrf before. one thing to watch out is gas costs - they can get crazy high if you’re not careful. maybe try batching rewards or letting users claim em instead of auto-sending? also, double-check ur math on the randomness calc, it could get wonky with low token counts. gl with ur project!