delegate-registry 0.0.1

Helper library for the delegate registry on Solana
Documentation
/// Returns the delegate registry program ID.
#[macro_export]
macro_rules! delegate_registry_program_id {
    () => {{
        use std::str::FromStr;
        Pubkey::from_str("De1egateWvUcfjtzjz3Y19NdYFwX7QjHNZG9MPNeD3QK").unwrap()
    }};
}

/// Returns true if the delegation account is valid, else false.
#[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
    }};
}

/// Returns a slice of the delegation account data.
/// If the slice is out of bounds, returns a zeroed array.
/// This macro does not check if the delegation account is valid.
#[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])
    };
}

/// Returns true if the all delegation account is valid and enabled, else false.
#[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
    };
}

/// Returns true if the program delegation account is valid and enabled, else false.
#[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
    };
}

/// Returns the amount from a token delegation account.
/// If the delegation account is not valid, returns 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
        }
    };
}