Skip to content

Commit

Permalink
Refactor after review
Browse files Browse the repository at this point in the history
  • Loading branch information
samchuk-vlad committed Oct 23, 2023
1 parent 914b6f9 commit 3c20d8c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/routes/creatorStaking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const createCreatorStakingRouter = (apis: Connections) => {
const info = await getCreatorsEraStake({
apis: apis.mixedApis,
era: era as string,
spaceIds: ids as string
spaceIds: (ids as string).split(',')
})

res.send(info)
Expand All @@ -50,7 +50,7 @@ const createCreatorStakingRouter = (apis: Connections) => {
const info = await getGeneralBackerInfo({
apis: apis.mixedApis,
account: account as string,
spaceIds: ids as string
spaceIds: (ids as string).split(',')
})

res.send(info)
Expand Down Expand Up @@ -79,7 +79,7 @@ const createCreatorStakingRouter = (apis: Connections) => {

const info = await getBackerRewards({
account: account as string,
spaceIds: ids as string
spaceIds: (ids as string).split(',')
})

res.send(info)
Expand Down
26 changes: 11 additions & 15 deletions src/services/creatorStaking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,40 @@ export const getGeneralEraInfo = async ({
api.query.creatorStaking.nextEraStartingBlock
])

const blockPerEra = api.consts.creatorStaking.blockPerEra.toJSON()
const blockPerEra = api.consts.creatorStaking.blockPerEra.toPrimitive()

const eraInfo = await api.query.creatorStaking.generalEraInfo(currentEraCodec)

return {
currentEra: currentEraCodec.toJSON() as string,
nextEraBlock: nextEraBlockCodec.toJSON() as string,
currentEra: currentEraCodec.toPrimitive() as string,
nextEraBlock: nextEraBlockCodec.toPrimitive() as string,
blockPerEra: blockPerEra as string,
...(eraInfo.toJSON() as EraInfo)
}
}

type CreatorsEraStakeProps = CreatorStakingProps & {
era: string
spaceIds: string
spaceIds: string[]
}

export const getCreatorsEraStake = async ({ apis, era, spaceIds }: CreatorsEraStakeProps) => {
const api = apis.subsocial

if (!api) return undefined

const ids = spaceIds.split(',')

const queryParams = ids.map((spaceId) => {
const queryParams = spaceIds.map((spaceId) => {
return [api.query.creatorStaking.creatorStakeInfoByEra, [spaceId, era]]
})

const eraStakesResult = await api.queryMulti(queryParams as any)

const eraStakes = {}

ids.forEach((id, index) => {
spaceIds.forEach((id, index) => {
const value = eraStakesResult[index]?.toJSON()
if (value || value !== null) {
if (value) {
eraStakes[id] = value
}
})
Expand All @@ -77,25 +76,23 @@ export const getCreatorsEraStake = async ({ apis, era, spaceIds }: CreatorsEraSt

type GeneralStakerInfoProps = CreatorStakingProps & {
account: string
spaceIds: string
spaceIds: string[]
}

export const getGeneralBackerInfo = async ({ apis, spaceIds, account }: GeneralStakerInfoProps) => {
const api = apis.subsocial

if (!api) return undefined

const ids = spaceIds.split(',')

const queryParams = ids.map((spaceId) => {
const queryParams = spaceIds.map((spaceId) => {
return [api.query.creatorStaking.generalBackerInfo, [account, spaceId]]
})

const generalStakerInfoResult = await api.queryMulti(queryParams as any)

const generalStakerInfo = {}

ids.forEach((id, index) => {
spaceIds.forEach((id, index) => {
const value = generalStakerInfoResult[index]?.toJSON() as any
if (value || value !== null) {
const stakes = value.stakes.sort((a, b) => new BN(b.era).minus(new BN(a.era)))
Expand Down Expand Up @@ -123,12 +120,11 @@ export const getBackerLocksByAccount = async ({ apis, account }: BackerLedgerPro

type StakerRewardsProps = {
account: string
spaceIds: string
spaceIds: string[]
}

export const getBackerRewards = async ({ account, spaceIds }: StakerRewardsProps) => {
const spaceIdsArray = spaceIds
.split(',')
.filter(isDef)
.map((id) => parseInt(id))

Expand Down

0 comments on commit 3c20d8c

Please sign in to comment.