pyth_client/
instruction.rs

1//! Program instructions for end-to-end testing and instruction counts
2
3use 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/// Instructions supported by the pyth-client program, used for testing and
15/// instruction counts
16#[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  /// Don't do anything for comparison
38  ///
39  /// No accounts required for this instruction
40  Noop,
41
42  PriceStatusCheck {
43    // A Price serialized as a vector of bytes. This field is stored as a vector of bytes (instead of a Price)
44    // so that we do not have to add Borsh serialization to all structs, which is expensive.
45    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
100/// Noop instruction for comparison purposes
101pub fn noop() -> Instruction {
102  Instruction {
103    program_id: id(),
104    accounts: vec![],
105    data: PythClientInstruction::Noop.try_to_vec().unwrap(),
106  }
107}
108
109// Returns ok if price account status matches given expected price status.
110pub 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}