npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@meshsdk/hydra

v1.9.0-beta.99

Published

Mesh Hydra package

Readme

@meshsdk/hydra

A powerful TypeScript SDK for interacting with Hydra Heads on Cardano.

npm version Documentation

Overview

Hydra is a layer 2 protocol for Cardano that enables fast, low-cost transactions by creating off-chain "heads" where multiple parties can interact. This package provides a complete SDK for:

  • Connecting to and managing Hydra Heads
  • Committing and decommitting funds
  • Building and submitting transactions within Hydra Heads
  • Handling all Hydra protocol operations (init, abort, close, contest, fanout)
  • Event-driven architecture for real-time updates

Installation

npm install @meshsdk/hydra

Quick Start

Basic Setup

import { HydraProvider, HydraInstance } from "@meshsdk/hydra";
import { BlockfrostProvider } from "@meshsdk/core";

const hydraProvider = new HydraProvider({
  httpUrl: "http://localhost:4001", // Your Hydra node URL
});

await hydraProvider.connect();

const blockchainProvider = new BlockfrostProvider("YOUR_BLOCKFROST_KEY");

const hydraInstance = new HydraInstance({
  provider: hydraProvider,
  fetcher: blockchainProvider,
  submitter: blockchainProvider,
});

Initialize a Head

await hydraProvider.init();

// Wait for all parties to commit
// Once all parties have committed, the head will open automatically

Commit Funds

const emptyCommitTx = await hydraInstance.commitEmpty();
await wallet.submitTx(emptyCommitTx);

Submit Transactions in Hydra Head

const txHash = await hydraProvider.newTx({
  type: "Tx ConwayEra",
  cborHex: unsignedTransactionCbor,
});

API Reference

HydraProvider

The main class for connecting to and managing Hydra Heads.

Constructor

new HydraProvider({
  httpUrl: string;       
  wsUrl?: string;      
  history?: boolean;     
  address?: string;      
})

Methods

Connection Management
  • connect() - Establishes connection to the Hydra Head
  • disconnect(timeout?: number) - Disconnects from the Hydra Head (default timeout: 5 minutes)
Head Lifecycle Commands
  • init() - Initializes a new Hydra Head (only when status is Idle)
  • abort() - Aborts a head before it opens (only during Initializing phase)
  • close() - Closes an open head, starting the contest period
  • contest() - Contests a closed head if needed
  • fanout() - Finalizes the head closure and distributes funds
Transaction Operations
  • newTx(transaction: hydraTransaction) - Submits a transaction to the open head
  • recover(txHash: string) - Recovers a deposit transaction by its hash
Data Fetching (implements IFetcher)
  • fetchUTxOs(address?: string) - Fetches UTxOs from the Hydra Head
  • fetchProtocolParameters() - Fetches protocol parameters
Transaction Submission (implements ISubmitter)
  • submitTx(tx: string) - Submits a transaction to the Hydra Head
Event Handling
  • onStatusChange(event: string, callback: Function) - Listen to Hydra events
  • onMessage(callback: Function) - Register a callback for all messages

Events

The provider emits various events:

  • Greetings - Initial connection message with head status
  • HeadIsInitializing - Head initialization started
  • HeadIsOpen - Head is now open and ready
  • HeadIsClosed - Head has been closed
  • HeadIsAborted - Head was aborted
  • TxValid - Transaction was accepted
  • TxInvalid - Transaction was rejected
  • Snapshot - New snapshot created
  • Commit - New commit received
  • Decommit - Decommit occurred
  • And more...

HydraInstance

A higher-level interface for interacting with Hydra Heads, providing convenient methods for common operations.

Constructor

new HydraInstance({
  provider: HydraProvider;
  fetcher: 'blockchainProvider';       
  submitter: 'blockchainProvider';   
})

Methods

Commit Operations
  • commitEmpty() - Creates an empty commit transaction (no funds)
  • commitFunds(txHash: string, outputIndex: number) - Commits a specific UTxO to the head
  • commitBlueprint(txHash: string, outputIndex: number, transaction: hydraTransaction) - Commits a UTxO as a blueprint transaction
Decommit Operations
  • decommit() - Decommits all your funds from the head

Examples

Complete Workflow Example

import { HydraProvider, HydraInstance } from "@meshsdk/hydra";
import { BlockfrostProvider, MeshTxBuilder, MeshWallet } from "@meshsdk/core";

// Setup providers
const hydraProvider = new HydraProvider({
  httpUrl: "http://localhost:4001",
});

const blockchainProvider = new BlockfrostProvider("YOUR_BLOCKFROST_KEY");

const hydraInstance = new HydraInstance({
  provider: hydraProvider,
  fetcher: blockchainProvider,
  submitter: blockchainProvider,
});

await hydraProvider.connect();
await hydraProvider.init();

const commitTx = await hydraInstance.commitFunds(txHash, outputIndex);
await wallet.submitTx(commitTx);

hydraProvider.onMessage("HeadIsOpen", async () => {
  const txBuilder = new MeshTxBuilder({
    fetcher: blockchainProvider,
    submitter: blockchainProvider,
    params: await hydraProvider.fetchProtocolParameters(),
    isHydra: true, 
  });

  // Add your transaction logic
  const unsignedTx = await txBuilder
    .sendLovelace(address, "1000000")
    .complete();

  const txHash = await hydraProvider.newTx({
    type: "Tx ConwayEra",
    cborHex: unsignedTx,
  });

  console.log("Transaction submitted:", txHash);
});

// When done, close the head
await hydraProvider.close();
await hydraProvider.fanout();

Event-Driven Example

const hydraProvider = new HydraProvider({
  httpUrl: "http://localhost:4001",
});

await hydraProvider.connect();

hydraProvider.onMessage((message) => {
  switch (message.tag) {
    case "Greetings":
      console.log("Connected! Head status:", message.headStatus);
      break;
    case "HeadIsOpen":
      console.log("Head is now open!");
      break;
    case "TxValid":
      console.log("Transaction accepted:", message.transactionId);
      break;
    case "TxInvalid":
      console.error("Transaction rejected:", message.validationError);
      break;
    case "Snapshot":
      console.log("New snapshot:", message.snapshot);
      break;
  }
});

Requirements

  • Node.js 16+ or modern browser with ES module support
  • Access to a Hydra node (local or remote)
  • Mesh SDK peer dependencies installed

Documentation

For more detailed documentation, examples, and guides, visit:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the terms specified in the LICENSE file.

Support

For support and questions:

  • Check the documentation
  • Open an issue on GitHub
  • Visit the Mesh community forums