Skip to content

Commit

Permalink
feedback: rework rent check and test tx dedupe
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Oct 26, 2023
1 parent cd22c3e commit 01f924d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
28 changes: 16 additions & 12 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3876,19 +3876,23 @@ where
&self,
) -> TokenResult<u64> {
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::<ExtensionType>())
.saturating_add(size_of::<Length>())
.saturating_add(size_of::<V>());
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::<V>().is_ok() {
Ok(0)
} else {
let new_account_len = account_data_len
.saturating_add(size_of::<ExtensionType>())
.saturating_add(size_of::<Length>())
.saturating_add(size_of::<V>());
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
Expand Down
2 changes: 1 addition & 1 deletion token/program-2022-test/tests/program_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions token/program-2022-test/tests/token_group_initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
)
)))
Expand Down

0 comments on commit 01f924d

Please sign in to comment.