tachyon_math_library/state/
function_data.rs1use crate::state::NUM_VALUES;
2use crate::{Cos, Exp, FunctionDataAccessors, FunctionLogic, FunctionType, Interpolation, Ln, Log10, Pubkey, Sin, ValueCode};
3use anchor_lang::prelude::*;
4
5use rust_decimal::Decimal;
6
7use crate::error::ErrorCode;
8
9#[account(zero_copy)]
10pub struct FunctionData {
11 pub initialized: u32,
12 pub domain_start: [u8; 16],
13 pub domain_end: [u8; 16],
14 pub interval: [u8; 16],
15 pub values: [[u8; 16]; NUM_VALUES],
16 pub value_codes: [u8; NUM_VALUES],
17 pub num_values: u32,
18 pub num_values_loaded: u32,
19 pub function_type: u32,
20}
21
22impl FunctionDataAccessors for FunctionData {
23 fn eval(&self, x: Decimal, interp: Interpolation, saturating: bool) -> Result<Decimal> {
24 let function_type = FunctionType::try_from(self.function_type).unwrap();
25
26 match function_type {
27 FunctionType::Exp => Exp::eval(self, x, interp, saturating),
28 FunctionType::Ln => Ln::eval(self, x, interp, saturating),
29 FunctionType::Log10 => Log10::eval(self, x, interp, saturating),
30 FunctionType::Sin => Sin::eval(self, x, interp, saturating),
31 FunctionType::Cos => Cos::eval(self, x, interp, saturating),
32 _ => err!(ErrorCode::MissingImplementation),
33 }
34 }
35
36 fn eval_load(&self, x_in: Decimal, y_in: Decimal) -> Result<(Decimal, ValueCode)> {
37 let function_type = FunctionType::try_from(self.function_type).unwrap();
38
39 match function_type {
40 FunctionType::Exp => Exp::validate_load(x_in, y_in),
41 FunctionType::Ln => Ln::validate_load(x_in, y_in),
42 FunctionType::Log10 => Log10::validate_load(x_in, y_in),
43 FunctionType::Sin => Sin::validate_load(x_in, y_in),
44 FunctionType::Cos => Cos::validate_load(x_in, y_in),
45 _ => err!(ErrorCode::MissingImplementation),
46 }
47 }
48
49 fn get_values_array(&self) -> &[[u8; 16]; NUM_VALUES] {
50 &self.values
51 }
52
53 fn get_value_codes_array(&self) -> &[u8; NUM_VALUES] {
54 &self.value_codes
55 }
56
57 fn get_values_array_mut(&mut self) -> &mut [[u8; 16]; NUM_VALUES] {
58 &mut self.values
59 }
60
61 fn get_value_codes_array_mut(&mut self) -> &mut [u8; NUM_VALUES] {
62 &mut self.value_codes
63 }
64
65 fn get_function_type(&self) -> u32 {
66 self.function_type
67 }
68
69 fn set_function_type(&mut self, ft: FunctionType) -> Result<()> {
70 self.function_type = ft as u32;
71 Ok(())
72 }
73
74 fn get_domain_start(&self) -> Result<Decimal> {
75 let domain_start = Decimal::deserialize(self.domain_start);
76 Ok(domain_start)
77 }
78
79 fn set_domain_start(&mut self, domain_start_raw: [u8; 16]) -> Result<()> {
80 self.domain_start = domain_start_raw;
81 Ok(())
82 }
83
84 fn get_domain_end(&self) -> Result<Decimal> {
85 let domain_end = Decimal::deserialize(self.domain_end);
86 Ok(domain_end)
87 }
88
89 fn set_domain_end(&mut self, domain_end_raw: [u8; 16]) -> Result<()> {
90 self.domain_end = domain_end_raw;
91 Ok(())
92 }
93
94 fn get_interval(&self) -> Result<Decimal> {
95 let interval = Decimal::deserialize(self.interval);
96 Ok(interval)
97 }
98
99 fn set_interval(&mut self, interval: [u8; 16]) -> Result<()> {
100 self.interval = interval;
101 Ok(())
102 }
103
104 fn get_num_values_loaded(&self) -> u32 {
105 self.num_values_loaded
106 }
107
108 fn increment_num_values_loaded(&mut self) -> Result<()> {
109 self.num_values_loaded += 1u32;
110 Ok(())
111 }
112
113 fn get_is_initialized(&self) -> bool {
114 self.initialized != 0u32
115 }
116
117 fn set_initialized_true(&mut self) -> Result<()> {
118 self.initialized = 1u32;
119 Ok(())
120 }
121
122 fn get_num_values(&self) -> Result<u32> {
123 Ok(self.num_values)
124 }
125
126 fn set_num_values(&mut self, num_values: u32) -> Result<()> {
127 self.num_values = num_values;
128 Ok(())
129 }
130}