# Creating Custom Transaction Type

## Creating a Custom Transaction Type for TheNewEconomy

Transaction types in **TheNewEconomy (TNE)** define the behavior of different financial operations, such as withdrawals, deposits, or currency conversions. This guide explains how to implement and register a custom transaction type.

***

### Overview

#### What is a Transaction Type?

A **Transaction Type** represents a specific operation within TNE's economy system. It specifies:

1. **Identifier**: A unique name for the transaction type.
2. **Taxation Rules**: How taxes are applied to the sender and recipient.

#### Registration

To use a custom transaction type, register it with TNE's transaction manager:

```java
TNECore.eco().transaction().addType(new CustomTransactionType());
```

***

### Steps to Create a Custom Transaction Type

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

Create a class that implements the `TransactionType` interface. Below is an example of a custom type called `CustomFeeType`.

```java
import net.tnemc.core.transaction.TransactionType;
import net.tnemc.core.transaction.tax.TaxEntry;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;

public class CustomFeeType implements TransactionType {

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

  @Override
  public Optional<TaxEntry> toTax() {
    // Define taxes applied to the recipient
    return Optional.of(new TaxEntry("flat", 5.00));
  }

  @Override
  public Optional<TaxEntry> fromTax() {
    // Define taxes applied to the sender
    return Optional.of(new TaxEntry("percent", 0.02)); // 2% fee
  }
}
```

***

#### Step 2: Register the Custom Transaction Type

To register your transaction type, use the following code:

```java
TNECore.eco().transaction().addType(new CustomFeeType());
```

***

### Examples of Default Transaction Types

#### **DepositType**

Handles deposits into accounts:

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

@Override
public Optional<TaxEntry> toTax() {
  return Optional.empty(); // No tax for recipients
}

@Override
public Optional<TaxEntry> fromTax() {
  // Define tax for depositors if enabled
  return Optional.of(new TaxEntry("flat", 2.00));
}
```

#### **WithdrawType**

Handles withdrawals from accounts:

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

@Override
public Optional<TaxEntry> toTax() {
  return Optional.empty(); // No tax for recipients
}

@Override
public Optional<TaxEntry> fromTax() {
  // Define tax for withdrawers if enabled
  return Optional.of(new TaxEntry("percent", 0.05)); // 5% fee
}
```

#### **PayType**

Handles payments between players:

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

@Override
public Optional<TaxEntry> toTax() {
  return Optional.of(new TaxEntry("flat", 1.00)); // Flat fee for recipients
}

@Override
public Optional<TaxEntry> fromTax() {
  return Optional.of(new TaxEntry("percent", 0.01)); // 1% fee for senders
}
```

***

### Key Methods in `TransactionType`

| Method         | Description                                                    |
| -------------- | -------------------------------------------------------------- |
| `identifier()` | Returns the unique identifier for the transaction type.        |
| `toTax()`      | Specifies the tax applied to the recipient of the transaction. |
| `fromTax()`    | Specifies the tax applied to the sender of the transaction.    |

***

### Notes

* **Unique Identifier**: Ensure your transaction type has a unique identifier using the `identifier()` method.
* **Tax Customization**: Use `toTax()` and `fromTax()` to define tax rules specific to your transaction type.
* **Registration**: Always register your custom type with `TNECore.eco().transaction().addType()`.

***

By implementing and registering custom transaction types, you can introduce tailored financial operations to enhance your server's economy system.
