BStaking Contract Reference

The BStaking contract handles staking operations for bTokens, enabling users to earn protocol trading fees.

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
ParameterTypeDescription
_bTokenBTokenThe bToken to stake
_useraddressUser receiving the stake position
_amountuint256Amount 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
ParameterTypeDescription
_bTokenBTokenThe staked bToken
_useraddressUser withdrawing
_amountuint256Amount 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) external

Rewards Functions

claim

Claim accumulated rewards.

function claim(BToken _bToken, address _user) external returns (uint256 amount_)
ParameterTypeDescription
_bTokenBTokenThe staked bToken
_useraddressUser 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

ErrorDescription
BStaking_StakeIsLockedAttempting to withdraw during lock period
BStaking_BTokenNotInitializedBToken pool not initialized

Reward Distribution

Rewards are distributed proportionally based on:

  1. Share of total stake : Your staked amount / total staked
  2. Time staked : Rewards accumulate over time
  3. 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);