Skip to the content.

API Reference

Complete API reference for Hydra-Yaci, including WebSocket API, example scripts, and NPM commands.

Table of Contents

NPM Scripts API

Prerequisites and Setup

npm run prerequisites

Check system requirements and prerequisites.

Usage:

npm run prerequisites

Checks:

Exit Codes:


npm run setup

Setup project directories and download Hydra binaries.

Usage:

npm run setup

Actions:


Key Management

npm run keys:generate

Generate cryptographic keys for all participants.

Usage:

npm run keys:generate

Generates:

Output Location:


Wallet Management

npm run wallets:fund

Fund participant wallets using Yaci DevKit.

Usage:

npm run wallets:fund

Amount:

Requirements:


Hydra Node Management

npm run hydra:start

Start all Hydra nodes (Alice, Bob, Carol).

Usage:

npm run hydra:start

Starts:

Requirements:


npm run hydra:stop

Stop all Hydra nodes.

Usage:

npm run hydra:stop

Actions:


Maintenance

npm run reset

Reset all state and keys.

Usage:

npm run reset

Warning: This deletes all generated keys and state!

Deletes:


Examples

npm run example:status

Check Yaci DevKit status and blockchain information.

Usage:

npm run example:status

Output:


npm run example:addresses

Generate and display participant addresses.

Usage:

npm run example:addresses

Output:


npm run example:fund

Fund wallets from Yaci faucet.

Usage:

npm run example:fund

Amount:


npm run example:open

Open a Hydra head.

Usage:

npm run example:open

Actions:

  1. Connects to Hydra nodes
  2. Sends Init message
  3. Waits for initialization
  4. Returns head ID

npm run example:workflow

Run complete Hydra workflow.

Usage:

npm run example:workflow

Workflow:

  1. Check Yaci status
  2. Generate addresses
  3. Fund wallets
  4. Open Hydra head
  5. Commit funds
  6. Send test payment
  7. Close head

WebSocket API

Connection

Connect to a Hydra node’s WebSocket API:

const ws = new WebSocket('ws://localhost:4001');

Endpoints:

Client Messages (Client → Server)

Init

Initialize a new Hydra head.

{
  "tag": "Init"
}

Requirements:

Response: HeadIsInitializing


Commit

Commit UTxO to the head.

{
  "tag": "Commit",
  "utxo": {
    "<tx_id>#<index>": {
      "address": "addr_test1...",
      "value": {
        "lovelace": 1000000
      }
    }
  }
}

Requirements:

Response: Committed


NewTx

Submit a new transaction to the head.

{
  "tag": "NewTx",
  "transaction": {
    "type": "Tx ConwayEra",
    "description": "",
    "cborHex": "84a500818258..."
  }
}

Requirements:

Response: TxValid or TxInvalid


Close

Close the Hydra head.

{
  "tag": "Close"
}

Requirements:

Response: HeadIsClosed


Abort

Abort head initialization.

{
  "tag": "Abort"
}

Requirements:

Response: HeadIsAborted


Server Messages (Server → Client)

Greetings

First message when connecting.

{
  "tag": "Greetings",
  "me": {
    "vkey": "a5c2e70fe94108c2cb1c559942f0152525ae2350f6dc7ce631b3c5313aff7932"
  },
  "headStatus": "Idle",
  "hydraNodeVersion": "1.2.0",
  "hydraHeadId": null,
  "snapshotUtxo": {},
  "networkInfo": {
    "networkConnected": true,
    "peersInfo": {
      "host.docker.internal:5001": true,
      "host.docker.internal:5002": true
    }
  }
}

HeadIsInitializing

Head initialization started.

{
  "tag": "HeadIsInitializing",
  "headId": "4ea01a230b166e43748e28b9ecc729dcfca925038be66d7f9a3238b8",
  "parties": [
    {"vkey": "a2a080fd6497ffba122fc8450b74e0732ae760952d52d3be144469472ee231fc"},
    {"vkey": "8f6f9e73f34cf65a8471c76f50aacafae66401a6fab3bd918fc67a7de8b43df1"},
    {"vkey": "a5c2e70fe94108c2cb1c559942f0152525ae2350f6dc7ce631b3c5313aff7932"}
  ]
}

Committed

Participant committed funds.

{
  "tag": "Committed",
  "party": {
    "vkey": "a5c2e70fe94108c2cb1c559942f0152525ae2350f6dc7ce631b3c5313aff7932"
  },
  "utxo": {
    "<tx_id>#<index>": {
      "address": "addr_test1...",
      "value": {"lovelace": 1000000}
    }
  }
}

HeadIsOpen

Head successfully opened.

{
  "tag": "HeadIsOpen",
  "utxo": {
    "<tx_id>#<index>": {
      "address": "addr_test1...",
      "value": {"lovelace": 3000000}
    }
  }
}

TxValid

Transaction confirmed valid.

{
  "tag": "TxValid",
  "transaction": {
    "type": "Tx ConwayEra",
    "cborHex": "..."
  },
  "utxo": {...}
}

TxInvalid

