Skip to main content

Overview

RektHub uses optimized libraries for bonding curve calculations. These utilities handle the constant product math that powers price discovery and trading.
For Integrators: You typically don’t need to use these libraries directly—the BondingCurve contract handles all calculations. This reference is useful if you’re building off-chain quoters, simulators, or custom trading algorithms.

BondingCurveMath

The core library for constant product (x × y = k) bonding curve calculations. Location: contracts/libraries/BondingCurveMath.sol

Purpose

  • Calculate token outputs for buy orders
  • Calculate native outputs for sell orders
  • Calculate fees as percentages (basis points)
  • Pure functions—no state modifications

Functions

calculateBuyReturn

Calculate how many tokens a buyer will receive for a given native token input.
Parameters:
  • nativeAmount (uint256): Amount of native tokens to spend (after fees)
  • virtualNativeReserves (uint256): Current virtual native reserves in the curve
  • virtualTokenReserves (uint256): Current virtual token reserves in the curve
Returns:
  • tokensOut: Amount of tokens the buyer will receive
Formula:
This implements the constant product formula: x * y = k Example Usage:
Important: This calculates the raw output before considering real reserve limits. The BondingCurve contract takes the minimum of this calculation and available real tokens.

calculateSellReturn

Calculate how many native tokens a seller will receive for selling tokens.
Parameters:
  • tokenAmount (uint256): Amount of tokens to sell
  • virtualNativeReserves (uint256): Current virtual native reserves in the curve
  • virtualTokenReserves (uint256): Current virtual token reserves in the curve
Returns:
  • nativeOut: Amount of native tokens the seller will receive (before fees)
Formula:
Example Usage:
Fee Deduction: calculateSellReturn returns the gross amount before fees. Always subtract the protocol fee (1%) to get the net amount users receive.

calculateFee

Calculate a fee as a percentage using basis points.
Parameters:
  • amount (uint256): Amount to calculate fee on
  • feeBasisPoints (uint256): Fee in basis points (100 = 1%, 10000 = 100%)
Returns:
  • fee: Calculated fee amount
Formula:
Example Usage:
Basis Points: 1 basis point = 0.01%. Common values: 1% = 100 bps, 5% = 500 bps, 10% = 1000 bps, 100% = 10000 bps

Next Steps

Bonding Curves Explained

Understand the economics

Bonding Curve Contract

See how it’s implemented

Events Reference

Track trades with events

Interfaces

Contract interfaces and ABIs