Logo

dev-resources.site

for different kinds of informations.

JSHooks API compare to CHooks

Published at
9/9/2024
Categories
xrpl
hooks
javascript
Author
tequ
Categories
3 categories in total
xrpl
open
hooks
open
javascript
open
Author
4 person written this
tequ
open
JSHooks API compare to CHooks

This post describes some simple differences between Hooks API for JSHooks and CHooks that are not yet documented.

For more information on the Hook API, please see this document.

JSHooks is currently under development, so this information is subject to change.

Hook API Arguments

CHooks takes a pointer and length as arguments.
JSHooks takes a buffer (number[] or hex string) as an argument.

// c
uint8_t value[32];
uint8_t key[2] = {'O', 'P'};
state(value, 32, key, 2);
Enter fullscreen mode Exit fullscreen mode
// js
const key = "4F50"; // hex("OP")
state(key);

const key = [0x4F,0x50];
state(key);
Enter fullscreen mode Exit fullscreen mode

Return value of Hook API

CHooks returns the number of bytes written (positive value) or the error code (negative value) as the return value.
JSHooks returns mostly buffer(number[]) or error code (negative value) as the return value.

// c
const result = state(value, 32, key, 2);

// result >= 0: success and bytecode size written
// result < 0: errorCode
Enter fullscreen mode Exit fullscreen mode
// js
const result = state(key);

// typeof result !== 'number': success and value buffer
// typeof result === 'number': errorCode
Enter fullscreen mode Exit fullscreen mode

Available Hook APIs

trace_num

Not available in JSHooks, use trace instead

trace_float

Not available in JSHooks, use trace instead.

otxn_json

Only available in JSHooks, get the transaction that invoked the Hook in JSON format.

// js
const tx = otxn_json();
/**
{
  "TransactionType": "Payment",
  "Account": "rN7n7otQDd6FazitYQeuPecJ9Du1rEnDr5",
  "Destination": "rN7n7otQDd6FazitYQeuPecJ9Du1rEnDr5",
  "Amount": "1000000",
  "Fee": "10",
  "Sequence": 1,
  "LastLedgerSequence": 2,
  "SigningPubKey": "0330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32E5A33F85BE12B9C2A",
  "TxnSignature": "30450221008A2E4720667A4E64B87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D8"
}
*/
Enter fullscreen mode Exit fullscreen mode

sto_to_json

Only available in JSHooks, convert STObject in Buffer format to json format

// js
const sto = sto_to_json('2A664A653F6140000000000027108114934150EB9C9848F1526CA1BAABBF3BB8CF66743D');
/**
{
  "Account": "rNRc2S2GSefSkTkAiyjE6LDzMonpeHp6jS",
  "Amount": "10000",
  "Expiration": 1716151615
}
*/
Enter fullscreen mode Exit fullscreen mode

sto_from_json

Only available in JSHooks, convert STObject in JSON format to Buffer format

// js
const stobj = {
  Account: "rNRc2S2GSefSkTkAiyjE6LDzMonpeHp6jS",
  Amount: "10000",
  Expiration: 1716151615
}
const sto = sto_from_json(stobj);
// [0x2A, 0x66, 0x4A, 0x65, 0x3F, 0x61, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x10, 0x81, 0x14, 0x93, 0x41, 0x50, 0xEB, 0x9C, 0x98, 0x48, 0xF1, 0x52, 0x6C, 0xA1, 0xBA, 0xAB, 0xBF, 0x3B, 0xB8, 0xCF, 0x66, 0x74, 0x3D]
Enter fullscreen mode Exit fullscreen mode
hooks Article's
30 articles in total
Favicon
Hooks Behind The Scenes 3, UseRef!!!
Favicon
A Complete Guide to All React Hooks for Beginners
Favicon
Using useReducer for Complex State Logic
Favicon
Descobrindo o useCallback do React
Favicon
Discovering React’s useCallback
Favicon
Mastering Code Quality in Laravel: Pint with Git Hooks and Docker
Favicon
React: leveraging custom hooks to extract reusable logic
Favicon
Hooks Behind The Scenes 2, useState!!
Favicon
Mastering React Functional Components: Hooks, useEffect, and Lifecycle Explained
Favicon
JSHooks API compare to CHooks
Favicon
The Ultimate Guide to Wall Hanging Hooks and Display Systems for Your Home and Office
Favicon
How to Detect if an Element is in View with React
Favicon
React useQuery : A Complete Guide
Favicon
{useState} HooK { Briefly Explained};
Favicon
React HooK= { Briefly Explained};
Favicon
What is useState?
Favicon
Actions - React 19
Favicon
React controlled and uncontrolled hooks
Favicon
React Concepts: Hook Proximity
Favicon
Demystifying React's useState Hook: A Comprehensive Guide
Favicon
Mastering React Hooks: Best Practices for Efficient and Maintainable Code
Favicon
Designing React Hooks for Flexibility
Favicon
Unleashing the Power of React Custom Hooks
Favicon
Understanding Context Hooks in React: A Beginner's Guide
Favicon
3 Ways To Create Engaging React Loading Screens with Hooks
Favicon
Understanding the useInteractionTimer Hook: Measuring Component Interaction Time in React
Favicon
Cats Fatc's Quick project
Favicon
All About Custom Hooks: Supercharge Your React Components
Favicon
Suggestion for new syntax of React reducers + context
Favicon
How To Define Typescript onChange Event In React

Featured ones: