Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b523bd6
Add data-feeds-store program for Data Feeds on CRE
cawthorne Oct 19, 2026
042d71f
data-feeds-store: trust senders through the allowed workflows only
cawthorne Oct 19, 2026
1915e5f
data-feeds-store: drop SVR and proxies, add a reader interface
cawthorne Oct 22, 2026
108b3ad
Add the data-feeds-proxy program
cawthorne Oct 22, 2026
7e640de
data-feeds-store: align with the EVM store, skip bad reports, per-pro…
cawthorne Oct 29, 2026
d789c28
Merge branch 'feature/df-cre-solana-legacy-store' into tmp/df-proxy-a…
cawthorne Oct 29, 2026
5bd630f
data-feeds-proxy: CPI events, 32-byte feed config seeds, version checks
cawthorne Oct 29, 2026
e203fe3
data-feeds-store: fit large batches in the heap, unique error codes, …
cawthorne Oct 30, 2026
d0ab70a
Merge branch 'feature/df-cre-solana-legacy-store' into feat/df-proxy-…
cawthorne Oct 30, 2026
15e1c9f
data-feeds-proxy: unique error codes, version check before rewriting …
cawthorne Oct 30, 2026
e23d74c
data-feeds-store: review fixes for batch reads, admin heap and callba…
cawthorne Oct 31, 2026
0123f3f
Merge branch 'feature/df-cre-solana-legacy-store' into tmp/df-proxy-a…
cawthorne Oct 31, 2026
d60db0a
data-feeds clients: take answers only from the called program, pin th…
cawthorne Oct 31, 2026
0da54ab
data-feeds-store: test a report whose callback program was closed
cawthorne Nov 2, 2026
c81b4d7
data-feeds-proxy client: test the return data and program checks
cawthorne Nov 2, 2026
854af7b
Merge branch 'feature/df-cre-solana-legacy-store' into tmp/df-proxy-a…
cawthorne Nov 2, 2026
5ce1736
data-feeds-store: only the upgrade authority creates stores
cawthorne Nov 3, 2026
c05e58b
Merge branch 'feature/df-cre-solana-legacy-store' into tmp/df-proxy-a…
cawthorne Nov 3, 2026
fd5ca92
data-feeds-proxy: only the upgrade authority creates proxies
cawthorne Nov 3, 2026
d94ba87
data-feeds-store: keep descriptions in the feed account
cawthorne Nov 9, 2026
c3edec1
data-feeds: thin proxy owns the feeds, DF1 write in the store
cawthorne Nov 12, 2026
762927a
data-feeds-store: one program that owns the feeds
cawthorne Nov 12, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions contracts/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ contract-reader-interface-secondary = "9SFyk8NmGYh5D612mJwUYhguCRY9cFgaS2vksrige
dummy_receiver = "5z38tFCAmcPJb1DXUHSoKQhR8qQ8o9aNZ8rZFWe6gH4L"
keystone_forwarder = "whV7Q5pi17hPPyaPksToDw1nMx6Lh8qmNWKFaLRQ4wz"
data_feeds_cache = "3kX63udXtYcsdj2737Wi2KGd2PhqiKPgAFAxstrjtRUa"
data_feeds_store = "9qCsUGTdKCanybkroJuPzAhuRU8P2SZN4NCexevSZAwu"
log-read-test = "J1zQwrBNBngz26jRPNWsUSZMHJwBwpkoDitXRV95LdK4"
mock-ccip-events = "CGn5MQX5GK9qKqERhjnADhd6i2LiSF6XUC2ewUHND1Mw"
mock_forwarder = "7kuEAA3mSC1Tz8gQjnvH7bKFda9xSPRRin9SZbH49cNK"
Expand All @@ -31,3 +32,8 @@ wallet = "id.json"

[scripts]
test = "pnpm run test"

[test]
# Workspace programs are upgradeable by the provider wallet: store and proxy initialize require
# the program's upgrade authority.
upgradeable = true
20 changes: 20 additions & 0 deletions contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"programs/*",
"crates/arrayvec",
"crates/chainlink-solana-data-feeds-cache",
"crates/chainlink-solana-data-feeds-store",
# Exclude crates/chainlink-solana-data-streams: it uses solana-sdk 1.17 which
# pulls zeroize <1.4, conflicting with anchor-spl 0.31 (zeroize ^1.7).
# Not used by any program; only for off-chain/tests (cfg(not(target_os = "solana"))).
Expand Down
21 changes: 21 additions & 0 deletions contracts/crates/chainlink-solana-data-feeds-store/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "chainlink_solana_data_feeds_store"
description = "Readers for the Chainlink Data Feeds store on Solana"
version = "0.0.1"
edition = "2021"
license = "MIT"

[lib]
crate-type = ["cdylib", "lib"]
name = "chainlink_solana_data_feeds_store"

[features]
default = []

