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

[solana] reclaim rent #1266

Merged
merged 19 commits into from
Feb 1, 2024
12 changes: 12 additions & 0 deletions target_chains/solana/programs/pyth-solana-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ pub mod pyth_solana_receiver {

Ok(())
}

pub fn reclaim_rent(_ctx: Context<ReclaimRent>) -> Result<()> {
Ok(())
}
}

pub const CONFIG_SEED: &str = "config";
Expand Down Expand Up @@ -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<u8>,
Expand Down
22 changes: 22 additions & 0 deletions target_chains/solana/programs/pyth-solana-receiver/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down
Loading