Skip to content

Commit

Permalink
SDK updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gallynaut committed Jun 27, 2023
1 parent 0ebad99 commit ac4aecf
Show file tree
Hide file tree
Showing 19 changed files with 266 additions and 200 deletions.
23 changes: 11 additions & 12 deletions examples/functions/01_basic_oracle/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ seeds = false
skip-lint = false

[programs.localnet]
basic_oracle = "9PAxFbRDepv1ziPeUW5KHcpgpgPtxWV4ZzVwqDBqymok"
basic_oracle = "HgVykECRLmtSfquJz7NTBg5H8SfL2ehTFZnzfDJQuxt2"

[provider]
cluster = "Localnet"
Expand All @@ -18,23 +18,22 @@ test = "pnpm exec ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
[test]
startup_wait = 15000

[[test.validator.clone]] # sb devnet oracle programID
address = "SW1TCH7qEPTdLsDHRgPuMQjbQxKdH2aBStViMFnt64f"

[[test.validator.clone]] # sb devnet oracle IDL
address = "Fi8vncGpNKbq62gPo56G4toCehWNy77GgqGkTaAF5Lkk"
[test.validator]
url = "https://api.devnet.solana.com"

# [[test.validator.clone]] # sb devnet oracle programID
# address = "SW1TCH7qEPTdLsDHRgPuMQjbQxKdH2aBStViMFnt64f"
# [[test.validator.clone]] # sb devnet oracle IDL
# address = "Fi8vncGpNKbq62gPo56G4toCehWNy77GgqGkTaAF5Lkk"
[[test.validator.clone]] # sb devnet oracle SbState
address = "CyZuD7RPDcrqCGbNvLCyqk6Py9cEZTKmNKujfPi3ynDd"

[[test.validator.clone]] # sb devnet oracle tokenVault
address = "7hkp1xfPBcD2t1vZMoWWQPzipHVcXeLAAaiGXdPSfDie"

[[test.validator.clone]] # sb devnet attestation programID
address = "SBAPyGPyvYEXTiTEfVrktmpvm3Bae3VoZmjYZ6694Ha"

[[test.validator.clone]] # sb devnet attestation IDL
address = "GjWPcr9QrdHk8At821qcZPM9NPpCDGivaTCMQG5nWj2m"

# [[test.validator.clone]] # sb devnet attestation programID
# address = "SBAPyGPyvYEXTiTEfVrktmpvm3Bae3VoZmjYZ6694Ha"
# [[test.validator.clone]] # sb devnet attestation IDL
# address = "GjWPcr9QrdHk8At821qcZPM9NPpCDGivaTCMQG5nWj2m"
[[test.validator.clone]] # sb devnet attestation State
address = "EuCumvrswq5HZC4ME1wh7Q7V6dN8DRetP3eM1gU4RPYp"
21 changes: 10 additions & 11 deletions examples/functions/01_basic_oracle/src/actions/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct Initialize<'info> {
)]
pub oracle: AccountLoader<'info, MyOracleState>,

pub function: AccountLoader<'info, FunctionAccountData>,

/// CHECK:
pub authority: Signer<'info>,

Expand All @@ -29,15 +31,14 @@ pub struct Initialize<'info> {

// SYSTEM ACCOUNTS
pub system_program: Program<'info, System>,

/// CHECK:
#[account(address = solana_program::sysvar::rent::ID)]
pub rent: AccountInfo<'info>,
// /// CHECK:
// #[account(address = solana_program::sysvar::rent::ID)]
// pub rent: AccountInfo<'info>,
}

#[derive(Clone, AnchorSerialize, AnchorDeserialize)]
pub struct InitializeParams {
pub mr_enclaves: Vec<[u8; 32]>,
// pub mr_enclaves: Vec<[u8; 32]>,
}

