Skip to content

Commit

Permalink
feat(programs): Add close function for program and upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
bguillaumat committed Dec 12, 2022
1 parent f8f5487 commit 0674ed8
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 15 deletions.
50 changes: 49 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions programs/program-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ default = []

[dependencies]
anchor-lang = "0.25.0"
spl-token = {version = "3.1.0", features = ["no-entrypoint"]}
squads-mpl = { path = "../squads-mpl", features = ["cpi"] }
206 changes: 192 additions & 14 deletions programs/program-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ declare_id!("SMPLKTQhrgo22hFCVq2VGX1KAktTWjeizkhrdB1eauK");

#[program]
pub mod program_manager {
use std::io::{Cursor, Write};
use std::ops::DerefMut;
use anchor_lang::__private::CLOSED_ACCOUNT_DISCRIMINATOR;
use anchor_lang::solana_program::{bpf_loader_upgradeable::upgrade};

use super::*;
Expand Down Expand Up @@ -43,8 +46,8 @@ pub mod program_manager {
// generate the upgrade instruction
let ix = upgrade(
&managed_program.program_address,
&buffer,
&authority,
&buffer,
&authority,
&spill
);
let uix = UpgradeInstruction::from(ix);
Expand All @@ -62,15 +65,47 @@ pub mod program_manager {
Ok(())
}

// TO DO
// pub fn close_managed_program_account() -> Result<()>{
// Ok(())
// }
pub fn close_managed_program_account(ctx: Context<RemoveManagedProgram>) -> Result<()>{
let dest_starting_lamports = ctx.accounts.authority.lamports();

// TO DO
// pub fn close_upgrade_account()->Result<()>{
// Ok(())
// }
let account = ctx.accounts.managed_program.to_account_info();
**ctx.accounts.authority.lamports.borrow_mut() = dest_starting_lamports
.checked_add(account.lamports())
.unwrap();
**account.lamports.borrow_mut() = 0;

let mut data = account.try_borrow_mut_data()?;
for byte in data.deref_mut().iter_mut() {
*byte = 0;
}

let dst: &mut [u8] = &mut data;
let mut cursor = Cursor::new(dst);
cursor.write_all(&CLOSED_ACCOUNT_DISCRIMINATOR).unwrap();

Ok(())
}

pub fn close_upgrade_account(ctx: Context<RemoveUpgrade>)->Result<()>{
let dest_starting_lamports = ctx.accounts.authority.lamports();

let account = ctx.accounts.program_upgrade.to_account_info();
**ctx.accounts.authority.lamports.borrow_mut() = dest_starting_lamports
.checked_add(account.lamports())
.unwrap();
**account.lamports.borrow_mut() = 0;

let mut data = account.try_borrow_mut_data()?;
for byte in data.deref_mut().iter_mut() {
*byte = 0;
}

let dst: &mut [u8] = &mut data;
let mut cursor = Cursor::new(dst);
cursor.write_all(&CLOSED_ACCOUNT_DISCRIMINATOR).unwrap();

Ok(())
}

// a function to run after an upgrade instruction via squads-mpl that will update some data
pub fn set_as_executed(ctx: Context<UpdateUpgrade>) -> Result<()>{
Expand Down Expand Up @@ -176,7 +211,7 @@ pub struct CreateProgramUpgrade<'info> {
constraint = matches!(multisig.is_member(creator.key()), Some(..)) || multisig.allow_external_execute @MsError::KeyNotInMultisig,
)]
pub multisig: Account<'info, Ms>,

#[account(
seeds = [
b"squad",
Expand Down Expand Up @@ -211,7 +246,7 @@ pub struct CreateProgramUpgrade<'info> {
], bump
)]
pub program_upgrade: Account<'info, ProgramUpgrade>,

#[account(mut)]
pub creator: Signer<'info>,
pub system_program: Program<'info, System>
Expand All @@ -230,7 +265,7 @@ pub struct UpdateUpgrade<'info> {
seeds::program = squads_mpl::ID,
)]
pub multisig: Account<'info, Ms>,

