Skip to content

Commit

Permalink
token 2022: add InitializeMember instruction from SPL Token Group i…
Browse files Browse the repository at this point in the history
…nterface
  • Loading branch information
buffalojoec committed Nov 14, 2023
1 parent f303142 commit 12dfa57
Show file tree
Hide file tree
Showing 5 changed files with 951 additions and 7 deletions.
72 changes: 72 additions & 0 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3811,4 +3811,76 @@ where
)
.await
}

/// Initialize a token-group member on a mint
pub async fn token_group_initialize_member<S: Signers>(
&self,
mint_authority: &Pubkey,
group_mint: &Pubkey,
group_update_authority: &Pubkey,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
self.process_ixs(
&[spl_token_group_interface::instruction::initialize_member(
&self.program_id,
&self.pubkey,
&self.pubkey,
mint_authority,
group_mint,
group_update_authority,
)],
signing_keypairs,
)
.await
}

async fn get_additional_rent_for_new_member(&self) -> TokenResult<u64> {
let account = self.get_account(self.pubkey).await?;
let account_lamports = account.lamports;
let mint_state = self.unpack_mint_info(account)?;
let new_account_len = mint_state
.try_get_account_len()?
.checked_add(ExtensionType::try_calculate_account_len::<Mint>(&[
ExtensionType::TokenGroupMember,
])?)
.ok_or(TokenError::Program(
spl_token_2022::error::TokenError::Overflow.into(),
))?;
let new_rent_exempt_minimum = self
.client
.get_minimum_balance_for_rent_exemption(new_account_len)
.await
.map_err(TokenError::Client)?;
Ok(new_rent_exempt_minimum.saturating_sub(account_lamports))
}

/// Initialize a token-group member on a mint
#[allow(clippy::too_many_arguments)]
pub async fn token_group_initialize_member_with_rent_transfer<S: Signers>(
&self,
payer: &Pubkey,
mint_authority: &Pubkey,
group_mint: &Pubkey,
group_update_authority: &Pubkey,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let additional_lamports = self.get_additional_rent_for_new_member().await?;
let mut instructions = vec![];
if additional_lamports > 0 {
instructions.push(system_instruction::transfer(
payer,
&self.pubkey,
additional_lamports,
));
}
instructions.push(spl_token_group_interface::instruction::initialize_member(
&self.program_id,
&self.pubkey,
&self.pubkey,
mint_authority,
group_mint,
group_update_authority,
));
self.process_ixs(&instructions, signing_keypairs).await
}
}
Loading

0 comments on commit 12dfa57

Please sign in to comment.