Skip to content

Commit

Permalink
Add Timestamp Validator contract
Browse files Browse the repository at this point in the history
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
arkavo-com committed Oct 23, 2024
1 parent 30f2efb commit 1c251cf
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
12 changes: 12 additions & 0 deletions etc/contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions etc/contracts/timestamp_validator/Cargo.toml
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 = []
79 changes: 79 additions & 0 deletions etc/contracts/timestamp_validator/lib.rs
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));
}
}

0 comments on commit 1c251cf

Please sign in to comment.