CCIP v2.0.0 USDPriceWith18Decimals API Reference

USDPriceWith18Decimals provides utilities for converting between token amounts and USD values using 18-decimal fixed-point precision.

It assumes:

  • token prices are expressed in USD with 18 decimals
  • all calculations are performed using integer arithmetic

This ensures consistent and deterministic fee and value calculations across CCIP contracts.

This library provides reusable helper functions and is not deployed as a standalone application-facing contract.

Usage Boundary

You do not call this library directly.

  • CCIP contracts use this library to convert between token amounts and USD values.
  • It is used internally for fee calculation and execution policy logic.
  • You should ensure that token prices are provided in the correct 18-decimal format.

Contract

libraries/USDPriceWith18Decimals.sol

Import

import {USDPriceWith18Decimals} from "chainlink-ccip/libraries/USDPriceWith18Decimals.sol";

Functions

_calcUSDValueFromTokenAmount

function _calcUSDValueFromTokenAmount(
  uint224 tokenPrice,
  uint256 tokenAmount
) internal pure returns (uint256)

Converts a token amount into its USD value using a fixed-point price.

ParameterTypeDescription
tokenPriceuint224Token price in USD with 18-decimal precision.
tokenAmountuint256Amount of tokens to convert.

Returns:

TypeDescription
uint256USD value with 18-decimal precision.

_calcTokenAmountFromUSDValue

function _calcTokenAmountFromUSDValue(
  uint224 tokenPrice,
  uint256 usdValue
) internal pure returns (uint256)

Converts a USD value into the corresponding token amount using a fixed-point price.

ParameterTypeDescription
tokenPriceuint224Token price in USD with 18-decimal precision.
usdValueuint256USD value with 18-decimal precision.

Returns:

TypeDescription
uint256Token amount.

Errors

No new custom errors declared.

Notes

  • All values are assumed to use 18-decimal fixed-point precision.
  • Division operations use integer arithmetic and round toward zero.
  • Conversions may lose precision due to integer division.
  • Token price must be non-zero to avoid division errors.
  • All interacting contracts must use the same pricing convention for correctness.

Usage context

Used by:

Get the latest Chainlink content straight to your inbox.