BSwap Contract Reference

The BSwap contract implements Baseline's power-law AMM curve for token trading. It handles all buy and sell operations, applying the n=2 curve with BLV translation.

📝

Contract: BSwap.sol Version: 21 License: AGPL-3.0-or-later

Overview

BSwap provides four trading entry points (exact input/output for buys and sells) and corresponding quote functions. All trades execute through the power-law curve:

y_buffer = K × (x/c)²

Functions

Trading Functions

buyTokensExactIn

Buy bTokens with an exact amount of reserves.

function buyTokensExactIn(
  BToken _bToken,
  uint256 _amountIn,
  uint256 _limitAmount
) external returns (uint256 amountOut_, uint256 feesReceived_)
ParameterTypeDescription
_bTokenBTokenThe bToken to buy
_amountInuint256Exact reserve amount to spend
_limitAmountuint256Minimum bTokens to receive (slippage protection)

Returns:

  • amountOut_: bTokens received
  • feesReceived_: Fees collected

Errors:

  • SlippageExceeded: If amountOut_ < _limitAmount

buyTokensExactOut

Buy an exact amount of bTokens.

function buyTokensExactOut(
  BToken _bToken,
  uint256 _amountOut,
  uint256 _limitAmount
) external returns (uint256 amountIn_, uint256 feesReceived_)
ParameterTypeDescription
_bTokenBTokenThe bToken to buy
_amountOutuint256Exact bTokens to receive
_limitAmountuint256Maximum reserves to spend (slippage protection)

sellTokensExactIn

Sell an exact amount of bTokens.

function sellTokensExactIn(
  BToken _bToken,
  uint256 _amountIn,
  uint256 _limitAmount
) external returns (uint256 amountOut_, uint256 feesReceived_)
ParameterTypeDescription
_bTokenBTokenThe bToken to sell
_amountInuint256Exact bTokens to sell
_limitAmountuint256Minimum reserves to receive (slippage protection)

sellTokensExactOut

Sell bTokens to receive an exact amount of reserves.

function sellTokensExactOut(
  BToken _bToken,
  uint256 _amountOut,
  uint256 _limitAmount
) external returns (uint256 amountIn_, uint256 feesReceived_)
ParameterTypeDescription
_bTokenBTokenThe bToken to sell
_amountOutuint256Exact reserves to receive
_limitAmountuint256Maximum bTokens to sell (slippage protection)

Quote Functions

Quote functions return expected trade results without executing.

quoteBuyExactIn

function quoteBuyExactIn(BToken _bToken, uint256 _amountIn) 
  external view returns (uint256 amountOut_, uint256 fee_)

quoteBuyExactOut

function quoteBuyExactOut(BToken _bToken, uint256 _amountOut) 
  external view returns (uint256 amountIn_, uint256 fee_)

quoteSellExactIn

function quoteSellExactIn(BToken _bToken, uint256 _amountIn) 
  external view returns (uint256 amountOut_, uint256 fee_)

quoteSellExactOut

function quoteSellExactOut(BToken _bToken, uint256 _amountOut) 
  external view returns (uint256 amountIn_, uint256 fee_)

View Functions

getCurveParams

Get current curve parameters for a bToken.

function getCurveParams(BToken _bToken) external view returns (CurveParams memory)

Returns: CurveParams struct containing:

  • k: Curve invariant
  • blv: Current BLV (floor price)
  • swapFeePct: Fee percentage

Errors

ErrorDescription
SlippageExceededTrade output doesn't meet limit
SolverFailedNumerical solver failed to converge
AmountExceedsLiquidityRequested amount exceeds available liquidity

Events

event Swap(
  BToken bToken,
  address sender,
  bool isBuy,
  uint256 amountIn,
  uint256 amountOut,
  uint256 fee
);

Usage Example

// Buy 100 bTokens, paying at most 110 ETH
uint256 amountIn;
uint256 fees;
(amountIn, fees) = bswap.buyTokensExactOut(
  bToken,
  100e18,   // 100 tokens
  110e18    // max 110 reserves
);
 
// Get quote first
(uint256 quotedOut, uint256 quotedFee) = bswap.quoteBuyExactIn(
  bToken,
  100e18    // 100 reserves
);

Related