> For the complete documentation index, see [llms.txt](https://perfectpool.gitbook.io/perfect-pool/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://perfectpool.gitbook.io/perfect-pool/game-contracts/ace-the-brackets-16.md).

# Ace the Brackets 16

This contract mints ERC-721 NFTs that represent bets on bracket tournament games run by the AceTheBrackets16 contract.

When a user wants to place a bet, they call the `safeMint` function and pass in the game ID and their bracket predictions. The contract checks that the predictions are valid. It collects the bet amount from the user, takes a protocol fee of 10% of the bet value, and adds the rest to the pot for that particular bet combination.

A bet is represented by a unique code derived from the game ID and bracket predictions. Bets that match the final game outcome split that pot evenly.

After a game finishes, users can call `claimTokens` to withdraw any winnings their NFT earned based on its associated bet. The contract checks the game status, winning bracket, and bet amount already claimed before sending winnings.

The NFT metadata is generated on-chain and contains the bet details. This allows wallets and interfaces to show the historical bet information tied to each NFT.

Some additional features:

* Game pot expires every 30 days by default. The number of days is defined in the AceTheBrackets16 contract.
* Each one of the 4 rounds lasts 10 minutes, with 10 initial minutes to bet. The game lasts 50 minutes from the betting period to end.
* The expired pot gets split between the treasury and the jackpot pot.
* Admins can change the betting token, ticket price, and add jackpot amount.
* Bet validation functions check the current status of each token on the bet.
* If no player gets the pot, 10% of the actual game pot goes to the players with the highest score.

#### Base Sepolia Contract:

<https://sepolia.basescan.org/address/0x899A9bD928cc2A2912d0B407fabD0642784511D1>

#### Base Contract:

<https://basescan.org/address/0x70A254c8201adbD88d88D17937d5e8aBb8B8095F>

### **Events**

* **BetPlaced**: Emitted when a bet is placed.
  * `_player`: `address`
  * `_gameId`: `uint256`
  * `_tokenId`: `uint256`
  * `_betCode`: `bytes32`
* **GamePotPlaced**: Emitted when the game pot is placed.
  * `_gameId`: `uint256`
  * `_pot`: `uint256`
* **GamePotDismissed**: Emitted when the game pot is dismissed.
  * `_gameId`: `uint256`
  * `_amount`: `uint256`
* **NoWinners**: Emitted when there are no winners for a game.
  * `_gameId`: `uint256`
* **PrizeClaimed**: Emitted when a prize is claimed.
  * `_tokenId`: `uint256`
  * `_amount`: `uint256`
* **PriceChanged**: Emitted when the ticket price is changed.
  * `_newPrice`: `uint256`
* **ProtocolFeeChanged**: Emitted when the protocol fee is changed.
  * `_newFee`: `uint8`
* **IterateGameData**: Emitted when game data is iterated.
  * `_gameId`: `uint256`
  * `_iterateStart`: `uint256`
  * `_iterateEnd`: `uint256`
* **IterationFinished**: Emitted when iteration is finished.
  * `_gameId`: `uint256`
* **GamePotDecided**: Emitted when the game pot is decided.
  * `_gameId`: `uint256`

### Structs

* **GameData**: Struct to store game data.
  * `tokenIds`: `uint256[]`
  * `iterateStart`: `uint256`
  * `pot`: `uint256`
  * `consolationPot`: `uint256`
  * `consolationPotClaimed`: `uint256`
  * `consolationWinners`: `mapping(uint8 => uint256[])`
  * `consolationPoints`: `uint8`
  * `potDismissed`: `bool`

### Constants

The note does not mention any constants.

### State Variables

* **\_nextTokenId**: `uint256 private` The next token ID to be minted.
* **gamesHub**: `IGamesHub public` The GamesHub contract.
* **token**: `IERC20 public` The ERC20 token used for payments.
* **jackpot**: `uint256 public` The jackpot amount.
* **price**: `uint256 public` The price of a ticket.
* **iterationSize**: `uint256 public` The size of each iteration.
* **consolationPerc**: `uint8 public` The percentage for consolation prizes.
* **protocolFee**: `uint8 public` The protocol fee percentage.
* **executionAddress**: `address public` The address authorized to execute certain functions.

### Modifiers

* `onlyAdmin()`: Ensures the caller is an admin.
* `onlyGameContract()`: Ensures the caller is the game contract.
* `onlyExecutor()`: Ensures the caller is the executor.

### Functions

#### Constructor

* Description: Initializes contract parameters.
* Arguments:
  * `_gamesHub`: `address` The address of the GamesHub contract.
  * `_executionAddress`: `address` The address of the Chainlink forwarder.
  * `_token`: `address` The address of the ERC20 token.

#### setExecutionAddress

* Description: Sets the forwarder address.
* Arguments:
  * `_executionAddress`: `address` The address of the Chainlink forwarder.
* Modifiers:
  * `onlyAdmin`: Ensures the caller is an admin.

#### changePrice

* Description: Changes the price of the ticket.
* Arguments:
  * `_newPrice`: `uint256` The new price of the ticket.
* Modifiers:
  * `onlyAdmin`: Ensures the caller is an admin.

#### changeConsolationPerc

* Description: Changes the consolation percentage.
* Arguments:
  * `_newPerc`: `uint8` The new consolation percentage.
* Modifiers:
  * `onlyAdmin`: Ensures the caller is an admin.

#### changeGamesHub

* Description: Changes the GamesHub contract address.
* Arguments:
  * `_gamesHub`: `address` The address of the new GamesHub contract.
* Modifiers:
  * `onlyAdmin`: Ensures the caller is an admin.

#### safeMint

* Description: Mints a new ticket and places a bet.
* Arguments:
  * `_gameId`: `uint256` The ID of the game to bet on.
  * `bets`: `uint256[15]` The array of bets for the game.

#### claimTokens

* Description: Claims the tokens won by a ticket. Only callable by the owner of the ticket.
* Arguments:
  * `_tokenId`: `uint256` The ID of the ticket to claim tokens from.
* Modifiers:
  * `nonReentrant`: Prevents reentrancy attacks.

#### claimAll

* Description: Claims all tokens on the input array. Iterates through the array, sums the amount to claim, and claims it. It skips the tokens where the amount to claim is 0.
* Arguments:
  * `_tokenIds`: `uint256[]` The array of token IDs to claim tokens from.
* Modifiers:
  * `nonReentrant`: Prevents reentrancy attacks.

#### setGamePot

* Description: Sets the game pot for a specific game. Only callable by the game contract.
* Arguments:
  * `_gameId`: `uint256` The ID of the game to set the pot for.
  * `betCode`: `bytes32` The bet code for the game.
* Modifiers:
  * `onlyGameContract`: Ensures the caller is the game contract.

#### iterateGameTokenIds

* Description: Iterates the game token IDs for a specific game. Only callable by the executor.
* Arguments:
  * `_gameId`: `uint256` The ID of the game to iterate the token IDs for.
  * `_iterateStart`: `uint256` The start iteration position.
  * `_iterateEnd`: `uint256` The end iteration position.
* Modifiers:
  * `onlyExecutor`: Ensures the caller is the executor.

#### changeIterationSize

* Description: Changes the iteration size. Only callable by the admin.
* Arguments:
  * `_newSize`: `uint256` The new iteration size.
* Modifiers:
  * `onlyAdmin`: Ensures the caller is an admin.

#### dismissGamePot

* Description: Dismisses the game pot for a specific game. Only callable by the executor.
* Arguments:
  * `_gameId`: `uint256` The ID of the game to dismiss the pot for.
  * `betCode`: `bytes32` The bet code for the game.
* Modifiers:
  * `onlyExecutor`: Ensures the caller is the executor.

#### increaseJackpot

* Description: Increases the pot by a certain amount. Only callable by the admin.
* Arguments:
  * `_amount`: `uint256` The amount to increase the pot by.
* Modifiers:
  * `onlyAdmin`: Ensures the caller is an admin.

#### tokenURI

* Description: Gets the token URI for a specific token.
* Arguments:
  * `_tokenId`: `uint256` The ID of the token.
* Returns:
  * `string`: The token URI.

#### getBetData

* Description: Gets the bet data for a specific token.
* Arguments:
  * `_tokenId`: `uint256` The ID of the token.
* Returns:
  * `uint256[15]`: The array of bets for the token.

#### getGameId

* Description: Gets the game ID for a specific token.
* Arguments:
  * `_tokenId`: `uint256` The ID of the token.
* Returns:
  * `uint256`: The ID of the game the token is betting on.

#### betValidator

* Description: Validates the bets for a specific token.
* Arguments:
  * `_tokenId`: `uint256` The ID of the token.
* Returns:
  * `uint8[15]`: The array of validation results for the bets.

#### betWinQty

* Description: Gets the quantity of winning bets for a specific token.
* Arguments:
  * `_tokenId`: `uint256` The ID of the token.
* Returns:
  * `uint8`: The quantity of winning bets for the token.

#### getTokenSymbols

* Description: Gets the symbols for the tokens bet on a specific token.
* Arguments:
  * `_tokenId`: `uint256` The ID of the token.
* Returns:
  * `string[15]`: The array of token symbols.

#### amountPrizeClaimed

* Description: Gets the amount to claim and the amount claimed for a specific token.
* Arguments:
  * `_tokenId`: `uint256` The ID of the token.
* Returns:
  * `uint256 amountToClaim`: The amount of tokens to claim.
  * `uint256 amountClaimed`: The amount of tokens already claimed.

#### potentialPayout

* Description: Gets the potential payout for a specific game.
* Arguments:
  * `gameId`: `uint256` The ID of the game.
* Returns:
  * `uint256 payout`: The potential payout amount.
  * `uint256 consolationPayout`: The potential consolation payout amount.

#### playerQuantity

* Description: Gets the quantity of players for a specific game.
* Arguments:
  * `gameId`: `uint256` The ID of the game.
* Returns:
  * `uint256`: The quantity of players for the game.

#### getGamePlayers

* Description: Gets the token IDs for a specific game.
* Arguments:
  * `gameId`: `uint256` The ID of the game.
* Returns:
  * `uint256[]`: The array of token IDs for the game.

#### getBetCodeToTokenIds

* Description: Gets the token IDs for a specific bet code.
* Arguments:
  * `betCode`: `bytes32` The bet code to get the token IDs for.
* Returns:
  * `uint256[]`: The array of token IDs for the bet code.

#### getGameWinners

* Description: Gets the token IDs for a specific game ID.
* Arguments:
  * `gameId`: `uint256` The ID of the game.
* Returns:
  * `uint256[]`: The array of token IDs for the game ID.

#### getGameConsolationData

* Description: Gets the game consolation prize data for a specific game.
* Arguments:
  * `gameId`: `uint256` The ID of the game.
* Returns:
  * `uint256[] consolationWinners`: The array of consolation winners.
  * `uint8 consolationPoints`: The points for the consolation winners.

#### getPotStatus

* Description: Gets the pot status for the pot of a specific game.
* Arguments:
  * `_gameId`: `uint256` The ID of the game.
* Returns:
  * `bool`: The status of the pot. If true, the pot iteration is finished and it can be claimed.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://perfectpool.gitbook.io/perfect-pool/game-contracts/ace-the-brackets-16.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
