From aa67b9531a30dfd96545aefd67416b7ec0aa3c1c Mon Sep 17 00:00:00 2001 From: cuteolaf Date: Tue, 6 Aug 2024 07:07:22 -0700 Subject: [PATCH] remove requests to delegates.json --- src/hooks/useValidatorHistory.ts | 33 +++++++++++++++++++---------- src/services/delegateService.ts | 36 +++++++++----------------------- src/services/validatorService.ts | 5 +---- 3 files changed, 33 insertions(+), 41 deletions(-) diff --git a/src/hooks/useValidatorHistory.ts b/src/hooks/useValidatorHistory.ts index 8d519a9e..60b5eab4 100644 --- a/src/hooks/useValidatorHistory.ts +++ b/src/hooks/useValidatorHistory.ts @@ -13,8 +13,8 @@ import { ValidatorStakeHistoryResponse, ValidatorsStakeHistoryResponse, } from "../model/validator"; -import { fetchVerifiedDelegates } from "../services/delegateService"; import { shortenHash } from "../utils/shortenHash"; +import verifiedDelegates from "../delegates"; export function useValidatorStakeHistory( address: string @@ -70,10 +70,12 @@ export function useValidatorsStakeHistory( ): ValidatorsStakeHistoryResponse { const rollbar = useRollbar(); - const [data, setData] = useState<{ - address: string; - data: ValidatorStakeHistory[]; - }[]>([]); + const [data, setData] = useState< + { + address: string; + data: ValidatorStakeHistory[]; + }[] + >([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(); @@ -83,7 +85,10 @@ export function useValidatorsStakeHistory( }); const valis = []; for (const validator of validators.data) { - if (hasWeightCopiers || !weightCopiers.includes(validator.address)) { + if ( + hasWeightCopiers || + !weightCopiers.includes(validator.address) + ) { valis.push(validator.address); } if (valis.length >= 12) break; @@ -94,9 +99,10 @@ export function useValidatorsStakeHistory( const from = now - 8 * 24 * 60 * 60 * 1000; const stats: ValidatorStakeHistoryPaginatedResponse = - await getValidatorStakeHistory(valis, new Date(from).toISOString()); - - const verifiedDelegates = await fetchVerifiedDelegates(); + await getValidatorStakeHistory( + valis, + new Date(from).toISOString() + ); const result: { address: string; @@ -104,13 +110,18 @@ export function useValidatorsStakeHistory( }[] = []; for (const vali of valis) { const data = stats.data.filter((stat) => stat.address === vali); - const current = validators.data.find((it) => it.address === vali); + const current = validators.data.find( + (it) => it.address === vali + ); data.push({ timestamp: new Date(now).toISOString().substring(0, 19), ...current, } as ValidatorStakeHistory); result.push({ - address: verifiedDelegates[vali]?.name ?? shortenHash(vali) ?? "", + address: + verifiedDelegates[vali]?.name ?? + shortenHash(vali) ?? + "", data, }); } diff --git a/src/services/delegateService.ts b/src/services/delegateService.ts index 5a83bf3d..0d349dde 100644 --- a/src/services/delegateService.ts +++ b/src/services/delegateService.ts @@ -9,6 +9,7 @@ import { PaginationOptions } from "../model/paginationOptions"; import { extractItems } from "../utils/extractItems"; import { fetchIndexer } from "./fetchService"; +import verifiedDelegates from "../delegates"; export type DelegateFilter = { [key: string]: any; @@ -55,21 +56,6 @@ export async function getValidatorBalances( return fetchValidatorBalances(filter); } -export async function fetchVerifiedDelegates() { - const res = await fetch( - "https://raw.githubusercontent.com/opentensor/bittensor-delegates/main/public/delegates.json", - { - method: "GET", - } - ); - - if (res.status !== 200) return {}; - - const delegates: Record = await res.json(); - - return delegates; -} - /*** PRIVATE ***/ async function fetchDelegates( @@ -82,12 +68,12 @@ async function fetchDelegates( delegates(first: $first, after: $after, filter: $filter, orderBy: $order) { nodes { id - account - delegate - action - amount - blockNumber - extrinsicId + account + delegate + action + amount + blockNumber + extrinsicId } pageInfo { endCursor @@ -104,7 +90,6 @@ async function fetchDelegates( order, } ); - const verifiedDelegates = await fetchVerifiedDelegates(); const items = extractItems( response.delegates, pagination, @@ -127,9 +112,9 @@ async function fetchDelegateBalances( delegateBalances(first: $first, after: $after, filter: $filter, orderBy: $order) { nodes { id - account - delegate - amount + account + delegate + amount updatedAt delegateFrom } @@ -148,7 +133,6 @@ async function fetchDelegateBalances( order, } ); - const verifiedDelegates = await fetchVerifiedDelegates(); const items = extractItems( response.delegateBalances, pagination, diff --git a/src/services/validatorService.ts b/src/services/validatorService.ts index e510438d..870c0004 100644 --- a/src/services/validatorService.ts +++ b/src/services/validatorService.ts @@ -14,9 +14,9 @@ import { extractItems } from "../utils/extractItems"; import { fetchHistorical, fetchIndexer } from "./fetchService"; import { isAddress } from "@polkadot/util-crypto"; import { DataError } from "../utils/error"; -import { fetchVerifiedDelegates } from "./delegateService"; import { DelegateInfo } from "../model/delegate"; import { rawAmountToDecimaledString } from "../utils/number"; +import verifiedDelegates from "../delegates"; export type ValidatorsFilter = object; @@ -60,7 +60,6 @@ export async function getValidator(filter: ValidatorsFilter) { } ); - const verifiedDelegates = await fetchVerifiedDelegates(); const data = extractItems( response.validators, { limit: 1 }, @@ -116,8 +115,6 @@ export async function getValidators( order, } ); - - const verifiedDelegates = await fetchVerifiedDelegates(); return extractItems( response.validators, pagination,