Skip to content

Commit 10a66ea

Browse files
authored
Merge pull request #56 from MeshJS/hydra-docs-update-new
update hydra-docs
2 parents f7021f8 + f62c487 commit 10a66ea

File tree

3 files changed

+122
-87
lines changed

3 files changed

+122
-87
lines changed

apps/docs/content/docs/hydra/instance.mdx

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ The `HydraInstance` is intialized together with `HydraProvider`, for accessing o
1414
```tsx
1515
import { HydraInstance, HydraProvider } from "@meshsdk/hydra";
1616

17-
const hydraProvider = new HydraProvider({
17+
const provider = new HydraProvider({
1818
httpUrl: "<hydra-API-URL>",
1919
});
2020

21-
const hydraInstance = new HydraInstance({
21+
const instance = new HydraInstance({
2222
provider: hydraProvider,
2323
fetcher: "<blockchainProvider>",
2424
submitter: "<blockchainProvider>",
@@ -31,7 +31,7 @@ If you don't want to commit any funds and only want to receive on layer 2, you c
3131
to open the head
3232

3333
```tsx
34-
const commit = await hydraInstance.commitEmpty();
34+
const commit = await instance.commitEmpty();
3535
const submitTx = await wallet.submitTx(commit);
3636
console.log("submitTx", submitTx);
3737
```
@@ -47,15 +47,15 @@ Commits funds to the Hydra head by selecting specific UTxOs to make available on
4747
**Returns:** The transaction CBOR hex ready to be partially signed
4848

4949
```tsx
50-
await commitFunds(txHash: string, outputIndex: number)
50+
await instance.commitFunds(txHash: string, outputIndex: number)
5151
```
5252

5353
```tsx
5454
const txHash =
5555
"00000000000000000000000000000000000000000000000000000000000000000";
5656
const outputIndex = 0;
5757

