Hydra Instance
The HydraInstance is a class interface for interacting with a Hydra head after initialization.
Overview
The HydraInstance
is intialized together with HydraProvider
, for accessing other methods to interact with Hydra head after HeadIsInitializing
phase.
Get started:
import { HydraInstance, HydraProvider } from "@meshsdk/hydra";
const provider = new HydraProvider({
httpUrl: "<hydra-API-URL>",
});
const instance = new HydraInstance({
provider: hydraProvider,
fetcher: "<blockchainProvider>",
submitter: "<blockchainProvider>",
});
Commit Empty
If you don't want to commit any funds and only want to receive on layer 2, you can request an empty commit transaction to open the head
const commit = await instance.commitEmpty();
const submitTx = await wallet.submitTx(commit);
console.log("submitTx", submitTx);
Commit Funds
Commits funds to the Hydra head by selecting specific UTxOs to make available on layer 2. Parameters:
txHash
outputIndex
Returns: The transaction CBOR hex ready to be partially signed
await instance.commitFunds(txHash: string, outputIndex: number)
const txHash =
"00000000000000000000000000000000000000000000000000000000000000000";
const outputIndex = 0;
const commitTx = await instance.commitFunds(txHash, outputIndex);
const signedTx = await wallet.signTx(commitTx, true);
const commitTxHash = await wallet.submitTx(signedTx);
Commit Blueprint
Commits a Cardano transaction blueprint to the Hydra head.
This is useful for advanced use cases such as commiting scriptUTxOs
.
Parameters:
txHash
outputIndex
hydraTransaction
await instance.commitBlueprint("txHash", outputIndex, {
cborHex: "<unsignedTx>",
description: "commit tx",
type: "Tx ConwayEra",
});
const commitTx = await instance.commitBlueprint(txHash, outputIndex, {
cborHex: "<unsignedTx>",
description: "commit tx",
type: "Tx ConwayEra",
});
const signedTx = await wallet.signTx(commitTx, true);
const commitTxHash = await wallet.submitTx(signedTx);
Incremental Commit
Incremental commit methods allow you commit additional UTxOs to an open hydra head after the initial commit:
The time it takes for it top be added after commit depends on the hydra-node
configuration parameter --deposit-period
To read more on incremental commit, see the Hydra documentation.
incrementalCommitFunds
await instance.incrementalCommitFunds(txHash: string, outputIndex: number)
incrementalBlueprintCommit
await instance.incrementalBlueprintCommit(txHash, outputIndex, {
cborHex: "unsignedTx",
description: "commit tx",
type: "Tx ConwayEra",
});
Basic Workflow
commit Funds
import { HydraInstance, HydraProvider } from "@meshsdk/hydra";
import { BlockfrostProvider } from "@meshsdk/core";
const provider = new HydraProvider({
httpUrl: "http://localhost:4001",
});
const instance = new HydraInstance({
provider: provider,
fetcher: "blockchainProvider",
submitter: "blockchainProvider",
});
await provider.connect();
await provider.init();
provider.onMessage((message) => {
const status =
message.tag === "Greetings"
? { headStatus: message.headStatus }
: { tag: message.tag };
if (
status.tag === "HeadIsInitializing" ||
status.headStatus === "Initializing"
) {
}
const commitTx = await instance.commitFunds(txHash, outputIndex);
const signedTx = await wallet.signTx(commitTx, true);
await wallet.submitTx(signedTx);
});
Blueprint Commit
provider.onMessage((message) => {
const status =
message.tag === "Greetings"
? { headStatus: message.headStatus }
: { tag: message.tag };
if (
status.tag === "HeadIsInitializing" ||
status.headStatus === "Initializing"
) {
const txBuilder = new MeshTxBuilder({
submitter: "<blockchainProvider>",
fetcher: "<blockchainProvider>",
verbose: true,
});
const unsignedTx = await txBuilder
.txIn(txHash, outputIndex)
.setFee("0")
.changeAddress(address)
.selectUtxosFrom(UTxOs)
.complete();
const commitTx = await instance.commitBlueprint(txHash, outputIndex, {
type: "Tx ConwayEra",
cborHex: unsignedTx,
description: "Commit Blueprint",
});
const signedTx = await wallet.signTx(commitTx);
const commitTxHash = await wallet.submitTx(signedTx);
console.log(commitTxHash);
}
});