light_client/rpc/
rpc_connection.rs

1use crate::rpc::errors::RpcError;
2use crate::transaction_params::TransactionParams;
3use async_trait::async_trait;
4use borsh::BorshDeserialize;
5use solana_program::clock::Slot;
6use solana_program::instruction::Instruction;
7use solana_sdk::account::{Account, AccountSharedData};
8use solana_sdk::commitment_config::CommitmentConfig;
9use solana_sdk::epoch_info::EpochInfo;
10use solana_sdk::hash::Hash;
11use solana_sdk::pubkey::Pubkey;
12use solana_sdk::signature::{Keypair, Signature};
13use solana_sdk::transaction::Transaction;
14use std::fmt::Debug;
15
16#[async_trait]
17pub trait RpcConnection: Send + Sync + Debug + 'static {
18    fn new<U: ToString>(url: U, commitment_config: Option<CommitmentConfig>) -> Self
19    where
20        Self: Sized;
21
22    fn get_payer(&self) -> &Keypair;
23    fn get_url(&self) -> String;
24
25    async fn health(&self) -> Result<(), RpcError>;
26    async fn get_block_time(&self, slot: u64) -> Result<i64, RpcError>;
27    async fn get_epoch_info(&self) -> Result<EpochInfo, RpcError>;
28
29    async fn get_program_accounts(
30        &self,
31        program_id: &Pubkey,
32    ) -> Result<Vec<(Pubkey, Account)>, RpcError>;
33    async fn process_transaction(
34        &mut self,
35        transaction: Transaction,
36    ) -> Result<Signature, RpcError>;
37    async fn process_transaction_with_context(
38        &mut self,
39        transaction: Transaction,
40    ) -> Result<(Signature, Slot), RpcError>;
41
42    async fn create_and_send_transaction_with_event<T>(
43        &mut self,
44        instructions: &[Instruction],
45        authority: &Pubkey,
46        signers: &[&Keypair],
47        transaction_params: Option<TransactionParams>,
48    ) -> Result<Option<(T, Signature, Slot)>, RpcError>
49    where
50        T: BorshDeserialize + Send + Debug;
51
52    async fn create_and_send_transaction<'a>(
53        &'a mut self,
54        instructions: &'a [Instruction],
55        payer: &'a Pubkey,
56        signers: &'a [&'a Keypair],
57    ) -> Result<Signature, RpcError> {
58        let blockhash = self.get_latest_blockhash().await?;
59        let transaction =
60            Transaction::new_signed_with_payer(instructions, Some(payer), signers, blockhash);
61        self.process_transaction(transaction).await
62    }
63
64    async fn confirm_transaction(&self, signature: Signature) -> Result<bool, RpcError>;
65    async fn get_account(&mut self, address: Pubkey) -> Result<Option<Account>, RpcError>;
66    fn set_account(&mut self, address: &Pubkey, account: &AccountSharedData);
67    async fn get_minimum_balance_for_rent_exemption(
68        &mut self,
69        data_len: usize,
70    ) -> Result<u64, RpcError>;
71    async fn airdrop_lamports(&mut self, to: &Pubkey, lamports: u64)
72        -> Result<Signature, RpcError>;
73
74    async fn get_anchor_account<T: BorshDeserialize>(
75        &mut self,
76        pubkey: &Pubkey,
77    ) -> Result<Option<T>, RpcError> {
78        match self.get_account(*pubkey).await? {
79            Some(account) => {
80                let data = T::deserialize(&mut &account.data[8..]).map_err(RpcError::from)?;
81                Ok(Some(data))
82            }
83            None => Ok(None),
84        }
85    }
86
87    async fn get_balance(&mut self, pubkey: &Pubkey) -> Result<u64, RpcError>;
88    async fn get_latest_blockhash(&mut self) -> Result<Hash, RpcError>;
89    async fn get_slot(&mut self) -> Result<u64, RpcError>;
90    async fn warp_to_slot(&mut self, slot: Slot) -> Result<(), RpcError>;
91    async fn send_transaction(&self, transaction: &Transaction) -> Result<Signature, RpcError>;
92}