Browse Source

feat: ata create idempotent and drop rent (#2153)

* feat: ata create idempotent and drop rent

* add the token program id

* cargo fmt

Co-authored-by: Arrowana <8245419+Arrowana@users.noreply.github.com>
Co-authored-by: henrye <henry@notanemail>
Pierre 2 years ago
parent
commit
98f26ce8cf
1 changed files with 28 additions and 1 deletions
  1. 28 1
      spl/src/associated_token.rs

+ 28 - 1
spl/src/associated_token.rs

@@ -2,6 +2,7 @@ use anchor_lang::solana_program::account_info::AccountInfo;
 use anchor_lang::solana_program::pubkey::Pubkey;
 use anchor_lang::Result;
 use anchor_lang::{context::CpiContext, Accounts};
+use spl_token;
 
 pub use spl_associated_token_account::{get_associated_token_address, ID};
 
@@ -10,7 +11,31 @@ pub fn create<'info>(ctx: CpiContext<'_, '_, '_, 'info, Create<'info>>) -> Resul
         ctx.accounts.payer.key,
         ctx.accounts.authority.key,
         ctx.accounts.mint.key,
-        ctx.accounts.token_program.key,
+        &spl_token::ID,
+    );
+    solana_program::program::invoke_signed(
+        &ix,
+        &[
+            ctx.accounts.payer,
+            ctx.accounts.associated_token,
+            ctx.accounts.authority,
+            ctx.accounts.mint,
+            ctx.accounts.system_program,
+            ctx.accounts.token_program,
+        ],
+        ctx.signer_seeds,
+    )
+    .map_err(Into::into)
+}
+
+pub fn create_idempotent<'info>(
+    ctx: CpiContext<'_, '_, '_, 'info, CreateIdemptotent<'info>>,
+) -> Result<()> {
+    let ix = spl_associated_token_account::instruction::create_associated_token_account_idempotent(
+        ctx.accounts.payer.key,
+        ctx.accounts.authority.key,
+        ctx.accounts.mint.key,
+        &spl_token::ID,
     );
     solana_program::program::invoke_signed(
         &ix,
@@ -37,6 +62,8 @@ pub struct Create<'info> {
     pub token_program: AccountInfo<'info>,
 }
 
+type CreateIdemptotent<'info> = Create<'info>;
+
 #[derive(Clone)]
 pub struct AssociatedToken;