# Why Two Phases?

Your cToken balance is encrypted as `euint64`. The wrapper contract needs to know the exact amount you're burning so it can send you the corresponding original tokens back. But it can't read encrypted values directly.

The solution: The wrapper marks the burned amount as "publicly decryptable" on-chain, exposing these encrypted handles via an event. Since the value is publicly decryptable, you (or anyone) can decrypt it using the fhEVM SDK and then call finalise with the decrypted value.

## Phase 1: Initiation <a href="#phase-1-initiation" id="phase-1-initiation"></a>

In the first phase, you send your cTokens to the wrapper and request an unshield.

**Step-by-Step:**

1. **Call** `confidentialTransferAndCal`**`l`** on the cToken contract
   * This is part of the ERC-7984 standard
   * Transfers your cTokens to the wrapper and triggers a callback
2. **Wrapper receives the callback**
   * The wrapper's `onConfidentialTransferReceived()` function is called
   * It extracts your recipient address from the callback data
3. **Wrapper burns your cTokens**
   * Calls `burn()` on the cToken contract
   * The burned amount is recorded as an encrypted value
4. **Burned amount marked as publicly decryptable**
   * Wrapper marks the burned amount as "publicly decryptable" on-chain
   * Encrypted handles become available for public decryption
5. **Request stored with a unique ID**
   * A `requestId` is generated (incremental counter)
   * Your recipient address and amounts are stored
   * `UnwrappedStarted` event is emitted with an encrypted handle

At this point, you can decrypt the publicly-decryptable value using the fhEVM SDK (typically takes less than a few seconds).

## Phase 2: Finalisation <a href="#phase-2-finalization" id="phase-2-finalization"></a>

Once you've decrypted the burned amount using the fhEVM SDK, you can finalise the unshield.

**Step-by-Step:**

1. **Decrypt the publicly-decryptable value**
   * Retrieve the encrypted handle from `UnwrappedStarted` event (via indexer or direct read)
   * Use fhEVM SDK to decrypt: `fhevm.decryptPublic(handle)`
   * Get the plaintext burned amount
2. **Call finalizeUnwrap on the wrapper**
   * `wrapper.finalizeUnwrap(requestId, decryptedBurnAmount)`
   * Contract uses the decrypted value to calculate the unwrap amount and fees
3. **Unshield fee is calculated and deducted**
   * **IMPORTANT**: Fee is calculated in cToken units, then converted to original tokens
   * Formula: f`eeAmount = ((burnAmount * unwrapFeeBasisPoints) / 10,000) * rate`
   * [Fee](/wiki/zaiffer/protocol-fees.md) is paid in underlying tokens (USDC, ETH, etc.), NOT cTokens
4. **Fee transferred to protocol**
   * Protocol fee recipient receives the unwrap fee (see section X))
   * In the original token (not cToken)
5. **Remaining tokens transferred to you**
   * You receive: (burnAmount \* rate) - feeAmount
   * In the original underlying token
6. **UnwrappedFinalized event emitted**
   * Contains success status, amounts, and fees for indexing

**Unshield flow diagram:**

<div data-full-width="false" data-with-frame="true"><figure><img src="/files/crh2ZSSf7b8ctDvqbeG1" alt=""><figcaption></figcaption></figure></div>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zaiffer.gitbook.io/wiki/zaiffer/unshielding-tokens-unwrapping/why-two-phases.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
