-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_state_delete.c
More file actions
112 lines (89 loc) · 3.2 KB
/
basic_state_delete.c
File metadata and controls
112 lines (89 loc) · 3.2 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
102
103
104
105
106
107
108
109
110
111
112
//**************************************************************
// Xahau Hook 101 Example ~ Basic State Delete Hook
// Author: @Handy_4ndy
//
// Description:
// Allows the owner to add or delete notes on-chain. Only the hook owner can invoke this hook.
//
// Triggers:
// ttINVOKE (Invoke transactions to the hook account)
//
// Parameters:
// 'VAL' (bytes): The note to add. If present, a new note is stored.
// 'DEL' (8 bytes): The note number to delete. If present, deletes the note with this number.
// 'CNT' (8 bytes, state): Counter for the number of notes stored (managed by the hook).
//
// Usage:
// - To add a note: Send an Invoke transaction with 'VAL' parameter.
// - To delete a note: Send an Invoke transaction with 'DEL' parameter (8-byte note number).
// - Only the hook owner can invoke this hook.
//
// Accepts:
// - Invoke transactions from the hook owner with 'VAL' parameter to add a note.
// - Invoke transactions from the hook owner with 'DEL' parameter to delete a note.
//
// Rejects:
// - Transactions not from the hook owner.
// - Non-Invoke transactions.
// - Invoke transactions without valid 'VAL' or 'DEL' parameters.
//**************************************************************
#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(5);
TRACESTR("BSM :: Basic State Management :: called");
uint8_t hook_acct[20];
hook_account(hook_acct, 20);
uint8_t otx_acc[20];
otxn_field(otx_acc, 20, sfAccount);
if (!BUFFER_EQUAL_20(hook_acct, otx_acc)) {
NOPE("Error: Only hook owner can invoke this hook");
}
int64_t tt = otxn_type();
if (tt != 99) {
NOPE("Error: Transaction must be an Invoke");
}
uint8_t val_buf[1024];
uint8_t val_key[3] = {'V', 'A', 'L'};
int64_t val_len = otxn_param(SBUF(val_buf), SBUF(val_key));
uint8_t del_buf[8];
uint8_t del_key[3] = {'D', 'E', 'L'};
int64_t del_len = otxn_param(SBUF(del_buf), SBUF(del_key));
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);
}
if (val_len > 0) {
uint8_t val_num_buf[8];
UINT64_TO_BUF(val_num_buf, count + 1);
if (state_set(val_buf, val_len, val_num_buf, 8) < 0) {
NOPE("Error: Could not set state");
}
count++;
UINT64_TO_BUF(count_buf, count);
if (state_set(SBUF(count_buf), SBUF(count_key)) < 0) {
NOPE("Error: Could not update count state");
}
DONE("Success: VAL added");
}
if (del_len == 8) {
if (state_set(0, 0, del_buf, 8) < 0) {
NOPE("Error: Could not delete VAL");
}
count--;
UINT64_TO_BUF(count_buf, count);
if (state_set(SBUF(count_buf), SBUF(count_key)) < 0) {
NOPE("Error: Could not update count state");
}
DONE("Success: VAL deleted");
}
NOPE("Error: No valid operation specified");
// Final guard
GUARD(1);
return 0;
}