# Use Smart Contracts With Basescan

The Megapot protocol operates autonomously, enabling anyone to build on it, or use it, without permission. This article covers usage via the neutral third-party app, Basescan. *Use the quick links below to learn how to use the contracts directly for the given actions. If you are new to Basescan, make sure to review the* [*Getting Started*](#getting-started) *section*:

* [Buy Tickets (10 or less)](#buy-tickets)
* [Buy Random Tickets (10 or less)](#buy-random-tickets)
* [Buy Tickets (more than 10)](#create-batch-order)
* [Claim Winnings](#claim-winnings)
* [Deposit Funds to Prize Pool](#deposit-to-prize-pool)
* [Initiate Withdrawal from Prize Pool](#initiate-withdrawal)
* [Finalize Withdrawal from Prize Pool](#finalize-withdrawal)
* [Claim Referral Fees](#claim-referral-fees)
* [Transfer USDC from Smart Contract Wallet](#transferring-usdc-from-a-smart-contract-wallet)

***

## Use the Contract Directly on Basescan

### Getting Started

![Basescan Connect Wallet](https://4144610446-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0Qe5CBrrfDdDUHNLiJQ9%2Fuploads%2Fgit-blob-1a49c81a84af12c673809f0b20f91426c7ea900e%2Fconnect-wallet.png?alt=media)

1. Navigate to the contract address on [Basescan](https://basescan.org)
2. Click the **Contract** tab (pink box), then **Write Contract** tab (green box). When approving USDC you will need to click on the **Write as Proxy** tab instead of **Write Contract**.
3. Click **Connect to Web3** (see red arrow in image above) and connect your wallet
4. Find the function you want to call and enter the required parameters

***

## Contract Addresses

All contracts are deployed on **Base** (Chain ID: 8453).

| Contract                          | Address                                      | Description                         |
| --------------------------------- | -------------------------------------------- | ----------------------------------- |
| Jackpot                           | `0x3bAe643002069dBCbcd62B1A4eb4C4A397d042a2` | Main lottery orchestrator           |
| JackpotLPManager                  | `0xE63E54DF82d894396B885CE498F828f2454d9dCf` | Backer deposit and share management |
| JackpotTicketNFT                  | `0x48FfE35AbB9f4780a4f1775C2Ce1c46185b366e4` | ERC-721 ticket NFTs                 |
| JackpotAutoSubscription           | `0x02A58B725116BA687D9356Eafe0fA771d58a37ac` | Auto-subscription management        |
| BatchPurchaseFacilitator          | `0x01774B531591b286b9f02C6Bc02ab3fD9526Aa76` | Batch ticket purchases              |
| JackpotRandomTicketBuyer          | `0xb9560b43b91dE2c1DaF5dfbb76b2CFcDaFc13aBd` | Random ticket purchases             |
| GuaranteedMinimumPayoutCalculator | `0x97a22361b6208aC8cd9afaea09D20feC47046CBD` | Prize tier calculations             |
| ScaledEntropyProvider             | `0x5D030DEC2e0d38935e662C0d2feD44B050c8Ae51` | Pyth randomness integration         |

***

## Player Functions

### Buy Tickets

**Contract:** Jackpot ([Basescan](https://basescan.org/address/0x3bAe643002069dBCbcd62B1A4eb4C4A397d042a2))

**Function:** `buyTickets`

Purchase lottery tickets for the current drawing.

**Parameters:**

* `_tickets`: Array of ticket structs, each containing:
  * `normals`: Array of 5 numbers (1 to ballMax, typically 1-30)
  * `bonusball`: Single number (1 to current bonusballMax)
* `_recipient`: Address to receive the ticket NFTs
* `_referrers`: Array of referrer addresses (can be empty `[]`)
* `_referralSplit`: Array of split percentages in 1e18 scale (must sum to 1e18 if referrers provided)
* `_source`: Telemetry identifier (use `0x0000000000000000000000000000000000000000000000000000000000000000`)

**Before calling:**

1. [Connect your wallet](#getting-started)
2. In a separate tabIn a separate tab, [Approve USDC spending](#approving-usdc) for the Jackpot contract (1e6 per ticket)

**Example: 1 ticket with numbers 1, 5, 10, 15, 20 and bonusball 3, no referrer:**

```
_tickets: [[[1,5,10,15,20], 3]]
_recipient: 0xYourAddress
_referrers: []
_referralSplit: []
_source: 0x0000000000000000000000000000000000000000000000000000000000000000
```

***

### Buy Random Tickets

**Contract:** JackpotRandomTicketBuyer ([Basescan](https://basescan.org/address/0xb9560b43b91dE2c1DaF5dfbb76b2CFcDaFc13aBd))

**Function:** `buyTickets`

Purchase tickets with randomly generated numbers. The contract handles number selection for you using on-chain randomness.

**Parameters:**

* `_count`: Number of random tickets to purchase
* `_recipient`: Address to receive the ticket NFTs
* `_referrers`: Array of referrer addresses (can be empty `[]`)
* `_referralSplitBps`: Array of split percentages in 1e18 scale (must sum to 1e18 if referrers provided)
* `_source`: Telemetry identifier (use `0x0000000000000000000000000000000000000000000000000000000000000000`)

**Before calling:**

1. [Connect your wallet](#getting-started)
2. In a separate tab, tabIn a separate tab, [Approve USDC spending](#approving-usdc) for the JackpotRandomTicketBuyer contract (1e6 per ticket)

**Example: 5 random tickets, no referrer:**

```
_count: 5
_recipient: 0xYourAddress
_referrers: []
_referralSplitBps: []
_source: 0x0000000000000000000000000000000000000000000000000000000000000000
```

***

### Claim Winnings

**Contract:** Jackpot ([Basescan](https://basescan.org/address/0x3bAe643002069dBCbcd62B1A4eb4C4A397d042a2))

**Function:** `claimWinnings`

Claim prizes from winning tickets. The ticket NFTs are burned upon claiming.

**Parameters:**

* `_userTicketIds`: Array of ticket NFT IDs to claim

**Before calling:**

1. [Connect your wallet](#getting-started)
2. In a separate browser tab, find your ticket IDs (see below)
3. Ensure the drawing has completed (tickets must be from a past drawing)

**Example claiming 2 tickets:**

```
_userTicketIds: [98765432109876543210987654321098765432109876543210987654321012345, 98765432109876543210987654321098765432109876543210987654321012346]
```

**How to find your ticket IDs:**

1. Navigate to `https://opensea.io/<YOUR_ADDRESS>?collectionSlugs=megapot-tickets` (replacing `<YOUR_ADDRESS>` with your wallet address) to view your Megapot Tickets. Clicking on one of the tickets should show the metadata below:

![OpenSea ticket metadata](https://4144610446-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0Qe5CBrrfDdDUHNLiJQ9%2Fuploads%2Fgit-blob-4cd36c78d7611d36a1104b174dfb26cbcf2dd9b5%2Fopensea-metadata.png?alt=media)

From here you have three options:

* **Preferred for smaller number of tickets:** For each ticket, locate the ticketId (yellow box). Add each id to the `_userTicketIds` array that you will pass in to `claimWinnings`. Optionally, you can use the next method as well.
* **Many tickets in one drawing:** Get the `drawingId` from any ticket (red box), then go to the [JackpotTicketNFT contract on Basescan](https://basescan.org/address/0x48FfE35AbB9f4780a4f1775C2Ce1c46185b366e4#readContract) and call `getUserTickets` with your address and the drawing ID to get all your ticket IDs for that drawing (see example below). Repeat for each drawing you participated in. It is recommended you claim one drawing at a time.
* **More than 75 tickets:** Break up your `claimWinnings` calls across multiple transactions to avoid running into gas limits.

**Example calling getUserTickets for drawing 42:**

```
_userAddress: 0xYourAddress
_drawingId: 42
```

***

## Backer Functions

### Deposit to Prize Pool

**Contract:** Jackpot ([Basescan](https://basescan.org/address/0x3bAe643002069dBCbcd62B1A4eb4C4A397d042a2))

**Function:** `lpDeposit`

Deposit USDC to back the prize pool and earn returns from ticket sales.

**Parameters:**

* `_amountToDeposit`: Amount of USDC in wei (6 decimals, so 1000000 = $1)

**Before calling:**

1. [Connect your wallet](#getting-started)
2. In a separate tab, [Approve USDC spending](#approving-usdc) for the Jackpot contract

**Example depositing $1,000:**

```
_amountToDeposit: 1000000000
```

***

### Initiate Withdrawal

**Contract:** Jackpot ([Basescan](https://basescan.org/address/0x3bAe643002069dBCbcd62B1A4eb4C4A397d042a2))

**Function:** `initiateWithdraw`

Begin the withdrawal process. Shares are moved to pending status.

**Parameters:**

* `_amountToWithdrawInShares`: Amount of shares to withdraw (6 decimals, like USDC)

**Before calling:**

1. [Connect your wallet](#getting-started)

**Note:** You can view your share balance by calling `getLPShares` on JackpotLPManager with your address.

**Example withdrawing 100 shares:**

```
_amountToWithdrawInShares: 100000000
```

***

### Finalize Withdrawal

**Contract:** Jackpot ([Basescan](https://basescan.org/address/0x3bAe643002069dBCbcd62B1A4eb4C4A397d042a2))

**Function:** `finalizeWithdraw`

Complete a pending withdrawal and receive USDC. Call this after the drawing completes.

**Parameters:** None (uses your connected wallet address)

**Before calling:**

1. [Connect your wallet](#getting-started)

***

### Emergency Withdrawal

**Contract:** Jackpot ([Basescan](https://basescan.org/address/0x3bAe643002069dBCbcd62B1A4eb4C4A397d042a2))

**Function:** `emergencyWithdrawLP`

Only available when emergency mode is enabled. Withdraws all LP positions immediately.

**Parameters:** None

**Before calling:**

1. [Connect your wallet](#getting-started)

***

## Referrer Functions

### Claim Referral Fees

**Contract:** Jackpot ([Basescan](https://basescan.org/address/0x3bAe643002069dBCbcd62B1A4eb4C4A397d042a2))

**Function:** `claimReferralFees`

Claim accumulated referral fees from ticket sales and winner payouts.

**Parameters:** None (uses your connected wallet address)

**Before calling:**

1. [Connect your wallet](#getting-started)

**Check your balance first:** Call `referralFees` on the Jackpot contract with your address to see your claimable amount.

***

## Auto-Subscription Functions

### Create Subscription

**Contract:** JackpotAutoSubscription ([Basescan](https://basescan.org/address/0x02A58B725116BA687D9356Eafe0fA771d58a37ac))

**Function:** `createSubscription`

Set up automatic daily ticket purchases.

**Parameters:**

* `_recipient`: Address to receive the tickets
* `_totalDays`: Number of days to subscribe
* `_dynamicTicketCount`: Number of random tickets per day
* `_userStaticTickets`: Array of static tickets (same numbers each day)
* `_referrers`: Referrer addresses
* `_referralSplit`: Referral split percentages (1e18 scale)

**Before calling:**

1. [Connect your wallet](#getting-started)
2. Calculate total cost: `totalDays × (dynamicTickets + staticTickets) × ticketPrice`
3. tabIn a separate tab, In a separate tab, [Approve USDC spending](#approving-usdc) for the JackpotAutoSubscription contract

**Example: 30-day subscription with 2 random tickets + 1 static ticket (1, 5, 10, 15, 20 with bonusball 3) per day, no referrer:**

```
_recipient: 0xYourAddress
_totalDays: 30
_dynamicTicketCount: 2
_userStaticTickets: [[[1,5,10,15,20], 3]]
_referrers: []
_referralSplit: []
```

***

### Cancel Subscription

**Contract:** JackpotAutoSubscription ([Basescan](https://basescan.org/address/0x02A58B725116BA687D9356Eafe0fA771d58a37ac))

**Function:** `cancelSubscription`

Cancel your subscription and receive a refund for unused days.

**Parameters:** None (uses your connected wallet address)

**Before calling:**

1. [Connect your wallet](#getting-started)

***

## Batch Purchase Functions

### Create Batch Order

**Contract:** BatchPurchaseFacilitator ([Basescan](https://basescan.org/address/0x01774B531591b286b9f02C6Bc02ab3fD9526Aa76))

**Function:** `createBatchOrder`

Create a batch order for purchasing many tickets at once.

**Parameters:**

* `_recipient`: Address to receive the tickets
* `_dynamicTicketCount`: Number of randomly generated tickets
* `_userStaticTickets`: Array of user-defined tickets
* `_referrers`: Referrer addresses
* `_referralSplit`: Referral split percentages

**Before calling:**

1. [Connect your wallet](#getting-started)
2. Calculate total cost: `totalTickets × ticketPrice`
3. tabIn a separate tab, In a separate tab, [Approve USDC spending](#approving-usdc) for the BatchPurchaseFacilitator contract

**Example: 50 random tickets + 2 static tickets (7, 14, 21, 28, 30 with bonusball 5) and (2, 4, 6, 8, 10 with bonusball 1), no referrer:**

```
_recipient: 0xYourAddress
_dynamicTicketCount: 50
_userStaticTickets: [[[7,14,21,28,30], 5], [[2,4,6,8,10], 1]]
_referrers: []
_referralSplit: []
```

***

### Cancel Batch Order

**Contract:** BatchPurchaseFacilitator ([Basescan](https://basescan.org/address/0x01774B531591b286b9f02C6Bc02ab3fD9526Aa76))

**Function:** `cancelBatchOrder`

Cancel a pending batch order and receive a full refund.

**Parameters:** None (uses your connected wallet address)

**Before calling:**

1. [Connect your wallet](#getting-started)

***

## Read Functions

These view functions help you check state before making transactions:

| Function                       | Contract                 | Description                                                  |
| ------------------------------ | ------------------------ | ------------------------------------------------------------ |
| `currentDrawingId`             | Jackpot                  | Current drawing number                                       |
| `getDrawingState(drawingId)`   | Jackpot                  | Drawing details (prizePool, ticketPrice, bonusballMax, etc.) |
| `ticketPrice`                  | Jackpot                  | Current ticket price in USDC wei                             |
| `referralFees(address)`        | Jackpot                  | Claimable referral fees for an address                       |
| `getLpInfo(address)`           | JackpotLPManager         | LP position details (shares, deposits, withdrawals)          |
| `getLPValueBreakdown(address)` | JackpotLPManager         | USDC-denominated breakdown of LP position                    |
| `getSubscriptionInfo(address)` | JackpotAutoSubscription  | Subscription details                                         |
| `getBatchOrderInfo(address)`   | BatchPurchaseFacilitator | Batch order details                                          |

***

## Approving USDC

Many Megapot functions require you to approve USDC spending before calling them. This is a standard ERC-20 pattern that allows the contract to transfer USDC on your behalf.

**USDC on Base:** [`0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913`](https://basescan.org/address/0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913)

### How to Approve

1. Go to the [USDC contract on Basescan](https://basescan.org/address/0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913#writeProxyContract)
2. Click the **Write as Proxy** tab
3. Connect your wallet
4. Find the `approve` function
5. Enter the parameters:
   * `spender`: The Megapot contract address you want to interact with (see table below)
   * `value`: The amount in USDC wei (6 decimals)
6. Click **Write** and confirm the transaction

### Amount Examples

| USDC Amount | Value (wei)  |
| ----------- | ------------ |
| $1          | `1000000`    |
| $10         | `10000000`   |
| $100        | `100000000`  |
| $1,000      | `1000000000` |

### Which Contract to Approve

| Action              | Contract                 | Address                                      |
| ------------------- | ------------------------ | -------------------------------------------- |
| Buy tickets         | Jackpot                  | `0x3bAe643002069dBCbcd62B1A4eb4C4A397d042a2` |
| Buy random tickets  | JackpotRandomTicketBuyer | `0xb9560b43b91dE2c1DaF5dfbb76b2CFcDaFc13aBd` |
| Deposit to pool     | Jackpot                  | `0x3bAe643002069dBCbcd62B1A4eb4C4A397d042a2` |
| Create subscription | JackpotAutoSubscription  | `0x02A58B725116BA687D9356Eafe0fA771d58a37ac` |
| Create batch order  | BatchPurchaseFacilitator | `0x01774B531591b286b9f02C6Bc02ab3fD9526Aa76` |

***

## Transferring USDC from a Smart Contract Wallet

If you created a wallet through the Megapot app, you have a smart contract wallet. To transfer USDC out of this wallet using Basescan, you'll need to use the `execute` function on your wallet contract.

### Prerequisites

1. **Export your private key** from the Megapot app
2. **Import the key into a browser wallet** (e.g., MetaMask, Rabby, or Coinbase Wallet)
3. **Navigate to your smart contract wallet address** on Basescan (e.g., `https://basescan.org/address/YOUR_WALLET_ADDRESS`)

### Creating the Transfer Calldata

To transfer USDC, you need to encode the `transfer(address,uint256)` function call. You can use this tool to generate the calldata:

1. Go to [HashEx ABI Encoder](https://abi.hashex.org/)
2. Select **"Auto-parse"** and enter:

   ```json
   [{
      "name": "transfer",
      "type": "function",
      "inputs": [
         {
            "name": "to",
            "type": "address"
         },
         {
            "name": "amount",
            "type": "uint256"
         }
      ]
   }]
   ```

   ![](https://4144610446-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0Qe5CBrrfDdDUHNLiJQ9%2Fuploads%2Fgit-blob-a4518708a83997f3d3c7835e8009eb87a0b2f949%2Fexample-json.png?alt=media)
3. Click `Parse` and select `transfer(address,uint256)` from the drop down ![](https://4144610446-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0Qe5CBrrfDdDUHNLiJQ9%2Fuploads%2Fgit-blob-22798fc8ef6fa629197e775a01b68c9ecd9eeeee%2Fselect-dropdown.png?alt=media)
4. Fill in the parameters:
   * **address**: The destination wallet address (where you want to send USDC)
   * **uint256**: The amount in USDC wei (6 decimals, so `1000000` = $1)
5. Copy the resulting encoded data (it will start with `0xa9059cbb...`)

### Executing the Transfer

![Execute function on Basescan](https://4144610446-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0Qe5CBrrfDdDUHNLiJQ9%2Fuploads%2Fgit-blob-c7b31803c8bbb9704fcc15d7cd737d100851b952%2Fexecute-basescan.png?alt=media)

1. On your smart contract wallet's Basescan page, click **Contract** → **Write Contract as Proxy**
2. Connect your browser wallet (the one with your exported private key)
3. Find the `execute` function and enter:
   * **payableAmount**: `0` (USDC transfers don't require ETH)
   * **target**: `0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913` (USDC contract on Base)
   * **value**: `0`
   * **data**: The encoded calldata from the previous step
4. Click **Write** and confirm the transaction in your wallet

### Example: Transferring $100 USDC

To send $100 USDC to address `0x1234...5678`:

1. Generate calldata with:
   * address: `0x1234567890123456789012345678901234567890`
   * uint256: `100000000` (100 × 10^6)
2. The encoded data will look like:

   ```
   0xa9059cbb0000000000000000000000001234567890123456789012345678901234567890000000000000000000000000000000000000000000000000000000005f5e100
   ```
3. Use this as the `data` parameter in the `execute` function

***

## Need Help?

Ask our [Support Chat](https://megapot.io/support) if you have any questions about direct contract interactions.
