Node.js SDK
This SDK allows you to interact with UTxORPC compatible nodes, enabling operations such as fetching blocks, submitting transactions, and syncing with the latest blocks.
Prerequisites
Before you begin, ensure you have the following installed:
- Node.js: Version 20.0 or higher.
- npm: The Node.js package manager.
Installation
To install the UTxORPC Node.js SDK in your project, use the following command:
npm install @utxorpc/sdk --save
Overview
The SDK provides three primary clients for interacting with UTxORPC:
- CardanoSyncClient: Provides an interface for synchronizing chain data, fetching blocks, and tracking the tip of the blockchain.
- CardanoQueryClient: Provides an interface for submitting transactions and monitoring their progress through various stages in their lifecycle.
- CardanoSubmitClient: Provides an interface for querying the state of the ledger with the main goal of constructing 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
The CardanoSyncClient
allows you to synchronize with the blockchain, follow the tip, and fetch specific blocks.
Example: Following the Tip
import { CardanoSyncClient } from "@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 "@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
The CardanoQueryClient
allows you to query blockchain data, including reading protocol parameters and searching UTXOs.
Example: Reading Blockchain Parameters
import { CardanoQueryClient } from "@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 "@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
The CardanoSubmitClient
is used to submit transactions to the blockchain.
Example: Submitting a Transaction
import { CardanoSubmitClient } from "@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
This SDK is designed to streamline the process of interacting with UTxORPC using Node.js. Whether you're syncing with the latest blockchain data, querying UTXOs, or submitting transactions, this SDK provides the necessary tools to build robust blockchain applications with ease.