1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use crate::rpc::errors::RpcError;
use crate::transaction_params::TransactionParams;
use account_compression::initialize_address_merkle_tree::{AnchorDeserialize, Pubkey};
use anchor_lang::solana_program::clock::Slot;
use anchor_lang::solana_program::instruction::Instruction;
use solana_sdk::account::{Account, AccountSharedData};
use solana_sdk::commitment_config::CommitmentConfig;
use solana_sdk::epoch_info::EpochInfo;
use solana_sdk::hash::Hash;
use solana_sdk::signature::{Keypair, Signature};
use solana_sdk::transaction::Transaction;
use std::fmt::Debug;

pub trait RpcConnection: Send + Sync + Debug + 'static {
    fn new<U: ToString>(_url: U, _commitment_config: Option<CommitmentConfig>) -> Self
    where
        Self: Sized,
    {
        unimplemented!()
    }

    fn health(&self) -> Result<(), RpcError> {
        unimplemented!()
    }

    fn get_block_time(&self, _slot: u64) -> Result<i64, RpcError> {
        unimplemented!()
    }

    fn get_program_accounts(&self, program_id: &Pubkey)
        -> Result<Vec<(Pubkey, Account)>, RpcError>;

    fn process_transaction(
        &mut self,
        transaction: Transaction,
    ) -> impl std::future::Future<Output = Result<Signature, RpcError>> + Send;

    fn process_transaction_with_context(
        &mut self,
        transaction: Transaction,
    ) -> impl std::future::Future<Output = Result<(Signature, Slot), RpcError>> + Send;

    fn create_and_send_transaction_with_event<T>(
        &mut self,
        instruction: &[Instruction],
        authority: &Pubkey,
        signers: &[&Keypair],
        transaction_params: Option<TransactionParams>,
    ) -> impl std::future::Future<Output = Result<Option<(T, Signature, Slot)>, RpcError>> + Send
    where
        T: AnchorDeserialize + Send + Debug;

    fn create_and_send_transaction<'a>(
        &'a mut self,
        instruction: &'a [Instruction],
        payer: &'a Pubkey,
        signers: &'a [&'a Keypair],
    ) -> impl std::future::Future<Output = Result<Signature, RpcError>> + Send + 'a {
        async move {
            let blockhash = self.get_latest_blockhash().await?;
            let transaction = Transaction::new_signed_with_payer(
                instruction,
                Some(payer),
                &signers.to_vec(),
                blockhash,
            );
            let signature = transaction.signatures[0];
            self.process_transaction(transaction).await?;
            Ok(signature)
        }
    }

    fn confirm_transaction(
        &self,
        transaction: Signature,
    ) -> impl std::future::Future<Output = Result<bool, RpcError>> + Send;

    fn get_payer(&self) -> &Keypair;
    fn get_account(
        &mut self,
        address: Pubkey,
    ) -> impl std::future::Future<Output = Result<Option<Account>, RpcError>> + Send;
    fn set_account(&mut self, address: &Pubkey, account: &AccountSharedData);

    fn get_minimum_balance_for_rent_exemption(
        &mut self,
        data_len: usize,
    ) -> impl std::future::Future<Output = Result<u64, RpcError>> + Send;

    fn airdrop_lamports(
        &mut self,
        to: &Pubkey,
        lamports: u64,
    ) -> impl std::future::Future<Output = Result<Signature, RpcError>> + Send;

    fn get_anchor_account<'a, T: AnchorDeserialize + 'static>(
        &'a mut self,
        pubkey: &'a Pubkey,
    ) -> impl std::future::Future<Output = Result<Option<T>, RpcError>> + Send + 'a {
        async move {
            match self.get_account(*pubkey).await? {
                Some(account) => {
                    let data = T::deserialize(&mut &account.data[8..]).map_err(RpcError::from)?;
                    Ok(Some(data))
                }
                None => Ok(None),
            }
        }
    }

    fn get_balance(
        &mut self,
        pubkey: &Pubkey,
    ) -> impl std::future::Future<Output = Result<u64, RpcError>> + Send;

    fn get_latest_blockhash(
        &mut self,
    ) -> impl std::future::Future<Output = Result<Hash, RpcError>> + Send;

    fn get_slot(&mut self) -> impl std::future::Future<Output = Result<u64, RpcError>> + Send;

    fn warp_to_slot(&mut self, _slot: Slot) -> Result<(), RpcError> {
        unimplemented!()
    }

    fn get_epoch_info(&self) -> Result<EpochInfo, RpcError> {
        unimplemented!()
    }

    fn send_transaction(
        &self,
        transaction: &Transaction,
    ) -> impl std::future::Future<Output = Result<Signature, RpcError>> + Send;

    fn get_url(&self) -> String;
}