From e7108e1211fa98f306f8e147204b84971994e499 Mon Sep 17 00:00:00 2001 From: Sean Date: Fri, 22 Jul 2022 22:47:11 +0200 Subject: [PATCH] fix(optimize): OS-SQD-SUG-03 --- programs/squads-mpl/src/lib.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/programs/squads-mpl/src/lib.rs b/programs/squads-mpl/src/lib.rs index 39564f3..86d831f 100644 --- a/programs/squads-mpl/src/lib.rs +++ b/programs/squads-mpl/src/lib.rs @@ -61,17 +61,21 @@ pub mod squads_mpl { if spots_left < 1{ // add space for 10 more keys let needed_len = curr_data_size + ( 10 * 32 ); + // reallocate more space + AccountInfo::realloc(&multisig_account_info, needed_len, false)?; + // if more lamports are needed, transfer them to the account let rent_exempt_lamports = ctx.accounts.rent.minimum_balance(needed_len).max(1); let top_up_lamports = rent_exempt_lamports.saturating_sub(ctx.accounts.multisig.to_account_info().lamports()); - AccountInfo::realloc(&multisig_account_info, needed_len, false)?; - invoke( - &transfer(ctx.accounts.member.key, &ctx.accounts.multisig.key(), top_up_lamports), - &[ - ctx.accounts.member.to_account_info().clone(), - multisig_account_info.clone(), - ctx.accounts.system_program.to_account_info().clone(), - ], - )?; + if top_up_lamports > 0 { + invoke( + &transfer(ctx.accounts.member.key, &ctx.accounts.multisig.key(), top_up_lamports), + &[ + ctx.accounts.member.to_account_info().clone(), + multisig_account_info.clone(), + ctx.accounts.system_program.to_account_info().clone(), + ], + )?; + } } ctx.accounts.multisig.reload()?; ctx.accounts.multisig.add_member(new_member)?;