> For the complete documentation index, see [llms.txt](https://rpslabs.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rpslabs.gitbook.io/docs/using-lotto-sdk/for-developers/smart-contracts/rps-raffle.md).

# RPS Raffle

RPS Raffle is the core smart contract powering our customizable raffle systems. It handles ticket generation, fund management, and raffle logistics, offering a range of configurable parameters for a tailor-made user experience.

## Roles

#### **Operator**

This backend account is crucial for managing the raffle mechanics. It is responsible for executing the raffle process once the winning tickets have been randomly selected. Importantly, the Operator is needed for this task because the raffle contract does not maintain information regarding ticket ownership.

#### **Router**

Represents the RPSRouter contract.

#### **Owner**

Holds ultimate control over raffle parameters, such as pot limits, ticket costs, and prize distributions.

***

## State variables

```solidity
uint256 public potLimit // raffle fund size threshold
uint256 public currentPotSize // current raffle fund volume
uint256 public raffleTicketCost // ticket cost in ether
uint128 private claimWindow // claim period in seconds
uint16 public numberOfWinners
uint16 public tradeFeeInBps; // raffle fee in bps (100 = 1%, 10000 = 100%)
uint32 public lastRaffleTicketId
uint16 public currentPotId
uint16 public protocolFeeInBps //  integrated protocol fee as a percentage of funds that go to the raffle; controlled by the raffle owner
```

## Mappings

```solidity
mapping(address => Prize) public claimablePrizes
mapping(uint256 => RequestStatus) public chainlinkRequests
mapping(uint16 => uint32[]) public winningTicketIds
mapping(uint16 => uint128) public prizeAmounts
mapping(address => uint256) public pendingAmounts  //  “partial ticket” amounts for each user
```

## External functions

```solidity
function executeTrade(
    uint256 _amountInWei,
    address _user
) external payable onlyRouter whenNotPaused
```

Generates tickets for the \_user based on \_amountInWei; can only be called by router.

***

```solidity
function batchExecuteTrade(
    BatchTradeParams[] memory trades,
) external payable onlyRouter whenNotPaused
```

Generates tickets for multiple trades.

***

```solidity
function executeRaffle(
    address[] calldata _winners
) external onlyOperator
```

This function represents the final stage of the raffle process and is exclusively invoked by the Operator account. It performs two critical tasks:

* **Winner Assignment**: It sets the addresses of the winning participants based on their associated ticket IDs.
* **Fund Release**: Upon this assignment, the raffle funds are unlocked, enabling winners to claim their prizes.

{% hint style="info" %}
The need for Operator intervention arises because the raffle contract does not store ticket ownership information
{% endhint %}

***

```solidity
function claim() external whenNotPaused
```

For raffle winners to claim their funds.
