Skip to content

Commit

Permalink
remove requests to delegates.json
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteolaf committed Aug 6, 2024
1 parent cbf36ef commit aa67b95
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 41 deletions.
33 changes: 22 additions & 11 deletions src/hooks/useValidatorHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<boolean>(true);
const [error, setError] = useState<DataError>();

Expand All @@ -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;
Expand All @@ -94,23 +99,29 @@ 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;
data: ValidatorStakeHistory[];
}[] = [];
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,
});
}
Expand Down
36 changes: 10 additions & 26 deletions src/services/delegateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, DelegateInfo> = await res.json();

return delegates;
}

/*** PRIVATE ***/

async function fetchDelegates(
Expand All @@ -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
Expand All @@ -104,7 +90,6 @@ async function fetchDelegates(
order,
}
);
const verifiedDelegates = await fetchVerifiedDelegates();
const items = extractItems(
response.delegates,
pagination,
Expand All @@ -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
}
Expand All @@ -148,7 +133,6 @@ async function fetchDelegateBalances(
order,
}
);
const verifiedDelegates = await fetchVerifiedDelegates();
const items = extractItems(
response.delegateBalances,
pagination,
Expand Down
5 changes: 1 addition & 4 deletions src/services/validatorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -60,7 +60,6 @@ export async function getValidator(filter: ValidatorsFilter) {
}
);

const verifiedDelegates = await fetchVerifiedDelegates();
const data = extractItems(
response.validators,
{ limit: 1 },
Expand Down Expand Up @@ -116,8 +115,6 @@ export async function getValidators(
order,
}
);

const verifiedDelegates = await fetchVerifiedDelegates();
return extractItems(
response.validators,
pagination,
Expand Down

0 comments on commit aa67b95

Please sign in to comment.