# Rewarder Module

### Data Structures

#### RewarderManager

The main structure that manages all rewarders for a specific pair.

```rust
struct RewarderManager has store {
    rewarders: vector<Rewarder>,
    last_update_timestamp: u64,
}
```

**Fields:**

* `rewarders`: Vector containing up to 5 rewarder configurations
* `last_update_timestamp`: Timestamp of the last reward calculation update

#### Rewarder

Individual rewarder configuration for a specific token type.

```rust
struct Rewarder has copy, drop, store {
    emission_per_ms: u128,
    reward_coin: TypeName
}
```

**Fields:**

* `emission_per_ms`: Rate of token emission per millisecond
* `reward_coin`: Type name of the reward token

#### RewarderGlobalVault

Global storage for all reward token balances across the system.

```rust
struct RewarderGlobalVault has key, store {
    id: UID,
    balances: Bag,
}
```

**Fields:**

* `id`: Unique identifier for the vault
* `balances`: Bag containing balance objects for each token type

***

### Core Functions

#### Initialization

**`new()`**

Creates a new RewarderManager instance.

```rust
public(friend) fun new(): RewarderManager
```

**Returns:** Empty RewarderManager with no rewarders configured

**`add_rewarder<RewardCoin>()`**

Adds a new rewarder for a specific reward token type.

```rust
public(friend) fun add_rewarder<RewardCoin>(
    manager: &mut RewarderManager
)
```

**Parameters:**

* `manager`: Mutable reference to RewarderManager
* `RewardCoin`: Type parameter for the reward token

**Validations:**

* Maximum 5 rewarders per pair
* No duplicate rewarders for the same token type

#### Reward Calculation

**`calculate_reward_emission()`**

Calculates and updates reward emissions based on elapsed time.

```rust
public(friend) fun calculate_reward_emission(
    manager: &mut RewarderManager,
    clock: &Clock
): vector<u128>
```

**Parameters:**

* `manager`: Mutable reference to RewarderManager
* `clock`: Clock object for timestamp access

**Returns:** Vector of reward amounts for each rewarder

**Logic:**

```rust
reward_emission = emission_per_ms × (current_time - last_update_time)
```

**`simulate_reward_emission()`**

Simulates reward calculation without updating the manager state.

```rust
public fun simulate_reward_emission(
    manager: &RewarderManager,
    clock: &Clock
): vector<u128>
```

**Use Case:** Preview reward amounts before actual distribution

***

### Reward Management

#### Deposit Rewards

**`deposit_reward<RewardCoin>()`**

Deposits reward tokens into the global vault.

```rust
public fun deposit_reward<RewardCoin>(
    global_config: &GlobalConfig,
    vault: &mut RewarderGlobalVault,
    reward: Balance<RewardCoin>
)
```

**Parameters:**

* `global_config`: Global configuration for access control
* `vault`: Mutable reference to global vault
* `reward`: Balance object containing tokens to deposit

**Process:**

1. Validate package version
2. Create balance entry if doesn't exist
3. Join deposited balance with existing balance
4. Emit deposit event

#### Withdraw Rewards

**`withdraw_reward<RewardCoin>()`**

Withdraws reward tokens from the global vault.

```rust
public(friend) fun withdraw_reward<RewardCoin>(
    vault: &mut RewarderGlobalVault,
    amount: u64,
): Balance<RewardCoin>
```

**Parameters:**

* `vault`: Mutable reference to global vault
* `amount`: Amount to withdraw

**Validations:**

* Rewarder must exist for the token type
* Sufficient balance must be available

#### Emergency Withdrawal

**`emergent_withdraw<CoinType>()`**

Emergency withdrawal function for administrators.

```rust
public fun emergent_withdraw<CoinType>(
    global_config: &GlobalConfig,
    vault: &mut RewarderGlobalVault,
    amount: u64,
    ctx: &mut TxContext
)
```

**Access Control:**

* Only pool managers can execute
* Withdrawn funds sent to designated receiver

#### Error Codes

| Code | Constant                    | Description                        |
| ---- | --------------------------- | ---------------------------------- |
| 3000 | `E_TOO_MANY_REWARDERS`      | Exceeds maximum rewarders per pair |
| 3001 | `E_REWARDER_ALREADY_EXISTS` | Duplicate rewarder for token type  |
| 3003 | `E_INSUFFICIENT_BALANCE`    | Insufficient vault balance         |
| 3004 | `E_REWARDER_NOT_FOUND`      | Rewarder not found for token type  |

### Events

#### Key Events

**RewardEmissionUpdateEvent**

```rust
struct RewardEmissionUpdateEvent has copy, drop {
    pair: ID,
    reward_type: TypeName,
    old_emission: u128,
    new_emission: u128,
}
```

Emitted when emission rates are updated.

**DepositEvent**

```rust
struct DepositEvent has copy, drop {
    vault: ID,
    reward_type: TypeName,
    amount: u64,
    total_balance: u64,
}
```

Emitted when tokens are deposited into the vault.

**WithdrawEvent**

```rust
struct WithdrawEvent has copy, drop {
    vault: ID,
    reward_type: TypeName,
    amount: u64,
    remaining_balance: u64,
}
```

Emitted when tokens are withdrawn from the vault.