[dependencies]
anchor-lang = "0.31.0"
bytemuck = { version = "1.20.0", features = ["derive", "min_const_generics", "extern_crate_alloc"] }
data-feeds-store = { path = "../../programs/data-feeds-store", features = ["cpi"] }

[lints]
workspace = true
290 changes: 290 additions & 0 deletions contracts/crates/chainlink-solana-data-feeds-store/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
//! Readers for the Chainlink Data Feeds store on Solana. Each data ID has one feed account: pass it
//! with the decimals you want, at least the feed's minimum and at most 18.
//!
//! Two ways to read the same data:
//! - **Decode the feed account** ([`read_latest_round`], [`read_round`]): no call to another
//! program. The account must be owned by the store program this crate is built for
//! ([`DATA_FEEDS_STORE_ID`]) and belong to the store state you pass; only the program's upgrade
//! authority creates store states, but pin the one you trust.
//! - **Call the store** ([`latest_round`], [`get_round`]): one call, the store checks the account.
//!
//! Both refuse a frozen feed, decimals below the feed's minimum, and a non-zero answer that would
//! round to zero. Round IDs keep increasing for the life of a feed.

use anchor_lang::prelude::*;
use anchor_lang::solana_program::program::get_return_data;
use data_feeds_store::{
cpi::accounts::FeedRead,
error::StoreError,
instructions::reads::to_round,
logic,
state::Feed,
utils::{decode_feed_into, RingRef},
};

pub use data_feeds_store::common::STORED_DECIMALS;
pub use data_feeds_store::logic::scale;
pub use data_feeds_store::state::Round;
pub use data_feeds_store::utils::feed_address;
pub use data_feeds_store::ID as DATA_FEEDS_STORE_ID;

fn decode<'a>(
feed: &'a AccountInfo,
store_state: &Pubkey,
data_id: &[u8; 16],
) -> Result<(Box<Feed>, std::cell::Ref<'a, &'a mut [u8]>)> {
require_keys_eq!(*feed.owner, DATA_FEEDS_STORE_ID, StoreError::InvalidAccount);
let data = feed.try_borrow_data()?;
let mut header: Box<Feed> = bytemuck::zeroed_box();
decode_feed_into(&data, store_state, data_id, &mut header)?;
Ok((header, data))
}

/// The latest round of a data ID at `decimals`, decoded from its feed account.
pub fn read_latest_round(
feed: &AccountInfo,
store_state: &Pubkey,
data_id: [u8; 16],
decimals: u8,
) -> Result<Round> {
let (header, _) = decode(feed, store_state, &data_id)?;
let round = logic::latest_round(&header).ok_or(StoreError::NoDataPresent)?;
to_round(&header, round, decimals)
}

/// A stored round of a data ID at `decimals`, decoded from its feed account.
pub fn read_round(
feed: &AccountInfo,
store_state: &Pubkey,
data_id: [u8; 16],
round_id: u64,
decimals: u8,
) -> Result<Round> {
let (header, data) = decode(feed, store_state, &data_id)?;
let round = logic::stored_round(round_id, &header, &RingRef::new(&data[..]))
.ok_or(StoreError::NoDataPresent)?;
to_round(&header, round, decimals)
}

/// The latest round of a data ID at `decimals`, through a call to the store.
pub fn latest_round<'info>(
store_program: AccountInfo<'info>,
store_state: AccountInfo<'info>,
feed: AccountInfo<'info>,
data_id: [u8; 16],
decimals: u8,
) -> Result<Round> {
require_keys_eq!(
store_program.key(),
DATA_FEEDS_STORE_ID,
ErrorCode::InvalidProgramId
);
let ctx = CpiContext::new(
store_program,
FeedRead {
state: store_state,
feed,
},
);
data_feeds_store::cpi::latest_round(ctx, data_id, decimals)?;
returned()
}

/// A stored round of a data ID at `decimals`, through a call to the store.
pub fn get_round<'info>(
store_program: AccountInfo<'info>,
store_state: AccountInfo<'info>,
feed: AccountInfo<'info>,
data_id: [u8; 16],
round_id: u64,
decimals: u8,
) -> Result<Round> {
require_keys_eq!(
store_program.key(),
DATA_FEEDS_STORE_ID,
ErrorCode::InvalidProgramId
);
let ctx = CpiContext::new(
store_program,
FeedRead {
state: store_state,
feed,
},
);
data_feeds_store::cpi::get_round(ctx, data_id, round_id, decimals)?;
returned()
}

