Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: close publisher caps #535

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does close = write_authority mean that it will give the lamports to write_authority?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added test

pub publisher_caps: AccountLoader<'info, PublisherCaps>,
}


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