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

# Treasury contracts

> The TSY token, and the delegate that tracks each depositor's credits, rates, and yield.

Multiliquid Treasury runs on two contracts. `Treasury` is the \$TSY ERC-20 token. `TreasuryDelegate` is the stablecoin delegate that mints and burns \$TSY through `MultiliquidSwap`, and records each depositor's credits and yield. For what Multiliquid Treasury is and who it serves, see [Multiliquid Treasury](https://docs.multiliquid.xyz/applications/liquid-treasury.md).

## How deposits and withdrawals map to swaps

Every deposit and withdrawal is a `MultiliquidSwap` leg settled by `TreasuryDelegate`, with the \$TSY registration's ID as `stablecoinDelegateID`.

| To | Swap | Route IDs | Effect |
| - | - | - | - |
| Deposit a stablecoin | Stablecoin to \$TSY | `6`, `7` | Credits the user, and mints \$TSY to the receiver |
| Deposit an RWA | RWA to \$TSY | `2`, `3` | Credits the user, and mints \$TSY to the receiver |
| Withdraw to a stablecoin | \$TSY to stablecoin | `6`, `7` | Burns the user's \$TSY, spends the user's credits, and pays the receiver from stablecoin custody |
| Withdraw to an RWA | \$TSY to RWA | `0`, `1` | Burns the user's \$TSY, spends the user's credits, and pays the receiver from RWA custody |

* **Credits stay with the user.** A deposit records credits for `user` and mints \$TSY to `receiver`, which can be another wallet. Transferring \$TSY does not move credits.
* **A withdrawal needs both.** It burns \$TSY from the user and spends the same amount of the user's credits. It reverts with `InsufficientCredits(requested, available)` if the user's credits are short.
* **The receiver must be whitelisted.** Deposits mint through `Treasury.mint`, which only mints to whitelisted addresses.
* **The delegate's whitelists apply.** A deposited stablecoin must be on the delegate's stablecoin whitelist, and an RWA on its RWA whitelist.

## Treasury token

| Property | Value |
| - | - |
| Standard | ERC-20, with EIP-2612 `permit` |
| Decimals | 18 |
| Proxy | UUPS, upgraded by `DEFAULT_ADMIN_ROLE` |

### Functions

| Function | Access | Rules |
| - | - | - |
| `mint(address to, uint256 amount)` | `MINTER_ROLE` | `to` must be whitelisted and `amount` nonzero |
| `bridgeMint(address to, uint256 amount)` | `BRIDGE_ROLE` | No whitelist. `to` and `amount` must be nonzero. Emits `BridgeMint`. |
| `burn(uint256 amount)` | Holder | `amount` must be nonzero |
| `burnFrom(address from, uint256 amount)` | Approved spender | Spends the allowance. Emits `BridgeBurn` when the caller holds `BRIDGE_ROLE`. |
| `forceBurn(address from, uint256 amount)` | `OPERATOR_ROLE` | Burns without approval. Emits `ForcedBurn`. |
| `forceTransfer(address from, address to, uint256 amount)` | `OPERATOR_ROLE` | Moves tokens without approval. Emits `ForcedTransfer`. |
| `addBridge(address bridge)`, `removeBridge(address bridge)` | `OPERATOR_ROLE` | Grants or revokes `BRIDGE_ROLE` |
| `addToWhitelist(address)`, `removeFromWhitelist(address)` | `WHITELISTER_ROLE` | Changes `whitelisted(address)` |
| `addToBlacklist(address)`, `removeFromBlacklist(address)`, `addToBlacklistBatch(address[])`, `removeFromBlacklistBatch(address[])` | `BLACKLISTER_ROLE` | Changes `blacklisted(address)` |
| `pause()`, `unpause()` | `PAUSER_ROLE` | Stops or resumes transfers, mints, and burns |

* **Transfers are open.** Any holder can transfer \$TSY. The whitelist applies only to `mint`.
* **The blacklist applies everywhere.** A blacklisted address cannot send, receive, mint, or burn.
* **Pausing stops every transfer, mint, and burn,** except `forceBurn` and `forceTransfer`, which also bypass the blacklist.

### Roles

| Role | Granted at initialization | Administered by |
| - | - | - |
| `DEFAULT_ADMIN_ROLE`, `OPERATOR_ROLE`, `PAUSER_ROLE`, `WHITELISTER_ROLE`, `BLACKLISTER_ROLE` | To the admin | `DEFAULT_ADMIN_ROLE` |
| `MINTER_ROLE` | To the admin | `OPERATOR_ROLE` |
| `BRIDGE_ROLE` | To no one | `OPERATOR_ROLE` |

`TreasuryDelegate` needs `MINTER_ROLE` to mint deposits.

### Bridges

Any contract with `BRIDGE_ROLE` can bridge \$TSY by burning it on one chain and calling `bridgeMint` on another. `TreasuryOFTAdapter` does this over LayerZero's OFT standard: it burns with `burnFrom`, which needs the holder's approval of the adapter, and mints with `bridgeMint`, sending a zero-address recipient's tokens to `0xdead`. Bridging moves \$TSY, never credits.