/// The return data of the last call, which must come from the store.
fn returned() -> Result<Round> {
match get_return_data() {
Some((from, data)) if from == DATA_FEEDS_STORE_ID => {
Round::try_from_slice(&data).map_err(|_| {
msg!("Malformed answer from the store");
ProgramError::InvalidAccountData.into()
})
}
_ => {
msg!("No answer from the store");
Err(ProgramError::IncorrectProgramId.into())
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use anchor_lang::solana_program::program_stubs::{set_syscall_stubs, SyscallStubs};
use anchor_lang::Discriminator;
use data_feeds_store::data_id::to_seed;
use data_feeds_store::state::{feed_account_len, StoredReport, FEED_RING_OFFSET};
use std::sync::Mutex;

static RETURN_DATA: Mutex<Option<(Pubkey, Vec<u8>)>> = Mutex::new(None);

struct Stubs;
impl SyscallStubs for Stubs {
fn sol_get_return_data(&self) -> Option<(Pubkey, Vec<u8>)> {
RETURN_DATA.lock().unwrap().clone()
}
}

#[test]
fn takes_the_answer_only_from_the_store() {
set_syscall_stubs(Box::new(Stubs));
let answer = |data: Option<(Pubkey, Vec<u8>)>| {
*RETURN_DATA.lock().unwrap() = data;
returned()
};
let round = Round {
round_id: 7,
answer: -42,
observations_timestamp: 1_700_000_000,
recorded_timestamp: 1_700_000_001,
};
let encoded = round.try_to_vec().unwrap();
assert_eq!(
answer(Some((DATA_FEEDS_STORE_ID, encoded.clone()))).unwrap(),
round
);
assert_eq!(
answer(None).unwrap_err(),
ProgramError::IncorrectProgramId.into()
);
assert_eq!(
answer(Some((Pubkey::new_unique(), encoded.clone()))).unwrap_err(),
ProgramError::IncorrectProgramId.into()
);
assert_eq!(
answer(Some((DATA_FEEDS_STORE_ID, encoded[..10].to_vec()))).unwrap_err(),
ProgramError::InvalidAccountData.into()
);
}

const E18: i128 = 1_000_000_000_000_000_000;

/// A feed account with rounds 1..=3 (answers 100, 200, 300 with 18 decimals).
fn feed_account(state: &Pubkey, data_id: &[u8; 16], frozen: bool) -> Vec<u8> {
let max = 4;
let mut data = vec![0u8; feed_account_len(max)];
let mut header: Feed = bytemuck::Zeroable::zeroed();
header.version = 1;
header.frozen = frozen as u8;
header.min_decimals = 8;
header.data_id = to_seed(data_id);
header.state = *state;
header.max_stored_reports = max;
for r in 1..=3u64 {
let report = StoredReport {
answer: r as i128 * 100 * E18,
observations_timestamp: r as u32 * 10,
recorded_timestamp: r as u32 * 10 + 1,
recorded_slot: r,
};
let offset = FEED_RING_OFFSET + (r % max) as usize * 32;
data[offset..offset + 32].copy_from_slice(bytemuck::bytes_of(&report));
}
header.round_id = 3;
header.latest_answer = 300 * E18;
header.latest_observations_timestamp = 30;
header.latest_recorded_timestamp = 31;
data[..8].copy_from_slice(Feed::DISCRIMINATOR);
data[8..8 + std::mem::size_of::<Feed>()].copy_from_slice(bytemuck::bytes_of(&header));
data
}

#[test]
fn decodes_feed_accounts() {
let (key, state) = (Pubkey::new_unique(), Pubkey::new_unique());
let id = [1u8; 16];
let mut lamports = 0;
let mut data = feed_account(&state, &id, false);
let info = AccountInfo::new(
&key,
false,
false,
&mut lamports,
&mut data,
&DATA_FEEDS_STORE_ID,
false,
0,
);
let latest = read_latest_round(&info, &state, id, 8).unwrap();
assert_eq!((latest.round_id, latest.answer), (3, 300_00000000));
let round = read_round(&info, &state, id, 2, 18).unwrap();
assert_eq!(
(round.round_id, round.answer, round.observations_timestamp),
(2, 200 * E18, 20)
);
assert!(
read_latest_round(&info, &state, id, 7).is_err(),
"below the minimum"
);
assert!(
read_latest_round(&info, &Pubkey::new_unique(), id, 8).is_err(),
"other store"
);
assert!(
read_latest_round(&info, &state, [2u8; 16], 8).is_err(),
"other data ID"
);
assert!(
read_round(&info, &state, id, 4, 8).is_err(),
"no such round"
);
}

#[test]
fn refuses_frozen_feeds_and_other_owners() {
let (key, state) = (Pubkey::new_unique(), Pubkey::new_unique());
let id = [1u8; 16];
let mut lamports = 0;
let mut data = feed_account(&state, &id, true);
let frozen = AccountInfo::new(
&key,
false,
false,
&mut lamports,
&mut data,
&DATA_FEEDS_STORE_ID,
false,
0,
);
assert!(read_latest_round(&frozen, &state, id, 8).is_err());

let mut lamports = 0;
let mut data = feed_account(&state, &id, false);
let other = Pubkey::new_unique();
let foreign = AccountInfo::new(
&key,
false,
false,
&mut lamports,
&mut data,
&other,
false,
0,
);
assert!(read_latest_round(&foreign, &state, id, 8).is_err());
}
}
Loading
Loading