# Sub-account policies

> The allocation and execution policies on sub-accounts, the hooks each one exposes, and every function that calls them.

Source: https://docs.trusslabs.io/vaults/policies/sub-account-policies

Sub-accounts are where a vault's capital is put to work. Two kinds of policy limit that. The allocation policy limits
how much USDC the vault may send into each sub-account. Execution policies limit what a tracked sub-account's swap
and lend modules may do with it. This page lists each hook and every function that calls it.

> **Key points**
>
> - The allocation policy is asked on every push of USDC into a sub-account, and never when capital comes back.
> - Every module on a tracked sub-account has an execution policy, asked before and after each execution.
> - The policies that exist today meter flows with limits that start at zero, so nothing moves until a limit is raised.
> - Attested sub-accounts have no execution policy. Their calls are checked against the call shapes their verifier allows.

## The allocation policy

`applyAllocationPolicy(subAccount, assetsToPush, policyData)` checks and meters one push, and reverts to refuse.
`policyData` is passed through from the caller, in a format the policy defines.

| Function | When it is asked |
|---|---|
| `pushToSubAccount` on the vault, which calls the sub-account's `deposit` | Once the sub-account is confirmed active and unpaused, before the USDC moves |

It is not asked when a sub-account sends capital back (`returnAssetsToVault`), when the vault asks for it back
(`requestAssetsFromSubAccount`), or when a sub-account funds an instant withdrawal. With no policy bound, pushes have
no cap or rate limit.

`SubAccountAllocationPolicy` is one contract per vault. The factory binds it on every sub-account it deploys; a
sub-account added to a live vault needs it bound with `setSubAccountAllocationPolicy`. For each sub-account it keeps a cap on net deployed principal, which is what was pushed less what came
back, and a push rate limit. Gains do not count toward the cap. Caps sit under a ceiling fixed at deployment.

## Execution policies

Each execution on a tracked sub-account calls its module's policy twice:

1. `applyExecutionPolicy(subAccount, module, intent, targets, policyData)` runs before the module acts. It sees the
   intended input and output, the tolerance, and the addresses the module will call.
2. `recordExecutionOutcome(subAccount, module, intent, spent, received)` runs after, with what the sub-account
   measured. It can still refuse, which reverts the whole execution.

| Function | Caller | When |
|---|---|---|
| `executeModule` | The VaultAdmin, or a vault manager or trader given that sub-account | Every swap or lend execution |
| `fundInstantRedemption` | The vault, during an instant withdrawal | When the default source withdraws from its lending venue |

`addModule` checks that the policy's `moduleType()` matches its module's family. Only a live sub-account of the vault
can call a policy's hooks.

## The execution policies that exist today

| Policy | One per | What it checks | What it meters |
|---|---|---|---|
| `SubAccountSwapPolicy` | Swap module | The swap's tolerance is within a ceiling fixed when the policy is created, below 100%, with no factory maximum | Flow out and in per asset, from what was measured |
| `SubAccountLendPolicy` | Vault | The call's tolerance is within a ceiling fixed at deployment, under the factory's maximum. A call names one venue and no other targets | Supply per venue, before the call. Withdrawals are never metered, so a venue can always be left |

The policy bounds the tolerance; the sub-account itself checks that what came back is worth at least what went in,
less that tolerance, at oracle prices. A venue or asset whose limit was never raised refuses any amount. Raising a
limit or a cap waits HIGH. Lowering one is immediate, and the Sentry holds the lowers. See
[Rate limits](https://docs.trusslabs.io/vaults/architecture/rate-limits.md).

## Where to read more

| Page | What it covers |
|---|---|
| [Policies](https://docs.trusslabs.io/vaults/policies.md) | Every slot, and what exists today |
| [Tracked sub-accounts](https://docs.trusslabs.io/vaults/architecture/tracked-sub-accounts.md) | Modules, executions and how a tracked sub-account is valued |
| [Attested sub-accounts](https://docs.trusslabs.io/vaults/architecture/attested-sub-accounts.md) | Call shapes and the verifier that checks them |
| [Rate limits](https://docs.trusslabs.io/vaults/architecture/rate-limits.md) | Every limit these policies keep, and who changes it |
| [Who can call what](https://docs.trusslabs.io/vaults/roles/function-permissions.md) | The tier and holders of every policy setting |
