From 718d151aad402141cbfa955d6b7da1fc853e9992 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 15 Jan 2024 09:33:46 +0100 Subject: [PATCH] Add validatorState to ChainsContext --- context/ChainsContext/helpers.tsx | 4 ++++ context/ChainsContext/index.tsx | 33 +++++++++++++++++++++++++++++-- context/ChainsContext/types.tsx | 14 +++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/context/ChainsContext/helpers.tsx b/context/ChainsContext/helpers.tsx index 0d0cf53..0ac88bd 100644 --- a/context/ChainsContext/helpers.tsx +++ b/context/ChainsContext/helpers.tsx @@ -41,6 +41,10 @@ export const setChain = (dispatch: Dispatch, chain: ChainInfo) => { dispatch({ type: "setChain", payload: chain }); }; +export const loadValidators = (dispatch: Dispatch) => { + dispatch({ type: "loadValidators" }); +}; + export const setNewConnection = (dispatch: Dispatch, newConnection: NewConnection) => { dispatch({ type: "setNewConnection", payload: newConnection }); }; diff --git a/context/ChainsContext/index.tsx b/context/ChainsContext/index.tsx index bbfef9b..5233027 100644 --- a/context/ChainsContext/index.tsx +++ b/context/ChainsContext/index.tsx @@ -1,3 +1,4 @@ +import { getAllValidators } from "@/lib/staking"; import { ReactNode, createContext, useContext, useEffect, useReducer } from "react"; import { emptyChain, isChainInfoFilled, setChain, setChains, setChainsError } from "./helpers"; import { getChain, getNodeFromArray, useChainsFromRegistry } from "./service"; @@ -6,7 +7,7 @@ import { Action, ChainsContextType, State } from "./types"; const ChainsContext = createContext(undefined); -const chainsReducer = (state: State, action: Action) => { +const chainsReducer = (state: State, action: Action): State => { switch (action.type) { case "setChains": { return { ...state, chains: action.payload }; @@ -26,11 +27,23 @@ const chainsReducer = (state: State, action: Action) => { addRecentChainNameInStorage(action.payload.registryName); setChainInUrl(action.payload, state.chains); - return { ...state, chain: action.payload }; + return { + ...state, + chain: action.payload, + validatorState: { validators: [], status: "initial" }, + }; } case "addNodeAddress": { return { ...state, chain: { ...state.chain, nodeAddress: action.payload } }; } + case "loadValidators": { + return state.validatorState.status === "initial" + ? { ...state, validatorState: { ...state.validatorState, status: "loading" } } + : state; + } + case "setValidatorState": { + return { ...state, validatorState: action.payload }; + } case "setNewConnection": { return { ...state, newConnection: action.payload }; } @@ -52,6 +65,7 @@ export const ChainsProvider = ({ children }: ChainsProviderProps) => { chain: emptyChain, chains: { mainnets: new Map(), testnets: new Map(), localnets: new Map() }, newConnection: { action: "edit" }, + validatorState: { validators: [], status: "initial" }, }); const { chainItems, chainItemsError } = useChainsFromRegistry(); @@ -78,6 +92,21 @@ export const ChainsProvider = ({ children }: ChainsProviderProps) => { })(); }, [state.chain]); + useEffect(() => { + (async function loadValidators() { + if (state.validatorState.status === "loading" && state.chain.nodeAddress) { + try { + const validators = await getAllValidators(state.chain.nodeAddress); + console.log({ validators }); + dispatch({ type: "setValidatorState", payload: { validators, status: "done" } }); + } catch (e) { + console.error(e instanceof Error ? e.message : "Failed to load validators"); + dispatch({ type: "setValidatorState", payload: { validators: [], status: "error" } }); + } + } + })(); + }, [state.chain.nodeAddress, state.validatorState.status]); + return {children}; }; diff --git a/context/ChainsContext/types.tsx b/context/ChainsContext/types.tsx index 5c8c650..20bf452 100644 --- a/context/ChainsContext/types.tsx +++ b/context/ChainsContext/types.tsx @@ -1,3 +1,4 @@ +import { Validator } from "cosmjs-types/cosmos/staking/v1beta1/staking"; import { RegistryAsset } from "../../types/chainRegistry"; export interface ChainsContextType { @@ -8,6 +9,7 @@ export interface ChainsContextType { export interface State { readonly chains: ChainItems; readonly chain: ChainInfo; + readonly validatorState: ValidatorState; readonly newConnection: NewConnection; readonly chainsError?: string | null; } @@ -36,6 +38,11 @@ export interface ChainInfo { readonly explorerLinks: ExplorerLinks; } +export interface ValidatorState { + readonly validators: readonly Validator[]; + readonly status: "initial" | "loading" | "done" | "error"; +} + export type ExplorerLinks = { readonly tx: string; readonly account: string; @@ -64,6 +71,13 @@ export type Action = readonly type: "addNodeAddress"; readonly payload: string; } + | { + readonly type: "loadValidators"; + } + | { + readonly type: "setValidatorState"; + readonly payload: ValidatorState; + } | { readonly type: "setNewConnection"; readonly payload: NewConnection;