# Transaction

## 📖 Transaction Module

The `transaction` module in the **ono-web** package provides utilities to create, sign, calculate fees, and send transactions on the ONO blockchain network.

***

### 📦 Importing

To use the transaction module, import it as follows:

```javascript
const { transaction } = require('ono-web');
```

***

### 🚀 Functions

#### 1. `transaction.setCoreHost(url)`

Sets a custom **ONO core node host URL** for sending transactions. By default, the host is `http://core.ono.gg`.

**📥 Parameters**

| Name  | Type | Description                 |
| ----- | ---- | --------------------------- |
| `url` | URL  | URL object of the core host |

**📝 Example**

```javascript
transaction.setCoreHost(new URL('http://my-custom-node.com'));
```

***

#### 2. `transaction.calculateFee(amount)`

Calculates the **transaction fee** based on a percentage of the transaction amount.

* **Fee formula**: `0.0001%` of the transaction amount, but **never more than 0.01 ONO**.

**📥 Parameters**

| Name     | Type   | Description                          |
| -------- | ------ | ------------------------------------ |
| `amount` | number | Amount to be sent in the transaction |

**✅ Returns**

* `number` — Calculated transaction fee (ONO).

**📝 Example**

```javascript
const fee = transaction.calculateFee(100);
console.log(fee); // 0.01 (max fee cap if amount is large enough)
```

***

#### 3. `transaction.generateTransaction(to, amount, keyPair)`

Generates and signs a **new transaction object**.

**📥 Parameters**

| Name      | Type   | Description                                            |
| --------- | ------ | ------------------------------------------------------ |
| `to`      | string | Recipient's public key (address)                       |
| `amount`  | number | Amount of ONO coins to send                            |
| `keyPair` | object | Object with `publicKey` and `privateKey` of the sender |

**✅ Returns**

* `Transaction` — A signed transaction object ready to send.

**🔑 Transaction Object Fields**

| Field       | Type   | Description                            |
| ----------- | ------ | -------------------------------------- |
| `hash`      | string | Hash of the transaction                |
| `from`      | string | Sender's public key                    |
| `to`        | string | Recipient's public key                 |
| `amount`    | number | Amount of ONO coins being sent         |
| `fee`       | number | Transaction fee                        |
| `timestamp` | number | Unix timestamp of transaction creation |
| `signature` | string | Cryptographic signature of the hash    |

**📝 Example**

```javascript
const keyPair = {
    publicKey: 'sender-public-key-hex',
    privateKey: 'sender-private-key-hex'
};

const tx = transaction.generateTransaction('recipient-public-key-hex', 10, keyPair);
console.log(tx);
```

***

#### 4. `transaction.sendTransaction(transactionData)`

Sends a **signed transaction** to the ONO core network for processing.

**📥 Parameters**

| Name              | Type        | Description                 |
| ----------------- | ----------- | --------------------------- |
| `transactionData` | Transaction | A signed transaction object |

**✅ Returns**

* `Promise<Object>` — Response from the core server, typically includes success status or error.

**📝 Example**

```javascript
const response = await transaction.sendTransaction(tx);
console.log(response); // { success: true, ... }
```

***

### ⚙️ Constants

#### Transaction Fee Percent

* **0.0001% of transaction amount**
* **Maximum fee cap**: 0.01 ONO

***

### 📚 Types

#### `Transaction`

```typescript
{
  hash: string;
  from: string;
  to: string;
  amount: number;
  fee: number;
  timestamp: number;
  signature: string;
}
```

***

### 💡 Usage Example (Full Flow)

```javascript
const { wallet, transaction } = require('ono-web');

(async () => {
    // Generate new wallet
    const walletData = await wallet.newWalletData();
    const hd = wallet.hdWallet(walletData.seed);
    const keyPair = wallet.generateKeyPair(hd, 0);

    // Generate and send transaction
    const tx = transaction.generateTransaction('recipient-public-key-hex', 50, {
        publicKey: keyPair.publicKey.toString('hex'),
        privateKey: keyPair.privateKey.toString('hex')
    });

    const response = await transaction.sendTransaction(tx);
    console.log(response);
})();
```

***

### 🚨 Notes

* Ensure the sender's address has enough ONO balance to cover both the **amount** and **fee**.
* Transactions are **signed locally**, ensuring security of private keys.
* You can change the ONO core node address via `setCoreHost()` if you want to use your own node.
