From 7e65fd6597af734849f3ed0c7eedf3978470b525 Mon Sep 17 00:00:00 2001 From: guibescos <59208140+guibescos@users.noreply.github.com> Date: Thu, 1 Feb 2024 15:51:24 +0000 Subject: [PATCH] [solana] reclaim rent (#1266) * Checkpoint * Checkpoint * Cleanup * Checkpoint, debug * Go * Checkpoint * Fix * Add new error and test * Cleanup * Add another test * Keep adding errors * Another test * Add comment * More * Do it * Again * Nice * Ship it --- .../programs/pyth-solana-receiver/src/lib.rs | 12 ++++++ .../programs/pyth-solana-receiver/src/sdk.rs | 22 +++++++++++ .../tests/test_post_updates.rs | 37 ++++++++++++++++++- 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/target_chains/solana/programs/pyth-solana-receiver/src/lib.rs b/target_chains/solana/programs/pyth-solana-receiver/src/lib.rs index 5d711ba5..a2afbba7 100644 --- a/target_chains/solana/programs/pyth-solana-receiver/src/lib.rs +++ b/target_chains/solana/programs/pyth-solana-receiver/src/lib.rs @@ -217,6 +217,10 @@ pub mod pyth_solana_receiver { Ok(()) } + + pub fn reclaim_rent(_ctx: Context) -> Result<()> { + Ok(()) + } } pub const CONFIG_SEED: &str = "config"; @@ -295,6 +299,14 @@ pub struct PostUpdatesAtomic<'info> { pub system_program: Program<'info, System>, } +#[derive(Accounts)] +pub struct ReclaimRent<'info> { + #[account(mut)] + pub payer: Signer<'info>, + #[account(mut, close = payer, constraint = price_update_account.write_authority == payer.key() @ ReceiverError::WrongWriteAuthority)] + pub price_update_account: Account<'info, PriceUpdateV1>, +} + #[derive(Debug, AnchorSerialize, AnchorDeserialize, Clone)] pub struct PostUpdatesAtomicParams { pub vaa: Vec, diff --git a/target_chains/solana/programs/pyth-solana-receiver/src/sdk.rs b/target_chains/solana/programs/pyth-solana-receiver/src/sdk.rs index 58228d7b..69bc94bc 100644 --- a/target_chains/solana/programs/pyth-solana-receiver/src/sdk.rs +++ b/target_chains/solana/programs/pyth-solana-receiver/src/sdk.rs @@ -88,6 +88,16 @@ impl accounts::AcceptGovernanceAuthorityTransfer { } } +impl accounts::ReclaimRent { + pub fn populate(payer: Pubkey, price_update_account: Pubkey) -> Self { + let _config = get_config_address(); + accounts::ReclaimRent { + payer, + price_update_account, + } + } +} + impl instruction::Initialize { pub fn populate(payer: &Pubkey, initial_config: Config) -> Instruction { Instruction { @@ -229,6 +239,18 @@ impl instruction::AcceptGovernanceAuthorityTransfer { } } +impl instruction::ReclaimRent { + pub fn populate(payer: Pubkey, price_update_account: Pubkey) -> Instruction { + let governance_accounts = + accounts::ReclaimRent::populate(payer, price_update_account).to_account_metas(None); + Instruction { + program_id: ID, + accounts: governance_accounts, + data: instruction::ReclaimRent {}.data(), + } + } +} + pub fn get_treasury_address() -> Pubkey { Pubkey::find_program_address(&[TREASURY_SEED.as_ref()], &ID).0 diff --git a/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_updates.rs b/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_updates.rs index 992fc8bd..3f767d15 100644 --- a/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_updates.rs +++ b/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_updates.rs @@ -10,7 +10,10 @@ use { program_simulator::into_transaction_error, pyth_solana_receiver::{ error::ReceiverError, - instruction::PostUpdates, + instruction::{ + PostUpdates, + ReclaimRent, + }, sdk::deserialize_accumulator_update_data, state::price_update::{ PriceUpdateV1, @@ -120,6 +123,38 @@ async fn test_post_updates() { Message::PriceFeedMessage(price_update_account.price_message), feed_2 ); + + // This poster doesn't have the write authority + let poster_2 = program_simulator.get_funded_keypair().await.unwrap(); + assert_eq!( + program_simulator + .process_ix_with_default_compute_limit( + ReclaimRent::populate(poster_2.pubkey(), price_update_keypair.pubkey()), + &vec![&poster_2], + None, + ) + .await + .unwrap_err() + .unwrap(), + into_transaction_error(ReceiverError::WrongWriteAuthority) + ); + + program_simulator + .process_ix_with_default_compute_limit( + ReclaimRent::populate(poster.pubkey(), price_update_keypair.pubkey()), + &vec![&poster], + None, + ) + .await + .unwrap(); + + assert_eq!( + program_simulator + .get_balance(price_update_keypair.pubkey()) + .await + .unwrap(), + 0 + ); } #[tokio::test]