# SymmioHook Facet

### Overview

The SymmioHook Facet handles the following callbacks:

* **onOpenPosition:** No-op. The Account Layer does not need to track position opens (quote IDs are tracked at sendQuote time in the CoreFacet).
* **onClosePosition:** Removes the closed quote ID from the virtual account. If the virtual account has no remaining quotes, it is automatically deleted and its funds returned to the parent sub-account.
* **onCancelQuote:** Same as close — removes the quote ID and potentially deletes the virtual account.
* **onFeeCharged:** No-op. The Account Layer does not need to track fee charges.

***

### onOpenPosition()

Called by Symmio core when a position is opened. No action is taken — this exists to prevent hook reverts.

```solidity
function onOpenPosition(uint256 quoteId, uint256 filledAmount, uint256 openedPrice, address partyA, address partyB) external onlySymmio whenNotPaused;
```

***

### onClosePosition()

Called by Symmio core when a position is closed. Removes the quote ID from the virtual account's tracked quotes. If the virtual account has no remaining quotes, the virtual account is automatically deleted.

```solidity
function onClosePosition(uint256 quoteId, uint256 filledAmount, uint256 closedPrice, address partyA, address partyB) external onlySymmio nonReentrant whenNotPaused;
```

**Virtual Account Deletion on Empty:**

When the last quote is removed from a virtual account:

1. Any remaining allocated balance is deallocated and transferred back to the parent sub-account via `internalTransferToBalance`.
2. The virtual account is marked as `isExists = false`.
3. The virtual account address is added to the reuse pool for its isolation type and symbol, allowing future virtual accounts with the same key to reuse the address.
4. The affiliate's `onVirtualAccountDeletion` hook is called.

**Events:** `VirtualAccountDeleted(address account, address parent)` — when auto-deletion occurs

***

### onCancelQuote()

Called by Symmio core when a quote is cancelled. Behaves identically to `onClosePosition` — removes the quote ID and potentially triggers virtual account deletion.

```solidity
function onCancelQuote(uint256 quoteId, address partyA, address partyB) external onlySymmio whenNotPaused;
```

***

### onFeeCharged()

Called by Symmio core when a fee is charged. No action is taken — this exists to prevent hook reverts.

```solidity
function onFeeCharged(uint256 quoteId, uint256 amount, address partyA, address partyB, uint256 symbolId, address affiliate, uint8 feeType) external onlySymmio whenNotPaused;
```

***

### Access Control

All callback functions include the `onlySymmio` modifier, ensuring they can only be called by the registered Symmio core diamond. This prevents external actors from manipulating virtual account state.
