Raffl

Git Source Inherits: ReentrancyGuardUpgradeable, EntriesManager, IRaffl Author: JA (@ubinatus) Raffl is a decentralized platform built on the Ethereum blockchain, allowing users to create and participate in raffles/lotteries with complete transparency, security, and fairness.

State Variables

factory

STATE Address of the RafflFactory
address public factory;

creator

User address that created the Raffl
address public creator;

prizes

Prizes contained in the Raffl
Prize[] public prizes;

deadline

Block timestamp for when the draw should be made and until entries are accepted
uint256 public deadline;

minEntries

Minimum number of entries required to execute the draw
uint256 public minEntries;

entryPrice

Price of the entry to participate in the Raffl
uint256 public entryPrice;

entryToken

Address of the ERC20 entry token (if applicable)
address public entryToken;

tokenGates

Array of token gates required for all participants to purchase entries.
TokenGate[] public tokenGates;

userRefund

Maps a user address to whether refund was made.
mapping(address => bool) public userRefund;

extraRecipient

Extra recipient to share the pooled funds.
ExtraRecipient public extraRecipient;

pool

Total pooled funds from entries acquisition
uint256 public pool;

settled

Whether the raffle is settled or not
bool public settled;

prizesRefunded

Whether the prizes were refunded when criteria did not meet.
bool public prizesRefunded;

gameStatus

Status of the Raffl game
GameStatus public gameStatus;

MAX_ENTRIES_PER_USER

Maximum number of entries a single address can hold.
uint64 internal constant MAX_ENTRIES_PER_USER = 2 ** 64 - 1;

MAX_TOTAL_ENTRIES

Maximum total of entries.
uint256 internal constant MAX_TOTAL_ENTRIES = 2 ** 256 - 1;

ONE

Percentages and fees are calculated using 18 decimals where 1 ether is 100%.
uint256 internal constant ONE = 1 ether;

manager

The manager that deployed this contract which controls the values for fee and feeCollector.
IFeeManager public manager;

Functions

onlyFactory

MODIFIERS
modifier onlyFactory();

constructor

constructor();

initialize

INITIALIZER
function initialize(
    address _entryToken,
    uint256 _entryPrice,
    uint256 _minEntries,
    uint256 _deadline,
    address _creator,
    Prize[] calldata _prizes,
    TokenGate[] calldata _tokenGatesArray,
    ExtraRecipient calldata _extraRecipient
)
    external
    override
    initializer;

criteriaMet

METHODS
function criteriaMet() external view override returns (bool);

deadlineExpired

Checks if the deadline has passed
function deadlineExpired() external view override returns (bool);

upkeepPerformed

Checks if raffle already perfomed the upkeep
function upkeepPerformed() external view override returns (bool);

poolFeeData

Returns the current pool fee associated to this Raffl.
function poolFeeData() external view returns (address, uint64);

getPrizes

Returns the current prizes associated to this Raffl.
function getPrizes() external view returns (Prize[] memory);

buyEntries

Purchase entries for the raffle. Handles the acquisition of entries for three scenarios: i) Entry is paid with network tokens, ii) Entry is paid with ERC-20 tokens, iii) Entry is free (allows up to 1 entry per user)
function buyEntries(uint256 quantity) external payable override nonReentrant;
Parameters
NameTypeDescription
quantityuint256The quantity of entries to purchase. Requirements: - If entry is paid with network tokens, the required amount of network tokens. - If entry is paid with ERC-20, the contract must be approved to spend ERC-20 tokens. - If entry is free, no payment is required. Emits EntriesBought event

refundEntries

Refund entries for a specific user. Invokable when the draw was not made because the min entries were not enought
function refundEntries(address user) external override nonReentrant;
Parameters
NameTypeDescription
useraddressThe address of the user whose entries will be refunded.

refundPrizes

Refund prizes to the creator. Invokable when the draw was not made because the min entries were not enought
function refundPrizes() external payable override nonReentrant;

_transferPrizes

HELPERS Transfers the prizes to the specified user.
function _transferPrizes(address user) private;
Parameters
NameTypeDescription
useraddressThe address of the user who will receive the prizes.

_transferPool

Transfers the pool balance to the creator of the raffle, after deducting any fees.
function _transferPool() private;

_purchaseEntry

Internal function to handle the purchase of entries with entry price greater than 0.
function _purchaseEntry(uint256 quantity) private;
Parameters
NameTypeDescription
quantityuint256The quantity of entries to purchase.

_purchaseFreeEntry

Internal function to handle the purchase of free entries with entry price equal to 0.
function _purchaseFreeEntry() private;

_ensureTokenGating

Ensures that the user has all the requirements from the tokenGates array
function _ensureTokenGating(address user) private view;
Parameters
NameTypeDescription
useraddressAddress of the user

setSuccessCriteria

FACTORY METHODS Access control: factory is the only allowed to called this method
function setSuccessCriteria(uint256 requestId) external override onlyFactory;

setFailedCriteria

Sets the criteria as settled, sets the GameStatus as FailedDraw and emits event DeadlineFailedCriteria Access control: factory is the only allowed to called this method
function setFailedCriteria() external override onlyFactory;

disperseRewards

Transfers the prizes to the provably fair and verifiable entrant, sets the GameStatus as SuccessDraw and emits event DrawSuccess Access control: factory is the only allowed to called this method through the Chainlink VRF Coordinator
function disperseRewards(uint256 requestId, uint256 randomNumber) external override onlyFactory nonReentrant;