> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rndrntwrk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# AGG

> The routing layer for value movement

# The Routing Layer

<Info>
  **Status:** Partially implemented. Solana settlement via Jupiter is live. Cross-chain settlement (the sw4p engine) and gasless transactions (Kora) are planned.
</Info>

**AGG (Aggregator)** is the routing layer for value movement across the RNDRNTWRK. It abstracts away the complexity of blockchain payments, allowing users to pay with any token on any chain.

## Routing Architecture

AGG functions like a DEX aggregator (Jupiter) but for **payments**, not just swaps. It solves the "n-to-1" problem: Users have `n` tokens on `m` chains, but the Merchant wants `1` specific token on `1` chain.

### 1. The 402 Handshake

The protocol begins when a client hits a gated resource.

**Server Response (402 Payment Required)**

```http theme={null}
HTTP/1.1 402 Payment Required
WWW-Authenticate: 555x402 realm="sector-13",
  amount="5.00",
  token="USDC",
  chain="solana",
  recipient="HuW...9zP"
```

### 2. Pathfinding

The AGG Client (SDK) calculates the optimal route based on the user's wallet state.

**Factors:**

* **Gas Cost**: Solana (less than \$0.001) vs Base vs Polygon.
* **Swap Slippage**: If paying with SOL to settle in USDC (routed via Jupiter).
* **Bridge Time**: If paying from Base to settle on Solana (via the sw4p engine).

**Example Route:**
`User (Base/ETH) -> Uniswap (USDC) -> the sw4p engine Bridge -> Solana (USDC) -> Merchant`

### 3. Solana Settlement (v0 + LUTs)

On Solana, AGG uses **Versioned Transactions (v0)** and **Address Lookup Tables (LUTs)** to pack complex logic into a single atomic transaction with sub-2-second finality.

**Transaction Structure:**

1. **Swap Instruction**: Jupiter CPI to convert User Token -> USDC.
2. **Fee Instruction**: Transfer platform fee to Treasury.
3. **Payment Instruction**: Transfer remaining amount to Merchant.
4. **Memo Instruction**: Attach `orderId` for reconciliation.

```rust theme={null}
pub fn settle_payment(ctx: Context<Settle>, amount: u64) -> Result<()> {
    token::transfer(ctx.accounts.user, ctx.accounts.merchant, amount)?;
    emit!(PaymentSettled { id: ctx.accounts.order.id });
}
```

## Fee Structure

AGG enforces a transparent fee model:

| Fee Type         | Rate     | Payer    | Description                                       |
| :--------------- | :------- | :------- | :------------------------------------------------ |
| **Platform Fee** | 0.5–1.0% | Merchant | Deducted from the settlement amount.              |
| **Gas Fee**      | Variable | User     | Network costs (gasless via Kora where supported). |

## Cross-Chain Settlement

For cross-chain payments (e.g., Base -> Solana), AGG utilizes **the sw4p engine** (Cross-Chain Transfer Protocol).

1. **Burn (Source)**: User burns USDC on source chain.
2. **Attest**: the engine observes and verifies the burn attestation.
3. **Mint (Destination)**: USDC minted on destination chain and forwarded to Merchant.

### Integrations

* **Jupiter**: Optimal swap routing on Solana.
* **Kora**: Gasless transaction sponsorship, users don't need SOL for gas.
* **the sw4p engine**: Non-custodial cross-chain USDC transfers.
