|
1 | | -import "@stdlib/common.tolk"; |
2 | | -import "../../lib/upgrades/type_and_version.tolk"; |
3 | | -import "../../lib/utils.tolk"; |
4 | | - |
5 | | -/// Counter contract (Tolk example) |
| 1 | +import "@stdlib/common.tolk" |
| 2 | +import "../../lib/upgrades/type_and_version.tolk" |
| 3 | +import "../../lib/utils.tolk" |
6 | 4 |
|
| 5 | +/// Counter contract + event emission (Tolk example) |
7 | 6 | /// Message to set the counter value. |
8 | 7 | struct (0x00000004) SetCount { |
9 | | - queryId: uint64; // Standard query_id field |
10 | | - newCount: uint32; // Argument for the operation |
| 8 | + queryId: uint64 // Standard query_id field |
| 9 | + newCount: uint32 // Argument for the operation |
11 | 10 | } |
12 | 11 |
|
13 | | -struct Storage { |
14 | | - id: uint32; |
15 | | - value: uint32; |
| 12 | +/// Message to increase the counter value. |
| 13 | +struct (0x10000005) IncreaseCount { |
| 14 | + queryId: uint64 // Standard query_id field |
| 15 | +} |
| 16 | + |
| 17 | +/// Event Topics |
| 18 | +const COUNT_SET_TOPIC = 0x0766fed0 // crc32("CountSet") |
| 19 | +const COUNT_INCREASED_TOPIC = 0x1947b328 // crc32("CountIncreased") |
| 20 | + |
| 21 | +/// Event emitted when the counter is set |
| 22 | +struct CountSet { |
| 23 | + id: uint32 |
| 24 | + value: uint32 |
16 | 25 | } |
17 | 26 |
|
18 | | -type IncomingMessage = SetCount; |
| 27 | +/// Event emitted when the counter is increased |
| 28 | +struct CountIncreased { |
| 29 | + id: uint32 |
| 30 | + value: uint32 |
| 31 | +} |
19 | 32 |
|
20 | | -fun loadData() { |
| 33 | +struct Storage { |
| 34 | + id: uint32 |
| 35 | + value: uint32 |
| 36 | +} |
| 37 | + |
| 38 | +fun Storage.load(): Storage { |
21 | 39 | return Storage.fromCell(contract.getData()); |
22 | 40 | } |
23 | 41 |
|
24 | | -fun saveData(data: Storage) { |
25 | | - contract.setData(data.toCell()); |
| 42 | +fun Storage.store(self) { |
| 43 | + return contract.setData(self.toCell()); |
26 | 44 | } |
27 | 45 |
|
| 46 | +type Msg = SetCount | IncreaseCount |
| 47 | + |
28 | 48 | fun onInternalMessage(in: InMessage) { |
29 | | - val msg = lazy IncomingMessage.fromSlice(in.body); // 63 error code is thrown if the message opcode is unknown |
| 49 | + val msg = lazy Msg.fromSlice(in.body); // 63 error code is thrown if the message opcode is unknown |
30 | 50 | match (msg) { |
31 | 51 | SetCount => { |
32 | | - /// Instructs the contract to step the counter. |
33 | | - var storage = loadData(); |
34 | | - storage.value = msg.newCount; |
35 | | - saveData(storage); |
| 52 | + /// Instructs the contract to set the counter. |
| 53 | + var st = lazy Storage.load(); |
| 54 | + st.value = msg.newCount; |
| 55 | + st.store(); |
| 56 | + emit<CountSet>(COUNT_SET_TOPIC, { id: st.id, value: st.value }); |
| 57 | + } |
| 58 | + IncreaseCount => { |
| 59 | + /// Instructs the contract to increase the counter. |
| 60 | + var st = lazy Storage.load(); |
| 61 | + st.value = st.value + 1; |
| 62 | + st.store(); |
| 63 | + emit<CountIncreased>(COUNT_INCREASED_TOPIC, { id: st.id, value: st.value }); |
36 | 64 | } |
37 | 65 | else => { |
38 | 66 | // ignore empty messages, "wrong opcode" for others |
39 | | - assert (in.body.isEmpty()) throw 0xFFFF |
| 67 | + assert (in.body.isEmpty()) throw 0xFFFF; |
40 | 68 | } |
41 | 69 | } |
42 | 70 | } |
43 | 71 |
|
44 | | -/// Gets the current counter value. |
45 | | -get fun value(): int { |
46 | | - val storage = loadData(); |
47 | | - return storage.value; |
48 | | -} |
49 | | - |
50 | 72 | /// Gets the current id of the contract. |
51 | 73 | get fun id(): int { |
52 | | - val storage = loadData(); |
| 74 | + val storage = Storage.load(); |
53 | 75 | return storage.id; |
54 | 76 | } |
55 | 77 |
|
| 78 | +/// Gets the current counter value. |
| 79 | +get fun value(): int { |
| 80 | + val storage = Storage.load(); |
| 81 | + return storage.value; |
| 82 | +} |
| 83 | + |
56 | 84 | /// Gets the current type and version of the contract. |
57 | 85 | get fun typeAndVersion(): (slice, slice) { |
58 | | - return TypeAndVersion { |
59 | | - typeStr: "com.chainlink.ton.examples.Counter", |
60 | | - versionStr: "1.0.0", |
61 | | - }.typeAndVersion(); |
| 86 | + return TypeAndVersion { typeStr: "com.chainlink.ton.examples.Counter", versionStr: "1.0.0" } |
| 87 | + .typeAndVersion(); |
62 | 88 | } |
0 commit comments