diff --git a/etc/contracts/README.md b/etc/contracts/README.md index 6d1cb90..da49bd6 100644 --- a/etc/contracts/README.md +++ b/etc/contracts/README.md @@ -52,6 +52,18 @@ Code hash 0x63a3ec45fd3ab905924a38227917e278de277c9b80d6865190d18d0d64f560bb Contract 5H6sLwXKBv3cdm5VVRxrvA8p5cux2Rrni5CQ4GRyYKo4b9B4 ``` +#### Timestamp + +```text +Code hash 0xee2250ba4215f273e571ecdfc2a373ccc96de5f82c19fcdaca889218fac5ac39 +Contract 5D35jFeQboveKiaQSxyLKENGqrnjgUc7B4D23QbhJK4Yr7jT +``` + +```shell +cargo contract upload --url ws://127.0.0.1:9944 --suri //Alice timestamp_validator/target/ink/timestamp_validator.wasm +cargo contract instantiate --url ws://127.0.0.1:9944 --suri //Alice timestamp_validator/target/ink/timestamp_validator.wasm --execute +``` + ```shell cargo contract upload --url ws://127.0.0.1:9944 --suri //Alice geo_fence_contract/target/ink/geo_fence_contract.wasm cargo contract instantiate --url ws://127.0.0.1:9944 --suri //Alice geo_fence_contract/target/ink/geo_fence_contract.wasm --execute diff --git a/etc/contracts/timestamp_validator/Cargo.toml b/etc/contracts/timestamp_validator/Cargo.toml new file mode 100755 index 0000000..b17af60 --- /dev/null +++ b/etc/contracts/timestamp_validator/Cargo.toml @@ -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 = [] diff --git a/etc/contracts/timestamp_validator/lib.rs b/etc/contracts/timestamp_validator/lib.rs new file mode 100755 index 0000000..27b7ff5 --- /dev/null +++ b/etc/contracts/timestamp_validator/lib.rs @@ -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)); + } +} \ No newline at end of file