# Building a policy

> What a new policy must satisfy to be bound, how its refusals and failures behave, and what no policy can do.

Source: https://docs.trusslabs.io/vaults/policies/building-a-policy

A policy is asked questions inside the flows people depend on, so a mistake in one can stop those flows. This page
sets out what a policy must satisfy before a vault accepts it, how a refusal and a failure differ in each slot, and
what no policy can do however it is written.

> **Key points**
>
> - A policy must implement its slot's hook, name the vault it serves, and answer to that vault's AccessManager.
> - A policy that reverts behaves differently by slot: a broken withdrawal policy refuses everyone, and a broken holder policy stops every share transfer.
> - No policy can stop a holder sending shares away, block the fee mint, or move the vault's funds.
> - A custom policy's own restricted functions have no rows in the roles table today.

## What a policy must satisfy

1. It implements the slot's interface: `IVaultDepositPolicy`, `IVaultHolderPolicy`, `IVaultWithdrawalPolicy`,
   `ISubAccountAllocationPolicy` or `ISubAccountExecutionPolicy`.
2. Its `vault()` returns the vault it serves. A policy serves one vault.
3. Its `authority()` returns that vault's AccessManager. Binding refuses a policy that fails either check.
4. An execution policy's `moduleType()` matches its module's family, and a module cannot be added without one.

A policy bound on a live vault waits the CRITICAL tier, and unbinding it waits the same. A holder or withdrawal policy
named at deployment is bound by the factory with no wait. It must exist before the vault does, so it is built
against the vault's predicted address and AccessManager. Each binding emits an event: `VaultDepositPolicySet`,
`VaultHolderPolicySet`, `VaultWithdrawalPolicySet`, `SubAccountAllocationPolicySet` or `ModuleAdded`.

A policy's own settings, such as an allowlist, answer to the vault's AccessManager. The roles table maps the
functions of the policies that exist today. A custom policy's own restricted functions have no rows in it.

## Refusals and failures

| Slot | A refusal | A revert |
|---|---|---|
| Deposit, request hook | Refuses the request | Refuses the request |
| Deposit, `settlementHeadroom` | A low answer fills less, or nothing | Stops every deposit settlement and the `depositCapacity()` view |
| Holder | Reverts the arrival with `HolderNotAllowed` | Reverts every arrival: share transfers, deposit claims and new deposits stop |
| Withdrawal | Refuses with `WithdrawalNotAllowed` | Counts as a refusal of everyone; the views read zero |
| Allocation | Refuses the push | Refuses the push |
| Execution | Reverts the execution | Reverts the execution, including an instant withdrawal's venue leg |

Hooks run inside user flows, so keep them cheap. The holder policy runs on every share transfer, and the withdrawal
policy on every redemption request and claim.

## What no policy can do

- It cannot stop a holder sending shares away. The sender is never asked about, and pausing the vault does not
  stop transfers either.
- It cannot block the fee mint, because the holder policy is never asked about the fee recipient.
- It cannot stop a settlement batch because of one address. Settlement asks neither the holder nor the withdrawal
  policy.
- It cannot move the vault's funds. The vault and its sub-accounts only ask a policy questions, and none sends it
  tokens or an approval.

## Before binding one

- Test it against a fork of the vault, through every function on [Vault policies](https://docs.trusslabs.io/vaults/policies/vault-policies.md) that asks it.
- Decide who can change its settings, and how fast. A list that must be quick to update cannot sit behind a long delay.
- Watch for the binding events. On a vault with timelocks, a binding is scheduled in public, so holders can see it
  before it runs.

## Where to read more

| Page | What it covers |
|---|---|
| [Policies](https://docs.trusslabs.io/vaults/policies.md) | Every slot, and what exists today |
| [Vault policies](https://docs.trusslabs.io/vaults/policies/vault-policies.md) | Every function that asks a vault policy |
| [Sub-account policies](https://docs.trusslabs.io/vaults/policies/sub-account-policies.md) | Every function that calls a sub-account policy |
| [Bind a policy](https://docs.trusslabs.io/vaults/guides/bind-a-policy.md) | The steps to bind, replace or remove one |
| [Tiers and timelocks](https://docs.trusslabs.io/vaults/roles/tiers-and-timelocks.md) | How the CRITICAL wait is scheduled and cancelled |
