This commit is contained in:
Conner Gallagher 2023-06-13 15:59:35 -06:00
parent d4c46e3db9
commit aeaf1f6c0d
6 changed files with 70 additions and 99 deletions

View File

@ -3613,7 +3613,7 @@ dependencies = [
[[package]] [[package]]
name = "switchboard-solana" name = "switchboard-solana"
version = "0.3.1" version = "0.3.2"
dependencies = [ dependencies = [
"anchor-lang", "anchor-lang",
"anchor-spl", "anchor-spl",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "switchboard-solana" name = "switchboard-solana"
version = "0.3.1" version = "0.3.2"
edition = "2021" edition = "2021"
description = "A Rust library to interact with Switchboard accounts." description = "A Rust library to interact with Switchboard accounts."
readme = "README.md" readme = "README.md"

View File

@ -1,4 +1,4 @@
use crate::{QuoteAccountData, SWITCHBOARD_ATTESTATION_PROGRAM_ID}; use crate::{cfg_client, QuoteAccountData, SWITCHBOARD_ATTESTATION_PROGRAM_ID};
use anchor_lang::prelude::*; use anchor_lang::prelude::*;
use anchor_lang::{Discriminator, Owner, ZeroCopy}; use anchor_lang::{Discriminator, Owner, ZeroCopy};
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
@ -161,65 +161,58 @@ impl FunctionAccountData {
format!("{}:{}", self.get_container(), self.get_version()) format!("{}:{}", self.get_container(), self.get_version())
} }
#[cfg(feature = "client")] cfg_client! {
#[cfg_attr(docsrs, doc(cfg(feature = "client")))] pub fn get_schedule(&self) -> Option<cron::Schedule> {
pub fn get_schedule(&self) -> Option<cron::Schedule> { if self.schedule[0] == 0 {
if self.schedule[0] == 0 { return None;
return None; }
let every_second = cron::Schedule::try_from("* * * * * *").unwrap();
let schedule = std::str::from_utf8(&self.schedule)
.unwrap_or("* * * * * *")
.trim_end_matches('\0');
let schedule = cron::Schedule::try_from(schedule);
Some(schedule.unwrap_or(every_second.clone()))
} }
let every_second = cron::Schedule::try_from("* * * * * *").unwrap();
let schedule = std::str::from_utf8(&self.schedule)
.unwrap_or("* * * * * *")
.trim_end_matches('\0');
let schedule = cron::Schedule::try_from(schedule);
Some(schedule.unwrap_or(every_second.clone()))
}
#[cfg(feature = "client")] pub fn get_last_execution_datetime(&self) -> chrono::DateTime<chrono::Utc> {
#[cfg_attr(docsrs, doc(cfg(feature = "client")))] chrono::DateTime::from_utc(
pub fn get_last_execution_datetime(&self) -> chrono::DateTime<chrono::Utc> { chrono::NaiveDateTime::from_timestamp_opt(self.last_execution_timestamp, 0).unwrap(),
chrono::DateTime::from_utc( chrono::Utc,
chrono::NaiveDateTime::from_timestamp_opt(self.last_execution_timestamp, 0).unwrap(), )
chrono::Utc, }
)
}
#[cfg(feature = "client")] pub fn should_execute(&self, now: chrono::DateTime<chrono::Utc>) -> bool {
#[cfg_attr(docsrs, doc(cfg(feature = "client")))] let schedule = self.get_schedule();
pub fn should_execute(&self, now: chrono::DateTime<chrono::Utc>) -> bool { if schedule.is_none() {
let schedule = self.get_schedule(); return false;
if schedule.is_none() { }
return false; let dt = self.get_last_execution_datetime();
let next_trigger_time = schedule.unwrap().after(&dt).next();
if next_trigger_time.is_none() {
return false;
}
let next_trigger_time = next_trigger_time.unwrap();
if next_trigger_time > now {
return false;
}
true
} }
let dt = self.get_last_execution_datetime();
let next_trigger_time = schedule.unwrap().after(&dt).next();
if next_trigger_time.is_none() {
return false;
}
let next_trigger_time = next_trigger_time.unwrap();
if next_trigger_time > now {
return false;
}
true
}
#[cfg(feature = "client")]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
pub fn next_execution_timestamp(&self) -> Option<chrono::DateTime<chrono::Utc>> {
let schedule = self.get_schedule();
if schedule.is_none() {
return None;
}
let dt = self.get_last_execution_datetime();
schedule.unwrap().after(&dt).next()
}
#[cfg(feature = "client")] pub fn next_execution_timestamp(&self) -> Option<chrono::DateTime<chrono::Utc>> {
#[cfg_attr(docsrs, doc(cfg(feature = "client")))] let schedule = self.get_schedule();
pub async fn fetch( if schedule.is_none() {
client: &solana_client::rpc_client::RpcClient, return None;
pubkey: Pubkey, }
) -> std::result::Result<Self, switchboard_common::Error> { let dt = self.get_last_execution_datetime();
crate::client::load_account(client, pubkey).await schedule.unwrap().after(&dt).next()
}
pub async fn fetch(
client: &solana_client::rpc_client::RpcClient,
pubkey: Pubkey,
) -> std::result::Result<Self, switchboard_common::Error> {
crate::client::load_account(client, pubkey).await
}
} }
} }

