bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 14쪽
er
A **claimer** is an address authorized to invoke the `claimRewards` function on behalf of a delegator. This role is useful for delegators who wish to transfer the responsibility of claiming rewards to another address without compromising security. For example, a delegator may set a smart contract as the claimer that automatically claims rewards as part of a broader yield strategy.
A delegator can have only one authorized claimer at any given time. If no claimer is explicitly set, the delegator address is considered its own authorized claimer by default.
To authorize an address to claim rewards on your behalf, call `setClaimer` on the `OperatorRewarder` contract with the address you wish to authorize to claim your rewards:
```solidity
IOperatorRewarder(rewarderAddress).setClaimer(claimerAddress);
```
### Claim commission fees
Operators can claim their accumulated commission fees from their `OperatorRewarder` contract. Only the [beneficiary](#operatorrewarder-beneficiary) set in the contract can claim the fees, and the fees are sent directly to the beneficiary.
```solidity
IOperatorRewarder(rewarderAddress).claimFee();
```
### Redeem shares
Redeeming from `OperatorStaking` contracts is a two-step process subject to a cooldown period (determined by the `ProtocolStaking` contract). The period is currently set to 7 days on mainnet (3 minutes on testnet) and is updatable by the owner. Note that `OperatorStaking` contract shares are transferable (as ordinary ERC20), and hence offer an alternative “withdrawal" process without being subject to the cooldown period.
All redemption requests are managed by a controller. See [The controller](#the-controller) for more information.
Also note that all operator staking shares use 20 decimals. See [Operator Staking decimals](#operator-staking-decimals) for more information.
```solidity
// 1. Request redeem
// shares: amount of shares to redeem.
// controllerAddress: the address that will manage this withdrawal (can be msg.sender)
// ownerAddress: the owner of the shares.
// releaseTime: the timestamp when the assets will be available for withdrawal.
uint48 releaseTime = operatorStaking.requestRedeem(shares, controllerAddress, ownerAddress);
// Wait for the cooldown period to pass
// 2. Redeem
// shares: amount of shares to redeem (use max uint256 for all claimable).
// receiverAddress: the address to receive the assets.
// controllerAddress: the same address used in step 1.
uint256 assetsReceived = operatorStaking.redeem(shares, receiverAddress, controllerAddress);
```
## Contract: ProtocolStaking
The `ProtocolStaking` contract acts as the root of the hierarchy where operators stake their pooled $ZAMA.
### Protocol Staking shares
The `ProtocolStaking` contract issues a share token to acknowledge the amount of $ZAMA staked by an operator. There are two separate protocol staking share tokens, one for each of the deployed KMS and coprocessor `ProtocolStaking` contracts:
* `$stZAMA-KMS`
* `$stZAMA-Coprocessor`
These tokens use 18 decimals and are non-transferable.
#### Staked token weight
The `ProtocolStaking` contract implements a non-linear weight system to determine reward distribution. Unlike linear staking models where rewards scale 1:1 with staked assets, `ProtocolStaking` utilizes a concave weighting function to prioritize protocol decentralization. The weight assigned to an operator is calculated as the square root of its staked balance:
```solidity
stakedAmount = ProtocolStaking.balanceOf(operator)
weight = Math.sqrt(stakedAmount)
```
This weighting system incentivizes broader participation and reduces the impact of large token holders on the reward distribution.
### Protocol Staking owner
The `ProtocolStaking` contract owner acts as the owner of the entire staking hierarchy. This owner is set on contract deployment, and the ownership authority is propagated to `OperatorStaking` contracts and `OperatorRewarder` contracts.
The owner is not the same as the `MANAGER_ROLE` on the `ProtocolStaking` contract. The `MANAGER_ROLE` is a separate role that can be granted to other addresses to perform specific [management functions](#manager-functions), such as updating the unstake cooldown period and reward rate.
### Operator eligibility
It is important to note that only *eligible* `OperatorStaking` contracts generate rewards when staking into the `ProtocolStaking` contract. Requesting eligibility is a manual process ending with a protocol governance proposal. As part of the process, operators are asked to run to run at least one Zama Protocol node (KMS or Coprocessor).
### User functions
Non-role-restricted public functions on `ProtocolStaking` are not intended for direct use. They should only be called by `OperatorStaking` contracts.
### Manager functions
The `MANAGER_ROLE` is granted to the [Protocol DAO governance](/protocol/protocol-apps/governance/governance) on mainnet. The following functions require the caller to have the `MANAGER_ROLE` set on the contract:
#### Manage eligible accounts
Manages which addresses are currently eligible to earn global rewards from the protocol (e.g., `OperatorStaking` contracts). Anyone can call `stake()` on the `ProtocolStaking` contract, but only eligible accounts will actually earn rewards on their staked tokens.
When an account's eligibility is added or removed, the contract automatically snapshots that account's reward state. This ensures that rewards are only accrued for the exact duration the account was eligible.
```solidity
// Add an eligible account
protocolStaking.addEligibleAccount(operatorAddress);
// Remove an eligible account
protocolStaking.removeEligibleAccount(operatorAddress);
```
#### Set reward rate
Adjusts the global tokens-per-second reward rate distributed among all eligible pools.
This function snapshots the current global reward state, ensuring that the old rate is accurately applied to all rewards earned up to the calling point, and the new rate only applies to rewards generated from this point forward.
```solidity
protocolStaking.setRewardRate(newRewardRate);
```
#### Set unstake cooldown period
Updates the mandatory waiting period between unstaking and releasing tokens. Existing unstake requests are unaffected. Note that since release times are strictly increasing per account, reducing the cooldown period only takes full effect once an account's previous, longer cooldowns have elapsed.
```solidity
protocolStaking.setUnstakeCooldownPeriod(newCooldownPeriod);
```
### View functions
#### Get earned rewards
Returns the amount of $ZAMA rewards currently accrued for an account that are available to be claimed. The `accountAddress` must be an [eligible](#operator-eligibility) account, otherwise, this function will always return 0.
```solidity
uint256 rewards = protocolStaking.earned(accountAddress);
```
#### Get staking token
Returns the address of the $ZAMA token used for staking and rewards.
```solidity
address token = protocolStaking.stakingToken();
```
#### Get staking weight
Returns the square-root weight for a given token amount. Used in the reward distribution calculation.
See [Staked token weight](#staked-token-weight) for more information.
```solidity
uint256 w = protocolStaking.weight(amount);
```
#### Get total staked weight
Returns the total eligible staked weight across all active operator pools.
```solidity
uint256 totalWeight = protocolStaking.totalStakedWeight();
```
#### Get unstake cooldown period
Returns the current cooldown period in seconds.
```solidity
uint256 cooldown = protocolStaking.unstakeCooldownPeriod();
```
#### Get awaiting release
Returns the total amount of unstaked tokens still pending release for an account. This includes tokens still in the cooldown period and tokens whose cooldown has already elapsed but have not yet been released via `release()`.
```solidity
uint256 pending = protocolStaking.awaitingRelease(accountAddress);
```
#### Get reward rate
Returns the current reward rate in $ZAMA tokens distributed per second across all eligible pools.
```solidity
uint256 rate = protocolStaking.rewardRate();
```
#### Get rewards recipient
Returns the configured recipient address for an account's rewards. If not set, returns the account address itself.
```solidity
address recipient = protocolStaking.rewardsRecipient(accountAddress);
```
#### Check account eligibility
Returns `true` if an account has the `ELIGIBLE_ACCOUNT_ROLE` and will earn rewards.
```solidity
bool eligible = protocolStaking.isEligibleAccount(accountAddress);
```
#### Get owner
Returns the owner address. See [Protocol Staking owner](#protocol-staking-owner) for more information.
```solidity
address protocolOwner = protocolStaking.owner();
```
### Events
| Event | Description |
| ------------------------------------------------- | ------------------------------------------------------------------------------------ |
| `RewardRateSet(rewardRate)` | Emitted when the global token rewards rate is updated. |
| `RewardsClaimed(account, recipient, amount)` | Emitted when an operator pool claims rewards. |
| `RewardsRecipientSet(account, recipient)` | Emitted when a staker's reward recipient is updated. |
| `TokensReleased(recipient, amount)` | Emitted when tokens are released to a recipient after the unstaking cooldown period. |
| `TokensStaked(account, amount)` | Emitted when $ZAMA is staked into the protocol. |
| `TokensUnstaked(account, amount, releaseTime)` | Emitted when an unstake is requested, initiating the cooldown. |
| `UnstakeCooldownPeriodSet(unstakeCooldownPeriod)` | Emitted when the owner adjusts the unstaking waiting period. |
### Errors
| Error | Cause |
| --------------------------------- | ------------------------------------------------------------------------- |
| `InvalidEligibleAccount(account)` | The zero address was attempted to be added to the eligible accounts list. |
| `InvalidUnstakeCooldownPeriod()` | The requested cooldown period is invalid. |
| `TransferDisabled()` | An attempt was made to transfer to or from the zero address. |
## Contract: OperatorStaking
The `OperatorStaking` contract serves as a dedicated staking pool for a specific network operator. It enables delegators to pool their $ZAMA tokens, which are then collectively staked into the `ProtocolStaking` contract to earn rewards.
### Operator Staking shares
Each operator has their own `OperatorStaking` instance acting as an [ERC4626](https://eips.ethereum.org/EIPS/eip-4626)-compliant vault. When users delegate $ZAMA, they receive operator-specific staking shares representing their proportional ownership of the pool's assets and future rewards. Each share token has a unique symbol and name to make them easily identifiable:
* **Symbol:** `stZAMA-<name>-<role>`
* **Name:** `<name> Staked ZAMA (<role>)`
Where:
* `<name>` is the operator name (e.g., `Zama`, `OpenZeppelin`, etc.).
* `<role>` is the role of the staking contract (`Coprocessor` or `KMS`).
These token shares are fully transferable and use 20 decimals.
#### Operator Staking decimals
To mitigate the well-known ERC4626 inflation attack, the `OperatorStaking` contract implements a decimal offset of 2. This means that 1 unit of the underlying asset is represented as 100 units of shares.
Because the underlying staked asset ($ZAMA) has 18 decimals, the resulting operator staking shares will always possess 20 decimals. When interacting with the contracts or calculating balances, it is important to remember this distinction.
For example, when looking at the total stake of a pool or calculating historical rewards across different contracts:
* Calling `totalSupply()` on an `OperatorStaking` contract returns the total pool shares in the form of virtual shares. If the value returned is **100 \* 10^20**, this equates to 100 `$stZAMA-Zama-KMS` shares because the shares use 20 decimals.
### The controller
`OperatorStaking` decouples share ownership from withdrawal management. This is handled via the **controller** role.
Every redemption request is tracked against a controller address rather than the share owner. This allows a user to delegate the administrative task of "watching the cooldown" to a separate address (like a hot wallet or a bot) without giving that address full control over their shares.
#### Authorize redemption operator
A controller can further delegate their power by calling `setOperator()`. An authorized operator can call the `redeem()` function on behalf of the controller.
```solidity
// msg.sender (the controller) authorizes operatorAddress
operatorStaking.setOperator(operatorAddress, true);
```
### User functions
#### Delegate (deposit)
Delegates $ZAMA tokens to the operator pool, minting staking shares proportional to the deposit.
See [Delegate $ZAMA](#delegate-usdzama) in the Quick Start guide.
#### Delegate with permit
Combines an [EIP-2612](https://eips.ethereum.org/EIPS/eip-2612) approval with delegation into a single transaction.
```solidity
uint256 shares = operatorStaking.depositWithPermit(assets, receiver, deadline, v, r, s);
```
#### Request redeem
Initiates the first step of the two-step unstaking process. Burns the specified shares and starts the cooldown timer. Returns the timestamp when the assets will be claimable.
See [Redeem shares](#redeem-shares) in the Quick Start guide.
#### Redeem
Completes a redemption after the cooldown has passed. Must be called by the [controller](#the-controller) (or an authorized operator). Pass `type(uint256).max` to redeem all claimable shares.
See [Redeem shares](#redeem-shares) in the Quick Start guide.
#### Stake excess
Restakes any excess liquid $ZAMA held by the `OperatorStaking` contract back into the `ProtocolStaking` contract. Excess tokens can accumulate from direct $ZAMA donations or transfers to the contract, or from unredeemed slashed positions.
{% hint style="info" %}
Note that slashing has not been implemented yet on the protocol.
{% endhint %}
```solidity
operatorStaking.stakeExcess();
```
### Owner functions
#### Set rewarder
Replaces the linked `OperatorRewarder` contract. This is a multi-step process handled internally by the `OperatorStaking` contract:
1. **Shutdown old rewarder**: The current `OperatorRewarder` is shut down, preventing future reward distributions from the old contract and allowing for final state snapshots.
2. **Update recipient**: The `ProtocolStaking` contract's `setRewardsRecipient` function is called to redirect all future $ZAMA rewards to the new rewarder contract.
3. **Start new rewarder**: Finally, the new `OperatorRewarder` is initialized via its `start()` function to begin tracking and distributing rewards.
```solidity
operatorStaking.setRewarder(newRewarderAddress);
```
### View functions
#### Get asset
Returns the address of the underlying staking asset ($ZAMA).
```solidity
address stakingAsset = operatorStaking.asset();
```
#### Get ProtocolStaking
Returns the address of the linked `ProtocolStaking` contract.
```solidity
address protocolStakingAddr = address(operatorStaking.protocolStaking());
```
#### Get rewarder
Returns the address of the currently linked `OperatorRewarder` contract.
```solidity
address rewarderAddr = operatorStaking.rewarder();
```
#### Get owner
Returns the owner address (inherited from `ProtocolStaking`).
```solidity
address protocolOwner = operatorStaking.owner();
```
#### Get total assets
Returns the total $ZAMA managed by this pool, including staked, liquid, and awaiting-release balances.
```solidity
uint256 total = operatorStaking.totalAssets();
```
#### Get pending redeem request
Returns the number of shares still not yet eligible for redemption in the cooldown queue for a given controller.
```solidity
uint256 pending = operatorStaking.pendingRedeemRequest(controllerAddress);
```
#### Get claimable redeem request
Returns the number of shares whose cooldown has elapsed and are now redeemable for a given controller, minus any shares already redeemed.
```solidity
uint256 claimable = operatorStaking.claimableRedeemRequest(controllerAddress);
```
#### Get total shares in redemption
Returns the total number of shares across all in-flight redemption requests.
```solidity
uint256 inFlight = operatorStaking.totalSharesInRedemption();
```
#### Preview deposit
Returns the number of shares that would be minted for a given deposit amount.
```solidity
uint256 shares = operatorStaking.previewDeposit(assets);
```
#### Preview redeem
Returns the amount of $ZAMA that would be received when redeeming a given number of shares.
```solidity
uint256 assets = operatorStaking.previewRedeem(shares);
```
#### Check operator approval
Returns `true` if `operator` is authorized to manage redemptions for `controller`.
```solidity
bool approved = operatorStaking.isOperator(controller, operator);
```
#### Get decimals
Returns the number of decimals for the share token. Always returns `20` due to the ERC4626 decimal offset.
```solidity
uint8 shareDecimals = operatorStaking.decimals();
```
### Events
| Event | Description |
| ----------------------------------------------------------------------- | ---------------------------------------------------------- |
| `OperatorSet(controller, operator, approved)` | Emitted when an operator approval is set for a controller. |
| `RedeemRequest(controller, owner, sender, shares, assets, releaseTime)` | Emitted when a user requests to redeem shares. |
| `RewarderSet(oldRewarder, newRewarder)` | Emitted when the Operator Rewarder contract is changed. |
### Errors
| Error | Cause |
| --------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
| `CallerNotProtocolStakingOwner(caller)` | The caller is not the owner of the contract. |
| `InvalidController()` | The controller address is zero. |
| `InvalidRewarder(rewarder)` | The new rewarder address is invalid. |
| `InvalidShares()` | The number of shares to redeem or request redemption is zero. |
| `NoExcessBalance(liquidBalance, assetsPendingRedemption)` | The liquid asset balance is insufficient to cover pending redemptions in `stakeExcess()`. |
| `Unauthorized()` | The caller to the redeem function is not the controller or an operator set by the controller. |
## Contract: OperatorRewarder
The `OperatorRewarder` handles the distribution of rewards and the claiming of operator commission fees. Every `OperatorStaking` pool has one corresponding `OperatorRewarder` contract, and the owner has the authority to set the rewarder contract for any `OperatorStaking` contract.
### OperatorRewarder beneficiary
The beneficiary of an `OperatorRewarder` contract is the address that can set and claim fees. The beneficiary is set on the deployment of the `OperatorRewarder` contract and can be changed by the owner through the [transferBeneficiary()](#transfer-beneficiary) function.
To find the beneficiary of an `OperatorRewarder` contract, you can use the [beneficiary()](#get-beneficiary) view function.
An `OperatorRewarder` beneficiary has the authority to change the fee percentage for the associated contract through the [setFee()](#set-commission-fee) function. The fee percentage is set in basis points, where 10000 is 100%. Note that fees are subject to a maximum of 20% (2000 basis points) set by the owner.
If an operator wants to receive "regular" staking rewards in addition to their commission fee, they can simply act as a delegator by staking assets into their own `OperatorStaking` contract. They would then receive both:
* The **Commission Fee** on the pool's total generated rewards.
* The **Proportional Reward** for the assets they personally staked.
### User functions
#### Claim rewards
See [Claim staking rewards](#claim-staking-rewards) in the Quick Start guide for full details.
#### Set rewards claimer
See [Set rewards claimer](#set-rewards-claimer)프로젝트로 돌아가기