impl Initialize<'_> {
Expand All @@ -46,19 +47,17 @@ impl Initialize<'_> {
_ctx: &Context<Self>,
params: &InitializeParams,
) -> anchor_lang::Result<()> {
if params.mr_enclaves.len() > 32 {
return Err(error!(BasicOracleError::ArrayOverflow));
}
// if params.mr_enclaves.len() > 32 {
// return Err(error!(BasicOracleError::ArrayOverflow));
// }
Ok(())
}

pub fn actuate(ctx: &Context<Self>, params: &InitializeParams) -> anchor_lang::Result<()> {
let program = &mut ctx.accounts.program.load_init()?;
program.bump = *ctx.bumps.get("program").unwrap_or(&0);
program.authority = ctx.accounts.authority.key();
// if !params.mr_enclaves.is_empty() {
// program.mr_enclaves = parse_mr_enclaves(&params.mr_enclaves)?;
// }
program.function = ctx.accounts.function.key();

let oracle = &mut ctx.accounts.oracle.load_init()?;
oracle.bump = *ctx.bumps.get("oracle").unwrap_or(&0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub struct RefreshPrices<'info> {
#[account(
seeds = [PROGRAM_SEED],
bump = program_state.load()?.bump,
has_one = function @ BasicOracleError::IncorrectSwitchboardFunction,
// has_one = function @ BasicOracleError::IncorrectSwitchboardFunction,
)]
pub program_state: AccountLoader<'info, MyProgramState>,

Expand All @@ -30,6 +30,8 @@ pub struct RefreshPrices<'info> {
pub enclave_signer: Signer<'info>,
}



#[derive(Clone, AnchorSerialize, AnchorDeserialize)]
pub struct RefreshPricesParams {
pub rows: Vec<OracleDataWithTradingSymbol>,
Expand Down
2 changes: 1 addition & 1 deletion examples/functions/01_basic_oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use utils::*;
pub mod actions;
pub use actions::*;

declare_id!("9PAxFbRDepv1ziPeUW5KHcpgpgPtxWV4ZzVwqDBqymok");
declare_id!("HgVykECRLmtSfquJz7NTBg5H8SfL2ehTFZnzfDJQuxt2");

pub const PROGRAM_SEED: &[u8] = b"BASICORACLE";

Expand Down
141 changes: 82 additions & 59 deletions examples/functions/01_basic_oracle/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,15 @@
use crate::*;
use bytemuck::{Pod, Zeroable};

#[repr(u8)]
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, AnchorSerialize, AnchorDeserialize)]
pub enum TradingSymbol {
#[default]
Unknown = 0,
BTC = 1,
USDC = 2,
ETH = 3,
SOL = 4,
DOGE = 5,
}

unsafe impl Pod for TradingSymbol {}
unsafe impl Zeroable for TradingSymbol {}

impl From<TradingSymbol> for u8 {
fn from(value: TradingSymbol) -> Self {
match value {
TradingSymbol::BTC => 1,
TradingSymbol::USDC => 2,
TradingSymbol::ETH => 3,
TradingSymbol::SOL => 4,
TradingSymbol::DOGE => 5,
_ => 0,
}
}
}
impl From<u8> for TradingSymbol {
fn from(value: u8) -> Self {
match value {
1 => TradingSymbol::BTC,
2 => TradingSymbol::USDC,
3 => TradingSymbol::ETH,
4 => TradingSymbol::SOL,
5 => TradingSymbol::DOGE,
_ => TradingSymbol::default(),
}
}
}

#[account(zero_copy(unsafe))]
#[derive(Default, Debug, AnchorSerialize)]
pub struct MyProgramState {
pub bump: u8,
pub authority: Pubkey,
pub function: Pubkey,
}

