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

# Emergency exit and upgradeability

> Withdraw from a generation-2 vault on chain without the platform (gas limit of at least ~0.67M), and how upgrades of generation-2 contracts are proposed and applied.

## Emergency exit

Same calls as for a
[generation-1 vault](https://docs.adipredictstreet.com/concepts/contracts/vaults.md#emergency-withdraw),
sent from your wallet, on each vault you hold:

```solidity theme={null}
initiateEmergencyWithdraw()            // vault owner only; starts the 7-day timelock
// ... wait 7 days, then within the following 7 days:
emergencyWithdrawERC20(token)          // sends the vault's full `token` balance to the vault owner
emergencyWithdrawERC1155(token, id)    // sends the vault's full balance of position `id` to the vault owner
```

* From `initiateEmergencyWithdraw()` on, the exchange rejects orders
  from that vault. `emergencyUnlockAt()` returns the unlock time.
* If the 7-day execution window lapses, call
  `initiateEmergencyWithdraw()` again.
* `GET /api/me/vault/emergency` shows the state (`none`, `waiting`,
  `executable`, `unlocksAt`, `initiatedAt`). Pass `?vault=0x…` to inspect a
  specific vault (default: active vault). It is read-only; the calls are yours.

> [!WARNING]
>
> On a generation-2 vault the final `emergencyWithdraw*` call needs a
> gas limit of at least \~0.67M. The vault forwards 600,000 gas to
> `DepositLimitRegistry.freezeUser(vaultOwner)` and reverts with
> `InsufficientGasForFreeze` if the transaction leaves less. Always estimate
> gas with `eth_estimateGas` or set a limit ≥ 0.67M. A fixed limit sized for
> generation 1 reverts without paying out.

```typescript theme={null}
const vault = new Contract(vaultAddress, ['function emergencyWithdrawERC20(address token)'], eoa);
const gas = await vault.emergencyWithdrawERC20.estimateGas(contracts.usdc);
await (await vault.emergencyWithdrawERC20(contracts.usdc, { gasLimit: gas + gas / 10n })).wait();
```

## Upgradeability

Generation-1 vaults cannot be upgraded. Generation-2 contracts are upgradeable proxies:

| Contract | Pattern | Upgrade mechanism |
| - | - | - |
| `MigrationVault` (generation-2 vaults) | ERC-1967 Beacon Proxy | `VaultFactoryGen2.proposeVaultUpgrade(impl)` → timelock → `VaultFactoryGen2.upgradeVaultImplementation(impl)` upgrades all generation-2 vaults at once |
| `VaultFactoryGen2`, `CTFExchangeGen2`, `NegRiskCtfExchangeGen2` | UUPS Proxy (`TimelockedUUPS`) | `proposeUpgrade(impl)` → timelock → `upgradeToAndCall(impl, data)` |
| `DepositLimitRegistry` | Transparent / UUPS Proxy | Shared timelock mixin (`TimelockedUpgrades`) |

* **Timelock:** Changes take effect only after `upgradeTimelock()` (minimum 1 hour, maximum 30 days; configured on chain).
* **Execution window:** Staged proposals must be executed within `UPGRADE_WINDOW` (7 days) after maturation.
* **Monitoring events:** Watch for `VaultUpgradeProposed(impl, activatesAt)` and `VaultImplementationSet(old, impl)` on `VaultFactoryGen2`, or `UpgradeProposed(impl, activatesAt)` and `UpgradeApplied(impl)` on the exchange proxies.
* **Permanent disable:** An admin can call `disableUpgrades(address(this))` to irreversibly permanently disable upgrades on that contract (`UpgradesDisabled`).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.