Skip to content

Commit 071fea6

Browse files
committed
docs: add code examples
1 parent 425e2ed commit 071fea6

File tree

1 file changed

+82
-4
lines changed

1 file changed

+82
-4
lines changed

main/guides/orchestration/chainhub.md

+82-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,100 @@
1-
# Chain Hub
1+
# ChainHub
22

3-
This [code module](https://github.com/Agoric/agoric-sdk/blob/859d8c0d151ff6f686583db1eaf72efb89cc7648/packages/orchestration/src/exos/chain-hub.js#L99) is designed for managing chain and IBC connection information. It facilitate the registration and retrieval of chain and connection data. Let's break down the components and functionalities of this code.
3+
The [ChainHub API](https://github.com/Agoric/agoric-sdk/blob/859d8c0d151ff6f686583db1eaf72efb89cc7648/packages/orchestration/src/exos/chain-hub.js#L99) is responsible managing chain and IBC connection information. It facilitate the registration and retrieval of chain and connection data.
44

5-
The `makeChainHub` function accepts a NameHub reference (`agoricNames`) and an optional Zone for managing data durability. Inside the function two `MapStores` are created:
5+
```js
6+
const zone = makeDurableZone(baggage);
7+
const { agoricNames } = remotePowers;
8+
const chainHub = makeChainHub(agoricNames, zone);
9+
```
10+
11+
The `makeChainHub` function accepts a `Remote<NameHub>` reference (`agoricNames`) and an optional `Zone` for managing data durability. The `makeChainHub` fuction creates a new `ChainHub` either in the specified zone or in the heap if no zone is provided. The resulting object is an Exo singleton, which means it has no previous state. Its state consists only of a cache of queries to `agoricNames` and the information provided in registration calls.
12+
13+
The `ChainHub` objects maintains two `MapStores`:
614

715
- `chainInfos`: for storing `CosmosChainInfo` objects.
816
- `connectionInfos`: for storing `IBCConnectionInfo` objects.
917

18+
These `MapStores` are not exposed directly. They are abstracted and used internally by the methods provided by the ChainHub.
19+
1020
# ChainHub Interface
1121

1222
The core functionality is encapsulated within the `makeChainHub` function, which sets up a new ChainHub in the given zone. The ChainHub is responsible for:
1323

14-
- **Registering Chain Information (`registerChain`)**: Stores information about a blockchain inside the `chainInfos` mapstore, which can be used for quickly looking up details without querying a remote source.
24+
- **Registering Chain Information (`registerChain`)**: Stores information about a chain inside the `chainInfos` mapstore, which can be used for quickly looking up details without querying a remote source.
25+
26+
```js
27+
const chainInfo = harden({
28+
chainId: 'agoric-3',
29+
icaEnabled: false,
30+
icqEnabled: false,
31+
pfmEnabled: false,
32+
ibcHooksEnabled: false,
33+
stakingTokens: [{ denom: 'uist' }],
34+
});
35+
let nonce = 0n;
36+
37+
const chainKey = `${chainInfo.chainId}-${(nonce += 1n)}`;
38+
39+
chainHub.registerChain(chainKey, chainInfo);
40+
```
41+
42+
The function takes two parameters: `name`, which is a `string` representing the unique identifier of the chain, and `chainInfo`, which is an object structured according to the `CosmosChainInfo` format.
1543

1644
- **Retrieving Chain Information (`getChainInfo`)**: Retrieves stored chain information from the `chainInfos` mapstore or fetches it from a remote source if not available locally.
1745

46+
```js
47+
chainHub.getChainInfo('agoric-3');
48+
```
49+
50+
The function takes a single parameter, `chainName`, which is a `string` template type `K`, and returns a promise (`Vow`) that resolves to `ActualChainInfo<K>`, providing detailed information about the specified chain based on its name.
51+
1852
- **Registering Connection Information (`registerConnection`)**: Stores information about a connection between two chains in `connectionInfos` mapstore, such as IBC connection details.
1953

54+
```js
55+
const chainConnection = {
56+
id: 'connection-0',
57+
client_id: '07-tendermint-2',
58+
counterparty: {
59+
client_id: '07-tendermint-2',
60+
connection_id: 'connection-1',
61+
prefix: {
62+
key_prefix: '',
63+
},
64+
},
65+
state: 3 /* IBCConnectionState.STATE_OPEN */,
66+
transferChannel: {
67+
portId: 'transfer',
68+
channelId: 'channel-1',
69+
counterPartyChannelId: 'channel-1',
70+
counterPartyPortId: 'transfer',
71+
ordering: 1 /* Order.ORDER_UNORDERED */,
72+
state: 3 /* IBCConnectionState.STATE_OPEN */,
73+
version: 'ics20-1',
74+
},
75+
};
76+
77+
chainHub.registerConnection('agoric-3', 'cosmoshub', chainConnection);
78+
```
79+
80+
The function accepts three parameters: `chainId1` and `chainId2`, both of which are `strings` representing the identifiers of the two chains being connected, and `connectionInfo`, which is an object containing the details of the IBC connection as specified by the `IBCConnectionInfo` format
81+
2082
- **Retrieving Connection Information (`getConnectionInfo`)**: Retrieves stored connection information from `connectionInfos` mapstore or fetches it from a remote source if not available locally.
2183

84+
```js
85+
const chainConnection = await E.when(
86+
chainHub.getConnectionInfo('agoric-3', 'cosmoshub'),
87+
);
88+
```
89+
90+
The function takes two parameters, `chain1` and `chain2`, each of which can be either a `string` representing a chain identifier or an `object` with a `chainId` property, and it returns a promise (`Vow`) that resolves with an `IBCConnectionInfo` object detailing the connection between the two chains.
91+
2292
- **Retrieving Combined Chain and Connection Information (`getChainsAndConnection`)**: A composite function that fetches information about two chains and their connection simultaneously.
93+
94+
```js
95+
const [agoric3, cosmoshub, connectionInfo] = await E.when(
96+
chainHub.getChainsAndConnection('agoric-3', 'cosmoshub'),
97+
);
98+
```
99+
100+
The function accepts two parameters, `chainName1` and `chainName2`, both of which are strings but defined as template types `C1` and `C2` respectively. It returns a promise (`Vow`) that resolves to a tuple containing the detailed information of both chains, `ActualChainInfo<C1>` and `ActualChainInfo<C2>`, along with their IBC connection information (`IBCConnectionInfo`).

0 commit comments

Comments
 (0)