# Collect Yield

### Rewards Management

#### `getPositionRewards(pair, positionId, binIds)`

Calculate pending reward amounts for a position across all rewarders.

**Parameters:**

* `pair: LBPair` - The LBPair containing the position
* `positionId: string` - ID of the position to check rewards for
* `binIds: number[]` - Array of bin IDs to calculate rewards for

**Returns:** `Promise<PositionReward[]>`

**Example:**

```typescript
const rewards = await ferraSDK.Position.getPositionRewards(pair, "0x123...", [1, 2, 3]);
rewards.forEach(reward => {
  console.log(`Pending ${reward.amount} of ${reward.coinType}`);
});
```

#### `getPositionRewardsV2(pair, positionId, binIds)`

Updated version of rewards calculation with improved performance.

**Parameters:** Same as V1 **Returns:** `Promise<PositionReward[]>`

#### `claimPositionRewards(pair, positionId, binIds, tx?)`

Claim all pending rewards for a position and transfer to sender.

**Parameters:**

* `pair: LBPair` - The LBPair containing the position
* `positionId: string` - ID of the position to claim rewards for
* `binIds: number[]` - Array of bin IDs to claim rewards from
* `tx?: Transaction` - Optional existing transaction to add operations to

**Returns:** `Promise<Transaction>`

**Example:**

```typescript
const tx = await ferraSDK.Position.claimPositionRewards(pair, "0x123...", binIds);
// All pending rewards will be transferred to sender
```

#### `claimPositionRewardsV2(pair, positionId, binIds, tx?)`

Updated version of reward claiming with optimized transaction structure.

**Parameters:** Same as V1 **Returns:** `Promise<Transaction>`

### Fees Management

#### `getPositionFees(pair, positionId, binIds)`

Calculate pending fee amounts for specific bins of a position.

**Parameters:**

* `pair: LBPair` - The LBPair containing the position
* `positionId: string` - ID of the position to check fees for
* `binIds: number[]` - Array of bin IDs to calculate fees for

**Returns:** `Promise<[PositionReward, PositionReward] | null>`

Returns a tuple of `[tokenX fees, tokenY fees]` or `null` if no fees found.

**Example:**

```typescript
const fees = await ferraSDK.Position.getPositionFees(pair, "0x123...", [8388608, 8388609]);
if (fees) {
  console.log(`Fees: ${fees[0].amount} tokenX, ${fees[1].amount} tokenY`);
}
```

#### `getPositionFeesV2(pair, positionId, binIds)`

Updated version of fee calculation with improved accuracy.

**Parameters:** Same as V1 **Returns:** `Promise<[PositionReward, PositionReward] | null>`

#### `claimPositionFee(pair, positionId, binIds, tx?)`

Claim accumulated fees for specific bins of a position.

**Parameters:**

* `pair: LBPair` - The LBPair containing the position
* `positionId: string` - ID of the position to claim fees for
* `binIds: number[]` - Array of bin IDs to claim fees from
* `tx?: Transaction` - Optional existing transaction to add operations to

**Returns:** `Promise<Transaction>`

**Example:**

```typescript
const binIds = [8388608, 8388609, 8388610];
const tx = await ferraSDK.Position.claimPositionFee(pair, "0x123...", binIds);
// Fees from specified bins will be transferred to sender
```

#### `claimPositionFeeV2(pair, positionId, binIds, tx?)`

Updated version of fee claiming with optimized batching.

**Parameters:** Same as V1 **Returns:** `Promise<Transaction>`

### Data Types

#### PositionReward

```typescript
interface PositionReward {
  amount: bigint;    // Amount of tokens
  coinType: string;  // Token type identifier
}
```

### V2 Methods

The module provides V2 versions of key methods that offer:

* **Improved Performance**: Optimized transaction structures
* **Better Accuracy**: Enhanced calculation algorithms
* **Gas Optimization**: Reduced transaction costs

**When to use V2:**

* For new integrations, prefer V2 methods
* V2 methods are backward compatible
* Better suited for high-volume operations

### Transaction Batching

The module implements intelligent batching for large operations:

```typescript
const BATCH_SIZE = 1000; // Process up to 1000 bins per transaction
```

This prevents transaction size limits and optimizes gas usage.

### Error Handling

#### Common Errors

1. **"Invalid sender address"**: SDK sender address not configured
2. **"Position not found"**: Position ID doesn't exist
3. **"Position is not match with pair id"**: Position doesn't belong to specified pair

### Performance Optimization

#### Caching Strategy

The module uses internal caching to optimize performance:

```typescript
private readonly _cache: Record<string, CachedContent> = {}
```

#### RPC Batching

Uses `RpcBatcher` for efficient data fetching:

```typescript
const batcher = new RpcBatcher(async () => {
  // Batch multiple RPC calls together
});
```

### Troubleshooting

#### Debug Checklist

1. **Verify sender address is set**: `sdk.senderAddress`
2. **Check position exists**: Use `getLbPosition(positionId)`
3. **Validate bin IDs**: Ensure bins belong to the position
4. **Test with small amounts first**: Avoid large transaction failures

#### Common Solutions

* **Gas estimation failures**: Use V2 methods for better optimization
* **Empty rewards**: Position may not have accumulated rewards yet
* **Transaction timeouts**: Reduce batch sizes or split operations
