Skip to content

Commit

Permalink
Merge pull request #268 from chain4travel/aeddaqqa/validators-filter
Browse files Browse the repository at this point in the history
fix(partnerConfig) : fix filtering with only validators
  • Loading branch information
aeddaqqa authored Nov 15, 2024
2 parents e00d895 + 66fb6b9 commit fd6bd2a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 33 deletions.
2 changes: 2 additions & 0 deletions src/@types/partners.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface PartnerDataType {
isValidator: boolean
contractAddress: any
id?: number
attributes?: AttributesType
}
Expand Down
21 changes: 2 additions & 19 deletions src/components/Partners/PartnerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import React, { useEffect, useState } from 'react'
import { PartnerDataType } from '../../@types/partners'
import { useAppSelector } from '../../hooks/reduxHooks'
import { useEffectOnce } from '../../hooks/useEffectOnce'
import useWallet from '../../hooks/useWallet'
import { selectValidators } from '../../redux/slices/app-config'
import { getActiveNetwork } from '../../redux/slices/network'
import PartnerBusinessFields from './PartnerBusinessFields'
import PartnerFlag from './PartnerFlag'
import PartnerLogo from './PartnerLogo'
Expand All @@ -20,18 +18,8 @@ interface PartnerCardProps {

const PartnerCard: React.FC<PartnerCardProps> = ({ partner, clickable, onClick }) => {
useEffectOnce(() => {})
const { getRegisteredNode } = useWallet()
const [isValidator, setIsValidator] = useState(false)
const validators = useAppSelector(selectValidators)
const { getAddress } = useWallet()
const activeNetwork = useAppSelector(getActiveNetwork)
const chackValidatorStatus = async (address: string) => {
try {
if (!pChainAddress) setIsValidator(false)
let nodeID = await getRegisteredNode(getAddress(address))
setIsValidator(!!validators.find(v => v.nodeID === nodeID))
} catch (e) {}
}
const {
attributes: {
companyName,
Expand All @@ -40,16 +28,11 @@ const PartnerCard: React.FC<PartnerCardProps> = ({ partner, clickable, onClick }
companyLogoColor,
country_flag,
logoBox,
pChainAddress,
pChainAddresses,
},
} = partner
useEffect(() => {
if (pChainAddresses) {
let partnerAddresses = pChainAddresses.find(
elem => elem.Network?.toLowerCase() === activeNetwork?.name?.toLowerCase(),
)
if (partnerAddresses) chackValidatorStatus(partnerAddresses.pAddress)
if (partner?.isValidator) {
setIsValidator(true)
}
}, [partner, validators])
return (
Expand Down
67 changes: 53 additions & 14 deletions src/redux/services/partners.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { ethers } from 'ethers'
import { ava as caminoClient } from 'wallet/caminoClient'
import store from 'wallet/store'
import { PartnerDataType, PartnersResponseType } from '../../@types/partners'
import {
Expand Down Expand Up @@ -124,6 +125,19 @@ async function getContractMappings(): Promise<Map<string, string>> {
return mappings
}

async function getRegisteredNode(address: string): Promise<string> {
return await caminoClient.PChain().getRegisteredShortIDLink(address)
}
const getAddress = address => {
if (address) {
let res = caminoClient
.PChain()
.addressFromBuffer(caminoClient.PChain().parseAddress(address))
return res
}
return ''
}

function checkMatch(data): boolean {
if (data.supportedResult.length === 0 && data.wantedResult.length === 0) {
return false
Expand Down Expand Up @@ -165,7 +179,7 @@ function getServiceName(fullName: unknown): string {
const getBaseUrl = () => {
const currentPath = typeof window !== 'undefined' ? window.location.hostname : ''
if (currentPath === 'localhost' || currentPath.includes('dev')) {
return BASE_URLS.dev
return BASE_URLS.prod
} else if (currentPath) {
return BASE_URLS.prod
} else {
Expand All @@ -184,7 +198,6 @@ const getBusinessBaseUrl = () => {
}
}


export const groupedBusinessFields = (businessField: any) => {
const grouped: Record<string, BusinessField> = {}
businessField.forEach(field => {
Expand Down Expand Up @@ -216,9 +229,8 @@ export const partnersApi = createApi({
const baseUrl = getBaseUrl()

let query = '?populate=*'

const selectedNetwork = store.getters['Network/selectedNetwork']
if (!isNaN(page) && !onMessenger) {
if (!isNaN(page) && !onMessenger && !validators) {
query += `&sort[0]=companyName:asc&pagination[page]=${page}&pagination[pageSize]=12`
}
if (businessField) {
Expand All @@ -235,7 +247,7 @@ export const partnersApi = createApi({
query += `&filters[companyName][$contains]=${companyName}`
}
if (validators) {
query += `&filters[$and][0][pChainAddresses][pAddress][$notNull]=true&filters[$and][1][pChainAddresses][Network][$eq]=${selectedNetwork.name.toLowerCase()}`
query += `&sort[0]=companyName:asc&pagination[page]=${0}&pagination[pageSize]=100&filters[$and][0][pChainAddresses][pAddress][$notNull]=true&filters[$and][1][pChainAddresses][Network][$eq]=${selectedNetwork.name.toLowerCase()}`
}
if (onMessenger === true) {
query += `&sort[0]=companyName:asc&_limit=-1&filters[$and][0][cChainAddresses][cAddress][$notNull]=true&filters[$and][1][cChainAddresses][Network][$eq]=${selectedNetwork.name.toLowerCase()}`
Expand All @@ -244,7 +256,10 @@ export const partnersApi = createApi({
return {
url: `${baseUrl}${query}`,
method: 'GET',
params: { onMessenger: onMessenger === true ? 'true' : 'false' },
params: {
onMessenger: onMessenger,
validators: validators,
},
}
},
async transformResponse(response: PartnersResponseType, meta, arg) {
Expand All @@ -259,7 +274,8 @@ export const partnersApi = createApi({
const provider = new ethers.JsonRpcProvider(providerUrl)

const contractMappings = await getContractMappings()
const onMessenger = arg.onMessenger === true
const onMessenger = arg.onMessenger
const onlyValidators = arg.validators

const partnersWithServices = await Promise.all(
response.data.map(async partner => {
Expand Down Expand Up @@ -329,24 +345,47 @@ export const partnersApi = createApi({
}
}),
)

// Filter out null values only if onMessenger is true
const filteredPartners = onMessenger
? partnersWithServices.filter(partner => partner !== null)
: partnersWithServices

let validators = (await caminoClient.PChain().getCurrentValidators()).validators
let partnersWithValidatorStatus = await Promise.all(
filteredPartners.map(async p => {
let pChainAddress = p.attributes.pChainAddresses.find(
elem =>
elem.Network.toLowerCase() === selectedNetwork.name.toLowerCase(),
)
if (pChainAddress?.pAddress) {
try {
let nodeID = await getRegisteredNode(
getAddress(pChainAddress?.pAddress),
)
let isValidator = !!validators.find(v => v.nodeID === nodeID)
if (isValidator) return { ...p, isValidator: true }
else return { ...p, isValidator: false }
} catch (error) {
return { ...p, isValidator: false }
}
}
return { ...p, isValidator: false }
}),
)
const filteredValidatorsPartners = onlyValidators
? partnersWithValidatorStatus.filter(partner => partner.isValidator)
: partnersWithValidatorStatus
// Update the meta information to reflect the new number of results
const updatedMeta = {
...response.meta,
pagination: {
...response.meta.pagination,
total: onMessenger
? filteredPartners.length
: response.meta.pagination.total,
total:
onMessenger || onlyValidators
? filteredValidatorsPartners.length
: response.meta.pagination.total,
},
}

return { data: filteredPartners, meta: updatedMeta }
return { data: filteredValidatorsPartners, meta: updatedMeta }
},
}),
fetchPartnerData: build.query<
Expand Down

0 comments on commit fd6bd2a

Please sign in to comment.