Smart Contract

Clock

A.e81193c424cfd3fb.Clock

Deployed

2d ago
Feb 26, 2026, 01:49:14 PM UTC

Dependents

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