Skip to content

Commit

Permalink
feat: close publisher caps (#535)
Browse files Browse the repository at this point in the history
* close publisher caps

* add some minimal tests
  • Loading branch information
guibescos authored Sep 13, 2024
1 parent 54ef7b6 commit e606f80
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
29 changes: 29 additions & 0 deletions staking/integration-tests/src/publisher_caps/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,32 @@ pub fn verify_publisher_caps(
);
svm.send_transaction(transaction)
}

pub fn close_publisher_caps(
svm: &mut LiteSVM,
signer: &Keypair,
payer: &Keypair,
publisher_caps: Pubkey,
) -> TransactionResult {
let accounts = publisher_caps::accounts::ClosePublisherCaps {
write_authority: signer.pubkey(),
publisher_caps,
};

let instruction_data = publisher_caps::instruction::ClosePublisherCaps {};

let instruction = Instruction {
program_id: publisher_caps::ID,
accounts: accounts.to_account_metas(None),
data: instruction_data.data(),
};

let transaction = Transaction::new_signed_with_payer(
&[instruction],
Some(&payer.pubkey()),
&[payer, signer],
svm.latest_blockhash(),
);

svm.send_transaction(transaction)
}
55 changes: 55 additions & 0 deletions staking/integration-tests/tests/publisher_caps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use {
integration_tests::{
assert_anchor_program_error,
publisher_caps::{
helper_functions::post_dummy_publisher_caps,
instructions::close_publisher_caps,
},
setup::{
setup,
SetupProps,
SetupResult,
},
},
publisher_caps::PublisherCapsError,
solana_sdk::{
signature::Keypair,
signer::Signer,
},
};


#[test]
fn test_close_publisher_caps() {
let SetupResult {
mut svm,
payer,
publisher_keypair,
..
} = setup(SetupProps {
init_config: true,
init_target: true,
init_mint: true,
init_pool_data: true,
init_publishers: true,
reward_amount_override: None,
});

let publisher_caps =
post_dummy_publisher_caps(&mut svm, &payer, publisher_keypair.pubkey(), 10);

assert!(svm.get_account(&publisher_caps).unwrap().lamports > 0);
let payer_balance_before = svm.get_account(&payer.pubkey()).unwrap().lamports;

assert_anchor_program_error!(
close_publisher_caps(&mut svm, &Keypair::new(), &payer, publisher_caps),
PublisherCapsError::WrongWriteAuthority,
0
);

close_publisher_caps(&mut svm, &payer, &payer, publisher_caps).unwrap();
assert_eq!(svm.get_account(&publisher_caps).unwrap().data.len(), 0);
assert_eq!(svm.get_account(&publisher_caps).unwrap().lamports, 0);

assert!(svm.get_account(&payer.pubkey()).unwrap().lamports > payer_balance_before);
}
12 changes: 12 additions & 0 deletions staking/programs/publisher-caps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ pub mod publisher_caps {

Ok(())
}

pub fn close_publisher_caps(_ctx: Context<ClosePublisherCaps>) -> Result<()> {
Ok(())
}
}

#[repr(C)]
Expand Down Expand Up @@ -222,6 +226,14 @@ pub struct VerifyPublisherCaps<'info> {
pub encoded_vaa: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct ClosePublisherCaps<'info> {
pub write_authority: Signer<'info>,
#[account(mut, close = write_authority, has_one = write_authority @ PublisherCapsError::WrongWriteAuthority)]
pub publisher_caps: AccountLoader<'info, PublisherCaps>,
}


#[error_code]
pub enum PublisherCapsError {
InvalidWormholeMessage,
Expand Down

0 comments on commit e606f80

Please sign in to comment.