#![allow(clippy::all)]
#[macro_export]
macro_rules! test_assertion {
($body: block) => {
(|| -> crate::Result<()> {
$body
Ok(())
})()
};
}
#[macro_export]
macro_rules! assert_does_not_throw {
($body: block $(,)?) => {
assert!($crate::test_assertion!($body).is_ok())
};
}
#[macro_export]
macro_rules! assert_throws {
($body: block, $right: expr $(,)?) => {
assert_eq!(
$crate::IntoCmpError::into_cmp_error($crate::test_assertion!($body).err()),
$crate::IntoCmpError::into_cmp_error(Some(::anchor_lang::prelude::error!($right)))
)
};
}
#[macro_export]
macro_rules! format_err {
($err: expr) => {
&*format!("{:?}: {}", $err, $err)
};
}
#[macro_export]
macro_rules! program_err {
($error:ident $(,)?) => {
Err(crate::ErrorCode::$error.into())
};
}
#[macro_export]
macro_rules! log_code_location {
() => {
msg!("Error thrown at {}:{}", file!(), line!());
};
}
#[macro_export]
macro_rules! unwrap_opt_block {
($body:block $($arg:tt)*) => {
$crate::unwrap_opt!(
#[allow(clippy::redundant_closure_call)]
(|| { $body } )() $($arg)*)
};
}
#[macro_export]
macro_rules! unwrap_checked {
($body:block $(,)?) => {
$crate::unwrap_opt_block!($body, $crate::VipersError::IntegerOverflow)
};
}
#[macro_export]
macro_rules! throw_err {
($error:ident $(,)?) => {
$crate::throw_err!(crate::ErrorCode::$error);
};
($error:expr $(,)?) => {
$crate::log_code_location!();
return Err(::anchor_lang::prelude::error!($error));
};
}
#[cfg(feature = "spl-associated-token-account")]
#[macro_export]
#[deprecated(
since = "1.5.6",
note = "This uses a lot of compute units due to the need to generate a PDA. This is also not a valid way to check ownership since ATAs can be transferred. Use assert_keys_eq on the token account owner instead."
)]
macro_rules! assert_ata {
($ata: expr, $owner: expr, $mint: expr $(,)?) => {
$crate::assert_ata!($ata, $owner, $mint, "ata mismatch")
};
($ata: expr, $owner: expr, $mint: expr, $msg: expr $(,)?) => {{
let __ata = $crate::AsKeyRef::as_key_ref(&$ata);
let __real_ata = $crate::ata::get_associated_token_address(
$crate::AsKeyRef::as_key_ref(&$owner),
$crate::AsKeyRef::as_key_ref(&$mint),
);
if &__real_ata != __ata {
msg!(
"ATA mismatch: {}: {} (left) != {} (right)",
$msg,
__ata,
__real_ata
);
msg!("Owner: {}", $crate::AsKeyRef::as_key_ref(&$owner));
msg!("Mint: {}", $crate::AsKeyRef::as_key_ref(&$mint));
$crate::throw_err!($crate::VipersError::ATAMismatch);
}
}};
}
#[macro_export]
macro_rules! assert_is_ata {
($ata: expr $(,)?) => {
$crate::assert_ata!($ata, "invalid ata")
};
($ata: expr, $msg: expr $(,)?) => {{
$crate::assert_owner!($ata, token, "ATA not owned by token program");
let __owner = $ata.owner;
let __mint = $ata.mint;
let __ata = anchor_lang::Key::key(&$ata);
let __real_ata =
$crate::spl_associated_token_account::get_associated_token_address(&__owner, &__mint);
if __real_ata != __ata {
msg!(
"Invalid ATA: {}: {} (left) != {} (right)",
$msg,
__ata,
__real_ata
);
msg!("Owner: {}", __owner);
msg!("Mint: {}", __mint);
$crate::throw_err!($crate::VipersError::InvalidATA);
}
}};
}
#[macro_export]
#[deprecated(
since = "1.5.6",
note = "As of Anchor 0.15, Anchor handles this for you automatically."
)]
macro_rules! assert_owner {
($program_account: expr, $owner: expr $(,)?) => {
$crate::assert_owner!($program_account, $owner, "owner mismatch")
};
($program_account: expr, $owner: ident $(,)?) => {
$crate::assert_owner!($program_account, $owner);
};
($program_account: expr, $owner: ident, $msg: expr $(,)?) => {
let __program_id = $crate::program_ids::$owner::ID;
$crate::assert_owner!($program_account, $owner, $msg);
};
($program_account: expr, $owner: expr, $msg: expr $(,)?) => {{
let __program_account =
anchor_lang::ToAccountInfo::to_account_info(&$program_account).owner;
let __owner = $crate::AsKeyRef::as_key_ref(&$owner);
if __program_account != __owner {
msg!(
"Owner mismatch: {}: expected {}, got {}",
$msg,
__program_account,
__owner
);
return Err($crate::VipersError::OwnerMismatch.into());
}
}};
}
#[macro_export]
macro_rules! assert_keys_eq {
($account_a: expr, $account_b: expr $(,)?) => {
$crate::assert_keys_eq!($account_a, $account_b, $crate::VipersError::KeyMismatch);
};
($account_a: expr, $account_b: expr, $err_code: ident $(,)?) => {
$crate::assert_keys_eq!($account_a, $account_b, crate::ErrorCode::$err_code);
};
($account_a: expr, $account_b: expr, $msg: literal $(,)?) => {
$crate::assert_keys_eq!(
$account_a,
$account_b,
$crate::VipersError::KeyMismatch,
&*format!("Key mismatch: {}", $msg),
);
};
($account_a: expr, $account_b: expr, $err: expr $(,)?) => {
$crate::assert_keys_eq!($account_a, $account_b, $err, $crate::format_err!($err));
};
($account_a: expr, $account_b: expr, $err: expr, $msg: expr $(,)?) => {{
let __key_a = &$account_a;
let __key_b = &$account_b;
let __account_a = $crate::AsKeyRef::as_key_ref(__key_a);
let __account_b = $crate::AsKeyRef::as_key_ref(__key_b);
if __account_a != __account_b {
msg!($msg);
msg!(stringify!($account_a != $account_b));
msg!("Left: {}", __account_a);
msg!("Right: {}", __account_b);
$crate::throw_err!($err);
}
}};
}
#[macro_export]
macro_rules! assert_is_zero_token_account {
($token_account: expr $(,)?) => {
$crate::assert_is_zero_token_account!(
$token_account,
$crate::VipersError::TokenAccountIsNonZero
);
};
($token_account: expr, $err_code: ident $(,)?) => {
$crate::assert_is_zero_token_account!($token_account, crate::ErrorCode::$err_code);
};
($token_account: expr, $msg: literal $(,)?) => {
$crate::assert_is_zero_token_account!(
$token_account,
$crate::VipersError::TokenAccountIsNonZero,
&*format!("Token account is non-zero: {}", $msg),
);
};
($token_account: expr, $err: expr $(,)?) => {
$crate::assert_is_zero_token_account!($token_account, $err, $crate::format_err!($err));
};
($token_account: expr, $err: expr, $msg: expr $(,)?) => {{
$crate::invariant!(
$token_account.amount == 0
&& $token_account.delegate.is_none()
&& $token_account.close_authority.is_none(),
$err,
$msg
);
}};
}
#[macro_export]
macro_rules! assert_keys_neq {
($account_a: expr, $account_b: expr $(,)?) => {
$crate::assert_keys_neq!(
$account_a,
$account_b,
$crate::VipersError::KeysMustNotMatch
);
};
($account_a: expr, $account_b: expr, $err_code: ident $(,)?) => {
$crate::assert_keys_neq!($account_a, $account_b, crate::ErrorCode::$err_code);
};
($account_a: expr, $account_b: expr, $msg: literal $(,)?) => {
$crate::assert_keys_neq!(
$account_a,
$account_b,
$crate::VipersError::KeysMustNotMatch,
&*format!("Keys must not match: {}", $msg),
);
};
($account_a: expr, $account_b: expr, $err: expr $(,)?) => {
$crate::assert_keys_neq!($account_a, $account_b, $err, $crate::format_err!($err));
};
($account_a: expr, $account_b: expr, $err: expr, $msg: expr $(,)?) => {{
let __key_a = &$account_a;
let __key_b = &$account_b;
let __account_a = $crate::AsKeyRef::as_key_ref(__key_a);
let __account_b = $crate::AsKeyRef::as_key_ref(__key_b);
if __account_a == __account_b {
msg!($msg);
msg!(stringify!($account_a == $account_b));
msg!("Left: {}", __account_a);
msg!("Right: {}", __account_b);
$crate::throw_err!($err);
}
}};
}
#[macro_export]
macro_rules! unwrap_or_err {
($option:expr, $error:ident $(,)?) => {
$option.ok_or_else(|| -> ProgramError { crate::ErrorCode::$error.into() })?
};
}
#[macro_export]
macro_rules! unwrap_int {
($option:expr $(,)?) => {
$crate::unwrap_opt!($option, $crate::VipersError::IntegerOverflow)
};
}
#[macro_export]
macro_rules! unwrap_bump {
($ctx:expr, $bump:literal $(,)?) => {
*$crate::unwrap_opt!(
$ctx.bumps.get($bump),
$crate::VipersError::UnknownBump,
format!("Unknown bump: {}", $bump)
)
};
}
#[macro_export]
macro_rules! try_or_err {
($result:expr, $error:expr $(,)?) => {
$result.map_err(|_| $error)?
};
}
#[macro_export]
macro_rules! invariant {
($invariant: expr $(,)?) => {
$crate::invariant!($invariant, $crate::VipersError::InvariantFailed);
};
($invariant: expr, $err_code: ident $(,)?) => {
$crate::invariant!($invariant, crate::ErrorCode::$err_code);
};
($invariant: expr, $err_code: ident, $msg: expr $(,)?) => {
$crate::invariant!($invariant, crate::ErrorCode::$err_code, $msg);
};
($invariant: expr, $msg: literal $(,)?) => {
$crate::invariant!(
$invariant,
$crate::VipersError::InvariantFailed,
&*format!("Invariant failed: {}", $msg)
);
};
($invariant:expr, $err:expr $(,)?) => {
$crate::invariant!($invariant, $err, $crate::format_err!($err));
};
($invariant:expr, $err:expr, $msg: expr $(,)?) => {{
if !($invariant) {
msg!($msg);
msg!(stringify!($invariant));
$crate::throw_err!($err);
}
}};
}
#[macro_export]
macro_rules! unwrap_opt {
($option: expr $(,)?) => {
$crate::unwrap_opt!(
$option,
$crate::VipersError::OptionUnwrapFailed,
$crate::format_err!($crate::VipersError::OptionUnwrapFailed)
)
};
($option: expr, $err_code: ident $(,)?) => {
$crate::unwrap_opt!($option, crate::ErrorCode::$err_code)
};
($option: expr, $msg: literal $(,)?) => {
$crate::unwrap_opt!($option, $crate::VipersError::OptionUnwrapFailed, $msg)
};
($option:expr, $err:expr $(,)?) => {
$crate::unwrap_opt!($option, $err, $crate::format_err!($err))
};
($option:expr, $err:expr, $msg: expr $(,)?) => {
$option.ok_or_else(|| -> anchor_lang::error::Error {
msg!("Option unwrap failed: {:?}", $err);
msg!(stringify!($option));
$crate::log_code_location!();
anchor_lang::prelude::error!($err)
})?
};
}
#[deprecated]
#[macro_export]
macro_rules! assert_keys {
($account_a: expr, $account_b: expr $(,)?) => {
$crate::assert_keys_eq!($account_a, $account_b, "key mismatch")
};
($account_a: expr, $account_b: expr, $msg: expr $(,)?) => {
$crate::assert_keys_eq!($account_a, $account_b, $msg)
};
}