View File

@ -1,3 +1,4 @@
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![allow(clippy::result_large_err)] #![allow(clippy::result_large_err)]
use anchor_lang::prelude::*; use anchor_lang::prelude::*;
use anchor_lang::solana_program::pubkey; use anchor_lang::solana_program::pubkey;
@ -12,27 +13,17 @@ pub use attestation_program::*;
// //
// This module is not intended to be part of the public API. In general, any // This module is not intended to be part of the public API. In general, any
// `doc(hidden)` code is not part of the public and stable API. // `doc(hidden)` code is not part of the public and stable API.
// #[macro_use] #[macro_use]
// #[doc(hidden)] #[doc(hidden)]
// pub mod macros; pub mod macros;
#[cfg(feature = "client")] cfg_client! {
#[cfg_attr(docsrs, doc(cfg(feature = "client")))] pub mod client;
#[cfg(not(target_os = "solana"))] pub use client::*;
pub mod client;
#[cfg(feature = "client")]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
#[cfg(not(target_os = "solana"))]
pub use client::*;
#[cfg(feature = "client")] pub mod sgx;
#[cfg_attr(docsrs, doc(cfg(feature = "client")))] pub use sgx::*;
#[cfg(not(target_os = "solana"))] }
pub mod sgx;
#[cfg(feature = "client")]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
#[cfg(not(target_os = "solana"))]
pub use sgx::*;
/// Seed used to derive the SbState PDA. /// Seed used to derive the SbState PDA.
pub const STATE_SEED: &[u8] = b"STATE"; pub const STATE_SEED: &[u8] = b"STATE";

View File

@ -1,28 +1,15 @@
macro_rules! cfg_solana { #[macro_export]
($($item:item)*) => {
$(
#[cfg(target_os = "solana")]
$item
)*
}
}
macro_rules! cfg_not_solana {
($($item:item)*) => {
$( #[cfg(not(target_os = "solana"))] $item )*
}
}
macro_rules! cfg_client { macro_rules! cfg_client {
($($item:item)*) => { ($($item:item)*) => {
$( $(
#[cfg(feature = "client")] #[cfg(feature = "client")]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))] #[cfg_attr(doc_cfg, doc(cfg(feature = "client")))]
$item $item
)* )*
} }
} }
#[macro_export]
macro_rules! cfg_not_client { macro_rules! cfg_not_client {
($($item:item)*) => { ($($item:item)*) => {
$( #[cfg(not(feature = "client"))] $item )* $( #[cfg(not(feature = "client"))] $item )*

View File

@ -279,13 +279,13 @@ impl AggregatorAccountData {
Ok(Clock::get()?.unix_timestamp < self.expiration) Ok(Clock::get()?.unix_timestamp < self.expiration)
} }
#[cfg(feature = "client")] cfg_client! {
#[cfg_attr(docsrs, doc(cfg(feature = "client")))] pub async fn fetch(
pub async fn fetch( client: &solana_client::rpc_client::RpcClient,
client: &solana_client::rpc_client::RpcClient, pubkey: Pubkey,
pubkey: Pubkey, ) -> std::result::Result<Self, switchboard_common::Error> {
) -> std::result::Result<Self, switchboard_common::Error> { crate::client::load_account(client, pubkey).await
crate::client::load_account(client, pubkey).await }
} }
} }