diff --git a/src/connections/networks/index.ts b/src/connections/networks/index.ts index b643935..39fe6ec 100644 --- a/src/connections/networks/index.ts +++ b/src/connections/networks/index.ts @@ -15,6 +15,7 @@ export const standalones: Networks = { kusama: { name: 'Kusama', ...resolveOnfinalityUrl('kusama'), + wsNode: 'wss://kusama-rpc.polkadot.io', icon: 'kusama.svg', isMixedConnection: true, isTransferable: true @@ -22,6 +23,7 @@ export const standalones: Networks = { polkadot: { name: 'Polkadot', ...resolveOnfinalityUrl('polkadot'), + wsNode: 'wss://rpc.polkadot.io', icon: 'polkadot.svg', isMixedConnection: true, isTransferable: true diff --git a/src/server.ts b/src/server.ts index a47c452..0f5a149 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,13 +1,12 @@ import express from 'express' import cors from 'cors' import timeout from 'connect-timeout' -import { reqTimeoutSecs , allowedOrigins, port } from './constant/env' +import { reqTimeoutSecs, allowedOrigins, port } from './constant/env' import { newLogger } from '@subsocial/utils' import { createRoutes } from './routes' import { Connections } from './connections' import { getOrCreateRedisCache } from './cache/redisCache'; -import { getValidatorsDataByRelayChains } from './services/validatorStaking' require('dotenv').config() @@ -69,22 +68,18 @@ export const startHttpServer = (apis: Connections) => { log.warn('Error connecting to redis', error?.message) }) - redis?.on('close', () => { + redis?.on('close', async () => { if (redisCache.isConnectionClosed) return redisCache.setIsRedisReady(false) redisCache.setIsConnectionClosed(true) log.warn('Redis connection closed') - - getValidatorsDataByRelayChains(apis) }) redis?.on('connect', async () => { log.info('Redis connected') redisCache.setIsConnectionClosed(false) await redisCache.checkConnection({ showLogs: true }) - - getValidatorsDataByRelayChains(apis) }) // for parsing multipart/form-data diff --git a/src/services/validatorStaking/validatorsInfo.ts b/src/services/validatorStaking/validatorsInfo.ts index 94b93c2..d77af85 100644 --- a/src/services/validatorStaking/validatorsInfo.ts +++ b/src/services/validatorStaking/validatorsInfo.ts @@ -8,10 +8,12 @@ import type { DeriveStakingElected, DeriveStakingWaiting } from '@polkadot/api-d import Cache from '../../cache' import { Connections } from '../../connections' import { relayChains } from '../crowdloan/types' -import { isEmptyObj } from '@subsocial/utils' +import { isEmptyObj, newLogger } from '@subsocial/utils' const validatorStakingInfoCache = new Cache('validator-staking-info', FIVE_MINUTES) +const log = newLogger('ValidatorStakingInfo') + /// https://github.dev/polkadot-js/apps/blob/fb8f7fe86b2945fcc71dcd11c67ebeeab35ee37e/packages/page-staking/src/useSortedTargets.ts#L120-L121 const parseValidatorStakingInfo = ( api: ApiPromise, @@ -127,7 +129,7 @@ function mergeValidatorsInfo( } } -export const getValidatorsData = async (api: any, network: string) => { +export const getValidatorsData = async (api: ApiPromise, network: string) => { try { const cacheData = await validatorStakingInfoCache.get(network) @@ -142,7 +144,7 @@ export const getValidatorsData = async (api: any, network: string) => { const waitingInfo = await api.derive.staking.waitingInfo() const activeEra = await api.query.staking.activeEra() - const era = activeEra.toJSON().index + const era = (activeEra.toJSON() as any).index const baseInfo = mergeValidatorsInfo(api, electedInfo, waitingInfo) @@ -158,22 +160,27 @@ export const getValidatorsData = async (api: any, network: string) => { info, loading: false }) - } catch { + } catch (e) { await validatorStakingInfoCache.set(network, { info: undefined, loading: false }) + + log.error('Error getting validators data by netwrok', network, e) } } export const getValidatorsDataByRelayChains = async (apis: Connections) => { - relayChains.forEach((network: string) => { + const promise = relayChains.map(async (network: string) => { const api = apis.wsApis?.[network] // const api = apis.mixedApis?.[network] if (!api) return - getValidatorsData(api, network) + + await getValidatorsData(api, network) }) + + await Promise.all(promise) } export const getValidatorsList = async ({ apis, network }: ValidatorStakingProps) => { @@ -186,7 +193,7 @@ export const getValidatorsList = async ({ apis, network }: ValidatorStakingProps const forceUpdate = needUpdate && (await needUpdate()) const cacheData = await validatorStakingInfoCache.get(network) - if (!cacheData || forceUpdate) { + if (!cacheData?.info || forceUpdate) { getValidatorsData(api, network) }