github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/dev/wasm/escrow/src/state.rs (about)

     1  use cosmwasm_std::{Addr, Env, Storage};
     2  use cosmwasm_storage::{singleton, singleton_read, ReadonlySingleton, Singleton};
     3  use schemars::JsonSchema;
     4  use serde::{Deserialize, Serialize};
     5  
     6  static CONFIG_KEY: &[u8] = b"config";
     7  
     8  #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
     9  pub struct State {
    10      pub arbiter: Addr,
    11      pub recipient: Addr,
    12      pub source: Addr,
    13      pub end_height: Option<u64>,
    14      pub end_time: Option<u64>,
    15  }
    16  
    17  impl State {
    18      pub fn is_expired(&self, env: &Env) -> bool {
    19          if let Some(end_height) = self.end_height {
    20              if env.block.height > end_height {
    21                  return true;
    22              }
    23          }
    24  
    25          if let Some(end_time) = self.end_time {
    26              if env.block.time.nanos() > end_time * 1000 {
    27                  return true;
    28              }
    29          }
    30          false
    31      }
    32  }
    33  
    34  pub fn config(storage: &mut dyn Storage) -> Singleton<State> {
    35      singleton(storage, CONFIG_KEY)
    36  }
    37  
    38  pub fn config_read(storage: &dyn Storage) -> ReadonlySingleton<State> {
    39      singleton_read(storage, CONFIG_KEY)
    40  }