Skip to content

Commit

Permalink
Merge pull request #49 from dappforce/fix-validator-staking
Browse files Browse the repository at this point in the history
Fix validator staking api
  • Loading branch information
olehmell authored Oct 18, 2023
2 parents 2f57387 + 39643da commit d0d81f7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
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

0 comments on commit d0d81f7

Please sign in to comment.