# Building multicall batches

> How operator keys batch restricted calls through their OwnedMulticall, how the vault's own multicall differs, and worked batches for common operations.

Source: https://docs.trusslabs.io/vaults/developers/multicall-batches

Operator keys in a Truss vault do not hold roles themselves. Each key owns an `OwnedMulticall`, a small contract that
holds the key's roles and can call several contracts in one transaction. The vault and each sub-account also have a
multicall of their own, for one caller's calls to that contract. This page shows how to build both kinds of batch,
how a delayed call goes through a multicall, and four batches for common operations.
[Operator multicalls](https://docs.trusslabs.io/vaults/roles/owned-multicall.md) explains why the setup is built this way.

> **Key points**
>
> - An operator key calls `multicall(targets, calls)` on its own `OwnedMulticall`, which makes each inner call as itself.
> - The vault's `multicall(bytes[])` keeps the original caller, so each inner call is checked against that caller.
> - Either kind is all or nothing: the first failing call reverts the whole batch.
> - Deliver claims in a later transaction than a settlement, so one failing claim cannot undo the settlement.

## The two kinds of multicall

| | `OwnedMulticall.multicall(targets, calls)` | `multicall(calls)` on the vault or a sub-account |
|---|---|---|
| Who may call it | Only its owner key, fixed for life | Anyone |
| Reaches | Any contracts, one target per call | That one contract |
| Caller each target sees | The multicall, which holds the roles | The original caller |
| Events | One `Executed(target, data, result)` per inner call, after the target's own | The targets' own |

A holder can use the vault's multicall to, say, claim and open a new request in one transaction. Restricted calls in
it are still checked against the holder, so it grants nothing.

## Encoding a batch

Encode each inner call with the target's ABI: `abi.encodeCall(TrussVault.settleDeposit, (ids))` in Solidity, or
`encodeFunctionData` in a client library. Put the target addresses and the calldata in two arrays of the same
length, in the order the calls should run, and send `multicall(targets, calls)` from the owner key.

## Delayed calls through a multicall

When a role has a delay, its holder schedules the exact call on the vault's AccessManager, waits, then executes it.
The multicall holds the role, so the multicall must schedule and execute:

```
targets = [manager]
calls   = [ schedule(vault, raiseEntryFee(newBps), 0) ]        // 0: the earliest time allowed
// after the delay:
targets = [vault]
calls   = [ raiseEntryFee(newBps) ]                            // or manager.execute(vault, data)
```

`OperationScheduled` carries the full calldata, so anyone can see what is coming and when.

## Example batches

Each batch is sent by the key that owns the multicall, which must hold every role the batch uses.

```
// 1. A settler settles, then delivers claims in a second transaction.
[vault]                     [ settleRedeem(ids, shares) ]
[vault, vault]              [ claimRedeemFor(c1), claimRedeemFor(c2) ]

// 2. A vault manager, or a trader that is also a settler, brings capital home and settles.
[subAccount, subAccount, vault]
                            [ executeModule(lendModule, intent, executionData, ""),   // withdraw USDC from a venue
                              returnAssetsToVault(ALL),                            // ALL = type(uint256).max
                              settleRedeem(ids, shares) ]

// 3. A vault manager sends capital out and puts it to work.
[vault, subAccount]         [ pushToSubAccount(subAccount, amount, ""),
                              executeModule(lendModule, intent, executionData, "") ]

// 4. A vault manager moves capital between sub-accounts: there is no direct transfer.
[subAccountA, vault]        [ returnAssetsToVault(amount), pushToSubAccount(subAccountB, amount, "") ]
```

`ALL` returns what is available once the earlier calls have run. An Allocator can push but not trade, and a Trader
can trade and return only on the sub-accounts that name it, so batches 3 and 4 need a vault manager's key. Skip a
claim that reverts, such as one for a controller a withdrawal policy refuses, rather than retrying the whole batch.

## Where to read more

| Page | What it covers |
|---|---|
| [Operator multicalls](https://docs.trusslabs.io/vaults/roles/owned-multicall.md) | Why operator keys act through a multicall, and how keys are rotated |
| [Profiles](https://docs.trusslabs.io/vaults/roles/profiles.md) | What each profile's multicall holds |
| [Withdrawals](https://docs.trusslabs.io/vaults/flow-of-funds/withdrawals.md) | Settling redemptions and delivering claims |