### Events and errors

| Event | Emitted when |
| - | - |
| `AddedToWhitelist(address indexed account)`, `RemovedFromWhitelist(address indexed account)` | The whitelist changes |
| `AddedToBlacklist(address indexed account)`, `RemovedFromBlacklist(address indexed account)` | The blacklist changes |
| `BridgeMint(address indexed bridge, address indexed to, uint256 amount)` | A bridge mints |
| `BridgeBurn(address indexed bridge, address indexed from, uint256 amount)` | A bridge burns |
| `ForcedBurn(address indexed operator, address indexed from, uint256 amount)` | An operator force-burns |
| `ForcedTransfer(address indexed operator, address indexed from, address indexed to, uint256 amount)` | An operator force-transfers |
| `BridgeAdded(address indexed bridge)`, `BridgeRemoved(address indexed bridge)` | A bridge is added or removed |

| Error | Raised when |
| - | - |
| `AccountNotWhitelisted(address)` | `mint` targets an address that is not whitelisted, or a removal targets one |
| `AccountBlacklisted(address)` | A blacklisted address sends or receives |
| `AccountNotBlacklisted(address)` | A blacklist removal targets an address that is not blacklisted |
| `ZeroAddress()` | An address argument is zero |
| `InvalidAmount()` | An amount is zero |
| `EnforcedPause()` | The token is paused |

## TreasuryDelegate

