From 01f924dae655b7509fda33458a4fb54732f41a1f Mon Sep 17 00:00:00 2001 From: Joe Date: Wed, 25 Oct 2023 14:22:07 +0200 Subject: [PATCH] feedback: rework rent check and test tx dedupe --- token/client/src/token.rs | 28 +++++++++++-------- token/program-2022-test/tests/program_test.rs | 2 +- .../tests/token_group_initialize.rs | 4 +-- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/token/client/src/token.rs b/token/client/src/token.rs index 327301e5c26..1efa507d247 100644 --- a/token/client/src/token.rs +++ b/token/client/src/token.rs @@ -3876,19 +3876,23 @@ where &self, ) -> TokenResult { let account = self.get_account(self.pubkey).await?; + let account_data_len = account.data.len(); let account_lamports = account.lamports; - let new_account_len = account - .data - .len() - .saturating_add(size_of::()) - .saturating_add(size_of::()) - .saturating_add(size_of::()); - 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)) + let mint_state = self.unpack_mint_info(account)?; + if mint_state.get_extension::().is_ok() { + Ok(0) + } else { + let new_account_len = account_data_len + .saturating_add(size_of::()) + .saturating_add(size_of::()) + .saturating_add(size_of::()); + 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 token-group on a mint diff --git a/token/program-2022-test/tests/program_test.rs b/token/program-2022-test/tests/program_test.rs index 7c301979fba..1400285a0c0 100644 --- a/token/program-2022-test/tests/program_test.rs +++ b/token/program-2022-test/tests/program_test.rs @@ -146,7 +146,7 @@ impl TestContext { let token_unchecked = Token::new_native(Arc::clone(&client), &id(), Arc::new(payer)); self.token_context = Some(TokenContext { decimals: native_mint::DECIMALS, - mint_authority: Keypair::new(), /*bogus*/ + mint_authority: Keypair::new(), /* bogus */ token, token_unchecked, alice: Keypair::new(), diff --git a/token/program-2022-test/tests/token_group_initialize.rs b/token/program-2022-test/tests/token_group_initialize.rs index 54ecf3c5dab..e9d1296c31f 100644 --- a/token/program-2022-test/tests/token_group_initialize.rs +++ b/token/program-2022-test/tests/token_group_initialize.rs @@ -131,7 +131,7 @@ async fn success_initialize() { &payer_pubkey, &token_context.mint_authority.pubkey(), &update_authority, - max_size, + 12, // Change so we get a different transaction &[&token_context.mint_authority], ) .await @@ -140,7 +140,7 @@ async fn success_initialize() { error, TokenClientError::Client(Box::new(TransportError::TransactionError( TransactionError::InstructionError( - 1, + 0, // No additional rent InstructionError::Custom(TokenError::ExtensionAlreadyInitialized as u32) ) )))