Module 0x2f66c09143acc52a85fec529a4e20c85::Timelock
Simple timelocked tao: allow extracting only when a certain time has passed
use 0x2f66c09143acc52a85fec529a4e20c85::Connector;
use 0x2f66c09143acc52a85fec529a4e20c85::Errors;
Resource Tao
Tao for a simple timelock: extract content
if unlock_time
has
passed (in seconds).
struct Tao<Content> has store, key
Fields
-
unlock_time: u64
-
content: Content
Function wrap
Create a new timelocked tao. unlock_time
is in seconds, will be
compared against Connector::current_timestamp()
on production
network. Time can be 0, at least for now.
public fun wrap<Content>(unlock_time: u64, content: Content): Timelock::Tao<Content>
Implementation
public fun wrap<Content>(unlock_time: u64, content: Content): Tao<Content> {
Tao<Content> { unlock_time, content }
}
Specification
ensures result.unlock_time == unlock_time && result.content == content;
Function read
Immutable read-only reference to the unlock time, and the content
.
public fun read<Content>(tao: &Timelock::Tao<Content>): (&u64, &Content)
Implementation
public fun read<Content>(tao: &Tao<Content>): (&u64, &Content) {
let Tao<Content> { unlock_time, content } = tao;
(unlock_time, content)
}
Specification
ensures result_1 == tao.unlock_time;
ensures result_2 == tao.content;
Function unwrap
Extract tao.content
if tao.unlock_time
has passed.
public fun unwrap<Content>(tao: Timelock::Tao<Content>): Content
Implementation
public fun unwrap<Content>(tao: Tao<Content>): Content {
let Tao<Content> { content, unlock_time } = tao;
let current_timestamp: u64 = Connector::current_timestamp();
assert!(current_timestamp > unlock_time, Errors::timelock_too_early());
content
}
Specification
aborts_if (tao.unlock_time >= Connector::current_timestamp());
pragma aborts_if_is_strict;