#[macro_export]
macro_rules! delegate_registry_program_id {
() => {{
use std::str::FromStr;
Pubkey::from_str("De1egateWvUcfjtzjz3Y19NdYFwX7QjHNZG9MPNeD3QK").unwrap()
}};
}
#[macro_export]
macro_rules! delegation_account_is_valid {
($delegation:expr, $name:expr, $($key:expr),* $(,)?) => {{
let pda_owner = delegate_registry_program_id!();
let (pda, _bump) = Pubkey::find_program_address(&[$name, $($key.as_ref(),)*], &pda_owner);
pda == $delegation.key() && $delegation.owner == &pda_owner
}};
}
#[macro_export]
macro_rules! delegation_account_slice {
($delegation:expr, $start:expr, $len:expr) => {
$delegation.data.borrow()[$start..$start + $len]
.try_into()
.unwrap_or([0; $len])
};
}
#[macro_export]
macro_rules! all_delegation_is_enabled {
($delegation:expr, $rights:expr, $from:expr, $to:expr) => {
delegation_account_is_valid!($delegation, b"all", $rights, $from, $to)
&& delegation_account_slice!($delegation, 8 + 32 * 3, 1)[0] != 0
};
}
#[macro_export]
macro_rules! program_delegation_is_enabled {
($delegation:expr, $rights:expr, $from:expr, $to:expr, $program:expr, $account:expr) => {
delegation_account_is_valid!(
$delegation,
b"program",
$rights,
$from,
$to,
$program,
$account
) && delegation_account_slice!($delegation, 8 + 32 * 5, 1)[0] != 0
};
}
#[macro_export]
macro_rules! token_delegation_amount {
($delegation:expr, $rights:expr, $from:expr, $to:expr, $program:expr, $mint:expr, $account:expr) => {
if delegation_account_is_valid!(
$delegation,
b"token",
$rights,
$from,
$to,
$program,
$mint,
$account
) {
u64::from_le_bytes(delegation_account_slice!($delegation, 8 + 32 * 6, 8))
} else {
0 as u64
}
};
}