Transaction rejected.

{
  "tag": "TxInvalid",
  "transaction": {...},
  "validationError": {
    "reason": "MissingInput",
    "utxo": "<tx_id>#<index>"
  }
}

SnapshotConfirmed

Snapshot confirmed by all parties.

{
  "tag": "SnapshotConfirmed",
  "snapshot": {
    "snapshotNumber": 1,
    "utxo": {...},
    "confirmedTransactions": [...]
  }
}

HeadIsClosed

Head closed on-chain.

{
  "tag": "HeadIsClosed",
  "snapshotNumber": 5,
  "contestationDeadline": "2024-01-15T12:00:00Z"
}

HeadIsFinalized

Head finalized and funds distributed.

{
  "tag": "HeadIsFinalized",
  "utxo": {
    "<tx_id>#<index>": {
      "address": "addr_test1...",
      "value": {"lovelace": 1500000}
    }
  }
}

Example Scripts

check-yaci.js

Check Yaci DevKit status.

Usage:

node examples/check-yaci.js

Output:


generate-address.js

Generate Cardano addresses from keys.

Usage:

node examples/generate-address.js

Output:


fund-from-faucet.js

Fund wallets from Yaci faucet.

Usage:

node examples/fund-from-faucet.js

Parameters:


open-hydra-head.js

Open a Hydra head.

Usage:

node examples/open-hydra-head.js

Flow:

  1. Connect to all nodes
  2. Send Init
  3. Wait for HeadIsInitializing
  4. Display head ID

commit-fund.js

Commit funds to head.

Usage:

node examples/commit-fund.js

Parameters:


send-payment.js

Send payment within Hydra head.

Usage:

node examples/send-payment.js

Parameters:


close-head.js

Close the Hydra head.

Usage:

node examples/close-head.js

Flow:

  1. Connect to node
  2. Send Close
  3. Wait for HeadIsClosed
  4. Wait for HeadIsFinalized

REST API (Yaci DevKit)

Base URL

http://localhost:8080/api/v1

Endpoints

GET /epochs/latest

Get latest epoch information.

Response:

{
  "epoch": 0,
  "blkCount": 45,
  "txCount": 0,
  "startTime": 1704364800,
  "endTime": null
}

GET /blocks/latest

Get latest block.

Response:

{
  "number": 45,
  "hash": "abc123...",
  "time": 1704364845,
  "epoch": 0,
  "slot": 900
}

GET /addresses/{address}

Get address information and balance.

Response:

{
  "address": "addr_test1...",
  "balance": {
    "lovelace": 1000000000
  },
  "utxos": [...]
}

POST /transactions

Submit a transaction.

Request:

{
  "cborHex": "84a500818258..."
}

Response:

{
  "txHash": "def456..."
}

Lucid Evolution API

Initialize Lucid

import { Lucid } from '@lucid-evolution/lucid';
import { Blockfrost } from '@lucid-evolution/provider';

const lucid = await Lucid.new(
  new Blockfrost('http://localhost:8080/api/v1'),
  'Custom'
);

Build Transaction

const tx = await lucid
  .newTx()
  .payToAddress('addr_test1...', { lovelace: 1000000n })
  .complete();

const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();

Query UTxOs

const utxos = await lucid.utxosAt('addr_test1...');

Get Wallet Address

const address = await lucid.wallet.address();

Monitoring API

Prometheus Metrics

Endpoints:

Key Metrics

hydra_head_status

Current head status (0=Idle, 1=Initializing, 2=Open, 3=Closed).

hydra_head_status 2.0

hydra_snapshot_number

Current snapshot number.

hydra_snapshot_number 5.0

hydra_tx_confirmation_time_seconds

Transaction confirmation time.

hydra_tx_confirmation_time_seconds_bucket{le="0.1"} 10

hydra_connected_peers

Number of connected peers.

hydra_connected_peers 2.0

Error Codes

WebSocket Errors

Code Message Description
1000 Normal closure Clean shutdown
1001 Going away Node shutting down
1006 Abnormal closure Connection lost

HTTP Status Codes

Code Description
200 Success
400 Bad request
404 Not found
500 Internal server error
503 Service unavailable

Type Definitions

UTxO

type UTxO = {
  [key: string]: {
    address: string;
    value: {
      lovelace: number;
      [assetId: string]: number;
    };
    datum?: string;
    datumHash?: string;
    scriptRef?: string;
  };
};

Party

type Party = {
  vkey: string;
};

Transaction

type Transaction = {
  type: "Tx ConwayEra" | "Tx BabbageEra";
  description: string;
  cborHex: string;
};

HeadStatus

type HeadStatus = 
  | "Idle"
  | "Initializing"
  | "Open"
  | "Closed"
  | "Final";

Rate Limits


Best Practices

  1. Always handle WebSocket reconnection
  2. Validate transaction CBOR before submitting
  3. Wait for SnapshotConfirmed before assuming finality
  4. Monitor Prometheus metrics for performance
  5. Use proper error handling for all API calls

Additional Resources


Need more examples? Check the Usage Guide or example scripts.