pub mod explorer;
pub mod pubkey;
pub mod thread;
use std::fmt::{Debug, Display, Formatter};
use anchor_lang::{prelude::Pubkey, prelude::*, AnchorDeserialize};
use base64;
#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug)]
pub struct CrateInfo {
pub spec: String,
pub blob: String,
}
impl Display for CrateInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "spec:{} blob:{}", self.spec, self.blob)
}
}
pub trait ProgramLogsDeserializable {
fn try_from_program_logs(
program_logs: Vec<String>,
program_id: &Pubkey,
) -> std::result::Result<Self, ErrorCode>
where
Self: Sized;
}
impl<T> ProgramLogsDeserializable for T
where
T: AnchorDeserialize,
{
fn try_from_program_logs(
program_logs: Vec<String>,
program_id: &Pubkey,
) -> std::result::Result<T, ErrorCode> {
let preimage = format!("Program return: {} ", program_id.to_string());
let get_return_data_base64 = program_logs
.iter()
.find(|&s| s.starts_with(&preimage))
.ok_or(ErrorCode::AccountDidNotDeserialize)?
.strip_prefix(&preimage)
.ok_or(ErrorCode::AccountDidNotDeserialize)?;
let decoded = base64::decode(get_return_data_base64)
.map_err(|_err| ErrorCode::AccountDidNotDeserialize)?;
T::try_from_slice(&decoded).map_err(|_err| ErrorCode::AccountDidNotDeserialize)
}
}