# Tickets

## Introduction

Tickets on the XRPL allow you to reserve sequence numbers for transactions, enabling asynchronous transaction submission. This feature is particularly useful in multi-signature setups, where sequence management can become challenging due to independent signers.

***

## Tickets Explained

### **What Are Tickets?**

Tickets are a feature on the XRP Ledger that allow you to preallocate sequence numbers for future transactions. This helps avoid sequence number conflicts, especially in scenarios where multiple parties (like signers in a MultiSignature setup) need to sign a transaction.

### **Why Use Tickets?**

In standard transactions, the Sequence field determines the order of execution. When signers asynchronously sign transactions, the sequence number may become outdated. Tickets provide a solution by reserving sequence numbers in advance, ensuring transactions can proceed without conflict.

### **How Tickets Work**

Tickets are created using the TicketCreate transaction. Once created, these tickets can be used in place of the Sequence field in any subsequent transaction.

***

## Advantages of Tickets

1. Avoiding Sequence Conflicts

   Tickets prevent sequence number mismatches in multi-signature transactions, where signers may not act in a synchronized manner.
2. Streamlining MultiSignature

   By reserving sequence numbers, Tickets simplify the signing and submission process in MultiSignature environments.
3. Flexibility for Transaction Management

   Tickets can be used to prioritize certain transactions or reserve future transaction slots, offering better control over transaction flow.

## Ticket Setup

### Prerequisites

To create a Ticket on the XRPL, your account must hold sufficient XRP to meet the reserve requirements for the associated account object. <https://xrpl.org/docs/concepts/accounts/reserves#owner-reserves>

### Create Wallet

```javascript
import { Client, Wallet } from "xrpl";

const client = new Client("wss://s.altnet.rippletest.net:51233");
await client.connect();

const wallet = Wallet.fromSeed("s...");
```

### Retrieve Tickets

You can query your account to retrieve active tickets.

```javascript
const accountObjects = await client.request({
   command: "account_objects",
   account: wallet.classicAddress,
   type: "ticket"
});

console.log('Active Tickets:', accountObjects.result.account_objects);
// tickets [
//   {
//     Account: 'rHH6GByFtaKXXAEeue4myzFuq4ftAZW9un',
//     TicketSequence: 2856829
//   }
// ]
```

### Using Tickets in Transactions

Once you have created tickets, you can use them in subsequent transactions by specifying the TicketSequence field instead of Sequence.

```javascript
const ticketSequence = accountObjects.result.account_objects[0].TicketSequence;

const transaction = {
   TransactionType: "Payment",
   Account: wallet.classicAddress,
   Destination: "rrrrrrrrrrrrrrrrrNAMEtxvNvQ",
   Amount: '1000000', // 1 XRP in drops
   Sequence: 0,
   TicketSequence: ticketSequence // Use the ticket's sequence number
};

try {
   const result = await client.submit(transaction, { autofill: true, failHard: true, wallet });
   console.log('Transaction Submit Result:', result);
} catch (error) {
   console.error('Failed to submit transaction:', error);
}
```

### Optional Remove a Ticket

Deleting a Ticket is sometimes required. To free up the associated Account Object. The way to do this is generate a no-op transaction

```javascript
const ticketToDelete = accountObjects.result.account_objects[0].TicketSequence;

const noopTransaction = {
   TransactionType: "AccountSet",
   Account: wallet.classicAddress,
   TicketSequence: ticketToDelete
};

try {
   const result = await client.submit(noopTransaction, { autofill: true, failHard: true, wallet });
   console.log('No-Op Transaction Result:', result);
} catch (error) {
   console.error('Failed to delete ticket:', error);
}
```

### Explanation of the Script

1. Ticket Creation
   * The TicketCreate transaction reserves sequence numbers for future transactions.
   * Specify the number of tickets you want to create using the TicketCount field.
2. Querying Tickets
   * Use the account\_objects command to fetch active tickets linked to your account.
3. Using Tickets
   * Include the TicketSequence field in your transaction to use a ticket instead of a regular sequence number.

### Key Considerations

#### **Cost of Tickets**

Each ticket costs 2 XRP to reserve. Ensure you have sufficient XRP balance in your account before creating tickets.

#### **Ticket Expiry**

Tickets remain valid until used or until the account’s Sequence surpasses the ticket’s TicketSequence.

#### **Managing Tickets**

Efficiently manage tickets to avoid unnecessary fees and ensure smooth transaction workflows.

#### **Combining Tickets with MultiSign**

Tickets are particularly effective in MultiSignature setups, preventing sequence mismatches and allowing independent signers to collaborate seamlessly.

With the Ticket feature, XRPL provides a powerful mechanism to manage transaction order and avoid sequence conflicts, especially in asynchronous environments like MultiSignature. By leveraging Tickets, you can ensure smooth and conflict-free transaction workflows.


---

# Agent Instructions: 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:

```
GET https://docs.xrpl-commons.org/xrpl-basics/tickets.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
