Clock
Sui has built-in support for a clock. The clock is a special object that can be used to get the current time. It is a shared object, and can be accessed by anyone.
Clock has a reserved address 0x6
. While being a shared object, it can't be accessed mutably, and a transaction attempting to do so will fail.
module examples::clock {
// Import the `clock` module and the `Clock` type.
use sui::clock::Clock;
/// A dummy object that hold the time it was created.
/// What if we could sell a timestamp...?
public struct Timestamp has key, store {
id: UID,
timestamp: u64,
}
/// Creates a new `Timestamp` Object using the `Clock`.
public fun new(clock: &Clock, ctx: &mut TxContext): Timestamp {
// The `timestamp_ms` is the main function of the `Clock` module.
let timestamp = clock.timestamp_ms();
Timestamp { timestamp, id: object::new(ctx) }
}
}