StakingPool
Share-based ETH staking per agent with an O(1) reward accumulator, unbonding, and pro-rata slashing.
StakingPool lets anyone stake ETH behind an agent, earn a share of its query fees, and be slashed for
its misbehavior. Inherits Ownable2Step, Pausable, and ReentrancyGuard.
constructor(
address initialOwner,
address agentRegistry_,
address treasury_,
uint256 minimumStake_,
uint256 unstakingPeriod_
)Accounting model#
pooledETH backs all shares of an agent, active and unbonding, so a slash lowers the share price pro-rata in O(1) and unbonding stake can't escape it.- Rewards use an accumulator over active shares only, so distributing and claiming never loop over stakers.
struct Pool {
uint256 totalShares; // Active + unbonding shares
uint256 activeShares; // Shares earning rewards
uint256 pooled; // ETH backing totalShares
uint256 accRewardPerShare; // Scaled by 1e18
uint256 stakerCount; // Stakers with active shares
}Types#
| Name | Type | Default | Description |
|---|---|---|---|
amount | uint256 | - | Current ETH value of active (non-unbonding) shares. |
shares | uint256 | - | Active shares. |
stakedAt | uint256 | - | First stake timestamp of the current position. |
pendingRewards | uint256 | - | Claimable rewards, including accrued ones. |
unbondingAmount | uint256 | - | Current ETH value of all unbonding shares. |
withdrawableAmount | uint256 | - | Current ETH value of unbonding shares that are already unlocked. |
nextUnlockTime | uint256 | - | Earliest unlock time among still-locked requests (0 if none). |
These are the fields of StakeInfo, returned by getStake.
Constants#
| Name | Value |
|---|---|
MAX_BPS | 10,000 |
MAX_SLASH_BPS | 5000 (50%) |
MAX_UNSTAKING_PERIOD | 30 days |
MAX_UNBONDING_REQUESTS | 20 per staker per agent |
Staker functions#
| Function | Access | Description |
|---|---|---|
stake(bytes32 agentId) payable | Anyone, not paused | Agent must be Active; msg.value must be non-zero and at least the minimum stake. Mints shares at the current price. Emits Staked. |
unstake(bytes32 agentId, uint256 amount) | Staker | Moves the shares worth amount (rounded up) into a new unbonding request unlocking after the unstaking period. type(uint256).max unbonds everything. Harvests rewards first. Emits Unstaked. |
withdraw(bytes32 agentId) | Staker | Pays out all unlocked unbonding requests at the current share price. Emits Withdrawn. |
claimRewards(bytes32 agentId) | Staker | Pays out pending rewards. Emits RewardsClaimed. |
Integration functions#
| Function | Access | Description |
|---|---|---|
distributeRewards(bytes32 agentId) payable | Anyone (called by the PaymentRouter) | Adds msg.value to the agent's reward accumulator. If the agent has no active shares, forwards it to the treasury. Emits RewardsDistributed. |
slash(bytes32 agentId, uint256 percentage, string reason) | Authorized slasher or owner | Removes pooled * percentage / 10000 (1 to 5000 bps), sends it to the treasury, and calls AgentRegistry.slashAgent. Emits Slashed. |
View functions#
| Function | Returns |
|---|---|
getStake(bytes32 agentId, address staker) | StakeInfo for the position. |
getTotalStake(bytes32 agentId) | ETH value of the agent's active shares. |
getStakerCount(bytes32 agentId) | Stakers with active shares. |
getPool(bytes32 agentId) | Raw Pool accounting. |
getMinimumStake() | Minimum ETH per stake call. |
getUnstakingPeriod() | Unbonding period in seconds. |
treasury(), agentRegistry(), authorizedSlashers(address) | Configuration. |
Admin functions#
| Function | Description |
|---|---|
setMinimumStake(uint256 minimum) | Emits MinimumStakeUpdated. |
setUnstakingPeriod(uint256 period) | At most 30 days. Applies to new unbonding requests. Emits UnstakingPeriodUpdated. |
setAuthorizedSlasher(address slasher, bool authorized) | Emits SlasherAuthorized. |
setTreasury(address treasury_) | Emits TreasuryUpdated. |
pause() / unpause() | Pausing blocks new stakes only. Unstaking, withdrawals, rewards, and slashing keep working. |
All admin functions are owner only. The deploy script doesn't authorize any slasher on the StakingPool,
so only the owner can slash until one is added.
Events#
event Staked(bytes32 indexed agentId, address indexed staker, uint256 amount, uint256 shares);
event Unstaked(bytes32 indexed agentId, address indexed staker, uint256 amount, uint256 shares, uint256 unlockTime);
event Withdrawn(bytes32 indexed agentId, address indexed staker, uint256 amount);
event Slashed(bytes32 indexed agentId, uint256 amount, string reason);
event RewardsDistributed(bytes32 indexed agentId, uint256 amount);
event RewardsClaimed(bytes32 indexed agentId, address indexed staker, uint256 amount);
event MinimumStakeUpdated(uint256 oldMinimum, uint256 newMinimum);
event UnstakingPeriodUpdated(uint256 oldPeriod, uint256 newPeriod);
event SlasherAuthorized(address indexed slasher, bool authorized);
event TreasuryUpdated(address oldTreasury, address newTreasury);Errors#
| Error | When |
|---|---|
AgentNotActive(bytes32 agentId) | Staking on an agent that isn't Active. |
InsufficientStake(uint256 required, uint256 provided) | Zero or below-minimum stake, or a stake too small to mint a share. |
InsufficientBalance(uint256 requested, uint256 available) | Unstaking more than the position is worth. |
TooManyUnbondingRequests() | Already 20 open unbonding requests. |
NoPendingUnstake() | withdraw with no unbonding requests. |
UnstakingPeriodNotElapsed(uint256 unlockTime) | withdraw before any request unlocked; returns the earliest unlock time. |
PoolInsolvent(bytes32 agentId) | Staking into a pool whose shares are backed by no ETH. |
InvalidSlashPercentage(uint256 percentage) | Slash of 0 or above 5000 bps. |
InvalidUnstakingPeriod(uint256 period) | Unstaking period above 30 days. |
NotAuthorizedSlasher(address caller) | slash from an unauthorized address. |
NothingToClaim() | claimRewards with no rewards. |
TransferFailed() | An ETH transfer failed. |
ZeroAddress() | Zero address in the constructor or a setter. |