BStaking Contract Reference
The BStaking contract handles staking operations for bTokens. Users can stake their tokens to earn a share of protocol trading fees.
Contract: BStaking.sol
Version: 1
License: AGPL-3.0-or-later
Overview
BStaking implements a reward accumulator pattern where fees are distributed proportionally to staked balances. Stakers earn rewards in the reserve asset (e.g., ETH/WETH).
Functions
Staking Functions
deposit
Stake bTokens to earn rewards.
function deposit(BToken _bToken, address _user, uint256 _amount) external| Parameter | Type | Description |
|---|---|---|
_bToken | BToken | The bToken to stake |
_user | address | User receiving the stake position |
_amount | uint256 | Amount of bTokens to stake |
Requirements:
- Caller must have approved BStaking to transfer bTokens
- BToken must be initialized
withdraw
Withdraw staked bTokens.
function withdraw(BToken _bToken, address _user, uint256 _amount) external| Parameter | Type | Description |
|---|---|---|
_bToken | BToken | The staked bToken |
_user | address | User withdrawing |
_amount | uint256 | Amount to withdraw |
Errors:
BStaking_StakeIsLocked: If stake is in lock period
withdrawMax
Withdraw all staked bTokens.
function withdrawMax(BToken _bToken, address _user) external returns (uint256 amount_)withdrawAndClaim
Withdraw staked tokens and claim earned rewards in one transaction.
function withdrawAndClaim(BToken _bToken, address _user, uint256 _amount) externalRewards Functions
claim
Claim accumulated rewards.
function claim(BToken _bToken, address _user) external returns (uint256 amount_)| Parameter | Type | Description |
|---|---|---|
_bToken | BToken | The staked bToken |
_user | address | User claiming rewards |
Returns:
amount_: Reserve tokens claimed
View Functions
getEarned
Get unclaimed rewards for a user.
function getEarned(BToken _bToken, address _user) external view returns (uint256)getAccumulator
Get current reward accumulator value.
function getAccumulator(BToken _bToken) external view returns (uint256)getCurrentRate
Get current reward distribution rate.
function getCurrentRate(BToken _bToken) external view returns (uint256)Events
event Deposit(BToken bToken, address user, uint256 amount, State.StakedAccount post);
event Withdraw(BToken bToken, address user, uint256 amount, State.StakedAccount post);
event Claim(BToken bToken, address user, uint256 amount);
event Liquidate(BToken bToken, address user, uint256 amount, State.StakedAccount post);Errors
| Error | Description |
|---|---|
BStaking_StakeIsLocked | Attempting to withdraw during lock period |
BStaking_BTokenNotInitialized | BToken pool not initialized |
Reward Distribution
Rewards are distributed proportionally based on:
- Share of total stake : Your staked amount / total staked
- Time staked : Rewards accumulate over time
- Trading volume : More fees = more rewards
The accumulator pattern ensures fair distribution regardless of when you stake.
Usage Example
// Stake 1000 bTokens
bToken.approve(address(bstaking), 1000e18);
bstaking.deposit(bToken, msg.sender, 1000e18);
// Check earned rewards
uint256 earned = bstaking.getEarned(bToken, msg.sender);
// Claim rewards
uint256 claimed = bstaking.claim(bToken, msg.sender);
// Withdraw and claim in one tx
bstaking.withdrawAndClaim(bToken, msg.sender, 500e18);Related
- Staking Guide : How to stake
- BLens Contract : View staking state