use std::{fmt::Debug, future::Future};
use light_concurrent_merkle_tree::light_hasher::Poseidon;
use light_indexed_merkle_tree::{
array::{IndexedArray, IndexedElement},
reference::IndexedMerkleTree,
};
use light_merkle_tree_reference::MerkleTree;
use light_sdk::{
compressed_account::CompressedAccountWithMerkleContext, event::PublicTransactionEvent,
proof::ProofRpcResult, token::TokenDataWithMerkleContext,
};
use num_bigint::BigUint;
use solana_sdk::pubkey::Pubkey;
use thiserror::Error;
use crate::rpc::RpcConnection;
pub mod test_indexer;
#[derive(Error, Debug)]
pub enum IndexerError {
#[error("RPC Error: {0}")]
RpcError(#[from] solana_client::client_error::ClientError),
#[error("failed to deserialize account data")]
DeserializeError(#[from] solana_sdk::program_error::ProgramError),
#[error("failed to copy merkle tree")]
CopyMerkleTreeError(#[from] std::io::Error),
#[error("error: {0:?}")]
Custom(String),
#[error("unknown error")]
Unknown,
}
pub trait Indexer<R: RpcConnection>: Sync + Send + Debug + 'static {
fn add_event_and_compressed_accounts(
&mut self,
event: &PublicTransactionEvent,
) -> (
Vec<CompressedAccountWithMerkleContext>,
Vec<TokenDataWithMerkleContext>,
);
fn create_proof_for_compressed_accounts(
&mut self,
compressed_accounts: Option<&[[u8; 32]]>,
state_merkle_tree_pubkeys: Option<&[Pubkey]>,
new_addresses: Option<&[[u8; 32]]>,
address_merkle_tree_pubkeys: Option<Vec<Pubkey>>,
rpc: &mut R,
) -> impl Future<Output = ProofRpcResult>;
fn get_compressed_accounts_by_owner(
&self,
owner: &Pubkey,
) -> Vec<CompressedAccountWithMerkleContext>;
}
#[derive(Debug, Clone)]
pub struct MerkleProof {
pub hash: String,
pub leaf_index: u64,
pub merkle_tree: String,
pub proof: Vec<[u8; 32]>,
pub root_seq: u64,
}
#[derive(Clone, Default, Debug, PartialEq)]
pub struct NewAddressProofWithContext {
pub merkle_tree: [u8; 32],
pub root: [u8; 32],
pub root_seq: u64,
pub low_address_index: u64,
pub low_address_value: [u8; 32],
pub low_address_next_index: u64,
pub low_address_next_value: [u8; 32],
pub low_address_proof: [[u8; 32]; 16],
pub new_low_element: Option<IndexedElement<usize>>,
pub new_element: Option<IndexedElement<usize>>,
pub new_element_next_value: Option<BigUint>,
}
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub struct StateMerkleTreeAccounts {
pub merkle_tree: Pubkey,
pub nullifier_queue: Pubkey,
pub cpi_context: Pubkey,
}
#[derive(Debug, Clone, Copy)]
pub struct AddressMerkleTreeAccounts {
pub merkle_tree: Pubkey,
pub queue: Pubkey,
}
#[derive(Debug, Clone)]
pub struct StateMerkleTreeBundle {
pub rollover_fee: u64,
pub merkle_tree: Box<MerkleTree<Poseidon>>,
pub accounts: StateMerkleTreeAccounts,
}
#[derive(Debug, Clone)]
pub struct AddressMerkleTreeBundle {
pub rollover_fee: u64,
pub merkle_tree: Box<IndexedMerkleTree<Poseidon, usize>>,
pub indexed_array: Box<IndexedArray<Poseidon, usize>>,
pub accounts: AddressMerkleTreeAccounts,
}