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 20, 2023
1 parent 7c9e306 commit 8d2e4ec
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 30 deletions.
26 changes: 4 additions & 22 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ use {
},
state::{Account, AccountState, Mint, Multisig},
},
spl_token_group_interface::state::TokenGroup,
spl_token_group_interface::state::{TokenGroup, TokenGroupMember},
spl_token_metadata_interface::state::{Field, TokenMetadata},
std::{
fmt, io,
Expand Down Expand Up @@ -3824,26 +3824,6 @@ where
.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>(
Expand All @@ -3854,7 +3834,9 @@ where
group_update_authority: &Pubkey,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let additional_lamports = self.get_additional_rent_for_new_member().await?;
let additional_lamports = self
.get_additional_rent_for_fixed_len_extension::<TokenGroupMember>()
.await?;
let mut instructions = vec![];
if additional_lamports > 0 {
instructions.push(system_instruction::transfer(
Expand Down
11 changes: 3 additions & 8 deletions token/program-2022-test/tests/token_group_initialize_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,24 +246,19 @@ async fn success_initialize_member() {
// fail double-init
let error = member1_token_context
.token
.token_group_initialize_member_with_rent_transfer(
&payer.pubkey(),
.token_group_initialize_member(
&member1_token_context.mint_authority.pubkey(),
&group_mint_keypair.pubkey(),
&group_authority.pubkey(),
&[
&payer,
&member1_token_context.mint_authority,
&group_authority,
],
&[&member1_token_context.mint_authority, &group_authority],
)
.await
.unwrap_err();
assert_eq!(
error,
TokenClientError::Client(Box::new(TransportError::TransactionError(
TransactionError::InstructionError(
1,
0,
InstructionError::Custom(TokenError::ExtensionAlreadyInitialized as u32)
)
)))
Expand Down

0 comments on commit 8d2e4ec

Please sign in to comment.