Skip to content

Commit bcb5550

Browse files
committed
feat(lazer): add treasury and fees to solana contract
1 parent a89055b commit bcb5550

File tree

2 files changed

+73
-21
lines changed
  • lazer/contracts/solana/programs/pyth-lazer-solana-contract

2 files changed

+73
-21
lines changed

lazer/contracts/solana/programs/pyth-lazer-solana-contract/src/lib.rs

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
mod signature;
22

3-
pub mod storage {
4-
use anchor_lang::prelude::{pubkey, Pubkey};
5-
6-
pub const ID: Pubkey = pubkey!("3rdJbqfnagQ4yx9HXJViD4zc4xpiSqmFsKpPuSCQVyQL");
7-
8-
#[test]
9-
fn test_storage_id() {
10-
use {crate::STORAGE_SEED, anchor_lang::prelude::Pubkey};
11-
12-
assert_eq!(
13-
Pubkey::find_program_address(&[STORAGE_SEED], &super::ID).0,
14-
ID
15-
);
16-
}
17-
}
18-
193
use {
20-
anchor_lang::{prelude::*, solana_program::pubkey::PUBKEY_BYTES},
4+
anchor_lang::{prelude::*, solana_program::pubkey::PUBKEY_BYTES, system_program},
215
std::mem::size_of,
226
};
237

@@ -28,6 +12,21 @@ pub use {
2812

2913
declare_id!("pytd2yyk641x7ak7mkaasSJVXh6YYZnC7wTmtgAyxPt");
3014

15+
pub const STORAGE_ID: Pubkey = pubkey!("3rdJbqfnagQ4yx9HXJViD4zc4xpiSqmFsKpPuSCQVyQL");
16+
pub const TREASURY_ID: Pubkey = pubkey!("EN4aB3soE5iuCG2fGj2r5fksh4kLRVPV8g7N86vXm8WM");
17+
18+
#[test]
19+
fn test_ids() {
20+
assert_eq!(
21+
Pubkey::find_program_address(&[STORAGE_SEED], &ID).0,
22+
STORAGE_ID
23+
);
24+
assert_eq!(
25+
Pubkey::find_program_address(&[TREASURY_SEED], &ID).0,
26+
TREASURY_ID
27+
);
28+
}
29+
3130
pub const MAX_NUM_TRUSTED_SIGNERS: usize = 2;
3231

3332
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, AnchorSerialize, AnchorDeserialize)]
@@ -44,12 +43,14 @@ impl TrustedSignerInfo {
4443
pub struct Storage {
4544
pub top_authority: Pubkey,
4645
pub num_trusted_signers: u8,
46+
pub single_update_fee_in_lamports: u64,
4747
pub trusted_signers: [TrustedSignerInfo; MAX_NUM_TRUSTED_SIGNERS],
4848
}
4949

5050
impl Storage {
5151
const SERIALIZED_LEN: usize = PUBKEY_BYTES
5252
+ size_of::<u8>()
53+
+ size_of::<u64>()
5354
+ TrustedSignerInfo::SERIALIZED_LEN * MAX_NUM_TRUSTED_SIGNERS;
5455

5556
pub fn initialized_trusted_signers(&self) -> &[TrustedSignerInfo] {
@@ -58,15 +59,18 @@ impl Storage {
5859
}
5960

6061
pub const STORAGE_SEED: &[u8] = b"storage";
62+
pub const TREASURY_SEED: &[u8] = b"treasury";
6163

6264
#[program]
6365
pub mod pyth_lazer_solana_contract {
66+
use anchor_lang::system_program;
6467
use signature::VerifiedMessage;
6568

6669
use super::*;
6770

6871
pub fn initialize(ctx: Context<Initialize>, top_authority: Pubkey) -> Result<()> {
6972
ctx.accounts.storage.top_authority = top_authority;
73+
ctx.accounts.storage.single_update_fee_in_lamports = 1;
7074
Ok(())
7175
}
7276

@@ -128,6 +132,17 @@ pub mod pyth_lazer_solana_contract {
128132
signature_index: u8,
129133
message_offset: u16,
130134
) -> Result<VerifiedMessage> {
135+
system_program::transfer(
136+
CpiContext::new(
137+
ctx.accounts.system_program.to_account_info(),
138+
system_program::Transfer {
139+
from: ctx.accounts.payer.to_account_info(),
140+
to: ctx.accounts.treasury.to_account_info(),
141+
},
142+
),
143+
ctx.accounts.storage.single_update_fee_in_lamports,
144+
)?;
145+
131146
signature::verify_message(
132147
&ctx.accounts.storage,
133148
&ctx.accounts.sysvar,
@@ -155,6 +170,15 @@ pub struct Initialize<'info> {
155170
bump,
156171
)]
157172
pub storage: Account<'info, Storage>,
173+
#[account(
174+
init,
175+
payer = payer,
176+
space = 0,
177+
owner = system_program::ID,
178+
seeds = [TREASURY_SEED],
179+
bump,
180+
)]
181+
pub treasury: AccountInfo<'info>,
158182
pub system_program: Program<'info, System>,
159183
}
160184

@@ -172,10 +196,20 @@ pub struct Update<'info> {
172196

173197
#[derive(Accounts)]
174198
pub struct VerifyMessage<'info> {
199+
#[account(mut)]
200+
pub payer: Signer<'info>,
175201
#[account(
176202
seeds = [STORAGE_SEED],
177203
bump,
178204
)]
179205
pub storage: Account<'info, Storage>,
206+
#[account(
207+
mut,
208+
owner = system_program::ID,
209+
seeds = [TREASURY_SEED],
210+
bump,
211+
)]
212+
pub treasury: AccountInfo<'info>,
213+
pub system_program: Program<'info, System>,
180214
pub sysvar: AccountInfo<'info>,
181215
}

lazer/contracts/solana/programs/pyth-lazer-solana-contract/tests/test1.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ async fn test1() {
3737
.data(),
3838
vec![
3939
AccountMeta::new(payer.pubkey(), true),
40-
AccountMeta::new(pyth_lazer_solana_contract::storage::ID, false),
40+
AccountMeta::new(pyth_lazer_solana_contract::STORAGE_ID, false),
41+
AccountMeta::new(pyth_lazer_solana_contract::TREASURY_ID, false),
4142
AccountMeta::new_readonly(system_program::ID, false),
4243
],
4344
)],
@@ -68,7 +69,7 @@ async fn test1() {
6869
.data(),
6970
vec![
7071
AccountMeta::new(payer.pubkey(), true),
71-
AccountMeta::new(pyth_lazer_solana_contract::storage::ID, false),
72+
AccountMeta::new(pyth_lazer_solana_contract::STORAGE_ID, false),
7273
],
7374
)],
7475
Some(&payer.pubkey()),
@@ -90,7 +91,12 @@ async fn test1() {
9091
message_offset,
9192
));
9293

93-
println!("ok1");
94+
let treasury_starting_lamports = banks_client
95+
.get_account(pyth_lazer_solana_contract::TREASURY_ID)
96+
.await
97+
.unwrap()
98+
.unwrap()
99+
.lamports;
94100
let mut transaction_verify = Transaction::new_with_payer(
95101
&[
96102
Instruction::new_with_bytes(
@@ -108,7 +114,10 @@ async fn test1() {
108114
}
109115
.data(),
110116
vec![
111-
AccountMeta::new_readonly(pyth_lazer_solana_contract::storage::ID, false),
117+
AccountMeta::new(payer.pubkey(), true),
118+
AccountMeta::new_readonly(pyth_lazer_solana_contract::STORAGE_ID, false),
119+
AccountMeta::new(pyth_lazer_solana_contract::TREASURY_ID, false),
120+
AccountMeta::new_readonly(system_program::ID, false),
112121
AccountMeta::new_readonly(sysvar::instructions::ID, false),
113122
],
114123
),
@@ -120,4 +129,13 @@ async fn test1() {
120129
.process_transaction(transaction_verify)
121130
.await
122131
.unwrap();
132+
assert_eq!(
133+
banks_client
134+
.get_account(pyth_lazer_solana_contract::TREASURY_ID)
135+
.await
136+
.unwrap()
137+
.unwrap()
138+
.lamports,
139+
treasury_starting_lamports + 1
140+
);
123141
}

0 commit comments

Comments
 (0)