Skip to main content

OpenPayPriceFeed

The OpenPayPriceFeed contract aggregates price data from Chainlink decentralized oracles to convert between USD and token amounts. It includes stablecoin shortcuts and staleness protection. Address (BSC Testnet): 0x1f34e070D4BB1eD3AaF37D8E3297b0a9A12a3399 Explorer: View on BscScan

How It Works

Key Design Decisions

Tokens marked as stablecoins (USDT, USDC) skip the oracle call entirely and return a fixed price of $1.00 (represented as 100000000 with 8 decimals). This saves gas and avoids unnecessary oracle dependencies.
The contract rejects price data older than the stalenessThreshold (default: 3600 seconds / 1 hour). If the Chainlink oracle has not updated within this window, all price queries revert with “Stale price”.
BNB (native token, address(0)) uses a dedicated nativeFeed Chainlink oracle rather than the token config mapping. This is set separately via setNativeFeed().

Read Functions

getPrice

Get the current USD price and decimal precision for a token.
Behavior:
  • For stablecoins: returns (100000000, 8) — equivalent to $1.00
  • For address(0): queries the nativeFeed Chainlink oracle
  • For other tokens: queries the token-specific Chainlink feed

getAmountInToken

Convert a USD amount to the equivalent token amount at the current price.
Example: “How many BNB tokens do I need to pay $25?“

getAmountInUsd

Convert a token amount to its USD equivalent at the current price.
Example: “How much USD is 0.5 BNB worth?”

Token Configuration

configureToken

Register a token with its Chainlink price feed. Owner only.
Example configurations:

tokenConfigs

Read the configuration for a registered token.

Admin Functions (Owner Only)

setNativeFeed

Set the Chainlink price feed for the native token (BNB).

setStalenessThreshold

Set the maximum age (in seconds) for oracle price data. Minimum: 60 seconds.
Setting the threshold too low may cause price queries to fail frequently during periods of low oracle update frequency. The default of 3600 seconds (1 hour) is appropriate for most use cases.

State Variables

Events

Example: Querying Prices with ethers.js

For mainnet deployment, use the official Chainlink BSC feeds:
Find all available Chainlink feeds at data.chain.link.