Smart Contract

Clock

A.097bafa4e0b48eef.Clock

Deployed

2d ago
Feb 26, 2026, 03:12:51 AM UTC

Dependents

0 imports
1
2/*
3A contract to mock time. 
4
5If you want to mock time create an function in your admin that enables the  Clock
6The clock will then start at block 0
7use the tick method to tick the time. 
8
9```
10//this is used to mock the clock, NB! Should consider removing this before deploying to mainnet?
11access(all) fun tickClock(_ time: UFix64) {
12    pre {
13        self.capability != nil: "Cannot use AdminProxy, ste capability first"
14    }
15    Clock.enable()
16    Clock.tick(time)
17}
18```
19
20You can then call this from a transaction like this:
21
22```
23import "YourThing"
24
25transaction(clock: UFix64) {
26    prepare(account: auth(BorrowValue) &Account) {
27
28        let adminClient=account.storage.borrow<&YourThing.AdminProxy>(from: YourThing.AdminProxyStoragePath)!
29        adminClient.tickClock(clock)
30
31    }
32}
33```
34
35In order to read the mocked time you use the following code in cadence
36
37```
38Clock.time()
39```
40
41Limitations: 
42- all contracts must live in the same account to (ab)use this trick
43
44*/
45access(all) contract Clock{
46    //want to mock time on emulator. 
47    access(contract) var fakeClock:UFix64
48    access(contract) var enabled:Bool
49
50    access(account) fun tick(_ duration: UFix64) {
51        self.fakeClock = self.fakeClock + duration
52    }
53
54    access(account) fun enable() {
55        self.enabled=true
56    }
57
58    //mocking the time! Should probably remove self.fakeClock in mainnet?
59    access(all) fun time() : UFix64 {
60        if self.enabled {
61            return self.fakeClock 
62        }
63        return getCurrentBlock().timestamp
64    }
65
66    init() {
67        self.fakeClock=0.0
68        self.enabled=false
69    }
70}
71
72