Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions src/api/MinersDashboardApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { useApiQuery } from './ApiUtils';
import { SSE_FALLBACK_INTERVAL } from './constants';
import type {
ActiveSwap,
CrownHistoryRow,
CrownRateHistoryRow,
CurrentCrownMap,
Direction,
HaltState,
LeaderboardRow,
MinerRateHistoryRow,
MinerStats,
NetworkOverview,
Range,
ScoreFactors,
} from './models';

const CROWN_REFRESH_MS = 12_000;

export const useCurrentCrown = () =>
useApiQuery<CurrentCrownMap>('crown', '/crown', CROWN_REFRESH_MS);

export const useCrownHistory = (params: {
direction: Direction;
fromBlock?: number;
toBlock?: number;
}) =>
useApiQuery<CrownHistoryRow[]>(
'crown-history',
'/crown/history',
CROWN_REFRESH_MS,
{
direction: params.direction,
fromBlock: params.fromBlock,
toBlock: params.toBlock,
},
);

export const useCrownRateHistory = (params: {
direction: Direction;
fromBlock?: number;
toBlock?: number;
blocks?: number;
}) =>
useApiQuery<CrownRateHistoryRow[]>(
'crown-rate-history',
'/crown/rate-history',
CROWN_REFRESH_MS,
{
direction: params.direction,
fromBlock: params.fromBlock,
toBlock: params.toBlock,
blocks: params.blocks,
},
);

export const useMinerLeaderboard = (range: Range = '30d') =>
useApiQuery<LeaderboardRow[]>(
'miners-leaderboard',
'/miners/leaderboard',
SSE_FALLBACK_INTERVAL,
{
range,
},
);

export const useMinerStats = (hotkey: string, range: Range = '30d') =>
useApiQuery<MinerStats>(
'miner-stats',
`/miners/${hotkey}/stats`,
SSE_FALLBACK_INTERVAL,
{ range },
!!hotkey,
);

export const useScoreFactorsWindow = (
hotkey: string,
direction: Direction,
fromBlock: number | undefined,
toBlock: number | undefined,
) =>
useApiQuery<ScoreFactors>(
'miner-score-factors-window',
`/miners/${hotkey}/score-factors`,
SSE_FALLBACK_INTERVAL,
{ direction, fromBlock, toBlock },
!!hotkey && fromBlock != null && toBlock != null && toBlock >= fromBlock,
);

export const useMinerSwaps = (
hotkey: string,
params: { limit?: number; offset?: number; status?: string } = {},
) =>
useApiQuery<{ rows: ActiveSwap[]; totalCount: number }>(
'miner-swaps',
`/miners/${hotkey}/swaps`,
SSE_FALLBACK_INTERVAL,
params,
!!hotkey,
);

export const useMinerRateHistory = (
hotkey: string,
params: { fromBlock?: number; toBlock?: number; blocks?: number } = {},
) =>
useApiQuery<MinerRateHistoryRow[]>(
'miner-rate-history',
`/miners/${hotkey}/rate-history`,
SSE_FALLBACK_INTERVAL,
params,
!!hotkey,
);

export const useNetworkOverview = (range: Range = '30d') =>
useApiQuery<NetworkOverview>(
'network-overview',
'/network/overview',
SSE_FALLBACK_INTERVAL,
{
range,
},
);

export const useHaltState = () =>
useApiQuery<HaltState>(
'network-halt-state',
'/network/halt-state',
CROWN_REFRESH_MS,
);
1 change: 1 addition & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './ApiUtils';
export * from './EventsApi';
export * from './MinersApi';
export * from './MinersDashboardApi';
export * from './ProtocolApi';
export * from './ReservationsApi';
export * from './StatsApi';
Expand Down
94 changes: 94 additions & 0 deletions src/api/models/MinersDashboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
export type Direction = 'BTC-TAO' | 'TAO-BTC';
export type Range = '24h' | '7d' | '30d' | '90d' | 'all';

export type CurrentCrown = {
uid: number | null;
hotkey: string | null;
rate: number | null;
sinceBlock: number | null;
};

export type CurrentCrownMap = Record<Direction, CurrentCrown>;

export type CrownHistoryRow = {
block: number;
hotkey: string;
uid: number | null;
rate: number;
};

export type CrownRateHistoryRow = {
block: number;
rate: number;
};

export type LeaderboardRow = {
uid: number;
hotkey: string;
crownShare: number;
successRate: number;
completedSwaps: number;
timedOutSwaps: number;
volumeTao: string;
collateralRao: string;
isActive: boolean;
currentCrownDirections: Direction[];
};

export type ScoreFactors = {
capacityFactor: number;
collateralRao: string;
maxSwapAmountRao: string;

volumeFactor: number;
volumeShareWindow: number;
crownShareWindow: number;
volumeTaoWindow: string;
networkVolumeTaoWindow: string;
previousCrownShareWindow: number;
previousVolumeFactor: number;

closedSwaps: number;
credibilityRamp: number;
credibilityRampTarget: number;
successRate30d: number;
successMultiplier: number;
};

export type MinerStats = {
uid: number | null;
totalSwaps: number;
completedSwaps: number;
timedOutSwaps: number;
successRate: number;
volumeTao: string;
avgFulfillSec: number | null;
avgCompleteSec: number | null;
crownShare: number;
isActive: boolean;
collateralRao: string;
activatedAt: number | null;
currentCrownDirections: Direction[];
scoreFactors: ScoreFactors;
};

export type MinerRateHistoryRow = {
block: number;
rate: number;
fromChain: string;
toChain: string;
};

export type PairMix = { pair: string; pct: number };

export type NetworkOverview = {
volumeTao: string;
totalSwaps: number;
networkSuccessRate: number;
activeMiners: number;
pairMix: PairMix[];
scoringWindowVolumeTao: string;
maxSwapAmountRao: string;
};

export type HaltState = { halted: boolean; asOfBlock: number };
2 changes: 2 additions & 0 deletions src/api/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './Events';
export * from './Miners';
export * from './MinersDashboard';
export * from './searchParams';
export * from './Protocol';
export * from './Reservations';
export * from './Stats';
Expand Down
28 changes: 28 additions & 0 deletions src/api/models/searchParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Direction, Range } from './MinersDashboard';

// Crown-grid window mode lives only on the URL — not on any API contract —
// so the type lives here next to the search-param guards that read it.
export type CrownRange = '1h' | '2h' | '4h';
export type RateRange = '1h' | '4h' | '24h' | '7d';

const RANGES: readonly Range[] = ['24h', '7d', '30d', '90d', 'all'];
const CROWN_RANGES: readonly CrownRange[] = ['1h', '2h', '4h'];
const RATE_RANGES: readonly RateRange[] = ['1h', '4h', '24h', '7d'];

export const isRange = (v: string | null): v is Range =>
RANGES.includes((v ?? '') as Range);

export const isDirection = (v: string | null): v is Direction =>
v === 'BTC-TAO' || v === 'TAO-BTC';

export const isCrownRange = (v: string | null): v is CrownRange =>
CROWN_RANGES.includes((v ?? '') as CrownRange);

export const isRateRange = (v: string | null): v is RateRange =>
RATE_RANGES.includes((v ?? '') as RateRange);

export const parseBlockParam = (v: string | null): number | null => {
if (v == null || v === '') return null;
const n = Number(v);
return Number.isInteger(n) && n >= 0 ? n : null;
};
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export * from './nav';
export * from './animated';
export * from './landing';
export * from './agents';
export * from './miners';
export * from './swap';
Loading
Loading