-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a new Timestamp Validator contract with basic functionality to validate timestamps. Updated the README to include deployment instructions for the new contract.
- Loading branch information
1 parent
30f2efb
commit 1c251cf
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[package] | ||
name = "timestamp_validator" | ||
version = "0.1.0" | ||
authors = ["[your_name] <[your_email]>"] | ||
edition = "2021" | ||
|
||
[dependencies] | ||
ink = { version = "5.0.0", default-features = false } | ||
scale = { package = "parity-scale-codec", version = "3.6.12", default-features = false, features = ["derive"] } | ||
scale-info = { version = "2.11.3", default-features = false, features = ["derive"], optional = true } | ||
|
||
[dev-dependencies] | ||
ink_e2e = { version = "5.0.0" } | ||
|
||
[lib] | ||
path = "lib.rs" | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"ink/std", | ||
"scale/std", | ||
"scale-info/std", | ||
] | ||
ink-as-dependency = [] | ||
e2e-tests = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#![cfg_attr(not(feature = "std"), no_std, no_main)] | ||
#![allow(unexpected_cfgs)] | ||
#[ink::contract] | ||
mod timestamp_validator { | ||
use scale::{Decode, Encode}; | ||
|
||
#[ink(storage)] | ||
pub struct TimestampValidator {} | ||
|
||
impl Default for TimestampValidator { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl TimestampValidator { | ||
#[ink(constructor)] | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
#[ink(message)] | ||
pub fn is_timestamp_valid(&self, timestamp: u64, start: u64, end: u64) -> bool { | ||
// Check if timestamp is within range (inclusive) | ||
timestamp >= start && timestamp <= end | ||
} | ||
|
||
#[ink(message)] | ||
pub fn validate_with_error(&self, timestamp: u64, start: u64, end: u64) -> Result<(), Error> { | ||
if timestamp < start { | ||
return Err(Error::TooEarly); | ||
} | ||
if timestamp > end { | ||
return Err(Error::TooLate); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Encode, Decode)] | ||
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] | ||
pub enum Error { | ||
TooEarly, | ||
TooLate, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::timestamp_validator::{Error, TimestampValidator}; | ||
|
||
#[ink::test] | ||
fn test_timestamp_validation() { | ||
let validator = TimestampValidator::new(); | ||
|
||
// Test valid timestamp | ||
assert!(validator.is_timestamp_valid(100, 50, 150)); | ||
|
||
// Test boundary conditions | ||
assert!(validator.is_timestamp_valid(50, 50, 150)); // Start boundary | ||
assert!(validator.is_timestamp_valid(150, 50, 150)); // End boundary | ||
|
||
// Test invalid timestamps | ||
assert!(!validator.is_timestamp_valid(49, 50, 150)); // Before start | ||
assert!(!validator.is_timestamp_valid(151, 50, 150)); // After end | ||
} | ||
|
||
#[ink::test] | ||
fn test_validate_with_error() { | ||
let validator = TimestampValidator::new(); | ||
|
||
// Test valid timestamp | ||
assert_eq!(validator.validate_with_error(100, 50, 150), Ok(())); | ||
|
||
// Test invalid timestamps | ||
assert_eq!(validator.validate_with_error(49, 50, 150), Err(Error::TooEarly)); | ||
assert_eq!(validator.validate_with_error(151, 50, 150), Err(Error::TooLate)); | ||
} | ||
} |