@river-build/web3
Version:
Dapps for our Space and Registry contracts
96 lines • 3.42 kB
JavaScript
import { INodeRegistryShim } from './INodeRegistryShim';
import { IStreamRegistryShim } from './IStreamRegistryShim';
import { IOperatorRegistryShim } from './IOperatorRegistryShim';
export class RiverRegistry {
config;
provider;
nodeRegistry;
streamRegistry;
operatorRegistry;
riverNodesMap = {};
constructor(config, provider) {
this.config = config;
this.provider = provider;
this.nodeRegistry = new INodeRegistryShim(this.config.addresses.riverRegistry, provider);
this.streamRegistry = new IStreamRegistryShim(this.config.addresses.riverRegistry, provider);
this.operatorRegistry = new IOperatorRegistryShim(this.config.addresses.riverRegistry, provider);
}
async getAllNodes(nodeStatus) {
const allNodes = await this.nodeRegistry.read.getAllNodes();
if (allNodes.length == 0) {
return undefined;
}
const registry = {};
for (const node of allNodes) {
if (nodeStatus && node.status != nodeStatus) {
continue;
}
if (nodeStatus !== undefined) {
registry[node.nodeAddress] = node;
}
// update in-memory registry
this.riverNodesMap[node.nodeAddress] = node;
}
if (nodeStatus !== undefined) {
return registry;
}
// if we've updated the entire registry return that
return this.riverNodesMap;
}
async getAllNodeUrls(nodeStatus) {
const allNodes = await this.nodeRegistry.read.getAllNodes();
if (allNodes.length == 0) {
return undefined;
}
const nodeUrls = [];
for (const node of allNodes) {
// get all nodes with optional status
if (nodeStatus && node.status != nodeStatus) {
continue;
}
nodeUrls.push({ url: node.url });
// update registry
this.riverNodesMap[node.nodeAddress] = node;
}
return nodeUrls;
}
async getOperationalNodeUrls() {
const NODE_OPERATIONAL = 2;
const nodeUrls = await this.getAllNodeUrls(NODE_OPERATIONAL);
if (!nodeUrls || nodeUrls.length === 0) {
throw new Error('No operational nodes found in registry');
}
return nodeUrls
.sort()
.map((x) => x.url)
.join(',');
}
async getStreamCount() {
return this.streamRegistry.read.getStreamCount();
}
async getStream(streamAddress) {
return this.streamRegistry.read.getStream(streamAddress);
}
async streamExists(streamAddress) {
try {
await this.getStream(streamAddress);
return true;
}
catch (error) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (error.reason === 'NOT_FOUND') {
return false;
}
throw error;
}
}
async getStreamCountOnNode(nodeAddress) {
return this.streamRegistry.read.getStreamCountOnNode(nodeAddress);
}
async getStreamCountsOnNodes(nodeAddresses) {
const getStreamCountOnNode = this.getStreamCountOnNode.bind(this);
const promises = nodeAddresses.map(getStreamCountOnNode);
return Promise.all(promises);
}
}
//# sourceMappingURL=RiverRegistry.js.map