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]]
name = "switchboard-solana"
version = "0.3.1"
version = "0.3.2"
dependencies = [
"anchor-lang",
"anchor-spl",

View File

@ -1,6 +1,6 @@
[package]
name = "switchboard-solana"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
description = "A Rust library to interact with Switchboard accounts."
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::{Discriminator, Owner, ZeroCopy};
use bytemuck::{Pod, Zeroable};
@ -161,65 +161,58 @@ impl FunctionAccountData {
format!("{}:{}", self.get_container(), self.get_version())
}
#[cfg(feature = "client")]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
pub fn get_schedule(&self) -> Option<cron::Schedule> {
if self.schedule[0] == 0 {
return None;
cfg_client! {
pub fn get_schedule(&self) -> Option<cron::Schedule> {
if self.schedule[0] == 0 {
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")]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
pub fn get_last_execution_datetime(&self) -> chrono::DateTime<chrono::Utc> {
chrono::DateTime::from_utc(
chrono::NaiveDateTime::from_timestamp_opt(self.last_execution_timestamp, 0).unwrap(),
chrono::Utc,
)
}
pub fn get_last_execution_datetime(&self) -> chrono::DateTime<chrono::Utc> {
chrono::DateTime::from_utc(
chrono::NaiveDateTime::from_timestamp_opt(self.last_execution_timestamp, 0).unwrap(),
chrono::Utc,
)
}
#[cfg(feature = "client")]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
pub fn should_execute(&self, now: chrono::DateTime<chrono::Utc>) -> bool {
let schedule = self.get_schedule();
if schedule.is_none() {
return false;
pub fn should_execute(&self, now: chrono::DateTime<chrono::Utc>) -> bool {
let schedule = self.get_schedule();
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")]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
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
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()
}
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)]
use anchor_lang::prelude::*;
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
// `doc(hidden)` code is not part of the public and stable API.
// #[macro_use]
// #[doc(hidden)]
// pub mod macros;
#[macro_use]
#[doc(hidden)]
pub mod macros;
#[cfg(feature = "client")]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
#[cfg(not(target_os = "solana"))]
pub mod client;
#[cfg(feature = "client")]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
#[cfg(not(target_os = "solana"))]
pub use client::*;
cfg_client! {
pub mod client;
pub use client::*;
#[cfg(feature = "client")]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
#[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::*;
pub mod sgx;
pub use sgx::*;
}
/// Seed used to derive the SbState PDA.
pub const STATE_SEED: &[u8] = b"STATE";

View File

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

View File

@ -279,13 +279,13 @@ impl AggregatorAccountData {
Ok(Clock::get()?.unix_timestamp < self.expiration)
}
#[cfg(feature = "client")]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
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
cfg_client! {
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
}
}
}