use std::fmt::Display;
use data_anchor_api::LedgerDataBlobError;
use data_anchor_blober::instruction::{
Close, ConfigureCheckpoint, DeclareBlob, DiscardBlob, FinalizeBlob, Initialize, InsertChunk,
};
use data_anchor_utils::DataAnchorUtilsError;
use nitro_sender::TransactionOutcome;
use solana_commitment_config::ParseCommitmentLevelError;
use solana_rpc_client_api::client_error::Error;
use thiserror::Error;
use crate::{
client::{ChainError, IndexerError, ProofError},
tx::{Compound, CompoundDeclare, CompoundFinalize, MessageBuilder},
};
#[derive(Debug, Error)]
pub enum DataAnchorClientError {
#[error(transparent)]
ChainErrors(#[from] ChainError),
#[error(transparent)]
Indexer(#[from] IndexerError),
#[error(transparent)]
Proof(#[from] ProofError),
#[error("Failed to query Solana RPC: {0}")]
SolanaRpc(#[from] Error),
#[error("Invalid commitment: {0}")]
InvalidCommitment(#[from] ParseCommitmentLevelError),
#[error("Invalid indexer url: {0}")]
InvalidIndexerUrl(#[from] jsonrpsee::core::client::Error),
#[error("Invalid indexer API token: {0}")]
InvalidIndexerApiToken(String),
#[error("Invalid key or namespace for blober")]
InvalidKeyOrNamespace,
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("Ledger data blob error: {0}")]
LedgerDataBlob(#[from] LedgerDataBlobError),
#[error("Invalid data: {0}")]
InvalidData(String),
#[error(transparent)]
UtilsError(#[from] DataAnchorUtilsError),
#[error(transparent)]
IndexerUrlError(#[from] crate::constants::IndexerUrlError),
#[error("Tokio task error: {0}")]
TokioTaskError(#[from] tokio::task::JoinError),
}
pub type DataAnchorClientResult<T = ()> = Result<T, DataAnchorClientError>;
#[derive(Error, Debug)]
pub enum OutcomeError {
#[error(
"Transaction outcomes were not successfull: \n{}",
.0.iter().filter_map(TransactionOutcome::error).map(|t| format!("- {}: {} [{}]", t.data, t.error, t.logs.join("\n"))).collect::<Vec<_>>().join("\n")
)]
Unsuccesful(Vec<TransactionOutcome<TransactionType>>),
}
#[derive(Debug, Clone, Copy)]
pub enum TransactionType {
CloseBlober,
Compound,
CompoundDeclare,
CompoundFinalize,
ConfigureCheckpoint,
DeclareBlob,
DiscardBlob,
FinalizeBlob,
InitializeBlober,
InsertChunk(u16),
}
impl Display for TransactionType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TransactionType::CloseBlober => write!(f, "CloseBlober"),
TransactionType::Compound => write!(f, "CompoundUpload"),
TransactionType::CompoundDeclare => write!(f, "CompoundDeclare"),
TransactionType::CompoundFinalize => write!(f, "CompoundFinalize"),
TransactionType::ConfigureCheckpoint => write!(f, "CreateCheckpoint"),
TransactionType::DeclareBlob => write!(f, "DeclareBlob"),
TransactionType::DiscardBlob => write!(f, "DiscardBlob"),
TransactionType::FinalizeBlob => write!(f, "FinalizeBlob"),
TransactionType::InitializeBlober => write!(f, "InitializeBlober"),
TransactionType::InsertChunk(i) => write!(f, "InsertChunk {i}"),
}
}
}
impl TransactionType {
pub(crate) fn num_signatures(&self) -> u16 {
match self {
TransactionType::CloseBlober => Close::NUM_SIGNATURES,
TransactionType::Compound => Compound::NUM_SIGNATURES,
TransactionType::CompoundDeclare => CompoundDeclare::NUM_SIGNATURES,
TransactionType::CompoundFinalize => CompoundFinalize::NUM_SIGNATURES,
TransactionType::ConfigureCheckpoint => ConfigureCheckpoint::NUM_SIGNATURES,
TransactionType::DeclareBlob => DeclareBlob::NUM_SIGNATURES,
TransactionType::DiscardBlob => DiscardBlob::NUM_SIGNATURES,
TransactionType::FinalizeBlob => FinalizeBlob::NUM_SIGNATURES,
TransactionType::InitializeBlober => Initialize::NUM_SIGNATURES,
TransactionType::InsertChunk(_) => InsertChunk::NUM_SIGNATURES,
}
}
pub(crate) fn compute_unit_limit(&self) -> u32 {
match self {
TransactionType::CloseBlober => Close::COMPUTE_UNIT_LIMIT,
TransactionType::Compound => Compound::COMPUTE_UNIT_LIMIT,
TransactionType::CompoundDeclare => CompoundDeclare::COMPUTE_UNIT_LIMIT,
TransactionType::CompoundFinalize => CompoundFinalize::COMPUTE_UNIT_LIMIT,
TransactionType::ConfigureCheckpoint => ConfigureCheckpoint::COMPUTE_UNIT_LIMIT,
TransactionType::DeclareBlob => DeclareBlob::COMPUTE_UNIT_LIMIT,
TransactionType::DiscardBlob => DiscardBlob::COMPUTE_UNIT_LIMIT,
TransactionType::FinalizeBlob => FinalizeBlob::COMPUTE_UNIT_LIMIT,
TransactionType::InitializeBlober => Initialize::COMPUTE_UNIT_LIMIT,
TransactionType::InsertChunk(_) => InsertChunk::COMPUTE_UNIT_LIMIT,
}
}
}