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.

solidity
constructor(
    address initialOwner,
    address agentRegistry_,
    address treasury_,
    uint256 minimumStake_,
    uint256 unstakingPeriod_
)

Accounting model#

  • pooled ETH 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.
solidity
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#

NameTypeDefaultDescription
amountuint256-Current ETH value of active (non-unbonding) shares.
sharesuint256-Active shares.
stakedAtuint256-First stake timestamp of the current position.
pendingRewardsuint256-Claimable rewards, including accrued ones.
unbondingAmountuint256-Current ETH value of all unbonding shares.
withdrawableAmountuint256-Current ETH value of unbonding shares that are already unlocked.
nextUnlockTimeuint256-Earliest unlock time among still-locked requests (0 if none).

These are the fields of StakeInfo, returned by getStake.

Constants#

NameValue
MAX_BPS10,000
MAX_SLASH_BPS5000 (50%)
MAX_UNSTAKING_PERIOD30 days
MAX_UNBONDING_REQUESTS20 per staker per agent

Staker functions#

FunctionAccessDescription
stake(bytes32 agentId) payableAnyone, not pausedAgent 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)StakerMoves 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)StakerPays out all unlocked unbonding requests at the current share price. Emits Withdrawn.
claimRewards(bytes32 agentId)StakerPays out pending rewards. Emits RewardsClaimed.

Integration functions#

FunctionAccessDescription
distributeRewards(bytes32 agentId) payableAnyone (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 ownerRemoves pooled * percentage / 10000 (1 to 5000 bps), sends it to the treasury, and calls AgentRegistry.slashAgent. Emits Slashed.

View functions#

FunctionReturns
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#

FunctionDescription
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#

solidity
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#

ErrorWhen
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.