# Migration

The Migration Facet provides one-time data migration functions needed when upgrading from v0.8.4 to v0.8.5. Two major features require existing storage to be backfilled: **Cross PartyB Mode** (unified balance management across all PartyAs) and **Aggregated Positions,** uPnL and funding calculations.&#x20;

***

### Overview

The Migration Facet provides the following key functionalities:

* **Quote Migration:** Populates aggregated position and funding structures from existing open quotes, initializes new quote fields (`accumulatedPaidFunding`), and backfills PartyB total position counters and PartyA↔PartyB connection caches.
* **Cross Locked Values Migration:** Sums per-PartyA balances (allocated, locked, pending locked) into the cross bucket (`address(0)`) for PartyBs that will operate in cross mode.
* **Verification Views:** Check whether individual quotes or PartyB balances have been migrated.

***

### Why Migration Is Needed

**Aggregated Positions:** In v0.8.4, calculating uPnL and funding debt required iterating every open quote — O(quotes) complexity. v0.8.5 introduces per-symbol aggregated position structures that enable O(symbols) calculations. Migration populates these structures from existing open positions.

**Cross PartyB Mode:** In v0.8.4, PartyB balances are tracked separately per PartyA. v0.8.5 introduces a unified "cross bucket" keyed by `address(0)` that aggregates all per-PartyA balances. When cross mode is enabled for a PartyB, solvency checks use this cross bucket, allowing unified capital management. Migration sums the existing per-PartyA balances into this bucket.

***

### migrateQuotes()

Backfills v0.8.5 derived state for a batch of existing quotes. Can be called multiple times with different batches — already-migrated quotes are silently skipped (idempotent). Only active quotes (status `OPENED`, `CLOSE_PENDING`, or `CANCEL_CLOSE_PENDING`) are processed; non-active quotes are skipped.

For each active quote, the function:

* Initializes `accumulatedPaidFunding` based on current funding rates
* Adds to `partyBAggregatedPositions` and `partyAAggregatedPositions` (per-symbol totals)
* Updates `activeSymbols` arrays for both parties
* Adds to aggregate funding structures (`weightedPaidFunding` counters)
* Updates the PartyB total positions counter (`partyBPositionsCount[partyB][address(0)]`)
* Populates the PartyA↔PartyB connection cache

**Function Signature:**

```solidity
function migrateQuotes(uint256[] calldata quoteIds) external onlyRole(MIGRATION_ROLE);
```

**Parameters:**

* `quoteIds`: Array of quote IDs to migrate. Typically all open quotes, processed in batches.

**Access:** Requires `MIGRATION_ROLE`.

**Example:**

```solidity
// Migrate quotes in batches of 100
uint256[] memory batch = getOpenQuoteIdsBatch(0, 100);
symmDiamond.migrateQuotes(batch);
```

**Events Emitted:**

* `QuotesMigrated(uint256 totalQuotes, uint256 quotesMigrated)` — `totalQuotes` is the batch size, `quotesMigrated` is how many were actually processed (excludes already-migrated and non-active quotes).

***

### migrateCrossLockedValues()

Aggregates per-PartyA balances into the cross bucket (`address(0)`) for a specific PartyB. Should be called while the system is paused during the upgrade. Reverts if called twice for the same PartyB (marks the PartyB as migrated to prevent double-counting).

For each PartyA provided, the function sums:

* `partyBAllocatedBalances[partyB][partyA]` → `partyBAllocatedBalances[partyB][address(0)]`
* `partyBLockedBalances[partyB][partyA]` → `partyBLockedBalances[partyB][address(0)]`
* `partyBPendingLockedBalances[partyB][partyA]` → `partyBPendingLockedBalances[partyB][address(0)]`

**Function Signature:**

```solidity
function migrateCrossLockedValues(address partyB, address[] calldata partyAs) external onlyRole(MIGRATION_ROLE);
```

**Parameters:**

* `partyB`: The PartyB address to migrate.
* `partyAs`: All PartyA addresses that have positions or balances with this PartyB.

**Access:** Requires `MIGRATION_ROLE`.

**Events Emitted:**

* `CrossLockedValuesMigrated(address partyB, uint256 partyAsProcessed)`

***

### isQuoteMigrated()

View function to check whether a specific quote has been migrated.

**Function Signature:**

```solidity
function isQuoteMigrated(uint256 quoteId) external view returns (bool);
```

**Parameters:**

* `quoteId`: The quote ID to check.

**Returns:** `true` if the quote has been migrated.

***

### isPartyBLockedValuesMigrated()

View function to check whether a PartyB's per-PartyA balances have been aggregated into the cross bucket.

**Function Signature:**

```solidity
function isPartyBLockedValuesMigrated(address partyB) external view returns (bool);
```

**Parameters:**

* `partyB`: The PartyB address to check.

**Returns:** `true` if the PartyB's locked values have been migrated.
