Skip to main content

rialo_s_config_program/
date_instruction.rs

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