ore_api/state/
automation.rs1use ore_mint_api::consts::ONE_ORE;
2use serde::{Deserialize, Serialize};
3use steel::*;
4
5use crate::state::{automation_pda, OreAccountV4};
6
7use super::OreAccountV1;
8
9#[repr(C)]
10#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
11pub struct AutomationV1 {
12 pub amount: u64,
14
15 pub authority: Pubkey,
17
18 pub balance: u64,
20
21 pub executor: Pubkey,
23
24 pub fee: u64,
26
27 pub strategy: u64,
29
30 pub mask: u64,
33
34 pub reload: u64,
36}
37
38#[repr(C)]
39#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
40pub struct AutomationV4 {
41 pub amount: u64,
43
44 pub authority: Pubkey,
46
47 pub balance: u64,
49
50 pub executor: Pubkey,
52
53 pub fee: u64,
55
56 pub strategy: u64,
58
59 pub mask: u64,
62
63 pub reload: u64,
65
66 pub total_sol_spent: u64,
68
69 pub total_ore_earned: u64,
71
72 pub conditions: AutomationConditions,
74}
75
76#[repr(C)]
78#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
79pub struct AutomationConditions {
80 pub max_production_cost: u64,
83
84 pub min_motherlode: u64,
87
88 pub max_motherlode: u64,
91}
92
93#[repr(u8)]
94#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
95pub enum AutomationStrategy {
96 Random = 0,
97 Preferred = 1,
98 Discretionary = 2,
99}
100
101impl AutomationStrategy {
102 pub fn from_u64(value: u64) -> Self {
103 Self::try_from(value as u8).unwrap()
104 }
105}
106
107impl Default for AutomationConditions {
108 fn default() -> Self {
109 Self {
110 max_production_cost: u64::MAX,
111 min_motherlode: 0,
112 max_motherlode: u64::MAX,
113 }
114 }
115}
116
117impl AutomationV4 {
118 pub fn pda(&self) -> (Pubkey, u8) {
119 automation_pda(self.authority)
120 }
121
122 pub fn production_cost(&self) -> u64 {
123 if self.total_ore_earned == 0 {
124 return 0;
125 }
126 ((self.total_sol_spent as u128) * (ONE_ORE as u128) / (self.total_ore_earned as u128))
127 as u64
128 }
129}
130
131account!(OreAccountV1, AutomationV1);
132account!(OreAccountV4, AutomationV4);
133
134#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
135pub enum Automation {
136 V1(AutomationV1),
137 V4(AutomationV4),
138}
139
140impl Automation {
141 pub fn amount(&self) -> u64 {
142 match self {
143 Automation::V1(a) => a.amount,
144 Automation::V4(a) => a.amount,
145 }
146 }
147
148 pub fn authority(&self) -> Pubkey {
149 match self {
150 Automation::V1(a) => a.authority,
151 Automation::V4(a) => a.authority,
152 }
153 }
154
155 pub fn balance(&self) -> u64 {
156 match self {
157 Automation::V1(a) => a.balance,
158 Automation::V4(a) => a.balance,
159 }
160 }
161
162 pub fn executor(&self) -> Pubkey {
163 match self {
164 Automation::V1(a) => a.executor,
165 Automation::V4(a) => a.executor,
166 }
167 }
168
169 pub fn fee(&self) -> u64 {
170 match self {
171 Automation::V1(a) => a.fee,
172 Automation::V4(a) => a.fee,
173 }
174 }
175
176 pub fn strategy(&self) -> u64 {
177 match self {
178 Automation::V1(a) => a.strategy,
179 Automation::V4(a) => a.strategy,
180 }
181 }
182
183 pub fn mask(&self) -> u64 {
184 match self {
185 Automation::V1(a) => a.mask,
186 Automation::V4(a) => a.mask,
187 }
188 }
189
190 pub fn reload(&self) -> u64 {
191 match self {
192 Automation::V1(a) => a.reload,
193 Automation::V4(a) => a.reload,
194 }
195 }
196}