You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A gas-optimized, factory-architected lottery system powered by **Chainlink VRF v2.5**. The protocol employs a **Cumulative Sum (Weighted)** algorithm to enable O(1) entry costs and O(N) winner selection, eliminating the "Out of Gas" DoS vulnerabilities common in naive array-looping implementations.
15
10
16
-
## 🚀 Live on Sepolia
11
+
The system utilizes a **Factory Pattern** to autonomously manage VRF subscriptions, ensuring that deployed instances are permissionless, immutable, and verifiably fair.
The deployer and sole owner of the `LotteryFactory`. Only you can call `createLottery()` and management functions (`fundSubscription`, `removeConsumer`, etc.).
23
+
### 1. Algorithmic Efficiency (Cumulative Sum)
24
+
To solve the scaling issue of large player sets (1,000+), the protocol abandons the standard `address[]` array loop.
25
+
***Entry (O(1)):** Players are stored via a cumulative weight mapping. State updates are constant time regardless of total player count.
26
+
***Selection (O(N) Optimized):** Winner determination uses a linear search over ticket ranges. While O(N), this is executed off-chain or via view functions for verification, ensuring the heavy lifting doesn't block critical execution.
32
27
33
-
2.**Factory**
34
-
- On deployment, automatically creates a Chainlink VRF v2.5 subscription and becomes its permanent owner.
35
-
- No manual subscription creation or ownership transfer required.
36
-
- Deploys new `Lottery` instances on demand.
37
-
- Adds each new lottery as a consumer to the shared subscription.
38
-
- Distributes all collected platform fees to its owner (you).
28
+
### 2. DevOps & Automation (Factory Pattern)
29
+
The `LotteryFactory` abstracts the complexity of Chainlink integration:
30
+
***Auto-Subscription:** On deployment, the Factory programmatically creates and funds a VRF v2.5 subscription.
31
+
***Ownership Abstraction:** The Factory retains ownership of the subscription, removing the need for manual consumer addition/removal.
32
+
***Fee Router:** Platform fees (1%) are automatically routed to the protocol treasury, separating revenue from the prize pool.
39
33
40
-
3.**Lottery Instances**
41
-
- Each lottery runs independently with its own ticket sales, deadline, and prize pool.
42
-
- After the deadline, Chainlink Automation calls `performUpkeep()` (requires manual upkeep registration once per lottery).
43
-
-`performUpkeep()` sends platform fees to you and requests randomness from Chainlink VRF.
44
-
- VRF callback selects the winner using the cumulative sum pattern.
45
-
- Winner claims the full prize pool; you keep the extra 1% platform fee paid by players.
46
-
47
-
**Result**: A scalable, autonomous lottery system where you maintain full control and collect all fees, while players enjoy verifiably fair randomness powered by Chainlink.
48
-
49
-
50
-
**Key Innovation**: Factory creates and owns the VRF subscription on deployment — no manual ownership transfer required.
34
+
### 3. Economic Model
35
+
***Prize Pool:** 100% of the base ticket price goes to the winner.
36
+
***Protocol Fee:** An additional 1% surcharge is collected as protocol revenue.
37
+
***Result:** Zero-sum fairness for players (no rake from the prize pot) + sustainable revenue for the protocol.
51
38
52
39
---
53
40
54
-
## 🔬 Core Design Decisions
55
-
56
-
### Weighted Randomness — Cumulative Sum Pattern
57
-
58
-
Players are stored with cumulative ticket counts instead of individual tickets.
59
-
60
-
Example:
61
-
| Player | Tickets | Cumulative Total | Ticket Range |
3.Linear search finds the player whose range contains the ID
43
+
### Validated Properties
44
+
1.**RNG Tamper-Proofing:** Randomness is derived exclusively from Chainlink VRF. The request-fulfill pattern prevents block-hash manipulation attacks.
45
+
2.**Payment Isolation:** The `PrizePool` and `FeePool` are strictly segregated logic paths. A math error in fee calculation cannot drain user prizes.
46
+
3.**Automation Fallback:** While Chainlink Automation handles the happy path, `performUpkeep` is public, allowing manual intervention if the Automation network experiences latency.
71
47
72
-
**Benefits**:
73
-
- Entry cost: O(1)
74
-
- Winner search: O(unique players) — cheap in practice
0 commit comments