> For the complete documentation index, see [llms.txt](https://tne.gitbook.io/tne-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tne.gitbook.io/tne-docs/developers/creating-custom-transaction-checks.md).

# Creating Custom Transaction Checks

## Creating Custom Transaction Checks for TheNewEconomy

Transaction checks in **TheNewEconomy (TNE)** are used to validate and enforce rules during economic transactions. These checks ensure transactions adhere to server rules, such as ensuring balances don't exceed limits or items meet specific criteria.

***

### Overview

#### What is a Transaction Check?

A **Transaction Check** is a validation step that runs during a transaction. It ensures that the transaction is valid based on predefined rules.

#### Registration

Custom transaction checks must be registered with the transaction manager:

```java
TNECore.eco().transaction().addCheck(new CustomTransactionCheck());
TNECore.eco().transaction().addCheck(new CustomTransactionCheck(), "custom_group");
```

***

### Steps to Create a Custom Transaction Check

#### Step 1: Implement the `TransactionCheck` Interface

Create a class that implements the `TransactionCheck` interface. Below is an example of a custom check that prevents transactions below a minimum amount.

```java
import net.tnemc.core.transaction.TransactionCheck;
import net.tnemc.core.transaction.Transaction;
import net.tnemc.core.transaction.TransactionParticipant;
import net.tnemc.core.account.holdings.modify.HoldingsModifier;
import net.tnemc.core.actions.EconomyResponse;
import net.tnemc.core.actions.response.GeneralResponse;
import org.jetbrains.annotations.NotNull;

import java.math.BigDecimal;

public class CustomMinimumAmountCheck implements TransactionCheck {

  @Override
  public String identifier() {
    return "min_amount";
  }

  @Override
  public EconomyResponse checkParticipant(Transaction transaction, @NotNull TransactionParticipant participant, HoldingsModifier modifier) {
    if (modifier.getAmount().compareTo(new BigDecimal("10.00")) < 0) {
      return new EconomyResponse(false, "Transaction amount must be at least 10.00!");
    }
    return GeneralResponse.SUCCESS;
  }
}
```

***

### Step 2: Register the Custom Check

After creating your check, register it with TNE's transaction manager:

```java
TNECore.eco().transaction().addCheck(new CustomMinimumAmountCheck());
```

You can also group checks under a custom group identifier for better organization:

```java
TNECore.eco().transaction().addCheck(new CustomMinimumAmountCheck(), "custom_group");
```

***

### Example: Default Checks in TNE

#### **MinimumBalanceCheck**

Ensures the account balance does not go below a minimum threshold.

```java
@Override
public String identifier() {
  return "minbal";
}

@Override
public EconomyResponse checkParticipant(Transaction transaction, @NotNull TransactionParticipant participant, HoldingsModifier modifier) {
  if (participant.getCombinedEnding().compareTo(currency.getMinBalance()) < 0) {
    return HoldingsResponse.MIN_HOLDINGS;
  }
  return GeneralResponse.SUCCESS;
}
```

#### **MaximumBalanceCheck**

Prevents transactions that would exceed the maximum allowed balance.

```java
@Override
public String identifier() {
  return "maxbal";
}

@Override
public EconomyResponse checkParticipant(Transaction transaction, @NotNull TransactionParticipant participant, HoldingsModifier modifier) {
  if (participant.getCombinedEnding().compareTo(currency.getMaxBalance()) > 0) {
    return HoldingsResponse.MAX_HOLDINGS;
  }
  return GeneralResponse.SUCCESS;
}
```

***

### Step 3: Grouping Checks

You can group checks using `TransactionCheckGroup` for easy management of related validations:

```java
TransactionCheckGroup customGroup = new TransactionCheckGroup("custom_group");
customGroup.addCheck("min_amount");
TNECore.eco().transaction().addCheckGroup(customGroup);
```

***

### Key Methods in `TransactionCheck`

| Method               | Description                                                                            |
| -------------------- | -------------------------------------------------------------------------------------- |
| `identifier()`       | Returns the unique identifier for the check.                                           |
| `checkParticipant()` | Validates the transaction for a specific participant and returns an `EconomyResponse`. |
| `process()`          | Executes the check for all participants in the transaction.                            |

***

### Notes

* **Unique Identifier**: Each check must have a unique identifier returned by `identifier()`.
* **Comprehensive Validation**: Use `checkParticipant()` to enforce custom rules for each transaction participant.
* **Organized Checks**: Use groups to organize and manage multiple related checks.

***

By creating and registering custom transaction checks, you can enforce complex and unique economic rules tailored to your server's needs.


---

# 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://tne.gitbook.io/tne-docs/developers/creating-custom-transaction-checks.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.
