gemachain_config_program/
date_instruction.rs

1use crate::{config_instruction, ConfigState};
2///
3/// A library for creating a trusted date oracle.
4///
5use bincode::{deserialize, serialized_size};
6use chrono::{
7    prelude::{Date, DateTime, TimeZone, Utc},
8    serde::ts_seconds,
9};
10use serde_derive::{Deserialize, Serialize};
11use gemachain_sdk::{instruction::Instruction, pubkey::Pubkey};
12
13#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
14pub struct DateConfig {
15    #[serde(with = "ts_seconds")]
16    pub date_time: DateTime<Utc>,
17}
18
19impl Default for DateConfig {
20    fn default() -> Self {
21        Self {
22            date_time: Utc.timestamp(0, 0),
23        }
24    }
25}
26impl DateConfig {
27    pub fn new(date: Date<Utc>) -> Self {
28        Self {
29            date_time: date.and_hms(0, 0, 0),
30        }
31    }
32
33    pub fn deserialize(input: &[u8]) -> Option<Self> {
34        deserialize(input).ok()
35    }
36}
37
38impl ConfigState for DateConfig {
39    fn max_space() -> u64 {
40        serialized_size(&Self::default()).unwrap()
41    }
42}
43
44/// Create a date account. The date is set to the Unix epoch.
45pub fn create_account(
46    payer_pubkey: &Pubkey,
47    date_pubkey: &Pubkey,
48    carats: u64,
49) -> Vec<Instruction> {
50    config_instruction::create_account::<DateConfig>(payer_pubkey, date_pubkey, carats, vec![])
51}
52
53/// Set the date in the date account. The account pubkey must be signed in the
54/// transaction containing this instruction.
55pub fn store(date_pubkey: &Pubkey, date: Date<Utc>) -> Instruction {
56    let date_config = DateConfig::new(date);
57    config_instruction::store(date_pubkey, true, vec![], &date_config)
58}