cronos_network/state/
snapshot_entry.rs1use {
2 crate::pda::PDA,
3 anchor_lang::{prelude::*, AnchorDeserialize},
4 std::convert::TryFrom,
5};
6
7pub const SEED_SNAPSHOT_ENTRY: &[u8] = b"snapshot_entry";
8
9#[account]
13#[derive(Debug)]
14pub struct SnapshotEntry {
15 pub id: u64,
16 pub delegate: Pubkey,
17 pub snapshot: Pubkey,
18 pub stake_amount: u64,
19 pub stake_offset: u64,
20}
21
22impl SnapshotEntry {
23 pub fn pda(snapshot: Pubkey, id: u64) -> PDA {
24 Pubkey::find_program_address(
25 &[
26 SEED_SNAPSHOT_ENTRY,
27 snapshot.as_ref(),
28 id.to_be_bytes().as_ref(),
29 ],
30 &crate::ID,
31 )
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
42pub trait SnapshotEntryAccount {
47 fn new(
48 &mut self,
49 id: u64,
50 delegate: Pubkey,
51 snapshot: Pubkey,
52 stake_offset: u64,
53 stake_amount: u64,
54 ) -> Result<()>;
55}
56
57impl SnapshotEntryAccount for Account<'_, SnapshotEntry> {
58 fn new(
59 &mut self,
60 id: u64,
61 delegate: Pubkey,
62 snapshot: Pubkey,
63 stake_offset: u64,
64 stake_amount: u64,
65 ) -> Result<()> {
66 self.id = id;
67 self.delegate = delegate;
68 self.snapshot = snapshot;
69 self.stake_offset = stake_offset;
70 self.stake_amount = stake_amount;
71 Ok(())
72 }
73}