`TreasuryDelegate` is a [stablecoin delegate](https://docs.multiliquid.xyz/evm/contracts/stablecoin-delegate.md) for \$TSY, with every base function, plus yield accounting.

### Credits and yield

| State | Meaning |
| - | - |
| `credits(user)` | Main credits, which earn yield |
| `yieldMultiplier(user)` | The main credits' growth, with 18 decimals. Starts at `1e18`. |
| `totalWithheldCredits(user)` | Credits from deposits not yet merged into main credits |
| `getWithheldCredits(user)` | Each withheld deposit: `tokenAddress`, `credits`, `releaseTimestamp`, `earningStartTimestamp`, `isRWA` |
| `lastAccrualDay(user)` | The last day whose rate the main credits have earned |

A user's main credits are worth `credits × yieldMultiplier / 1e18`. Each deposit adds one withheld record of the minted amount:

| Deposit | Earns from | Merges into main credits |
| - | - | - |
| RWA | Its deposit day, counted in whole days | At least 24 hours after the deposit |
| Stablecoin | 24 hours after the deposit, including that first partial day | At least 24 hours after the deposit, once the rate for its first earning day is posted |

* **Merging.** Records merge oldest first, and only once the user's main credits have earned every posted rate. Each record's yield since its earning start is added as it merges, and the multiplier becomes the value-weighted blend.
* **Withdrawing.** A withdrawal spends withheld credits first, newest first, at face value, and then main credits at the multiplier. A deposit withdrawn before it merges earns nothing.
* **Fees.** The protocol and capital provider fees on a deposit or withdrawal are minted as new \$TSY, with their own withheld credits, to the protocol fee vault and to stablecoin custody.

### Daily rates

A rate poster publishes one gross APY per finished UTC day, numbered `block.timestamp / 86400`.

* **In order.** `postDailyRate(dayNumber, grossRate)` accepts only `lastRatePostingDay() + 1`, and only after that day has ended.
* **Net of the management fee.** The delegate stores `grossRate − managementFee()` in `dailyRates(day)` and compounds daily at `(1 + net)^(1/365) − 1`.
* **Bounded.** A gross rate cannot exceed 100%, or `maxDailyRate()` when it is set, and cannot be below the management fee.
* **Accrual stops at a gap.** Credits earn up to yesterday, and stop at the first day with no posted rate until the poster fills it.
* **Corrections.** `correctDailyRate(dayNumber, grossRate)` rewrites a posted day between `firstRateDay()` and `lastRatePostingDay()`. Users whose credits already earned that day keep their multiplier, unless `setUserYieldMultiplier` repairs it.

A withdrawal to a stablecoin also earns the time since the last posted day ended, at that day's rate, up to `maxFallbackRateSeconds()`. While that window is zero, its initial value, there is no partial yield. A withdrawal to an RWA earns whole posted days only.

### Accrual

Accrual is permissionless. `MultiliquidSwap` accrues the user before every \$TSY leg, and anyone can accrue a user directly:

| Function | Effect |
| - | - |
| `accrueInterest(address user, uint256 maxDays, uint256 maxRecords)` | Applies up to `maxDays` posted rates, then merges up to `maxRecords` eligible records. Both limits must be nonzero. Returns `daysAccrued`, `recordsMerged`, `dailyAccrualComplete`, and `creditsFullyMerged`. |
| `batchAccrueInterest(address[] users)` | Accrues each user without limits |

Use the bounded form to catch up a user who has not accrued for a long time within your gas limit. Both functions revert while the delegate is paused.

### Reads

| Function | Returns |
| - | - |
| `getYieldAmount(address user, uint256 redeemValue, bool stablecoinWithdrawal)` | The value a withdrawal of `redeemValue` \$TSY returns, from stored state |
| `getAmountForTargetValue(address user, uint256 targetValue, bool stablecoinWithdrawal)` | The \$TSY a withdrawal needs to return `targetValue` |
| `getDailyRate(uint256 dayNumber)` | The day's net APY and whether it is posted |
| `dailyRates(uint256)`, `ratePosted(uint256)` | The same, as separate reads |
| `getCurrentDay()` | `block.timestamp / 86400` |
| `firstRateDay()`, `lastRatePostingDay()` | The first and last posted days |
| `managementFee()`, `maxDailyRate()`, `maxFallbackRateSeconds()` | The yield parameters |
| `treasury()` | The `Treasury` token |

### Administration

| Function | Access | Rules |
| - | - | - |
| `postDailyRate(uint256 dayNumber, uint256 grossRate)` | `RATE_POSTER_ROLE` | The next day only, once it has ended |
| `correctDailyRate(uint256 dayNumber, uint256 grossRate)` | `OPERATOR_ROLE` | A day already posted |
| `setMaxFallbackRateSeconds(uint256 maxSeconds)` | `OPERATOR_ROLE` | Nonzero, so the fallback cannot be switched off once set |
| `setManagementFee(uint256 fee)` | Capital provider admin | At most 100%, and at most `maxDailyRate()` when it is set |
| `setMaxDailyRate(uint256 maxRate)` | `DEFAULT_ADMIN_ROLE` | At most 100%, and zero or at least the management fee. Zero removes the cap. |
| `setUserYieldMultiplier(address user, uint256 newMultiplier)` | `DEFAULT_ADMIN_ROLE` | At least `1e18`. Repairs a user's multiplier after a correction. |

`TreasuryDelegate` defines its own `OPERATOR_ROLE` and `RATE_POSTER_ROLE`, separate from the token's. `initialize` posts the first rate for the deployment day and can grant `RATE_POSTER_ROLE`.

### Events and errors

| Event | Emitted when |
| - | - |
| `FirstRatePosted(uint256 dayNumber)` | The first rate is posted at initialization |
| `DailyRatePosted(uint256 indexed dayNumber, uint256 rate, address indexed poster)` | A rate is posted; `rate` is net |
| `DailyRateCorrected(uint256 indexed dayNumber, uint256 oldRate, uint256 newRate, address indexed corrector)` | A rate is corrected |
| `InterestAccrued(address indexed user, uint256 fromDay, uint256 toDay, uint256 daysAccrued, uint256 multiplierAfter)` | Main credits earn posted rates |
| `PendingCreditsAdded(address indexed user, uint256 amount, uint256 depositDay)` | A deposit or fee adds a withheld record |
| `PendingCreditsMerged(address indexed user, uint256 pendingAmount, uint256 newCredits)` | Withheld records merge |
| `WithheldCreditsReduced(address indexed user, uint256 amount, uint256 remainingWithheld)` | A withdrawal spends withheld credits |
| `MainCreditsReduced(address indexed user, uint256 amount, uint256 remainingCredits)` | A withdrawal spends main credits |
| `YieldClaimed(address indexed user, uint256 creditsRedeemed, uint256 yieldAmount, uint256 effectiveValue)` | A withdrawal pays yield on main credits |
| `ManagementFeeUpdated(uint256 oldFee, uint256 newFee)` | The management fee changes |
| `MaxDailyRateUpdated(uint256 oldRate, uint256 newRate)` | The rate cap changes |
| `MaxFallbackRateSecondsUpdated(uint256 oldWindow, uint256 newWindow)` | The fallback window changes |
| `YieldMultiplierUpdated(address indexed user, uint256 oldMultiplier, uint256 newMultiplier, address indexed admin)` | An admin repairs a multiplier |

| Error | Raised when |
| - | - |
| `InsufficientCredits(uint256 requested, uint256 available)` | A withdrawal exceeds the user's credits |
| `MustPostSequentially(uint256 dayNumber, uint256 expectedDay)` | A posted day is not the next day |
| `CannotPostRateForUnfinishedDay(uint256 dayNumber, uint256 currentDay)` | A posted day has not ended |
| `CannotPostFutureRate(uint256 dayNumber, uint256 currentDay)` | A corrected day is after the last posted day |
| `CannotCorrectBeforeFirstRate(uint256 dayNumber, uint256 firstRateDay)` | A corrected day is before the first posted day |
| `RateTooHigh(uint256 rate, uint256 maxRate)` | A rate or cap is above its limit |
| `ManagementFeeTooHigh(uint256 fee)` | A management fee is above 100% or above the gross rate |
| `InvalidFallbackRateWindow()` | The fallback window is zero |
| `InvalidMaxDays()`, `InvalidMaxRecords()` | An accrual limit is zero |
| `InvalidYieldMultiplier(uint256 multiplier)` | A repaired multiplier is below `1e18` |

The base delegate's errors also apply, as [Stablecoin delegates](https://docs.multiliquid.xyz/evm/contracts/stablecoin-delegate.md#errors) lists.


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