Add Megapot to Your Site
This guide covers the different ways to purchase Megapot tickets programmatically. Choose the right method based on your use case.
Quick Reference
For dApp based execution:
<=10 tickets, single drawing
Jackpot
Immediate
Static only
>10 tickets, single drawing
BatchPurchaseFacilitator
Keeper-executed
Static + Dynamic
Multiple drawings
JackpotAutoSubscription
Keeper-executed
Static + Dynamic
For execution from a smart contract
<=10 tickets, pre-defined tickets
Jackpot
Immediate
Static only
<=10 tickets, random tickets
JackpotRandomTicketBuyer
Immediate
Dynamic only
>10 tickets, single drawing
BatchPurchaseFacilitator
Keeper-executed
Static + Dynamic
Multiple drawings
JackpotAutoSubscription
Keeper-executed
Static + Dynamic
Key Concepts
Static vs Dynamic Tickets
Static tickets are user-defined number combinations. The client specifies exactly which 5 normal numbers and bonusball to play.
Dynamic tickets are randomly generated on-chain. The contract generates valid random numbers at execution time.
Recommended Limits
When using BatchPurchaseFacilitator or JackpotAutoSubscription:
Limit static tickets to 100 per order
If purchasing more than 100 tickets, make the rest dynamic
This recommendation exists for gas efficiency and storage optimization. Static tickets must be stored on-chain and validated individually, while dynamic tickets are generated on-the-fly during execution.
Ticket Structure
All methods use the same ticket format:
The current normalBallMax is 30. The bonusballMax varies by drawing and can be read from the drawing state. You can check the Jackpot contract for the most up to date normalBallMax and bonusballMax by calling Jackpot.getDrawingState(currentDrawingId).
Direct Purchase: Jackpot.buyTickets
Jackpot.buyTicketsWhen to use: Single-drawing purchases of less than or equal to 10 tickets where you want full control over number selection (on or off-chain) or are fine generating random tickets in the client.
This is the most direct method. Your client generates the ticket numbers, and tickets are minted immediately in the same transaction.
Interface
Parameters
_tickets
Ticket[]
Array of tickets to purchase. Each ticket has normals (5 unique numbers 1-30) and bonusball (1 number within drawing's bonusball range)
_recipient
address
Address that will own the ticket NFTs
_referrers
address[]
Referrer addresses for fee sharing. Pass empty array [] if none
_referralSplit
uint256[]
Weight for each referrer, scaled to 1e18. Must sum to 1e18 if provided. Pass empty array [] if no referrers
_source
bytes32
Identifier for your application (used for analytics). e.g., bytes32("my-app")
Example Parameters
2 tickets with no referrer:
Prerequisites
Read the current ticket price from the drawing state
Approve USDC spending:
usdc.approve(jackpotAddress, ticketPrice * ticketCount)Ensure ticket numbers are valid (normals in range, unique, sorted; bonusball in range)
Batch Purchase: BatchPurchaseFacilitator
BatchPurchaseFacilitatorWhen to use: Single-drawing purchases of more than 10 tickets.
Batch orders are prepaid and executed by keepers in chunks. This is more gas-efficient for large orders and supports a mix of static (user-defined) and dynamic (random) tickets.
Interface
Parameters
_recipient
address
Address that will own the ticket NFTs
_dynamicTicketCount
uint64
Number of random tickets to generate
_userStaticTickets
Ticket[]
User-defined tickets (max 100 recommended)
_referrers
address[]
Referrer addresses for fee sharing
_referralSplit
uint256[]
Weight for each referrer, scaled to 1e18
Example Parameters
50 tickets: 40 dynamic + 10 static, no referrer:
500 tickets, all dynamic:
Prerequisites
Read the current ticket price
Calculate total cost:
ticketPrice * (dynamicTicketCount + staticTickets.length)Approve USDC spending on the
BatchPurchaseFacilitatoraddressEnsure no existing active batch order for this recipient
Execution Flow
You call
createBatchOrder, USDC is transferred, order is queuedProtocol keepers call
executeBatchOrderto process tickets in batchesLarge orders may take multiple blocks to complete
Tickets are minted to the recipient as they're processed
Cancellation
Call cancelBatchOrder() to cancel and receive a refund for unexecuted tickets. Orders are also auto-cancelled and refunded to the ticket recipient address if:
The drawing locks before execution completes
The order was created for a previous drawing
Recurring Purchase: JackpotAutoSubscription
JackpotAutoSubscriptionWhen to use: Purchases that span multiple drawings, regardless of ticket count per day.
Subscriptions let users prepay for tickets across many drawings. The same static tickets play every drawing, combined with fresh dynamic tickets.
Interface
Parameters
_recipient
address
Address that will own the ticket NFTs each drawing
_totalDays
uint64
Number of drawings to participate in
_dynamicTicketCount
uint64
Random tickets to purchase per drawing
_userStaticTickets
Ticket[]
User-defined tickets to play every drawing (max 100 recommended)
_referrers
address[]
Referrer addresses for all purchases
_referralSplit
uint256[]
Weight for each referrer, scaled to 1e18
Example Parameters
30 days, 5 dynamic tickets per day, no static tickets:
7 days, 3 dynamic + 2 lucky numbers per day:
Prerequisites
Read the current ticket price (this price is locked for the subscription duration)
Calculate total cost:
ticketPrice * totalDays * (dynamicTicketCount + staticTickets.length)Approve USDC spending on the
JackpotAutoSubscriptionaddressEnsure no existing active subscription for this recipient
Price Protection
The ticket price is locked when you create the subscription. If the protocol increases ticket prices, your subscription is automatically cancelled and remaining funds are refunded to the ticket recipient address. This protects users from unexpected cost increases.
Cancellation
Call cancelSubscription() to cancel and receive a refund for remaining days. The subscription also auto-cancels if:
Ticket price changes from the subscribed price
Referrer configuration becomes invalid
Integration with Batch Facilitator
For subscriptions with large daily ticket counts, the AutoSubscription contract automatically routes execution through the BatchPurchaseFacilitator for efficiency.
Random Tickets: JackpotRandomTicketBuyer
JackpotRandomTicketBuyerWhen to use:
On-chain integrations that don't want to implement random ticket generation
Quick purchases where the user doesn't care about specific numbers
Any scenario where you want random numbers without client-side generation
This helper contract generates random tickets using on-chain entropy (prevrandao, block hash, nonce) and calls Jackpot.buyTickets directly. Tickets are minted immediately.
Interface
Parameters
_count
uint256
Number of random tickets to generate and purchase
_recipient
address
Address that will own the ticket NFTs
_referrers
address[]
Referrer addresses for fee sharing. Pass empty array if none
_referralSplitBps
uint256[]
Weight for each referrer, scaled to 1e18. Must sum to 1e18 if provided
_source
bytes32
Identifier for your application
Example Parameters
5 random tickets, no referrer:
Prerequisites
Read the current ticket price from the Jackpot contract
Approve USDC spending on the
JackpotRandomTicketBuyeraddress:usdc.approve(randomBuyerAddress, ticketPrice * count)
Note on Randomness
The on-chain randomness used for ticket generation is sufficient for creating unpredictable ticket numbers, but it is not the same as the cryptographically secure Pyth Network entropy used for drawing winners. The drawing itself remains provably fair.
Common Patterns
Calculating Total Cost
Read ticketPrice from:
USDC Approval
Always approve the correct contract address:
Jackpot.buyTickets
Jackpot contract
JackpotRandomTicketBuyer.buyTickets
JackpotRandomTicketBuyer contract
BatchPurchaseFacilitator.createBatchOrder
BatchPurchaseFacilitator contract
JackpotAutoSubscription.createSubscription
JackpotAutoSubscription contract
Last updated
