Deno

Deno with Node.js SDK

The UTxORPC SDK, an npm package for interacting with UTxORPC-compatible nodes, is also compatible with Deno thanks to the latest 'Deno 2' update. This allows you to fetch blocks, submit transactions, and synchronize with the latest blocks seamlessly using Deno's now native npm package support.

Prerequisites

Ensure you have Deno 2.0 or higher installed. Deno 2 is compatible with Node.js and npm packages, which allows you to use the @utxorpc/sdk package directly in your Deno project.

To install Deno, refer to the official Deno installation guide (opens in a new tab).

Project Setup

You can set up a new Deno project with the deno init command, which generates a deno.json configuration file to manage dependencies and settings.

deno init

After running this command, you’ll see a deno.json file in your project directory.

Installation with deno add

With Deno 2, you can use deno add to install npm packages without needing a package.json or node_modules folder. To install the UTxORPC SDK, run:

deno add npm:@utxorpc/sdk

Running this command will add @utxorpc/sdk to your project and update deno.json with an entry for @utxorpc/sdk as a dependency.

Manual Installation

In Deno 2, you can also use npm packages without a package.json or node_modules folder as long as you used deno init . Instead, you can directly specify the package using the npm: prefix and it will automatically do everything for you even without the import in your deno.json.

import { CardanoSyncClient, CardanoQueryClient, CardanoSubmitClient } from "npm:@utxorpc/sdk";

If you'd prefer to use a configuration file to manage dependencies, add an npm: specifier in your deno.json configuration if you manually added it without using deno add and even when using this command it should also look like this:

// deno.json
{
  "imports": {
    "@utxorpc/sdk": "npm:@utxorpc/sdk"
  }
}

Then import as follows:

import { CardanoSyncClient, CardanoQueryClient, CardanoSubmitClient } from "@utxorpc/sdk";

Overview

The SDK includes three primary clients for interacting with UTxORPC nodes:

  1. CardanoSyncClient: Synchronize chain data, fetch blocks, and track blockchain tips.
  2. CardanoQueryClient: Submit transactions and monitor their progress.
  3. CardanoSubmitClient: Query the ledger state and construct new transactions.

Usage

The following code samples assume that the UTxORPC node is running locally on localhost:50051. If your node is hosted remotely or on a different server, replace "http://localhost:50051" with the appropriate server URL and port for your environment.

For more details on configuring your node, refer to the UTxORPC Ecosystem Servers Documentation.

Below are examples of how to use each of the clients in the SDK.

1. CardanoSyncClient

CardanoSyncClient helps you follow the tip and fetch specific blocks.

Example: Following the Tip

import { CardanoSyncClient } from "npm:@utxorpc/sdk";
 
async function followTipExample() {
  const syncClient = new CardanoSyncClient({
    uri: "http://localhost:50051"
  });
 
  const tip = syncClient.followTip([
    { slot: 54131816, hash: "34c65aba4b299113a488b74e2efe3a3dd272d25b470d25f374b2c693d4386535" },
  ]);
 
  for await (const event of tip) {
    console.log(event);
  }
}
 
followTipExample().catch(console.error);

Example: Fetching a Block

import { CardanoSyncClient } from "npm:@utxorpc/sdk";
 
async function fetchBlockExample() {
  const syncClient = new CardanoSyncClient({
    uri: "http://localhost:50051"
  });
 
  const block = await syncClient.fetchBlock({
    slot: 54131816,
    hash: "34c65aba4b299113a488b74e2efe3a3dd272d25b470d25f374b2c693d4386535",
  });
 
  console.log(block);
}
 
fetchBlockExample().catch(console.error);

2. CardanoQueryClient

CardanoQueryClient lets you query blockchain data, read protocol parameters, and search UTXOs.

Example: Reading Blockchain Parameters

import { CardanoQueryClient } from "npm:@utxorpc/sdk";
 
async function readParamsExample() {
  const queryClient = new CardanoQueryClient({
    uri: "http://localhost:50051"
  });
 
  const params = await queryClient.readParams();
  console.log(params);
}
 
readParamsExample().catch(console.error);

Example: Searching UTXOs by Address

import { CardanoQueryClient } from "npm:@utxorpc/sdk";
 
async function searchUtxosByAddressExample() {
  const queryClient = new CardanoQueryClient({
    uri: "http://localhost:50051"
  });
 
  const utxos = await queryClient.searchUtxosByAddress(
    Buffer.from("705c87cbca3a88cbfee6f6ad820acea99f484b4830fc632610f2a30146", "hex")
  );
 
  utxos.forEach((utxo) => {
    console.log(utxo);
  });
}
 
searchUtxosByAddressExample().catch(console.error);

3. CardanoSubmitClient

CardanoSubmitClient allows transaction submission.

Example: Submitting a Transaction

import { CardanoSubmitClient } from "npm:@utxorpc/sdk";
 
async function submitTxExample() {
  const submitClient = new CardanoSubmitClient({
    uri: "http://localhost:50051"
  });
 
  const txSample = "84a300d90102818258203dc5d9977e7b3d51acaea81031d2f461404536b2828549b73876a5980295f81b00018282581d60916c769efc6e2a3339594818a1d0c3998c29e3a6303d8711de8567591a004c4b4082581d60916c769efc6e2a3339594818a1d0c3998c29e3a6303d8711de8567591b0000000253bcffb3021a0002990da100d9010281825820526fa19e3694cda4f3c0d2fb2d2bb8768925eccc49a89d5f12b1972644ac769858403d6d6599193b17e67827cd9f48aaf35ac762c6fb0c5402c52724f307b69ff96f3f7e6c3fb107670c28679c148bf510f479c01a34b9d95d0dbb7e4ff6f3cb560af5f6";
  const txHash = await submitClient.submitTx(Buffer.from(txSample, "hex"));
 
  const txHashHex = Buffer.from(txHash).toString("hex");
  console.log(txHashHex);
}
 
submitTxExample().catch(console.error);

Conclusion

The NodeJS UTxORPC SDK which is compatible with the Deno runtime allows seamless integration with UTxORPC nodes. With Deno’s compatibility with npm, you can access @utxorpc/sdk easily and use its tools for building robust blockchain applications in a secure and modern environment while utilizing Deno's wide variety of tools.