Price Feed

Oracle-style price feed with staleness detection

Deployed Anchor 0.32
Program ID 6uafjLqqMeP4DgYqDuVPCL9BVabC7ruQC6SnDdUvXqC5
Binary Size 215K
Cluster Devnet

Instructions

IX initialize_feed

Creates a new price feed for a trading pair with the specified decimal precision.

ParameterTypeDescription
symbolStringTrading pair symbol, e.g. "SOL/USD" (max 16 characters)
decimalsu8Number of decimal places for the price value
IX update_price

Publishes a new price update with a confidence interval. Only callable by the authority.

ParameterTypeDescription
pricei64Current price value (signed to support negative rates)
confidenceu64Confidence interval around the price
IX read_price

Reads the current price from the feed. Returns an error if the price data is stale.

ParameterTypeDescription
No parameters
IX transfer_authority

Transfers the price update authority to a new wallet.

ParameterTypeDescription
new_authorityPubkeyThe new authority wallet address

Accounts

PriceFeed

FieldTypeDescription
authorityPubkeyOracle operator with update permissions
symbolStringTrading pair symbol (e.g. "SOL/USD")
decimalsu8Price decimal precision
pricei64Latest price value
confidenceu64Confidence interval width
last_updatei64Timestamp of the last price update
update_countu64Total number of price updates
bumpu8PDA bump seed

PDA Derivation

feed
seeds = [b"feed", authority]

Error Codes

SymbolTooLong Feed symbol exceeds the maximum 16 character limit
StalePrice The price data has not been updated recently enough to be reliable
Overflow Arithmetic overflow in price calculation

Usage Example

import { Program, BN } from "@coral-xyz/anchor";
import { PublicKey } from "@solana/web3.js";

// Initialize a SOL/USD price feed with 8 decimal places
const [feedPda] = PublicKey.findProgramAddressSync(
  [Buffer.from("feed"), authority.publicKey.toBuffer()],
  program.programId
);

await program.methods
  .initializeFeed("SOL/USD", 8)
  .accounts({
    feed: feedPda,
    authority: authority.publicKey,
  })
  .signers([authority])
  .rpc();

// Publish a price update: $145.50 with $0.25 confidence
await program.methods
  .updatePrice(
    new BN(14_550_000_000),    // price: $145.50 (8 decimals)
    new BN(25_000_000)         // confidence: $0.25
  )
  .accounts({
    feed: feedPda,
    authority: authority.publicKey,
  })
  .signers([authority])
  .rpc();

// Read the current price
const feedData = await program.account.priceFeed.fetch(feedPda);
console.log(`Price: ${feedData.price.toNumber() / 1e8}`);
console.log(`Confidence: +/- ${feedData.confidence.toNumber() / 1e8}`);