-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_state_counter.c
More file actions
101 lines (77 loc) · 3.13 KB
/
basic_state_counter.c
File metadata and controls
101 lines (77 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//**************************************************************
// Xahau Hook 101 Example ~ Basic State Counter Hook
// Author: @Handy_4ndy
//
// Description:
// Increments a counter in state each time a Payment transaction is processed on the hook account. The counter is stored in hook state under the key 'CNT'. It counts all Payment transactions (incoming and outgoing). The hook owner can also manually update the counter via an Invoke transaction.
//
// Triggers:
// ttPAYMENT (increments counter)
// ttINVOKE (manual update by owner)
//
// Parameters:
// 'CNT' (8 bytes): The new value to set for the counter (for manual update).
//
// Usage:
// - Deploy the hook to your account.
// - Payment transactions increment the counter automatically.
// - To manually update the counter, send an Invoke transaction with 'CNT' parameter (only hook owner).
//
// Accepts:
// - Payment transactions (increments counter).
// - Invoke transactions from the hook owner with 'CNT' parameter (updates counter).
// - Other transaction types (accepts without action).
//
// Rejects:
// - Invoke transactions not from the hook owner.
// - Invoke transactions without valid 'CNT' parameter.
//**************************************************************
#include "hookapi.h"
#include <stdint.h>
#define DONE(x) accept(SBUF(x), __LINE__)
#define NOPE(x) rollback(SBUF(x), __LINE__)
#define GUARD(maxiter) _g(__LINE__, (maxiter)+1)
int64_t hook(uint32_t reserved) {
TRACESTR("BSU :: Basic State Update :: called");
uint8_t hook_acct[20];
hook_account(hook_acct, 20);
uint8_t otx_acc[20];
otxn_field(otx_acc, 20, sfAccount);
int64_t tt = otxn_type();
if (tt == 99) { // Invoke
if (!BUFFER_EQUAL_20(hook_acct, otx_acc)) {
NOPE("BSU :: Error :: Only hook owner can invoke this hook");
}
uint8_t cnt_buf[8];
uint8_t cnt_param[3] = {'C', 'N', 'T'};
int64_t cnt_len = otxn_param(SBUF(cnt_buf), SBUF(cnt_param));
if (cnt_len != 8) {
NOPE("BSU :: Error :: CNT parameter must be 8 bytes");
}
uint64_t new_count = UINT64_FROM_BUF(cnt_buf);
if (state_set(SBUF(cnt_buf), SBUF(cnt_param)) < 0) {
NOPE("BSU :: Error :: Could not update counter state on Invoke");
}
TRACESTR("BasicStateUpdate: Counter manually updated");
DONE("Success: Counter manually updated via Invoke");
} else if (tt == ttPAYMENT) {
// Increment counter on Payment transactions
uint8_t count_buf[8];
uint8_t count_key[3] = {'C', 'N', 'T'};
uint64_t count = 0;
if (state(SBUF(count_buf), SBUF(count_key)) >= 0) {
count = UINT64_FROM_BUF(count_buf);
}
count++;
UINT64_TO_BUF(count_buf, count);
if (state_set(SBUF(count_buf), SBUF(count_key)) < 0) {
NOPE("BSU :: Error :: Could not update counter state on Payment");
}
TRACEVAR(count);
DONE("BSU :: Success :: Counter incremented for Payment transaction");
} else {
DONE("BSU: Transaction type not handled, accepting");
}
_g(1,1); // Guard
return 0;
}