converted to macro for feature specific functions

This commit is contained in:
Conner Gallagher 2023-06-13 20:07:20 -06:00
parent 5527c87045
commit 0b6e3c5b99
9 changed files with 240 additions and 238 deletions

View File

@ -1,4 +1,5 @@
use crate::prelude::*; use crate::prelude::*;
use crate::*;
use anchor_lang::{Discriminator, Owner, ZeroCopy}; use anchor_lang::{Discriminator, Owner, ZeroCopy};
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
use std::cell::Ref; use std::cell::Ref;
@ -107,12 +108,12 @@ impl AttestationPermissionAccountData {
self.permissions & p as u32 != 0 self.permissions & p as u32 != 0
} }
#[cfg(feature = "client")] cfg_client! {
#[cfg_attr(doc_cfg, 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 }
} }
} }

View File

@ -1,4 +1,5 @@
use crate::prelude::*; use crate::prelude::*;
use crate::*;
use anchor_lang::{Discriminator, Owner, ZeroCopy}; use anchor_lang::{Discriminator, Owner, ZeroCopy};
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
use std::cell::Ref; use std::cell::Ref;
@ -114,12 +115,12 @@ impl AttestationQueueAccountData {
.any(|x| x.to_vec() == mr_enclave.to_vec()) .any(|x| x.to_vec() == mr_enclave.to_vec())
} }
#[cfg(feature = "client")] cfg_client! {
#[cfg_attr(doc_cfg, 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 }
} }
} }

View File

@ -1,4 +1,5 @@
use crate::prelude::*; use crate::prelude::*;
use crate::*;
use anchor_lang::{Discriminator, Owner, ZeroCopy}; use anchor_lang::{Discriminator, Owner, ZeroCopy};
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
use std::cell::Ref; use std::cell::Ref;
@ -146,12 +147,12 @@ impl QuoteAccountData {
true true
} }
#[cfg(feature = "client")] cfg_client! {
#[cfg_attr(doc_cfg, 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 }
} }
} }

View File

