clockwork_network/state/
snapshot_entry.rs

1use {
2    anchor_lang::{prelude::*, AnchorDeserialize},
3    std::convert::TryFrom,
4};
5
6pub const SEED_SNAPSHOT_ENTRY: &[u8] = b"snapshot_entry";
7
8/**
9 * SnapshotEntry
10 */
11#[account]
12#[derive(Debug)]
13pub struct SnapshotEntry {
14    pub id: u64,
15    pub snapshot: Pubkey,
16    pub stake_amount: u64,
17    pub stake_offset: u64,
18    pub worker: Pubkey,
19}
20
21impl SnapshotEntry {
22    pub fn pubkey(snapshot: Pubkey, id: u64) -> Pubkey {
23        Pubkey::find_program_address(
24            &[
25                SEED_SNAPSHOT_ENTRY,
26                snapshot.as_ref(),
27                id.to_be_bytes().as_ref(),
28            ],
29            &crate::ID,
30        )
31        .0
32    }
33}
34
35impl TryFrom<Vec<u8>> for SnapshotEntry {
36    type Error = Error;
37    fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
38        SnapshotEntry::try_deserialize(&mut data.as_slice())
39    }
40}
41
42/**
43 * SnapshotEntryAccount
44 */
45
46pub trait SnapshotEntryAccount {
47    fn new(
48        &mut self,
49        id: u64,
50        snapshot: Pubkey,
51        stake_offset: u64,
52        stake_amount: u64,
53        worker: Pubkey,
54    ) -> Result<()>;
55}
56
57impl SnapshotEntryAccount for Account<'_, SnapshotEntry> {
58    fn new(
59        &mut self,
60        id: u64,
61        snapshot: Pubkey,
62        stake_offset: u64,
63        stake_amount: u64,
64        worker: Pubkey,
65    ) -> Result<()> {
66        self.id = id;
67        self.snapshot = snapshot;
68        self.stake_offset = stake_offset;
69        self.stake_amount = stake_amount;
70        self.worker = worker;
71        Ok(())
72    }
73}