Full lesson
solana-fact-oracle
First Oracle with open and subscription feeds
What it does
From the project README:
At Fact Finance, we're reshaping web3 infrastructure with tailored, reliable real-world data. By forging strategic partnerships with official data providers, including research institutes, sports leagues, and governmental data agencies, we empower businesses to confidently navigate regional nuances. Our commitment to education and sustainability ensures a lasting impact and growth, driving tangible progress in the digital landscape. Join us in revolutionizing web3’s future—one data point at a time.
- Original
- FactFinance/solana-fact-oracle
- SuperStack fork
- Blockchains/solana-fact-oracle @
dcdca42 - Stars
- 0
- Last push (original)
- 2024-10-29
- Languages
- Rust, TypeScript
- Detected stack
- Anchor
- Categories
- Other
- Licence
- MIT
Architecture
19 source files. By extension: .rs ×18, .ts ×1.
| Top-level directory | Source files |
|---|---|
programs | 11 |
wormhole | 7 |
tests | 1 |
On-chain programs: programs/consumer, programs/oracle
Dapp: deploying
This fork is queued for its own live dapp on Sol.College, with a Devnet/Mainnet switch. It appears here, at /dapps/solana-fact-oracle/, once it is deployed and checked on devnet. Until then, run it locally with the commands below.
Key code, explained
Excerpts chosen automatically where the code uses the most Solana concepts, pinned to the fork's commit.
programs/oracle/src/instructions/license.rs L13–35 @ dcdca42 · MIT · original // Return an error if the caller is not the owner return err!(OracleErrors::AccessDenied); } // Set the license of the data feed to the provided value datafeed.license = license; // Log the new license msg!("New license {}", datafeed.license); // Return success Ok(())} // Definition of accounts for setting the license of the data feed#[derive(Accounts)]pub struct SetLicense<'info> { #[account(seeds = [ b"_settings"],bump)] pub settings: Account<'info, Settings>, pub signer: Signer<'info>, #[account(mut)] pub datafeed: Account<'info, DataFeed>,}- Line 14
return err!(OracleErrors::AccessDenied);: An explicit validation that aborts the instruction with an error. - Line 21
msg!("New license {}", datafeed.license);: Logs to the program log, which is visible in explorers and the test output. - Line 30
#[account(seeds = [ b"_settings"],bump)]: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match. - Line 30
#[account(seeds = [ b"_settings"],bump)]: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve. - Line 32
pub signer: Signer<'info>,: Requires this account to have signed the transaction.
wormhole/src/context.rs L1–24 @ dcdca42 · MIT · originaluse anchor_lang::prelude::*;use crate::constant::*;use crate::account::*;use std::str::FromStr;use anchor_lang::solana_program::sysvar::{rent, clock};use crate::wormhole::*;use hex::decode;extern crate borsh;use borsh::BorshDeserialize; #[derive(Accounts)]pub struct Initialize<'info> { #[account( init, seeds=[b"config".as_ref()], payer=owner, bump, space=8+32+32+1024 )] pub config: Account<'info, Config>, #[account(mut)] pub owner: Signer<'info>, pub system_program: Program<'info, System>}- Line 14
init,: Creates and pays for a new account. Space and payer must be specified. - Line 15
seeds=[b"config".as_ref()],: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match. - Line 17
bump,: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve. - Line 22
pub owner: Signer<'info>,: Requires this account to have signed the transaction.
programs/oracle/src/instructions/feed.rs L49–72 @ dcdca42 · MIT · original // Log the new value msg!("New value {} for {}", datafeed.value, symbol); } // Return success Ok(())} // Definition of accounts for initializing the Oracle#[derive(Accounts)]#[instruction(feedid: u16)]pub struct InitializeDatafeed<'info> { #[account( init, payer = signer, space = 256, seeds = [signer.key().as_ref(), b"_datafeed", feedid.to_string().as_ref()], bump )] pub datafeed: Account<'info, DataFeed>, #[account( init, payer = signer, space = 256,- Line 51
msg!("New value {} for {}", datafeed.value, symbol);: Logs to the program log, which is visible in explorers and the test output. - Line 62
init,: Creates and pays for a new account. Space and payer must be specified. - Line 65
seeds = [signer.key().as_ref(), b"_datafeed", feedid.to_string().as_ref()],: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match. - Line 66
bump: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve.
programs/oracle/src/instructions/subscription.rs L13–36 @ dcdca42 · MIT · original return err!(OracleErrors::AccessDenied); } // Amount to transfer let amount: u64 = settings_account.price ; // Create the transfer instruction let cpi_accounts = Transfer { from: ctx.accounts.sender.to_account_info().clone(), to: ctx.accounts.receiver.to_account_info().clone(), authority: ctx.accounts.sender.to_account_info().clone(), }; let cpi_program = ctx.accounts.token.to_account_info(); let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts); // Perform the token transfer token::transfer(cpi_ctx, amount)?; msg!("Received 1 Token from {} to the new subscriber {}", ctx.accounts.sender.key(), address); let subscribers_account = &mut ctx.accounts.subscribers; // Add the subscriber address if it's not already present if !subscribers_account.subscribers.iter().any(|p| p == &address) {- Line 13
return err!(OracleErrors::AccessDenied);: An explicit validation that aborts the instruction with an error. - Line 26
let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);: Makes a cross-program invocation into another program. - Line 29
token::transfer(cpi_ctx, amount)?;: An SPL token transfer through the Token Program. - Line 31
msg!("Received 1 Token from {} to the new subscriber {}", ctx.accounts.sender.key(), addre: Logs to the program log, which is visible in explorers and the test output.
How to run, build and test
These commands come from the repo's own manifests (package.json scripts, Anchor.toml, Cargo, go.mod, Makefile). Check the README for prerequisites and environment variables.
| Directory | Command | Runs |
|---|---|---|
. | anchor build | compile programs and generate IDLs |
. | anchor test | local validator + test suite |
. | anchor run test | yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts |
. | cargo build | Rust workspace build |
. | cargo test | Rust tests |
What you could build from it
- Fork the SuperStack copy and extend it within the Other gaps listed on the idea page.
- Learn the pattern first: Program Derived Addresses (PDAs)
- Learn the pattern first: Cross-Program Invocations (CPIs)