-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_state_execution_limit.c
More file actions
74 lines (61 loc) · 2.12 KB
/
basic_state_execution_limit.c
File metadata and controls
74 lines (61 loc) · 2.12 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
//**************************************************************
// Xahau Hook 101 Example ~ Basic State Execution Limit Hook
// Author: @Handy_4ndy
//
// Description:
// Enforces an execution limit by decrementing a counter in state each time a Payment transaction is processed on the hook account. The counter starts at a hardcoded value of 5 and decreases by 1 for each Payment transaction. Once the counter reaches 0, further Payment transactions are rejected.
//
// Triggers:
// ttPAYMENT (incoming and outgoing payments)
//
// Parameters:
// None (counter is managed internally)
//
// Usage:
// - Deploy the hook to your account.
// - Each Payment transaction decrements the counter.
// - When the counter reaches 0, further Payment transactions are rejected.
//
// Accepts:
// - All non-Payment transactions.
// - Payment transactions while the counter is above 0.
//
// Rejects:
// - Payment transactions when the counter is 0 or below.
//**************************************************************
#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) {
GUARD(3);
TRACESTR("BasicStateLimit: called");
int64_t tt = otxn_type();
if (tt != ttPAYMENT) {
DONE("Info: Not a Payment transaction, accepting");
}
// Retrieve current count from state, default to 5 if not set
uint8_t count_buf[8];
uint8_t count_key[3] = {'C', 'N', 'T'};
uint64_t count = 5;
if (state(SBUF(count_buf), SBUF(count_key)) >= 0) {
count = UINT64_FROM_BUF(count_buf);
}
// Check if limit reached
if (count <= 0) {
NOPE("Error: Execution limit reached");
}
// Decrement the counter
count--;
UINT64_TO_BUF(count_buf, count);
// Store the new count in state
if (state_set(SBUF(count_buf), SBUF(count_key)) < 0) {
NOPE("Error: Could not update counter state");
}
TRACEVAR(count);
DONE("Success: Counter decremented for Payment transaction");
// Final guard
GUARD(1);
return 0;
}