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 16, 2023
1 parent 5f75990 commit 9ab3658
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 29 deletions.
26 changes: 5 additions & 21 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use spl_token_group_interface::state::TokenGroupMember;

use {
crate::{
client::{ProgramClient, ProgramClientError, SendTransaction, SimulateTransaction},
Expand Down Expand Up @@ -3834,26 +3836,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 @@ -3864,7 +3846,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 9ab3658

Please sign in to comment.