#[derive(Copy, Clone, Default, AnchorSerialize, AnchorDeserialize)]
#[repr(packed)]
#[zero_copy(unsafe)]
pub struct OracleData {
pub oracle_timestamp: i64,
pub price: i128,
Expand All @@ -59,10 +19,32 @@ pub struct OracleData {
pub twap_24hr: i128,
}

#[derive(Copy, Clone, Default, AnchorSerialize, AnchorDeserialize)]
pub struct OracleDataBorsh {
pub oracle_timestamp: i64,
pub price: i128,
pub volume_1hr: i128,
pub volume_24hr: i128,
pub twap_1hr: i128,
pub twap_24hr: i128,
}
impl From<OracleDataBorsh> for OracleData {
fn from(value: OracleDataBorsh) -> Self {
Self {
oracle_timestamp: value.oracle_timestamp,
price: value.price.clone(),
volume_1hr: value.volume_1hr.clone(),
volume_24hr: value.volume_24hr.clone(),
twap_1hr: value.twap_1hr.clone(),
twap_24hr: value.twap_24hr.clone(),
}
}
}

#[derive(Copy, Clone, Default, AnchorSerialize, AnchorDeserialize)]
pub struct OracleDataWithTradingSymbol {
pub symbol: TradingSymbol,
pub data: OracleData,
pub data: OracleDataBorsh,
}

impl OracleData {
Expand All @@ -81,7 +63,7 @@ impl OracleData {
}
}

// #[repr(packed)]
#[repr(packed)]
#[account(zero_copy(unsafe))]
pub struct MyOracleState {
pub bump: u8,
Expand All @@ -93,32 +75,33 @@ pub struct MyOracleState {
// can always re-allocate to add more
// pub reserved: [u8; 2400],
}
impl Default for MyOracleState {
fn default() -> Self {
unsafe { std::mem::zeroed() }
}
}

impl MyOracleState {
pub fn save_rows(
&mut self,
rows: &Vec<OracleDataWithTradingSymbol>,
) -> anchor_lang::Result<()> {
for row in rows.iter() {
match row.symbol {
TradingSymbol::BTC => {
self.btc = row.data;
TradingSymbol::Btc => {
msg!("saving BTC price, {}", { row.data.price });
self.btc = row.data.into();
}
TradingSymbol::USDC => {
self.usdc = row.data;
TradingSymbol::Usdc => {
msg!("saving USDC price, {}", { row.data.price });
self.usdc = row.data.into();
}
TradingSymbol::ETH => {
self.eth = row.data;
TradingSymbol::Eth => {
msg!("saving ETH price, {}", { row.data.price });
self.eth = row.data.into();
}
TradingSymbol::SOL => {
self.sol = row.data;
TradingSymbol::Sol => {
msg!("saving SOL price, {}", { row.data.price });
self.sol = row.data.into();
}
TradingSymbol::DOGE => {
self.doge = row.data;
TradingSymbol::Doge => {
msg!("saving DOGE price, {}", { row.data.price });
self.doge = row.data.into();
}
_ => {
msg!("no trading symbol found for {:?}", row.symbol);
Expand All @@ -130,3 +113,43 @@ impl MyOracleState {
Ok(())
}
}

#[repr(u8)]
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, AnchorSerialize, AnchorDeserialize)]
pub enum TradingSymbol {
#[default]
Unknown = 0,
Btc = 1,
Usdc = 2,
Eth = 3,
Sol = 4,
Doge = 5,
}

unsafe impl Pod for TradingSymbol {}
unsafe impl Zeroable for TradingSymbol {}

impl From<TradingSymbol> for u8 {
fn from(value: TradingSymbol) -> Self {
match value {
TradingSymbol::Btc => 1,
TradingSymbol::Usdc => 2,
TradingSymbol::Eth => 3,
TradingSymbol::Sol => 4,
TradingSymbol::Doge => 5,
_ => 0,
}
}
}
impl From<u8> for TradingSymbol {
fn from(value: u8) -> Self {
match value {
1 => TradingSymbol::Btc,
2 => TradingSymbol::Usdc,
3 => TradingSymbol::Eth,
4 => TradingSymbol::Sol,
5 => TradingSymbol::Doge,
_ => TradingSymbol::default(),
}
}
}
Loading

0 comments on commit ac4aecf

Please sign in to comment.