Network

Networks Module

This module is responsible for watching and updating the Ethereum network data. It uses signals to maintain a reactive state of network data.

Import

This module imports the following dependencies:

  • signal from @preact/signals-react

  • KiroboService from @kiroboio/fct-service

  • endpoints from ./endpoints

  • validator, validateFind, validateItem, ServiceFind from ./validator

  • EthNetworkPatchDto, EthNetworkResponseDto from ./types/eth-network.dto

Constants

  • ENDPOINT: contains the endpoint of the Ethereum network service.

  • defaultNetworkData: a default object representing the network data.

Functions

networkName(netId: string | number)

This function returns the network name based on the provided network ID.

Example:

const name = networkName(1);
console.log(name); // 'main'

unwatchNetworks()

This function removes all listeners for the Ethereum network service and resets the network state.

Example:

await unwatchNetworks();

watchNetwork(netId: number)

This function watches for network updates in the specified network ID. It updates the network state accordingly when there are changes in the network data.

Example:

await watchNetwork(1); // Watches the 'main' network

updateNetwork(net: EthNetworkResponseDto | EthNetworkPatchDto)

This function updates the network data with the provided network object.

Example:

const netData = {
  height: 1000,
  kiroPrice: '2',
  gasPrice: '100',
  netId: 'main',
  updatedAt: '1625798600',
  online: true,
  contracts: undefined,
  name: 'main',
};
updateNetwork(netData);

watchNetworks()

This function watches for network updates and sets up listeners for the Ethereum network service to respond to events like network data changes.

Example:

await watchNetworks();

Exports

  • networkName: a function to get the network name from a network ID.

  • network: the signal object containing the network data.

  • unwatchNetworks: a function to stop watching network updates.

  • watchNetwork: a function to start watching network updates for a specific network ID.

  • watchNetworks: a function to start watching network updates for all networks.

Last updated