// derive the program manager from the multisig
#[account(
seeds = [
Expand Down Expand Up @@ -267,7 +302,7 @@ pub struct UpdateUpgrade<'info> {
constraint = !program_upgrade.executed @MsError::InvalidInstructionAccount,
)]
pub program_upgrade: Account<'info, ProgramUpgrade>,

// check that the transaction is derived from the multisig
#[account(
owner = squads_mpl::ID,
Expand Down Expand Up @@ -306,4 +341,147 @@ pub struct UpdateUpgrade<'info> {
seeds::program = squads_mpl::ID,
)]
pub authority: Signer<'info>,
}

#[derive(Accounts)]
pub struct RemoveManagedProgram<'info> {
#[account(
seeds = [
b"squad",
multisig.create_key.as_ref(),
b"multisig"
],
bump = multisig.bump,
seeds::program = squads_mpl::ID,
)]
pub multisig: Account<'info, Ms>,

#[account(
mut,
seeds = [
b"squad",
multisig.key().as_ref(),
b"pmanage"
],
bump = program_manager.bump
)]
pub program_manager: Account<'info,ProgramManager>,

// derive the managed program from the program manager
#[account(
mut,
seeds = [
b"squad",
program_manager.key().as_ref(),
&managed_program.managed_program_index.to_le_bytes(),
b"program"
],
bump = managed_program.bump,
)]
pub managed_program: Account<'info, ManagedProgram>,

// check that the transaction is derived from the multisig
#[account(
owner = squads_mpl::ID,
seeds = [
b"squad",
multisig.key().as_ref(),
&transaction.transaction_index.to_le_bytes(),
b"transaction"
],
bump = transaction.bump,
seeds::program = squads_mpl::ID,
)]
pub transaction: Account<'info, MsTransaction>,

#[account(
mut,
seeds = [
b"squad",
multisig.key().as_ref(),
&transaction.authority_index.to_le_bytes(),
b"authority"
],
bump = transaction.authority_bump,
seeds::program = squads_mpl::ID,
)]
pub authority: Signer<'info>,
}

#[derive(Accounts)]
pub struct RemoveUpgrade<'info> {
// multisig account needs to come from squads-mpl
#[account(
seeds = [
b"squad",
multisig.create_key.as_ref(),
b"multisig"
],
bump = multisig.bump,
seeds::program = squads_mpl::ID,
)]
pub multisig: Account<'info, Ms>,

// derive the program manager from the multisig
#[account(
seeds = [
b"squad",
multisig.key().as_ref(),
b"pmanage"
],
bump = program_manager.bump
)]
pub program_manager: Account<'info, ProgramManager>,

// derive the managed program from the program manager
#[account(
mut,
seeds = [
b"squad",
program_manager.key().as_ref(),
&managed_program.managed_program_index.to_le_bytes(),
b"program"
],
bump = managed_program.bump,
)]
pub managed_program: Account<'info, ManagedProgram>,

// derive the upgrade from the managed program
#[account(
mut,
seeds = [
b"squad",
managed_program.key().as_ref(),
&program_upgrade.upgrade_index.to_le_bytes(),
b"pupgrade"
], bump = program_upgrade.bump,
)]
pub program_upgrade: Account<'info, ProgramUpgrade>,

// check that the transaction is derived from the multisig
#[account(
owner = squads_mpl::ID,
seeds = [
b"squad",
multisig.key().as_ref(),
&transaction.transaction_index.to_le_bytes(),
b"transaction"
],
bump = transaction.bump,
seeds::program = squads_mpl::ID,
)]
pub transaction: Account<'info, MsTransaction>,

#[account(
mut,
seeds = [
b"squad",
multisig.key().as_ref(),
&transaction.authority_index.to_le_bytes(),
b"authority"
],
bump = transaction.authority_bump,
seeds::program = squads_mpl::ID,
)]
pub authority: Signer<'info>,
}

0 comments on commit 0674ed8

Please sign in to comment.