Skip to main content

ore_api/state/
automation.rs

1use ore_mint_api::consts::ONE_ORE;
2use serde::{Deserialize, Serialize};
3use steel::*;
4
5use crate::state::{automation_pda, OreAccount};
6
7#[repr(C)]
8#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
9pub struct Automation {
10    /// The amount of SOL to deploy on each territory per round.
11    pub amount: u64,
12
13    /// The authority of this automation account.
14    pub authority: Pubkey,
15
16    /// The amount of SOL this automation has left.
17    pub balance: u64,
18
19    /// The executor of this automation account.
20    pub executor: Pubkey,
21
22    /// The amount of SOL the executor should receive in fees.
23    pub fee: u64,
24
25    /// The strategy this automation uses.
26    pub strategy: u64,
27
28    /// The mask of squares this automation should deploy to if preferred strategy.
29    /// If strategy is Random, first bit is used to determine how many squares to deploy to.
30    pub mask: u64,
31
32    /// Whether or not to auto-reload SOL winnings into the automation balance.
33    pub reload: u64,
34
35    /// The total SOL spent (lost to fees) by this automation.
36    pub total_sol_spent: u64,
37
38    /// The total ORE earned by this automation.
39    pub total_ore_earned: u64,
40
41    /// Conditions that must be met for the automation to deploy.
42    pub conditions: AutomationConditions,
43}
44
45/// Conditions that gate whether an automation deploys in a given round.
46#[repr(C)]
47#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
48pub struct AutomationConditions {
49    /// Max production cost EMA (lamports per whole ORE). Deploy blocked if EMA exceeds this.
50    /// Default: u64::MAX (no upper bound).
51    pub max_production_cost: u64,
52
53    /// Min motherlode amount (ORE units). Deploy blocked if motherlode is below this.
54    /// Default: 0 (no lower bound).
55    pub min_motherlode: u64,
56
57    /// Max motherlode amount (ORE units). Deploy blocked if motherlode exceeds this.
58    /// Default: u64::MAX (no upper bound).
59    pub max_motherlode: u64,
60}
61
62#[repr(u8)]
63#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
64pub enum AutomationStrategy {
65    Random = 0,
66    Preferred = 1,
67    Discretionary = 2,
68}
69
70impl AutomationStrategy {
71    pub fn from_u64(value: u64) -> Self {
72        Self::try_from(value as u8).unwrap()
73    }
74}
75
76impl Default for AutomationConditions {
77    fn default() -> Self {
78        Self {
79            max_production_cost: u64::MAX,
80            min_motherlode: 0,
81            max_motherlode: u64::MAX,
82        }
83    }
84}
85
86impl Automation {
87    pub fn pda(&self) -> (Pubkey, u8) {
88        automation_pda(self.authority)
89    }
90
91    pub fn production_cost(&self) -> u64 {
92        if self.total_ore_earned == 0 {
93            return 0;
94        }
95        ((self.total_sol_spent as u128) * (ONE_ORE as u128) / (self.total_ore_earned as u128))
96            as u64
97    }
98}
99
100account!(OreAccount, Automation);