Smart Contract
Counter
A.28eab429d0e72f0d.Counter
1import FlowToken from 0x1654653399040a61
2import FungibleToken from 0xf233dcee88fe0abe
3import FlowTransactionScheduler from 0xe467b9dd11fa00df
4
5access(all) contract Counter {
6 /// Handler resource that implements the Scheduled Transaction interface
7 access(all) resource Handler: FlowTransactionScheduler.TransactionHandler {
8 access(FlowTransactionScheduler.Execute) fun executeTransaction(id: UInt64, data: AnyStruct?) {
9 Counter.increment()
10 // Schedule for the next second
11 let future = getCurrentBlock().timestamp + 1.0
12 let priority = FlowTransactionScheduler.Priority.Low
13 let executionEffort: UInt64 = 1000
14 let estimate = FlowTransactionScheduler.estimate(
15 data: data,
16 timestamp: future,
17 priority: priority,
18 executionEffort: executionEffort
19 )
20
21 assert(
22 estimate.timestamp != nil || priority == FlowTransactionScheduler.Priority.Low,
23 message: estimate.error ?? "estimation failed"
24 )
25
26 if Counter.account.storage.borrow<&AnyResource>(from: /storage/Counter) == nil {
27 let handler <- Counter.createHandler()
28 Counter.account.storage.save(<-handler, to: /storage/Counter)
29 }
30 // Withdraw FLOW fees from this contract's account vault
31 let vaultRef = Counter.account.storage
32 .borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
33 ?? panic("missing FlowToken vault on contract account")
34 let fees <- vaultRef.withdraw(amount: estimate.flowFee ?? 0.0) as! @FlowToken.Vault
35 let handlerCap = Counter.account.capabilities.storage
36 .issue<auth(FlowTransactionScheduler.Execute) &{FlowTransactionScheduler.TransactionHandler}>(/storage/Counter)
37
38 let receipt <- FlowTransactionScheduler.schedule(
39 handlerCap: handlerCap,
40 data: data,
41 timestamp: future,
42 priority: priority,
43 executionEffort: executionEffort,
44 fees: <-fees
45 )
46
47 log("Loop transaction id: ".concat(receipt.id.toString()).concat(" at ").concat(receipt.timestamp.toString()))
48
49 destroy receipt
50 }
51 }
52 access(self) var count: Int
53
54 // Event to be emitted when the counter is incremented
55 access(all) event CounterIncremented(newCount: Int)
56
57 // Event to be emitted when the counter is decremented
58 access(all) event CounterDecremented(newCount: Int)
59
60 init() {
61 self.count = 0
62
63 let future = getCurrentBlock().timestamp + 1.0
64 let priority = FlowTransactionScheduler.Priority.Low
65 let executionEffort: UInt64 = 1000
66 let estimate = FlowTransactionScheduler.estimate(
67 data: "",
68 timestamp: future,
69 priority: priority,
70 executionEffort: executionEffort
71 )
72
73 assert(
74 estimate.timestamp != nil || priority == FlowTransactionScheduler.Priority.Low,
75 message: estimate.error ?? "estimation failed"
76 )
77
78 if Counter.account.storage.borrow<&AnyResource>(from: /storage/Counter) == nil {
79 let handler <- Counter.createHandler()
80 Counter.account.storage.save(<-handler, to: /storage/Counter)
81 }
82 // Withdraw FLOW fees from this contract's account vault
83 let vaultRef = Counter.account.storage
84 .borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
85 ?? panic("missing FlowToken vault on contract account")
86 let fees <- vaultRef.withdraw(amount: estimate.flowFee ?? 0.0) as! @FlowToken.Vault
87 let handlerCap = Counter.account.capabilities.storage
88 .issue<auth(FlowTransactionScheduler.Execute) &{FlowTransactionScheduler.TransactionHandler}>(/storage/Counter)
89
90 let receipt <- FlowTransactionScheduler.schedule(
91 handlerCap: handlerCap,
92 data: "",
93 timestamp: future,
94 priority: priority,
95 executionEffort: executionEffort,
96 fees: <-fees
97 )
98
99 destroy receipt
100 }
101
102 // Public function to increment the counter
103 access(self) fun increment() {
104 self.count = self.count + 1
105 emit CounterIncremented(newCount: self.count)
106 }
107
108 // Public function to decrement the counter
109 access(self) fun decrement() {
110 self.count = self.count - 1
111 emit CounterDecremented(newCount: self.count)
112 }
113
114 // Public function to get the current count
115 view access(all) fun getCount(): Int {
116 return self.count
117 }
118
119 access(self) fun createHandler(): @Counter.Handler {
120 return <-create Handler()
121 }
122
123}