|
| 1 | +--- |
| 2 | +title: "Hydra Instance" |
| 3 | +description: "The HydraInstance is a class interface for interacting with a Hydra head after initialization." |
| 4 | +--- |
| 5 | + |
| 6 | +import Link from "fumadocs-core/link"; |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +The `HydraInstance` is intialized together with `HydraProvider`, for accessing other methods to interact with Hydra head after `HeadIsInitializing` phase. |
| 11 | + |
| 12 | +## Get started: |
| 13 | + |
| 14 | +```tsx |
| 15 | +import { HydraInstance, HydraProvider } from "@meshsdk/hydra"; |
| 16 | + |
| 17 | +const hydraProvider = new HydraProvider({ |
| 18 | + httpUrl: "<hydra-API-URL>", |
| 19 | +}); |
| 20 | + |
| 21 | +const hydraInstance = new HydraInstance({ |
| 22 | + provider: hydraProvider, |
| 23 | + fetcher: "<blockchainProvider>", |
| 24 | + submitter: "<blockchainProvider>", |
| 25 | +}); |
| 26 | +``` |
| 27 | + |
| 28 | +### Commit Empty |
| 29 | + |
| 30 | +If you don't want to commit any funds and only want to receive on layer 2, you can request an empty commit transaction |
| 31 | +to open the head |
| 32 | + |
| 33 | +```tsx |
| 34 | +const commit = await hydraInstance.commitEmpty(); |
| 35 | +const submitTx = await wallet.submitTx(commit); |
| 36 | +console.log("submitTx", submitTx); |
| 37 | +``` |
| 38 | + |
| 39 | +### Commit Funds |
| 40 | + |
| 41 | +Commits funds to the Hydra head by selecting specific UTxOs to make available on layer 2. |
| 42 | +**Parameters:** |
| 43 | + |
| 44 | +- `txHash` |
| 45 | +- `outputIndex` |
| 46 | + |
| 47 | +**Returns:** The transaction CBOR hex ready to be partially signed |
| 48 | + |
| 49 | +```tsx |
| 50 | +await commitFunds(txHash: string, outputIndex: number) |
| 51 | +``` |
| 52 | + |
| 53 | +```tsx |
| 54 | +const txHash = |
| 55 | + "00000000000000000000000000000000000000000000000000000000000000000"; |
| 56 | +const outputIndex = 0; |
| 57 | + |
| 58 | +const commitTx = await hydraInstance.commitFunds(txHash, outputIndex); |
| 59 | +const signedTx = await wallet.signTx(commitTx, true); |
| 60 | +const commitTxHash = await wallet.submitTx(signedTx); |
| 61 | +``` |
| 62 | + |
| 63 | +### Commit Blueprint |
| 64 | + |
| 65 | +Commits a Cardano transaction blueprint to the Hydra head. |
| 66 | +This is useful for advanced use cases such as commiting `scriptUTxOs`. |
| 67 | + |
| 68 | +**Parameters:** |
| 69 | + |
| 70 | +- `txHash` |
| 71 | +- `outputIndex` |
| 72 | +- `hydraTransaction` |
| 73 | + |
| 74 | +```tsx |
| 75 | +await hydraInstance.commitBlueprint("txHash", outputIndex, { |
| 76 | + cborHex: "<unsignedTx>", |
| 77 | + description: "commit tx", |
| 78 | + type: "Tx ConwayEra", |
| 79 | +}); |
| 80 | +``` |
| 81 | + |
| 82 | +```tsx |
| 83 | +const commitTx = await hydraInstance.commitBlueprint(txHash, outputIndex, { |
| 84 | + cborHex: "<unsignedTx>", |
| 85 | + description: "commit tx", |
| 86 | + type: "Tx ConwayEra", |
| 87 | +}); |
| 88 | +const signedTx = await wallet.signTx(commitTx, true); |
| 89 | +const commitTxHash = await wallet.submitTx(signedTx); |
| 90 | +``` |
| 91 | + |
| 92 | +### Incremental Commit |
| 93 | + |
| 94 | +Incremental commit methods allow you commit additional UTxOs to an open hydra head after the initial commit: |
| 95 | +The time it takes for it top be added after commit depends on the `hydra-node` configuration parameter `--deposit-period` |
| 96 | + |
| 97 | +To read more on incremental commit, see [the Hydra documentation](https://hydra.family/head-protocol/docs/how-to/incremental-commit). |
| 98 | + |
| 99 | +### incrementalCommitFunds |
| 100 | +```tsx |
| 101 | +await incrementalCommitFunds(txHash: string, outputIndex: number) |
| 102 | +``` |
| 103 | +### incrementalBlueprintCommit |
| 104 | +```tsx |
| 105 | +await incrementalBlueprintCommit( |
| 106 | + txHash, |
| 107 | + outputIndex, |
| 108 | + { |
| 109 | + cborHex: "unsignedTx", |
| 110 | + description: "commit tx", |
| 111 | + type: "Tx ConwayEra", |
| 112 | + } |
| 113 | +) |
| 114 | +``` |
| 115 | + |
| 116 | +## Basic Workflow |
| 117 | + |
| 118 | +### commit Funds |
| 119 | + |
| 120 | +```tsx |
| 121 | +import { HydraInstance, HydraProvider } from "@meshsdk/hydra"; |
| 122 | +import { BlockfrostProvider } from "@meshsdk/core"; |
| 123 | + |
| 124 | +const provider = new HydraProvider({ |
| 125 | + httpUrl: "http://localhost:4001", |
| 126 | +}); |
| 127 | + |
| 128 | +const hydraInstance = new HydraInstance({ |
| 129 | + provider: provider, |
| 130 | + fetcher: "blockchainProvider", |
| 131 | + submitter: "blockchainProvider", |
| 132 | +}); |
| 133 | + |
| 134 | +await provider.connect(); |
| 135 | + |
| 136 | +await provider.init(); |
| 137 | + |
| 138 | +const commitTx = await hydraInstance.commitFunds(txHash, outputIndex); |
| 139 | +const signedTx = await wallet.signTx(commitTx, true); |
| 140 | +await wallet.submitTx(signedTx); |
| 141 | +``` |
| 142 | + |
| 143 | +### Blueprint Commit |
| 144 | + |
| 145 | +```tsx |
| 146 | +const txBuilder = new MeshTxBuilder({ |
| 147 | + submitter: "blockchainProvider", |
| 148 | + fetcher: "blockchainProvider", |
| 149 | + verbose: true, |
| 150 | +}); |
| 151 | + |
| 152 | +const unsignedTx = await txBuilder |
| 153 | + .txIn(txHash, outputIndex) |
| 154 | + .txOut(address, [{ unit: "lovelace", quantity: amount }]) |
| 155 | + .setFee("0") |
| 156 | + .changeAddress(address) |
| 157 | + .selectUtxosFrom(UTxOs) |
| 158 | + .complete(); |
| 159 | + |
| 160 | +const commitTx = await instance.commitBlueprint(txHash, outputIndex, { |
| 161 | + type: "Tx ConwayEra", |
| 162 | + cborHex: unsignedTx, |
| 163 | + description: "Commit Blueprint", |
| 164 | +}); |
| 165 | + |
| 166 | +const signedTx = await wallet.signTx(commitTx); |
| 167 | +const commitTxHash = await wallet.submitTx(signedTx); |
| 168 | +console.log(commitTxHash); |
| 169 | +``` |
0 commit comments