light_token_client/actions/create_ata.rs
1//! Create Associated Token Account actions for Light Token.
2//!
3//! These actions provide clean interfaces for creating Light Token ATAs.
4
5use light_client::rpc::{Rpc, RpcError};
6use light_token::instruction::{get_associated_token_address, CreateAssociatedTokenAccount};
7use solana_keypair::Keypair;
8use solana_pubkey::Pubkey;
9use solana_signature::Signature;
10use solana_signer::Signer;
11
12/// Parameters for creating an associated token account for a Light Token mint.
13///
14/// # Example
15/// ```ignore
16/// // Non-idempotent (fails if ATA exists)
17/// CreateAta {
18/// mint,
19/// owner,
20/// idempotent: false,
21/// }.execute(&mut rpc, &payer).await?;
22///
23/// // Idempotent (no-op if ATA exists)
24/// CreateAta {
25/// mint,
26/// owner,
27/// idempotent: true,
28/// }.execute(&mut rpc, &payer).await?;
29/// ```
30#[derive(Default, Clone, Debug)]
31pub struct CreateAta {
32 /// The mint public key.
33 pub mint: Pubkey,
34 /// The owner of the ATA.
35 pub owner: Pubkey,
36 /// Whether to use idempotent mode (no-op if ATA exists).
37 pub idempotent: bool,
38}
39
40impl CreateAta {
41 /// Execute the create_ata action via RPC.
42 ///
43 /// # Arguments
44 /// * `rpc` - RPC client
45 /// * `payer` - Transaction fee payer keypair
46 ///
47 /// # Returns
48 /// `Result<(Signature, Pubkey), RpcError>` - The transaction signature and ATA public key
49 pub async fn execute<R: Rpc>(
50 self,
51 rpc: &mut R,
52 payer: &Keypair,
53 ) -> Result<(Signature, Pubkey), RpcError> {
54 let mut instruction_builder =
55 CreateAssociatedTokenAccount::new(payer.pubkey(), self.owner, self.mint);
56
57 if self.idempotent {
58 instruction_builder = instruction_builder.idempotent();
59 }
60
61 let ix = instruction_builder
62 .instruction()
63 .map_err(|e| RpcError::CustomError(format!("Failed to create instruction: {}", e)))?;
64
65 let signature = rpc
66 .create_and_send_transaction(&[ix], &payer.pubkey(), &[payer])
67 .await?;
68
69 Ok((
70 signature,
71 get_associated_token_address(&self.owner, &self.mint),
72 ))
73 }
74}