#[macro_export]
macro_rules! cfg_client {
($($item:item)*) => {
$(
#[cfg(all(feature = "client"))]
$item
)*
};
}
#[macro_export]
macro_rules! build_ix_compat {
($program_id:expr, $accounts:expr, $params:expr) => {{
$crate::utils::build_ix($program_id, $accounts, $params)
}};
}
#[macro_export]
macro_rules! return_ix_compat {
($ix:expr) => {{
#[cfg(all(feature = "solana-v2", feature = "client"))]
{
Ok(unsafe {
$crate::instruction_compat::mixed_version::convert_any_instruction_to_compat_unsafe(
$ix,
)
}
.to_v2())
}
#[cfg(not(all(feature = "solana-v2", feature = "client")))]
{
Ok($ix)
}
}};
}
#[macro_export]
macro_rules! cfg_secrets {
($($item:item)*) => {
$(
#[cfg(all(feature = "secrets"))]
$item
)*
};
}
#[macro_export]
macro_rules! cfg_storage {
($($item:item)*) => {
$(
#[cfg(all(feature = "storage"))]
$item
)*
};
}
#[macro_export]
macro_rules! retry {
($attempts:expr, $delay_ms:expr, $expr:expr) => {{
async {
let mut attempts = $attempts;
loop {
match $expr {
Ok(val) => break Ok(val),
Err(_) if attempts > 1 => {
attempts -= 1;
tokio::time::sleep(tokio::time::Duration::from_millis($delay_ms)).await;
}
Err(e) => break Err(e),
}
}
}
}};
}
#[macro_export]
macro_rules! blocking_retry {
($attempts:expr, $delay_ms:expr, $expr:expr) => {{
let mut attempts = $attempts;
loop {
match $expr {
Ok(val) => break Ok(val),
Err(_) if attempts > 1 => {
attempts -= 1;
std::thread::sleep(tokio::time::Duration::from_millis($delay_ms));
}
Err(e) => break Err(e),
}
}
}};
}
#[macro_export]
macro_rules! impl_account_deserialize {
($struct_name:ident) => {
use anchor_client;
use anchor_lang::prelude::{Error, ErrorCode};
impl anchor_client::anchor_lang::AccountDeserialize for $struct_name {
fn try_deserialize(buf: &mut &[u8]) -> Result<Self, Error> {
use $crate::anchor_traits::Discriminator;
if buf.len() < $struct_name::discriminator().len() {
return Err(ErrorCode::AccountDiscriminatorMismatch.into());
}
let given_disc = &buf[..8];
if $struct_name::discriminator() != given_disc {
return Err(ErrorCode::AccountDiscriminatorMismatch.into());
}
Self::try_deserialize_unchecked(buf)
}
fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self, Error> {
let data: &[u8] = &buf[8..];
bytemuck::try_from_bytes(data)
.map(|r: &Self| *r)
.map_err(|_| ErrorCode::AccountDidNotDeserialize.into())
}
}
};
}