Skip to content

Commit

Permalink
feat(lazer): add treasury and fees to solana contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Riateche committed Nov 27, 2024
1 parent a89055b commit 166386b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
mod signature;

pub mod storage {
use anchor_lang::prelude::{pubkey, Pubkey};

pub const ID: Pubkey = pubkey!("3rdJbqfnagQ4yx9HXJViD4zc4xpiSqmFsKpPuSCQVyQL");

#[test]
fn test_storage_id() {
use {crate::STORAGE_SEED, anchor_lang::prelude::Pubkey};

assert_eq!(
Pubkey::find_program_address(&[STORAGE_SEED], &super::ID).0,
ID
);
}
}

use {
anchor_lang::{prelude::*, solana_program::pubkey::PUBKEY_BYTES},
crate::signature::VerifiedMessage,
anchor_lang::solana_program::sysvar,
anchor_lang::{prelude::*, solana_program::pubkey::PUBKEY_BYTES, system_program},
std::mem::size_of,
};

Expand All @@ -28,6 +14,21 @@ pub use {

declare_id!("pytd2yyk641x7ak7mkaasSJVXh6YYZnC7wTmtgAyxPt");

pub const STORAGE_ID: Pubkey = pubkey!("3rdJbqfnagQ4yx9HXJViD4zc4xpiSqmFsKpPuSCQVyQL");
pub const TREASURY_ID: Pubkey = pubkey!("EN4aB3soE5iuCG2fGj2r5fksh4kLRVPV8g7N86vXm8WM");

#[test]
fn test_ids() {
assert_eq!(
Pubkey::find_program_address(&[STORAGE_SEED], &ID).0,
STORAGE_ID
);
assert_eq!(
Pubkey::find_program_address(&[TREASURY_SEED], &ID).0,
TREASURY_ID
);
}

pub const MAX_NUM_TRUSTED_SIGNERS: usize = 2;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, AnchorSerialize, AnchorDeserialize)]
Expand All @@ -44,12 +45,14 @@ impl TrustedSignerInfo {
pub struct Storage {
pub top_authority: Pubkey,
pub num_trusted_signers: u8,
pub single_update_fee_in_lamports: u64,
pub trusted_signers: [TrustedSignerInfo; MAX_NUM_TRUSTED_SIGNERS],
}

impl Storage {
const SERIALIZED_LEN: usize = PUBKEY_BYTES
+ size_of::<u8>()
+ size_of::<u64>()
+ TrustedSignerInfo::SERIALIZED_LEN * MAX_NUM_TRUSTED_SIGNERS;

pub fn initialized_trusted_signers(&self) -> &[TrustedSignerInfo] {
Expand All @@ -58,15 +61,15 @@ impl Storage {
}

pub const STORAGE_SEED: &[u8] = b"storage";
pub const TREASURY_SEED: &[u8] = b"treasury";

#[program]
pub mod pyth_lazer_solana_contract {
use signature::VerifiedMessage;

use super::*;

pub fn initialize(ctx: Context<Initialize>, top_authority: Pubkey) -> Result<()> {
ctx.accounts.storage.top_authority = top_authority;
ctx.accounts.storage.single_update_fee_in_lamports = 1;
Ok(())
}

Expand Down Expand Up @@ -128,6 +131,17 @@ pub mod pyth_lazer_solana_contract {
signature_index: u8,
message_offset: u16,
) -> Result<VerifiedMessage> {
system_program::transfer(
CpiContext::new(
ctx.accounts.system_program.to_account_info(),
system_program::Transfer {
from: ctx.accounts.payer.to_account_info(),
to: ctx.accounts.treasury.to_account_info(),
},
),
ctx.accounts.storage.single_update_fee_in_lamports,
)?;

signature::verify_message(
&ctx.accounts.storage,
&ctx.accounts.sysvar,
Expand Down Expand Up @@ -155,6 +169,18 @@ pub struct Initialize<'info> {
bump,
)]
pub storage: Account<'info, Storage>,
#[account(
init,
payer = payer,
space = 0,
owner = system_program::ID,
seeds = [TREASURY_SEED],
bump,
)]
/// CHECK: this is a system program account but using anchor's `SystemAccount`
/// results in invalid output from the Accounts proc macro. No extra checks
/// are necessary because all necessary constraints are specified in the attribute.
pub treasury: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}

Expand All @@ -172,10 +198,31 @@ pub struct Update<'info> {

#[derive(Accounts)]
pub struct VerifyMessage<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(
seeds = [STORAGE_SEED],
bump,
)]
pub storage: Account<'info, Storage>,
pub sysvar: AccountInfo<'info>,
#[account(
mut,
owner = system_program::ID,
seeds = [TREASURY_SEED],
bump,
)]
/// CHECK: this is a system program account but using anchor's `SystemAccount`
/// results in invalid output from the Accounts proc macro. No extra checks
/// are necessary because all necessary constraints are specified in the attribute.
pub treasury: AccountInfo<'info>,
pub system_program: Program<'info, System>,
pub sysvar: Program<'info, Sysvar>,
}

pub struct Sysvar;

impl Id for Sysvar {
fn id() -> Pubkey {
sysvar::ID
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ async fn test1() {
.data(),
vec![
AccountMeta::new(payer.pubkey(), true),
AccountMeta::new(pyth_lazer_solana_contract::storage::ID, false),
AccountMeta::new(pyth_lazer_solana_contract::STORAGE_ID, false),
AccountMeta::new(pyth_lazer_solana_contract::TREASURY_ID, false),
AccountMeta::new_readonly(system_program::ID, false),
],
)],
Expand Down Expand Up @@ -68,7 +69,7 @@ async fn test1() {
.data(),
vec![
AccountMeta::new(payer.pubkey(), true),
AccountMeta::new(pyth_lazer_solana_contract::storage::ID, false),
AccountMeta::new(pyth_lazer_solana_contract::STORAGE_ID, false),
],
)],
Some(&payer.pubkey()),
Expand All @@ -90,7 +91,12 @@ async fn test1() {
message_offset,
));

println!("ok1");
let treasury_starting_lamports = banks_client
.get_account(pyth_lazer_solana_contract::TREASURY_ID)
.await
.unwrap()
.unwrap()
.lamports;
let mut transaction_verify = Transaction::new_with_payer(
&[
Instruction::new_with_bytes(
Expand All @@ -108,7 +114,10 @@ async fn test1() {
}
.data(),
vec![
AccountMeta::new_readonly(pyth_lazer_solana_contract::storage::ID, false),
AccountMeta::new(payer.pubkey(), true),
AccountMeta::new_readonly(pyth_lazer_solana_contract::STORAGE_ID, false),
AccountMeta::new(pyth_lazer_solana_contract::TREASURY_ID, false),
AccountMeta::new_readonly(system_program::ID, false),
AccountMeta::new_readonly(sysvar::instructions::ID, false),
],
),
Expand All @@ -120,4 +129,13 @@ async fn test1() {
.process_transaction(transaction_verify)
.await
.unwrap();
assert_eq!(
banks_client
.get_account(pyth_lazer_solana_contract::TREASURY_ID)
.await
.unwrap()
.unwrap()
.lamports,
treasury_starting_lamports + 1
);
}

0 comments on commit 166386b

Please sign in to comment.