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 // Only set fee_payer if payer differs from authority
50 let fee_payer = if payer.pubkey() != authority.pubkey() {
51 Some(payer.pubkey())
52 } else {
53 None
54 };
55
56 let ix = TransferInstruction {
57 source: self.source,
58 destination: self.destination,
59 amount: self.amount,
60 authority: authority.pubkey(),
61 max_top_up: None,
62 fee_payer,
63 }
64 .instruction()
65 .map_err(|e| RpcError::CustomError(format!("Failed to create instruction: {}", e)))?;
66
67 let mut signers = vec![payer];
68 if authority.pubkey() != payer.pubkey() {
69 signers.push(authority);
70 }
71
72 rpc.create_and_send_transaction(&[ix], &payer.pubkey(), &signers)
73 .await
74 }
75}