Skip to main content

light_token_client/actions/
transfer.rs

1//! Transfer actions for Light Token.
2//!
3//! These actions provide clean interfaces for transferring Light Tokens.
4
5use light_client::rpc::{Rpc, RpcError};
6use light_token::instruction::Transfer as TransferInstruction;
7use solana_keypair::Keypair;
8use solana_pubkey::Pubkey;
9use solana_signature::Signature;
10use solana_signer::Signer;
11
12/// Parameters for transferring Light Tokens between accounts.
13///
14/// # Example
15/// ```ignore
16/// Transfer {
17///     source,
18///     destination,
19///     amount: 1000,
20///     ..Default::default()
21/// }.execute(&mut rpc, &payer, &authority).await?;
22/// ```
23#[derive(Default, Clone, Debug)]
24pub struct Transfer {
25    /// Source token account.
26    pub source: Pubkey,
27    /// Destination token account.
28    pub destination: Pubkey,
29    /// Amount of tokens to transfer.
30    pub amount: u64,
31}
32
33impl Transfer {
34    /// Execute the transfer action via RPC.
35    ///
36    /// # Arguments
37    /// * `rpc` - RPC client
38    /// * `payer` - Transaction fee payer keypair (also pays for rent top-ups)
39    /// * `authority` - Authority that can spend from the source account
40    ///
41    /// # Returns
42    /// `Result<Signature, RpcError>` - The transaction signature
43    pub async fn execute<R: Rpc>(
44        self,
45        rpc: &mut R,
46        payer: &Keypair,
47        authority: &Keypair,
48    ) -> Result<Signature, RpcError> {
49        let ix = TransferInstruction {
50            source: self.source,
51            destination: self.destination,
52            amount: self.amount,
53            authority: authority.pubkey(),
54            fee_payer: payer.pubkey(),
55        }
56        .instruction()
57        .map_err(|e| RpcError::CustomError(format!("Failed to create instruction: {}", e)))?;
58
59        let mut signers = vec![payer];
60        if authority.pubkey() != payer.pubkey() {
61            signers.push(authority);
62        }
63
64        rpc.create_and_send_transaction(&[ix], &payer.pubkey(), &signers)
65            .await
66    }
67}