Skip to content

Commit

Permalink
feat(rent-reclamation): add config_transaction_accounts_close
Browse files Browse the repository at this point in the history
  • Loading branch information
vovacodes committed Nov 15, 2023
1 parent 72e3c3b commit 4bdff43
Show file tree
Hide file tree
Showing 16 changed files with 1,397 additions and 2 deletions.
10 changes: 10 additions & 0 deletions programs/squads_multisig_program/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ pub enum MultisigError {
TimeLockExceedsMaxAllowed,
#[msg("Account is not owned by Multisig program")]
IllegalAccountOwner,
#[msg("Rent reclamation is disabled for this multisig")]
RentReclamationDisabled,
#[msg("Invalid rent collector address")]
InvalidRentCollector,
#[msg("Proposal is for another multisig")]
ProposalForAnotherMultisig,
#[msg("Transaction is for another multisig")]
TransactionForAnotherMultisig,
#[msg("Transaction doesn't match proposal")]
TransactionNotMatchingProposal,
}
2 changes: 2 additions & 0 deletions programs/squads_multisig_program/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub use proposal_activate::*;
pub use proposal_create::*;
pub use proposal_vote::*;
pub use spending_limit_use::*;
pub use transaction_accounts_close::*;
pub use vault_transaction_create::*;
pub use vault_transaction_execute::*;

Expand All @@ -27,5 +28,6 @@ mod proposal_activate;
mod proposal_create;
mod proposal_vote;
mod spending_limit_use;
mod transaction_accounts_close;
mod vault_transaction_create;
mod vault_transaction_execute;
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use anchor_lang::prelude::*;

use crate::errors::*;
use crate::state::*;

#[derive(Accounts)]
pub struct ConfigTransactionAccountsClose<'info> {
#[account(
seeds = [SEED_PREFIX, SEED_MULTISIG, multisig.create_key.as_ref()],
bump = multisig.bump,
)]
pub multisig: Account<'info, Multisig>,

#[account(mut, close = rent_collector)]
pub proposal: Account<'info, Proposal>,

/// ConfigTransaction corresponding to the `proposal`.
#[account(mut, close = rent_collector)]
pub transaction: Account<'info, ConfigTransaction>,

/// The rent collector.
/// CHECK: We do the checks in validate().
#[account(mut)]
pub rent_collector: AccountInfo<'info>,

pub system_program: Program<'info, System>,
}

impl ConfigTransactionAccountsClose<'_> {
fn validate(&self) -> Result<()> {
let Self {
multisig,
proposal,
transaction,
rent_collector,
..
} = self;

//region multisig

// Has to have `rent_collector` set.
let multisig_rent_collector_key = multisig
.rent_collector
.ok_or(MultisigError::RentReclamationDisabled)?
.key();
//endregion

//region rent_collector

// Has to match the `multisig.rent_collector`.
require_keys_eq!(
multisig_rent_collector_key,
rent_collector.key(),
MultisigError::InvalidRentCollector
);
//endregion

//region proposal

// Has to be for the `multisig`.
require_keys_eq!(
proposal.multisig,
multisig.key(),
MultisigError::ProposalForAnotherMultisig
);

// Has to be either stale or in a terminal state.
require!(
proposal.transaction_index <= multisig.stale_transaction_index
|| proposal.status.is_terminal(),
MultisigError::InvalidProposalStatus
);
//endregion

//region transaction

// Has to be for the `multisig`.
require_keys_eq!(
transaction.multisig,
multisig.key(),
MultisigError::TransactionForAnotherMultisig
);

// Has to be for the `proposal`.
require_eq!(
transaction.index,
proposal.transaction_index,
MultisigError::TransactionForAnotherMultisig
);
//endregion

Ok(())
}

/// Close accounts for stale config transactions or config transactions in terminal states: `Executed`, `Rejected`, or `Cancelled`.
#[access_control(_ctx.accounts.validate())]
pub fn config_transaction_accounts_close(_ctx: Context<Self>) -> Result<()> {
// Anchor will close the accounts for us.
Ok(())
}
}
6 changes: 6 additions & 0 deletions programs/squads_multisig_program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,10 @@ pub mod squads_multisig_program {
) -> Result<()> {
SpendingLimitUse::spending_limit_use(ctx, args)
}

pub fn config_transaction_accounts_close(
ctx: Context<ConfigTransactionAccountsClose>,
) -> Result<()> {
ConfigTransactionAccountsClose::config_transaction_accounts_close(ctx)
}
}
9 changes: 9 additions & 0 deletions programs/squads_multisig_program/src/state/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,12 @@ pub enum ProposalStatus {
/// Proposal has been cancelled.
Cancelled { timestamp: i64 },
}

impl ProposalStatus {
pub fn is_terminal(&self) -> bool {
match self {
Self::Rejected { .. } | Self::Executed { .. } | Self::Cancelled { .. } => true,
_ => false,
}
}
}
62 changes: 62 additions & 0 deletions sdk/multisig/idl/squads_multisig_program.json
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,43 @@
}
}
]
},
{
"name": "configTransactionAccountsClose",
"accounts": [
{
"name": "multisig",
"isMut": false,
"isSigner": false
},
{
"name": "proposal",
"isMut": true,
"isSigner": false
},
{
"name": "transaction",
"isMut": true,
"isSigner": false,
"docs": [
"ConfigTransaction corresponding to the `proposal`."
]
},
{
"name": "rentCollector",
"isMut": true,
"isSigner": false,
"docs": [
"The rent collector."
]
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
}
],
"args": []
}
],
"accounts": [
Expand Down Expand Up @@ -2479,6 +2516,31 @@
"code": 6031,
"name": "IllegalAccountOwner",
"msg": "Account is not owned by Multisig program"
},
{
"code": 6032,
"name": "RentReclamationDisabled",
"msg": "Rent reclamation is disabled for this multisig"
},
{
"code": 6033,
"name": "InvalidRentCollector",
"msg": "Invalid rent collector address"
},
{
"code": 6034,
"name": "ProposalForAnotherMultisig",
"msg": "Proposal is for another multisig"
},
{
"code": 6035,
"name": "TransactionForAnotherMultisig",
"msg": "Transaction is for another multisig"
},
{
"code": 6036,
"name": "TransactionNotMatchingProposal",
"msg": "Transaction doesn't match proposal"
}
],
"metadata": {
Expand Down
124 changes: 124 additions & 0 deletions sdk/multisig/src/generated/errors/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4bdff43

Please sign in to comment.