DEV Community

tequ
tequ

Posted on

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

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (1)

Collapse
 
xrpl365 profile image
Chris Dangerfield β€’

Nice πŸ‘
Loving how true to JS the implementation is…

Image of Timescale

PostgreSQL for Agentic AI β€” Build Autonomous Apps on One Stack 1️⃣

pgai turns PostgreSQL into an AI-native database for building RAG pipelines and intelligent agents. Run vector search, embeddings, and LLMsβ€”all in SQL

Build Today

πŸ‘‹ Kindness is contagious

If this article connected with you, consider tapping ❀️ or leaving a brief comment to share your thoughts!

Okay