pyth_client/
instruction.rs1use bytemuck::bytes_of;
4
5use crate::{PriceStatus, Price};
6
7use {
8 crate::id,
9 borsh::{BorshDeserialize, BorshSerialize},
10 solana_program::instruction::Instruction,
11 crate::PriceConf,
12};
13
14#[derive(Clone, Debug, BorshSerialize, BorshDeserialize, PartialEq)]
17pub enum PythClientInstruction {
18 Divide {
19 numerator: PriceConf,
20 denominator: PriceConf,
21 },
22 Multiply {
23 x: PriceConf,
24 y: PriceConf,
25 },
26 Add {
27 x: PriceConf,
28 y: PriceConf,
29 },
30 ScaleToExponent {
31 x: PriceConf,
32 expo: i32,
33 },
34 Normalize {
35 x: PriceConf,
36 },
37 Noop,
41
42 PriceStatusCheck {
43 price_account_data: Vec<u8>,
46 expected_price_status: PriceStatus
47 }
48}
49
50pub fn divide(numerator: PriceConf, denominator: PriceConf) -> Instruction {
51 Instruction {
52 program_id: id(),
53 accounts: vec![],
54 data: PythClientInstruction::Divide { numerator, denominator }
55 .try_to_vec()
56 .unwrap(),
57 }
58}
59
60pub fn multiply(x: PriceConf, y: PriceConf) -> Instruction {
61 Instruction {
62 program_id: id(),
63 accounts: vec![],
64 data: PythClientInstruction::Multiply { x, y }
65 .try_to_vec()
66 .unwrap(),
67 }
68}
69
70pub fn add(x: PriceConf, y: PriceConf) -> Instruction {
71 Instruction {
72 program_id: id(),
73 accounts: vec![],
74 data: PythClientInstruction::Add { x, y }
75 .try_to_vec()
76 .unwrap(),
77 }
78}
79
80pub fn scale_to_exponent(x: PriceConf, expo: i32) -> Instruction {
81 Instruction {
82 program_id: id(),
83 accounts: vec![],
84 data: PythClientInstruction::ScaleToExponent { x, expo }
85 .try_to_vec()
86 .unwrap(),
87 }
88}
89
90pub fn normalize(x: PriceConf) -> Instruction {
91 Instruction {
92 program_id: id(),
93 accounts: vec![],
94 data: PythClientInstruction::Normalize { x }
95 .try_to_vec()
96 .unwrap(),
97 }
98}
99
100pub fn noop() -> Instruction {
102 Instruction {
103 program_id: id(),
104 accounts: vec![],
105 data: PythClientInstruction::Noop.try_to_vec().unwrap(),
106 }
107}
108
109pub fn price_status_check(price: &Price, expected_price_status: PriceStatus) -> Instruction {
111 Instruction {
112 program_id: id(),
113 accounts: vec![],
114 data: PythClientInstruction::PriceStatusCheck { price_account_data: bytes_of(price).to_vec(), expected_price_status }
115 .try_to_vec()
116 .unwrap(),
117 }
118}