JS Node

Run and edit TypeScript code on-the-fly with Deno runtime.

Add a node

Upload your node with "Add Deno node" button in /dashboard/nodes page, need 2 files:

  • Node definition (.json)

  • Source code

Documentation

Code requirements

Your code MUST export a default class that implements CommandTrait. The class name doesn't matter.

CommandTrait is defined in @space-operator/flow-lib, it has 3 methods, one is required:

export interface CommandTrait {
  /**
   * Deserialize each inputs from `Value` to the type of your choice.
   * This function will be called before passing inputs to `run()`.
   * If not implemented, `Value.toJSObject()` will be called for each inputs.
   */
  deserializeInputs?(inputs: Record<string, Value>): Record<string, any>;
  /**
   * Serialize each output to a `Value`.
   * This function will be called after each `run()`.
   * If not implemented, `new Value(output)` will be called for each outputs.
   */
  serializeOutputs?(outputs: Record<string, any>): Record<string, Value>;
  /**
   * This function will be called every time the command is run.
   * @param ctx Context
   * @param params Map of input_name => input_value.
   */
  run(ctx: Context, params: Record<string, any>): Promise<Record<string, any>>;
}

run() will be called every time the node is run.

Input parameters:

  • ctx: Context: context object contains information about the current invocation, as well as services available to the node. See documentation.

  • params: Record<string, any> : Input values, this is a map of input_name => value

Return type: run() should return a map of output_name => output_value on success, and throw an exception on errors.

Using BaseCommand class

We also provide a BaseCommand class that will convert input and output values based on node definition. All you have to do is extends it:

Using libraries

We do not support import maps yet, therefore your code must use npm: or jsr: when importing libraries.

Read more: npm: specifier

Example node - node that performs a simple addition

Source code template:

Use params to access input values.

Node definition template

Example with a signature request from a wallet

Option 1 - Bundled node

Bundling a node allows you to combine the instructions with other nodes to create a single transaction. All nodes in a flow will be bundled and the user will sign one for one transaction. The flow will build the message and submit the transaction.

Note, when bundling a node, you must update the Node Definition to specify if and how a command would output Solana instructions, and the order with which it will return its outputs:

  • before: list of output names returned before instructions are sent.

  • signature: name of the signature's output port.

  • after: list of output names returned after instructions are sent.

Option 2 - Plain TypeScript Node

You can manually build a message, request signature, and submit a transaction. The flow will not collect the instructions from this node and will execute it as an independent node.

Access Solana client and requestSignature method through the Context. https://jsr.io/@space-operator/flow-lib/doc/~/Context

Packages

How it works

When running the node, the backend will import your class and run it like this:

Last updated

Was this helpful?