OpenPayPriceFeed
TheOpenPayPriceFeed 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
Stablecoin Shortcut
Stablecoin Shortcut
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.Staleness Protection
Staleness Protection
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”.Native Token Support
Native Token Support
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.| Parameter | Type | Description |
|---|---|---|
token | address | Token address, or address(0) for native BNB |
| Return Value | Type | Description |
|---|---|---|
price | uint256 | USD price (scaled by 10^decimals) |
decimals_ | uint8 | Price decimal precision (typically 8 for Chainlink) |
- For stablecoins: returns
(100000000, 8)— equivalent to $1.00 - For
address(0): queries thenativeFeedChainlink oracle - For other tokens: queries the token-specific Chainlink feed
getAmountInToken
Convert a USD amount to the equivalent token amount at the current price.| Parameter | Type | Description |
|---|---|---|
token | address | Token address |
usdAmount | uint256 | USD amount to convert |
getAmountInUsd
Convert a token amount to its USD equivalent at the current price.| Parameter | Type | Description |
|---|---|---|
token | address | Token address |
tokenAmount | uint256 | Token amount to convert |
Token Configuration
configureToken
Register a token with its Chainlink price feed. Owner only.| Parameter | Type | Description |
|---|---|---|
token | address | ERC-20 token address |
feed | address | Chainlink price feed address (can be address(0) for stablecoins) |
isStablecoin | bool | If true, returns fixed $1.00 price without oracle call |
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.State Variables
| Variable | Type | Default | Description |
|---|---|---|---|
nativeFeed | address | — | Chainlink BNB/USD feed address |
stalenessThreshold | uint256 | 3600 | Max oracle data age in seconds |
Events
Example: Querying Prices with ethers.js
Chainlink Price Feeds Reference
For mainnet deployment, use the official Chainlink BSC feeds:| Pair | Mainnet Feed | Testnet |
|---|---|---|
| BNB/USD | 0x0567F2323251f0Aab15c8dFb1967E4e8A7D42aeE | MockPriceFeed |
| BTC/USD | 0x264990fbd0A4796A3E3d8E37C4d5F87a3aCa5Ebf | MockPriceFeed |
| ETH/USD | 0x9ef1B8c0E4F7dc8bF5719Ea496883DC6401d5b2e | MockPriceFeed |