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_)| Parameter | Type | Description |
|---|---|---|
_bToken | BToken | The bToken to buy |
_amountIn | uint256 | Exact reserve amount to spend |
_limitAmount | uint256 | Minimum bTokens to receive (slippage protection) |
Returns:
amountOut_: bTokens receivedfeesReceived_: Fees collected
Errors:
SlippageExceeded: IfamountOut_ < _limitAmount
buyTokensExactOut
Buy an exact amount of bTokens.
function buyTokensExactOut(
BToken _bToken,
uint256 _amountOut,
uint256 _limitAmount
) external returns (uint256 amountIn_, uint256 feesReceived_)| Parameter | Type | Description |
|---|---|---|
_bToken | BToken | The bToken to buy |
_amountOut | uint256 | Exact bTokens to receive |
_limitAmount | uint256 | Maximum 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_)| Parameter | Type | Description |
|---|---|---|
_bToken | BToken | The bToken to sell |
_amountIn | uint256 | Exact bTokens to sell |
_limitAmount | uint256 | Minimum 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_)| Parameter | Type | Description |
|---|---|---|
_bToken | BToken | The bToken to sell |
_amountOut | uint256 | Exact reserves to receive |
_limitAmount | uint256 | Maximum 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 invariantblv: Current BLV (floor price)swapFeePct: Fee percentage
Errors
| Error | Description |
|---|---|
SlippageExceeded | Trade output doesn't meet limit |
SolverFailed | Numerical solver failed to converge |
AmountExceedsLiquidity | Requested 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
- BMM Mechanics: Power-law curve explanation
- BLens Contract : View functions