| Crates.io | hooks-rs |
| lib.rs | hooks-rs |
| version | 0.2.0 |
| created_at | 2023-10-13 13:58:33.226795+00 |
| updated_at | 2025-04-04 13:55:08.178495+00 |
| description | Hooks in Rust for Xahau |
| homepage | |
| repository | https://github.com/9oelm/hooks-rs/ |
| max_upload_size | |
| id | 1002328 |
| size | 481,906 |
Xahau hooks in Rust and CLI to build and deploy hooks.
[!WARNING] Only MacOS and Linux are supported for now. If you are on Windows, please use Docker (see below).
Before anything, install deno.
deno install --allow-all --allow-scripts --global jsr:@hooks-rs/cli --name hooks # install the CLI as "hooks"
hooks # see help
hooks up # install binary dependencies
hooks new hooks-example # init new project called "hooks-example"
cd hooks-example
hooks account # create a new account for deployment
hooks build # build hook
hooks deploy --hook-on INVOKE # deploy the hook
new <projectName> - Initializes a new hooks-rs template in a new folder in the current working
directory
up - Installs all prerequisite binaries
check - Checks if all prerequisite binaries are installed and available in PATH
account - Create a new testnet account stored in account.json
build - Compile and preprocess a hook
deploy - Build and deploy a hook to Xahau network
uninstall - Uninstall all prerequisite binaries installed by 'up' command.
test - Run tests for the project
This is the most basic accept.rs hook, and it looks like this:
#![no_std]
#![no_main]
use hooks_rs::*;
#[no_mangle]
pub extern "C" fn cbak(_: u32) -> i64 {
0
}
#[no_mangle]
pub extern "C" fn hook(_: u32) -> i64 {
// Every hook needs to import guard function
// and use it at least once
max_iter(1);
// Log it to the debug stream, which can be inspected on the website or via wss connection
let _ = trace(b"accept.rs: Called.", b"", DataRepr::AsUTF8);
// Accept all
accept(b"accept.rs: Finished.", line!().into());
}
Other examples are under /examples directory. Check them out!.
Most implementations are thoroughly documented. Please check out the book and crate docs.
Sometimes, it might be tricky to install the CLI right on your machine and run it because it installs several binaries in your system, which might sometimes go weird. In such a case, use 9oel/hooks-cli:latest to run the CLI without having to affect your local machine, like this:
docker run \
--init \
-p 1993:1993 \
-v $PWD:/app \
9oel/hooks-cli:latest hooks new my-project
cd my-project
docker run \
--init \
-p 1993:1993 \
-v $PWD:/app \
9oel/hooks-cli:latest hooks build
Most APIs are supported but yet to be tested. Contributions are welcome. The list below shows which features are tested and which ones are not.
Control
acceptrollbackEmitted transaction
etxn_burdenetxn_detailsetxn_fee_baseetxn_nonceetxn_reserveetxn_generationemitFloat
float_setfloat_multiplyfloat_mulratiofloat_negatefloat_comparefloat_sumfloat_stofloat_sto_setfloat_invertfloat_dividefloat_onefloat_exponentfloat_mantissafloat_signfloat_intfloat_rootfloat_logLedger
fee_baseledger_seqledger_last_hashledger_last_timeledger_nonceledger_keyletState
statestate_setstate_foreignstate_foreign_setTrace
tracetrace_numtrace_floatOriginating transaction
otxn_burdenotxn_fieldotxn_generationotxn_idotxn_typeotxn_slototxn_parammeta_slotUtilities
util_raddrutil_accidutil_verifyutil_sha512hutil_keyletHook context
hook_accounthook_hashhook_paramhook_param_sethook_skiphook_poshook_againSerialization
sto_subfield
sto_subarray
sto_emplace
sto_erase
sto_validate
Slot
slotslot_clearslot_countslot_setslot_sizeslot_subarrayslot_subfieldslot_typexpop_slotslot_floatThe latest header files can be found at https://github.com/XRPLF/hook-macros
trace API will be helpful. The debug stream is in the format of wss://xahau-test.net/debugstream/{r-address}. For example, wss://xahau-test.net/debugstream/rBDwqnirUMhMaYwzGCP1wqju51crjcBCVf.Due to an upstream dependency wasm-bindgen-test not being up to date with the latest Rust nightly, the Rust toolchain version must not go higher than nightly-2024-10-17.
There are still many parts of the C API that are not supported yet. The first priority for now would be to get all of them working.
If you have all checkboxes below ticked, you can proceed to create a PR:
/tests folder to see the existing integration test examples. Currently, each integration test would cover the smallest unit of testable 'chunk', because hooks is impossible to be tested without a live connection with an actual XRPL node, no matter if it is on local docker or is one of the testnet nodes. Therefore, an integration test is somewhat like an unit test but still connects to an actual XRPL node to run the hook to see if it actually works. Make sure the all of the state changes caused by the hook are correctly asserted, before and after the execution of the hook./examples folder. The example hook file should contain the most minimal example of how the feature that you have added can be used. Use this example as a documentation for the function as well, so that it can appear in the generated documentation.Remember that this repository is a novel combination of two amazing concepts: Rust and Hooks. And neither of these are easy nor familiar with the general audience. If you are not sure where to start, probably start with the book, which will give you some idea to start with.