futurePrice

Futures

Computes the fair price of a futures contract using continuous compounding.

Avg. gas

400

Max abs. error

1.2e-9

at a $1,000 spot

Max rel. error

2e-12

Signature

solidity
function futurePrice(
    uint128 spot,
    uint32  timeToExp,
    uint64  rate
) internal pure returns (uint256 price)

Parameters

NameTypeDescription
spotuint128Current spot price of the underlying, 18-decimal fixed-point.
timeToExpuint32Time to contract expiration in seconds. timeToExp == 0 returns spot unchanged.
rateuint64Annualized cost-of-carry (risk-free) rate, 18-decimal fixed-point.

Returns

NameTypeDescription
priceuint256Futures price in 18-decimal fixed-point. Always ≥ spot for a non-negative rate.

Bounds

BoundValue
MIN_SPOT1e-6 smallest allowed spot price (1e12)
MAX_SPOT1e15 largest allowed spot price (1e33)
MAX_EXPIRATION2 years (63,072,000 seconds)
MAX_RATE400% annual (4e18)

Behavior

  • Validates all three inputs against module-wide constants and reverts with a typed error on any violation.
  • No strike and no volatility — a futures price depends only on spot, time, and the cost-of-carry rate.
  • Fast-path on expiration: when timeToExp == 0, returns spot unchanged (the carry factor is e⁰ = 1).
  • Shorter horizon than the options modules: MAX_EXPIRATION is 2 years, not 32.
  • Composes a single DeFiMath primitive — expPositive, a fast path of exp for the non-negative r·τ guaranteed by validation.
  • Pure internal function; no external calls, no storage. Inlined into the caller's bytecode at compile time.

How it works

The fair forward/futures price is the spot compounded continuously at the cost-of-carry rate to expiry:

F=SerTF = S \cdot e^{r T}

timeToExp (seconds) is annualized by dividing by SECONDS_IN_YEAR, the exponent r·τ is formed, and the carry factor e^(r·τ) is evaluated with Math.expPositive — a branch of exp specialized for non-negative inputs, which the input bounds guarantee. A single multiply by spot gives the price. That one transcendental is why it lands at just ~400 gas.

Because the price scales linearly with spot, its error is fundamentally relative and scale-invariant: 2e-12, inherited from exp. The 1.2e-9 absolute figure is that same error expressed in dollars at a $1,000 spot; it scales with the underlying. Head-to-head measurements live in defimath-compare.

Errors

ErrorTrigger
SpotLowerBoundErrorspot ≤ MIN_SPOT
SpotUpperBoundErrorspot ≥ MAX_SPOT
TimeToExpiryUpperBoundErrortimeToExp ≥ MAX_EXPIRATION
RateUpperBoundErrorrate ≥ MAX_RATE

Example

solidity
import "defimath-lib/contracts/derivatives/Futures.sol";

uint256 price = Futures.futurePrice(
    1000e18,         // spot = $1,000
    90 days,         // 90 days to expiry
    0.05e18          // 5% cost-of-carry rate
);
// price ≈ 1012.4e18  (spot compounded at 5% for 90 days)