@ -1,5 +1,5 @@
use super::super::accounts::*;
use crate::prelude::*; use crate::prelude::*;
use crate::*;
use anchor_lang::solana_program::entrypoint::ProgramResult; use anchor_lang::solana_program::entrypoint::ProgramResult;
use anchor_lang::solana_program::instruction::Instruction; use anchor_lang::solana_program::instruction::Instruction;
use anchor_lang::solana_program::program::{invoke, invoke_signed}; use anchor_lang::solana_program::program::{invoke, invoke_signed};
@ -202,204 +202,202 @@ impl<'info> FunctionVerify<'info> {
] ]
} }
#[cfg(feature = "client")] cfg_client! {
#[cfg_attr(doc_cfg, doc(cfg(feature = "client")))] pub async fn build(
pub async fn build( client: &solana_client::rpc_client::RpcClient,
client: &solana_client::rpc_client::RpcClient, fn_signer: std::sync::Arc<solana_sdk::signer::keypair::Keypair>,
fn_signer: std::sync::Arc<solana_sdk::signer::keypair::Keypair>, pubkeys: &FunctionVerifyPubkeys,
pubkeys: &FunctionVerifyPubkeys, mr_enclave: [u8; 32],
mr_enclave: [u8; 32], ) -> std::result::Result<Instruction, switchboard_common::error::Error> {
) -> std::result::Result<Instruction, switchboard_common::error::Error> { let fn_signer_pubkey = crate::client::to_pubkey(fn_signer)?;
let fn_signer_pubkey = crate::client::to_pubkey(fn_signer)?;
let current_time = std::time::SystemTime::now() let current_time = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH) .duration_since(std::time::UNIX_EPOCH)
.unwrap() .unwrap()
.as_secs() as i64; .as_secs() as i64;
let fn_data: FunctionAccountData = load_account(&client, pubkeys.function).await?; let fn_data: FunctionAccountData = load_account(&client, pubkeys.function).await?;
let verifier_quote: QuoteAccountData = load_account(&client, pubkeys.verifier).await?; let verifier_quote: QuoteAccountData = load_account(&client, pubkeys.verifier).await?;
let queue_data: AttestationQueueAccountData = let queue_data: AttestationQueueAccountData =
crate::client::load_account(&client, fn_data.attestation_queue).await?; crate::client::load_account(&client, fn_data.attestation_queue).await?;
// let escrow = fn_data.escrow; // let escrow = fn_data.escrow;
let (fn_quote, _) = Pubkey::find_program_address( let (fn_quote, _) = Pubkey::find_program_address(
&[b"QuoteAccountData", &pubkeys.function.to_bytes()], &[b"QuoteAccountData", &pubkeys.function.to_bytes()],
&SWITCHBOARD_ATTESTATION_PROGRAM_ID, &SWITCHBOARD_ATTESTATION_PROGRAM_ID,
); );
let (verifier_permission, _) = Pubkey::find_program_address( let (verifier_permission, _) = Pubkey::find_program_address(
&[ &[
b"PermissionAccountData", b"PermissionAccountData",
&queue_data.authority.to_bytes(), &queue_data.authority.to_bytes(),
&fn_data.attestation_queue.to_bytes(), &fn_data.attestation_queue.to_bytes(),
&pubkeys.payer.to_bytes(), &pubkeys.payer.to_bytes(),
], ],
&SWITCHBOARD_ATTESTATION_PROGRAM_ID, &SWITCHBOARD_ATTESTATION_PROGRAM_ID,
); );
let (fn_permission, _) = Pubkey::find_program_address( let (fn_permission, _) = Pubkey::find_program_address(
&[ &[
b"PermissionAccountData", b"PermissionAccountData",
&queue_data.authority.to_bytes(), &queue_data.authority.to_bytes(),
&fn_data.attestation_queue.to_bytes(), &fn_data.attestation_queue.to_bytes(),
&pubkeys.function.to_bytes(), &pubkeys.function.to_bytes(),
], ],
&SWITCHBOARD_ATTESTATION_PROGRAM_ID, &SWITCHBOARD_ATTESTATION_PROGRAM_ID,
); );
let (state, _) = let (state, _) =
Pubkey::find_program_address(&[b"STATE"], &SWITCHBOARD_ATTESTATION_PROGRAM_ID); Pubkey::find_program_address(&[b"STATE"], &SWITCHBOARD_ATTESTATION_PROGRAM_ID);
let accounts = FunctionVerifyAccounts { let accounts = FunctionVerifyAccounts {
function: pubkeys.function, function: pubkeys.function,
fn_signer: fn_signer_pubkey, fn_signer: fn_signer_pubkey,
fn_quote, fn_quote,
verifier_quote: pubkeys.verifier, verifier_quote: pubkeys.verifier,
secured_signer: verifier_quote.authority, secured_signer: verifier_quote.authority,
attestation_queue: fn_data.attestation_queue, attestation_queue: fn_data.attestation_queue,
escrow: fn_data.escrow, escrow: fn_data.escrow,
receiver: pubkeys.reward_receiver, receiver: pubkeys.reward_receiver,
verifier_permission, verifier_permission,
fn_permission, fn_permission,
state, state,
token_program: anchor_spl::token::ID, token_program: anchor_spl::token::ID,
payer: pubkeys.payer, payer: pubkeys.payer,
system_program: anchor_lang::solana_program::system_program::ID, system_program: anchor_lang::solana_program::system_program::ID,
}; };
let next_allowed_timestamp = fn_data let next_allowed_timestamp = fn_data
.next_execution_timestamp() .next_execution_timestamp()
.map(|x| x.timestamp()) .map(|x| x.timestamp())
.unwrap_or(i64::MAX); .unwrap_or(i64::MAX);
Ok(Self::build_ix( Ok(Self::build_ix(
accounts, accounts,
current_time, current_time,
next_allowed_timestamp,
false,
mr_enclave,
))
}
#[cfg(feature = "client")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "client")))]
fn build_ix(
accounts: FunctionVerifyAccounts,
observed_time: i64,
next_allowed_timestamp: i64,
is_failure: bool,
mr_enclave: [u8; 32],
) -> Instruction {
Instruction {
program_id: SWITCHBOARD_ATTESTATION_PROGRAM_ID,
accounts: accounts.to_account_metas(None),
data: FunctionVerifyParams {
observed_time,
next_allowed_timestamp, next_allowed_timestamp,
is_failure, false,
mr_enclave, mr_enclave,
))
}
fn build_ix(
accounts: FunctionVerifyAccounts,
observed_time: i64,
next_allowed_timestamp: i64,
is_failure: bool,
mr_enclave: [u8; 32],
) -> Instruction {
Instruction {
program_id: SWITCHBOARD_ATTESTATION_PROGRAM_ID,
accounts: accounts.to_account_metas(None),
data: FunctionVerifyParams {
observed_time,
next_allowed_timestamp,
is_failure,
mr_enclave,
}
.data(),
} }
.data(),
} }
} }
} }
#[cfg(feature = "client")] cfg_client! {
#[cfg_attr(doc_cfg, doc(cfg(feature = "client")))] pub struct FunctionVerifyAccounts {
pub struct FunctionVerifyAccounts { pub function: Pubkey,
pub function: Pubkey, pub fn_signer: Pubkey,
pub fn_signer: Pubkey, pub fn_quote: Pubkey,
pub fn_quote: Pubkey, pub verifier_quote: Pubkey,
pub verifier_quote: Pubkey, pub secured_signer: Pubkey,
pub secured_signer: Pubkey, pub attestation_queue: Pubkey,
pub attestation_queue: Pubkey, pub escrow: Pubkey,
pub escrow: Pubkey, pub receiver: Pubkey,
pub receiver: Pubkey, pub verifier_permission: Pubkey,
pub verifier_permission: Pubkey, pub fn_permission: Pubkey,
pub fn_permission: Pubkey, pub state: Pubkey,
pub state: Pubkey, pub token_program: Pubkey,
pub token_program: Pubkey, pub payer: Pubkey,
pub payer: Pubkey, pub system_program: Pubkey,
pub system_program: Pubkey, }
}
#[cfg(feature = "client")] impl ToAccountMetas for FunctionVerifyAccounts {
#[cfg_attr(doc_cfg, doc(cfg(feature = "client")))] fn to_account_metas(&self, _: Option<bool>) -> Vec<AccountMeta> {
impl ToAccountMetas for FunctionVerifyAccounts { vec![
fn to_account_metas(&self, _: Option<bool>) -> Vec<AccountMeta> { AccountMeta {
vec![ pubkey: self.function,
AccountMeta { is_signer: false,
pubkey: self.function, is_writable: true,
is_signer: false, },
is_writable: true, AccountMeta {
}, pubkey: self.fn_signer,
AccountMeta { is_signer: true,
pubkey: self.fn_signer, is_writable: false,
is_signer: true, },
is_writable: false, AccountMeta {
}, pubkey: self.fn_quote,
AccountMeta { is_signer: false,
pubkey: self.fn_quote, is_writable: true,
is_signer: false, },
is_writable: true, AccountMeta {
}, pubkey: self.verifier_quote,
AccountMeta { is_signer: false,
pubkey: self.verifier_quote, is_writable: false,
is_signer: false, },
is_writable: false, AccountMeta {
}, pubkey: self.secured_signer,
AccountMeta { is_signer: true,
pubkey: self.secured_signer, is_writable: false,
is_signer: true, },
is_writable: false, AccountMeta {
}, pubkey: self.attestation_queue,
AccountMeta { is_signer: false,
pubkey: self.attestation_queue, is_writable: false,
is_signer: false, },
is_writable: false, AccountMeta {
}, pubkey: self.escrow,
AccountMeta { is_signer: false,
pubkey: self.escrow, is_writable: true,
is_signer: false, },
is_writable: true, AccountMeta {
}, pubkey: self.receiver,
AccountMeta { is_signer: false,
pubkey: self.receiver, is_writable: true,
is_signer: false, },
is_writable: true, AccountMeta {
}, pubkey: self.verifier_permission,
AccountMeta { is_signer: false,
pubkey: self.verifier_permission, is_writable: false,
is_signer: false, },
is_writable: false, AccountMeta {
}, pubkey: self.fn_permission,
AccountMeta { is_signer: false,
pubkey: self.fn_permission, is_writable: false,
is_signer: false, },
is_writable: false, AccountMeta {
}, pubkey: self.state,
AccountMeta { is_signer: false,
pubkey: self.state, is_writable: true,
is_signer: false, },
is_writable: true, AccountMeta {
}, pubkey: self.token_program,
AccountMeta { is_signer: false,
pubkey: self.token_program, is_writable: false,
is_signer: false, },
is_writable: false, AccountMeta {
}, pubkey: self.payer,
AccountMeta { is_signer: true,
pubkey: self.payer, is_writable: true,
is_signer: true, },
is_writable: true, AccountMeta {
}, pubkey: self.system_program,
AccountMeta { is_signer: false,
pubkey: self.system_program, is_writable: false,
is_signer: false, },
is_writable: false, ]
}, }
]
} }
} }

View File

@ -123,12 +123,12 @@ impl OracleAccountData {
)) ))
} }
#[cfg(feature = "client")] cfg_client! {
#[cfg_attr(doc_cfg, 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 }
} }
} }

View File

@ -156,12 +156,13 @@ impl OracleQueueAccountData {
)) ))
} }
#[cfg(feature = "client")] cfg_client! {
#[cfg_attr(doc_cfg, 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 }
} }
} }

View File

@ -124,12 +124,12 @@ impl VrfAccountData {
Ok(self.current_round.result) Ok(self.current_round.result)
} }
#[cfg(feature = "client")] cfg_client! {
#[cfg_attr(doc_cfg, 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 }
} }
} }

View File

@ -134,12 +134,12 @@ impl VrfLiteAccountData {
Ok(self.result) Ok(self.result)
} }
#[cfg(feature = "client")] cfg_client! {
#[cfg_attr(doc_cfg, 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 }
} }
} }

View File

@ -97,13 +97,13 @@ impl VrfPoolAccountData {
)) ))
} }
#[cfg(feature = "client")] cfg_client! {
#[cfg_attr(doc_cfg, 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 }
} }
} }