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