gmsol_solana_utils/
program.rs

1use std::ops::Deref;
2
3use solana_sdk::{pubkey::Pubkey, signer::Signer};
4
5#[cfg(client)]
6use solana_client::nonblocking::rpc_client::RpcClient;
7
8use crate::transaction_builder::{Config, TransactionBuilder};
9
10/// Program.
11pub struct Program<C> {
12    program_id: Pubkey,
13    cfg: Config<C>,
14}
15
16impl<C> Program<C> {
17    /// Create a new [`Program`].
18    pub fn new(program_id: Pubkey, cfg: Config<C>) -> Self {
19        Self { program_id, cfg }
20    }
21
22    /// Create a Solana RPC Client.
23    #[cfg(client)]
24    pub fn rpc(&self) -> RpcClient {
25        self.cfg.rpc()
26    }
27
28    /// Get the program id.
29    pub fn id(&self) -> &Pubkey {
30        &self.program_id
31    }
32}
33
34impl<C: Deref<Target = impl Signer> + Clone> Program<C> {
35    /// Create a [`TransactionBuilder`].
36    pub fn transaction(&self) -> TransactionBuilder<C> {
37        TransactionBuilder::new(self.program_id, &self.cfg)
38    }
39}
40
41impl<C: Deref<Target = impl Signer>> Program<C> {
42    /// Get the pubkey of the payer.
43    pub fn payer(&self) -> Pubkey {
44        self.cfg.payer()
45    }
46}