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:

Scenario
Contract
Execution
Ticket Types

<=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

Scenario
Contract
Execution
Ticket Types

<=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.

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

When 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

Parameter
Type
Description

_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:

circle-info

Prerequisites

  1. Read the current ticket price from the drawing state

  2. Approve USDC spending: usdc.approve(jackpotAddress, ticketPrice * ticketCount)

  3. Ensure ticket numbers are valid (normals in range, unique, sorted; bonusball in range)


Batch Purchase: BatchPurchaseFacilitator

When 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

Parameter
Type
Description

_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:

circle-info

Prerequisites

  1. Read the current ticket price

  2. Calculate total cost: ticketPrice * (dynamicTicketCount + staticTickets.length)

  3. Approve USDC spending on the BatchPurchaseFacilitator address

  4. Ensure no existing active batch order for this recipient

Execution Flow

  1. You call createBatchOrder, USDC is transferred, order is queued

  2. Protocol keepers call executeBatchOrder to process tickets in batches

  3. Large orders may take multiple blocks to complete

  4. 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

When 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

Parameter
Type
Description

_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:

circle-info

Prerequisites

  1. Read the current ticket price (this price is locked for the subscription duration)

  2. Calculate total cost: ticketPrice * totalDays * (dynamicTicketCount + staticTickets.length)

  3. Approve USDC spending on the JackpotAutoSubscription address

  4. Ensure 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

When 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

Parameter
Type
Description

_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:

circle-info

Prerequisites

  1. Read the current ticket price from the Jackpot contract

  2. Approve USDC spending on the JackpotRandomTicketBuyer address: 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:

Method
Approve USDC to

Jackpot.buyTickets

Jackpot contract

JackpotRandomTicketBuyer.buyTickets

JackpotRandomTicketBuyer contract

BatchPurchaseFacilitator.createBatchOrder

BatchPurchaseFacilitator contract

JackpotAutoSubscription.createSubscription

JackpotAutoSubscription contract

Last updated