sablier_thread_program/state/
versioned_thread.rs

1use crate::{ClockData, ExecContext, SerializableInstruction, Thread, Trigger};
2use anchor_lang::{prelude::*, AccountDeserialize};
3
4#[derive(Clone, Debug, PartialEq)]
5pub enum VersionedThread {
6    V1(Thread),
7}
8
9impl VersionedThread {
10    pub fn authority(&self) -> Pubkey {
11        match self {
12            Self::V1(t) => t.authority,
13        }
14    }
15
16    pub fn created_at(&self) -> ClockData {
17        match self {
18            Self::V1(t) => t.created_at.clone(),
19        }
20    }
21
22    pub fn exec_context(&self) -> Option<ExecContext> {
23        match self {
24            Self::V1(t) => t.exec_context,
25        }
26    }
27
28    pub fn id(&self) -> Vec<u8> {
29        match self {
30            Self::V1(t) => t.id.clone(),
31        }
32    }
33
34    pub fn domain(&self) -> Option<Vec<u8>> {
35        match self {
36            Self::V1(t) => t.domain.clone(),
37        }
38    }
39
40    pub fn next_instruction(&self) -> Option<SerializableInstruction> {
41        match self {
42            Self::V1(t) => t.next_instruction.clone(),
43        }
44    }
45
46    pub fn paused(&self) -> bool {
47        match self {
48            Self::V1(t) => t.paused,
49        }
50    }
51
52    pub fn program_id(&self) -> Pubkey {
53        match self {
54            Self::V1(_) => crate::ID,
55        }
56    }
57
58    pub fn pubkey(&self) -> Pubkey {
59        match self {
60            Self::V1(_) => Thread::pubkey(self.authority(), self.id(), self.domain()),
61        }
62    }
63
64    pub fn rate_limit(&self) -> u64 {
65        match self {
66            Self::V1(t) => t.rate_limit,
67        }
68    }
69
70    pub fn trigger(&self) -> Trigger {
71        match self {
72            Self::V1(t) => t.trigger.clone(),
73        }
74    }
75}
76
77impl AccountDeserialize for VersionedThread {
78    fn try_deserialize(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
79        Self::try_deserialize_unchecked(buf)
80    }
81
82    fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
83        // Try first to deserialize into ThreadV2.
84        // If this fails, try to deserialize into ThreadV1.
85        Ok(VersionedThread::V1(Thread::try_deserialize(buf)?))
86    }
87}
88
89impl TryFrom<Vec<u8>> for VersionedThread {
90    type Error = Error;
91    fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
92        VersionedThread::try_deserialize(&mut data.as_slice())
93    }
94}