58-
const commitTx = await hydraInstance.commitFunds(txHash, outputIndex);
58+
const commitTx = await instance.commitFunds(txHash, outputIndex);
5959
const signedTx = await wallet.signTx(commitTx, true);
6060
const commitTxHash = await wallet.submitTx(signedTx);
6161
```
@@ -72,15 +72,15 @@ This is useful for advanced use cases such as commiting `scriptUTxOs`.
7272
- `hydraTransaction`
7373

7474
```tsx
75-
await hydraInstance.commitBlueprint("txHash", outputIndex, {
75+
await instance.commitBlueprint("txHash", outputIndex, {
7676
cborHex: "<unsignedTx>",
7777
description: "commit tx",
7878
type: "Tx ConwayEra",
7979
});
8080
```
8181

8282
```tsx
83-
const commitTx = await hydraInstance.commitBlueprint(txHash, outputIndex, {
83+
const commitTx = await instance.commitBlueprint(txHash, outputIndex, {
8484
cborHex: "<unsignedTx>",
8585
description: "commit tx",
8686
type: "Tx ConwayEra",
@@ -97,20 +97,19 @@ The time it takes for it top be added after commit depends on the `hydra-node` c
9797
To read more on incremental commit, see [the Hydra documentation](https://hydra.family/head-protocol/docs/how-to/incremental-commit).
9898

9999
### incrementalCommitFunds
100+
100101
```tsx
101-
await incrementalCommitFunds(txHash: string, outputIndex: number)
102+
await instance.incrementalCommitFunds(txHash: string, outputIndex: number)
102103
```
104+
103105
### incrementalBlueprintCommit
106+
104107
```tsx
105-
await incrementalBlueprintCommit(
106-
txHash,
107-
outputIndex,
108-
{
109-
cborHex: "unsignedTx",
110-
description: "commit tx",
111-
type: "Tx ConwayEra",
112-
}
113-
)
108+
await instance.incrementalBlueprintCommit(txHash, outputIndex, {
109+
cborHex: "unsignedTx",
110+
description: "commit tx",
111+
type: "Tx ConwayEra",
112+
});
114113
```
115114

116115
## Basic Workflow
@@ -125,45 +124,65 @@ const provider = new HydraProvider({
125124
httpUrl: "http://localhost:4001",
126125
});
127126

128-
const hydraInstance = new HydraInstance({
127+
const instance = new HydraInstance({
129128
provider: provider,
130129
fetcher: "blockchainProvider",
131130
submitter: "blockchainProvider",
132131
});
133132

134133
await provider.connect();
135-
136134
await provider.init();
137135

138-
const commitTx = await hydraInstance.commitFunds(txHash, outputIndex);
139-
const signedTx = await wallet.signTx(commitTx, true);
140-
await wallet.submitTx(signedTx);
136+
provider.onMessage((message) => {
137+
const status =
138+
message.tag === "Greetings"
139+
? { headStatus: message.headStatus }
140+
: { tag: message.tag };
141+
if (
142+
status.tag === "HeadIsInitializing" ||
143+
status.headStatus === "Initializing"
144+
) {
145+
}
146+
const commitTx = await instance.commitFunds(txHash, outputIndex);
147+
const signedTx = await wallet.signTx(commitTx, true);
148+
await wallet.submitTx(signedTx);
149+
});
141150
```
142151

143152
### Blueprint Commit
144153

145154
```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",
155+
provider.onMessage((message) => {
156+
const status =
157+
message.tag === "Greetings"
158+
? { headStatus: message.headStatus }
159+
: { tag: message.tag };
160+
if (
161+
status.tag === "HeadIsInitializing" ||
162+
status.headStatus === "Initializing"
163+
) {
164+
const txBuilder = new MeshTxBuilder({
165+
submitter: "<blockchainProvider>",
166+
fetcher: "<blockchainProvider>",
167+
verbose: true,
168+
});
169+
170+
const unsignedTx = await txBuilder
171+
.txIn(txHash, outputIndex)
172+
.setFee("0")
173+
.changeAddress(address)
174+
.selectUtxosFrom(UTxOs)
175+
.complete();
176+
177+
const commitTx = await instance.commitBlueprint(txHash, outputIndex, {
178+
type: "Tx ConwayEra",
179+
cborHex: unsignedTx,
180+
description: "Commit Blueprint",
181+
});
182+
183+
const signedTx = await wallet.signTx(commitTx);
184+
const commitTxHash = await wallet.submitTx(signedTx);
185+
console.log(commitTxHash);
186+
}
164187
});
165-
166-
const signedTx = await wallet.signTx(commitTx);
167-
const commitTxHash = await wallet.submitTx(signedTx);
168-
console.log(commitTxHash);
169188
```

apps/docs/content/docs/providers/hydra.mdx

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import { HydraProvider } from "@meshsdk/hydra";
1414

1515
// Hydra Head URL and PORT: e.g. http://123.45.67.890:4001
1616

17-
const provider = new HydraProvider("<httpUrl>");
17+
const provider = new HydraProvider({
18+
httpUrl: <hydra-api-url>
19+
});
1820
```
1921

2022
## connect
@@ -25,6 +27,25 @@ Establishes a connection to a Hydra Head. This is typically managed through the
2527
await provider.connect();
2628
```
2729

30+
## hydra-head messages
31+
32+
Listens to messages from hydra-head
33+
34+
```tsx
35+
provider.onMessage((message) =>
36+
console.log("messages received from hydra", message)
37+
);
38+
```
39+
40+
can be used together with connect to Receive Messages after connect
41+
42+
```tsx
43+
await provider.connect();
44+
provider.onMessage((message) =>
45+
console.log("messages received from hydra", message)
46+
);
47+
```
48+
2849
## disconnect
2950

3051
Closes the active connection to a Hydra Head. This is typically managed through the HydraProvider.
@@ -35,10 +56,10 @@ Parameter (Optional)
3556

3657
```tsx
3758
// Disconnect with default timeout (5 minutes)
38-
await disconnect(timeout);
59+
await provider.disconnect();
3960

4061
// Disconnect with custom timeout (e.g., 10 minutes)
41-
await disconnect(10 * 60 * 1000);
62+
await provider.disconnect(10 * 60 * 1000);
4263
```
4364

4465
## Hydra commands APIs
@@ -81,8 +102,12 @@ The `newTx` method accepts the following arguments:
81102
- type: Any transaction is tried to decode as a 'ConwayEra' transaction, which mostly is backward compatible to previous eras.
82103
- description(optional): transaction description
83104

105+
**returns**
106+
107+
- txId(transaction Hash)
108+
84109
```tsx
85-
async newTx({
110+
async provider.newTx({
86111
cborHex: "<unsignedTx>",
87112
description: "commit tx",
88113
type: "Tx ConwayEra",
@@ -172,73 +197,57 @@ Finalize a head UTxOs to L1 after the contestation period passed.
172197
await provider.fanout();
173198
```
174199

175-
```tsx
176-
provider.onMessage((message) => {
177-
if (
178-
message.headStatus === "FanoutPossible" ||
179-
message.tag === "HeadIsFinalized"
180-
) {
181-
provider.fanout();
182-
}
183-
});
184-
```
185-
186200
## Hydra Query APIs
187201

188-
### Fetch Address UTxOs
202+
### Fetch UTxOs
189203

190-
Fetch UTxOs controlled by an address.
204+
Get UTxOs for a given hash.
205+
Optionally, you can specify the index of the index output.
191206

192207
```tsx
193-
await provider.fetchAddressUTxOs(
194-
"addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9"
195-
);
208+
await provider.fetchUTxOs("txHash");
196209
```
197210

198-
Optionally, you can filter UTXOs containing a particular asset by providing `asset`, where it is the concatenation of policy ID and asset.
211+
### Fetch Address UTxOs
212+
213+
Fetch UTxOs controlled by an address.
214+
215+
Optionally, you can filter UTXOs containing a particular asset by providing `asset`, where it is the concatenation of policy ID and assetname.
199216

200217
```tsx
201-
// Assest f.e. d9312da562da182b02322fd8acb536f37eb9d29fba7c49dc172555274d657368546f6b656e
202-
await fetchAddressUTxOs(address: string, asset?: string)
218+
await provider.fetchAddressUTxOs(address: string, asset?: string)
203219
```
204220

205-
### Fetch Protocol Parameters
221+
### Fetch Asset Addresses
206222

207-
Fetch the latest protocol parameters.
223+
Fetches the addresses and quantities for a given Cardano asset.
208224

209225
```tsx
210-
await provider.fetchProtocolParameters();
226+
await provider.fetchAssetAddresses(asset: string):
211227
```
212228

213-
Optionally, you can provide an epoch number to fetch the protocol parameters of that epoch.
214-
215-
### Fetch UTxOs
229+
### Fetch collection Assest
216230

217-
Get UTxOs for a given hash.
231+
Fetches the list of assets for a given policy ID.
218232

219233
```tsx
220-
await provider.fetchUTxOs("txHash");
234+
await provider.fetchCollectionAssets(policyId: string)
221235
```
222236

223-
Optionally, you can specify the index of the index output.
237+
### Fetch Protocol Parameters
238+
239+
Fetch the latest protocol parameters.
224240

225241
```tsx
226-
await provider.fetchUTxOs("txHash", 0);
242+
await provider.fetchProtocolParameters();
227243
```
228244

245+
Optionally, you can provide an epoch number to fetch the protocol parameters of that epoch.`
246+
229247
### Listens for new messages from Hydra node
230248

231249
Listens for new messages from Hydra node. The callback function will be called with the message as the only argument. Check <Link href="https://hydra.family/head-protocol/api-reference">all events emitted</Link> by the Hydra node.
232250

233-
```tsx
234-
provider.onMessage((message) => {
235-
console.log("HydraProvider received message", message);
236-
// do something with the message
237-
});
238-
```
239-
240-
The callback function is typed, so you can access the message properties directly.
241-
242251
```tsx
243252
provider.onMessage((message) => {
244253
if (message.tag === "Greetings") {
@@ -252,5 +261,6 @@ provider.onMessage((message) => {
252261
Submit a serialized transaction to the network.
253262

254263
```tsx
255-
await provider.submitTx(signedTx);
264+
const txHash = await provider.submitTx(signedTx);
265+
console.log("txHash", txHash);
256266
```

apps/docs/data/links-hydra.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ export const metaHydraTutorial = {
1111
link: "/hydra/tutorial",
1212
};
1313

14+
export const metaHydraInstance = {
15+
title: "Hydra Instance",
16+
desc: "The HydraInstance is a class interface for interacting with a Hydra head after initialization.",
17+
link: "/hydra/instance",
18+
};
1419
export const linksHydra = [
1520
// metaHydraGettingStarted,
21+
metaHydraInstance,
1622
metaHydraProvider,
1723
metaHydraTutorial,
1824
];

0 commit comments

Comments
 (0)