Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validator staking api #49

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/connections/networks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ export const standalones: Networks = {
kusama: {
name: 'Kusama',
...resolveOnfinalityUrl('kusama'),
wsNode: 'wss://kusama-rpc.polkadot.io',
icon: 'kusama.svg',
isMixedConnection: true,
isTransferable: true
},
polkadot: {
name: 'Polkadot',
...resolveOnfinalityUrl('polkadot'),
wsNode: 'wss://rpc.polkadot.io',
icon: 'polkadot.svg',
isMixedConnection: true,
isTransferable: true
Expand Down
9 changes: 2 additions & 7 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -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
Expand Down
21 changes: 14 additions & 7 deletions src/services/validatorStaking/validatorsInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>('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,
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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) => {
Expand All @@ -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)
}

Expand Down
Loading