#[cfg(feature = "anchor")]
use anchor_lang::solana_program;
#[cfg(not(feature = "anchor"))]
use crate::solana_program;
use solana_program::pubkey::Pubkey;
fn derive_lookup_table_address(authority_address: &Pubkey, recent_block_slot: u64) -> (Pubkey, u8) {
use crate::on_demand::ADDRESS_LOOKUP_TABLE_PROGRAM_ID;
Pubkey::find_program_address(
&[authority_address.as_ref(), &recent_block_slot.to_le_bytes()],
&ADDRESS_LOOKUP_TABLE_PROGRAM_ID.to_bytes().into(),
)
}
use crate::{cfg_client, utils, ON_DEMAND_DEVNET_PID, ON_DEMAND_MAINNET_PID};
const LUT_SIGNER_SEED: &[u8] = b"LutSigner";
pub fn find_lut_signer(k: &Pubkey) -> Pubkey {
let pid = if utils::is_devnet() {
ON_DEMAND_DEVNET_PID
} else {
ON_DEMAND_MAINNET_PID
};
Pubkey::find_program_address(&[LUT_SIGNER_SEED, k.as_ref()], &pid).0
}
pub fn find_lut_of(k: &Pubkey, lut_slot: u64) -> Pubkey {
derive_lookup_table_address(k, lut_slot).0
}
cfg_client! {
use crate::OnDemandError;
use anchor_client::solana_client::nonblocking::rpc_client::RpcClient;
use spl_associated_token_account::solana_program::address_lookup_table::state::AddressLookupTable;
use spl_associated_token_account::solana_program::address_lookup_table::AddressLookupTableAccount;
pub async fn fetch(client: &RpcClient, address: &Pubkey) -> Result<AddressLookupTableAccount, OnDemandError> {
let converted_address: anchor_client::solana_sdk::pubkey::Pubkey = address.to_bytes().into();
let account = client.get_account_data(&converted_address)
.await
.map_err(|_| OnDemandError::AddressLookupTableFetchError)?;
let lut = AddressLookupTable::deserialize(&account)
.map_err(|_| OnDemandError::AddressLookupTableDeserializeError)?;
let out = AddressLookupTableAccount {
key: address.to_bytes().into(),
addresses: lut.addresses.iter().cloned().collect(),
};
Ok(out)
}
}