Anish-Agnihotri / MultiRaffle

NFT distribution with (1) randomized, multi-winner raffles and (2) bulk on-chain metadata generation.
GNU Affero General Public License v3.0
269 stars 52 forks source link

Getting a refund when claiming tickets #9

Closed tzumby closed 3 years ago

tzumby commented 3 years ago

Hey @Anish-Agnihotri, thanks for providing this sample contract. Both this and the article are really good resources!

I'm looking at the refund mechanism and wondering if it wouldn't be easier to just compare the number of winningTickets with the entriesPerAddress(address) and just send the MINT_COST * (entriesPerAddress - winningTickets) as a refund ?

The advantage here is that we don't need to keep track of the losing tickets as well (which would have swapped indices because of the shuffling.

        // Refund losing tickets

        uint256 entriesPerAddress = entriesPerAddress[msg.sender];

        if (winningTickets != entriesPerAddress) {
            // Payout value equal to number of bought tickets - paid for winning tickets
            (bool sent, ) = payable(msg.sender).call{
                value: (entriesPerAddress - winningTickets) * MINT_COST
            }("");
            require(sent, "Unsuccessful in refund");
        }

        // Emit claim event
        emit RaffleClaimed(msg.sender, winningTickets, entriesPerAddress - winningTickets);