-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_state_add.c
More file actions
76 lines (60 loc) · 2.11 KB
/
basic_state_add.c
File metadata and controls
76 lines (60 loc) · 2.11 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
//**************************************************************
// Xahau Hook 101 Example ~ Basic State Add Hook
// Author: @Handy_4ndy
//
// Description:
// Allows the owner to add custom state on-chain. Only the hook owner can invoke this hook.
//
// Triggers:
// ttINVOKE (Invoke transactions to the hook account)
//
// Parameters:
// 'KEY' (bytes): The custom key for the state.
// 'VAL' (bytes): The value to store for the key.
//
// Usage:
// - Send an Invoke transaction with 'KEY' and 'VAL' parameters to add state.
// - Only the hook owner can invoke this hook.
// - Use hex strings for parameters (see: https://transia-rnd.github.io/xrpl-hex-visualizer/).
//
// Accepts:
// - Invoke transactions from the hook owner with valid 'KEY' and 'VAL' parameters.
//
// Rejects:
// - Transactions not from the hook owner.
// - Invoke transactions without valid 'KEY' or 'VAL' 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) {
TRACESTR("BasicStateAdd: 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 key_buf[256];
uint8_t key_param[3] = {'K', 'E', 'Y'};
int64_t key_len = otxn_param(SBUF(key_buf), SBUF(key_param));
uint8_t val_buf[1024];
uint8_t val_param[3] = {'V', 'A', 'L'};
int64_t val_len = otxn_param(SBUF(val_buf), SBUF(val_param));
if (key_len <= 0 || val_len <= 0) {
NOPE("Error: Both KEY and VAL parameters are required");
}
if (state_set(val_buf, val_len, key_buf, key_len) < 0) {
NOPE("Error: Could not set state");
}
DONE("Success: State added");
_g(1, 1); // Guard
return 0;
}