From 4fbf1cd876aaf98362dfe797db442fcf894e527f Mon Sep 17 00:00:00 2001 From: gala1234 Date: Wed, 30 Mar 2022 17:03:00 +0200 Subject: [PATCH] connexion to fetch validator urls --- .../src/components/ValidatorSelector.tsx | 43 ++++++++++--------- nym-wallet/src/requests/index.ts | 2 +- nym-wallet/src/requests/validators.ts | 18 ++++---- nym-wallet/src/types/rust/index.ts | 1 + nym-wallet/src/utils/index.ts | 12 +++++- 5 files changed, 45 insertions(+), 31 deletions(-) diff --git a/nym-wallet/src/components/ValidatorSelector.tsx b/nym-wallet/src/components/ValidatorSelector.tsx index 44f2023cb6..84459a1a5f 100644 --- a/nym-wallet/src/components/ValidatorSelector.tsx +++ b/nym-wallet/src/components/ValidatorSelector.tsx @@ -1,47 +1,50 @@ -import React, { useEffect, useState } from 'react'; -import { FormControl, InputLabel, ListItemText, MenuItem, Select, SelectChangeEvent, Typography } from '@mui/material'; -// import { getValidators } from '../requests'; +import React, { useContext, useEffect, useState } from 'react'; +import { ListItemText, MenuItem, Select, SelectChangeEvent, Typography } from '@mui/material'; +import { ClientContext } from '../context/main'; +import { validatorUrls } from '../utils'; -interface ValidatorDropdownProps { - onChangeValidatorSelection: (validator: TValidatorOption) => void; -} +type TValidatorUrl = string; -type TValidatorOption = string; - -export const ValidatorSelector: React.FC = ({ +export const ValidatorSelector: React.FC<{ onChangeValidatorSelection: (validator: TValidatorUrl) => void }> = ({ onChangeValidatorSelection, }) => { - const [validators, setValidators] = useState(null); - const [selectedValidator, setSelectedValidator] = useState(''); - + const [validators, setValidators] = useState(); + const [selectedValidator, setSelectedValidator] = useState(''); + + const { + network + } = useContext(ClientContext); + useEffect(() => { - // (async () => { - // await getValidators(); - // })(); - setValidators(['aaa', 'bbb', 'ccc']); + (async () => { + if(network) { + const validator = await validatorUrls(network); + setValidators(validator?.urls); + } + })(); }, []); useEffect(() => { onChangeValidatorSelection(selectedValidator); }, [selectedValidator]); - return validators && + return ( - }; + )}; \ No newline at end of file diff --git a/nym-wallet/src/requests/index.ts b/nym-wallet/src/requests/index.ts index 43a24b1430..fec39c1e89 100644 --- a/nym-wallet/src/requests/index.ts +++ b/nym-wallet/src/requests/index.ts @@ -5,4 +5,4 @@ export * from './contract'; export * from './vesting'; export * from './network'; export * from './queries'; -// export * from './validators'; +export * from './validators'; diff --git a/nym-wallet/src/requests/validators.ts b/nym-wallet/src/requests/validators.ts index 673a2367b6..f72b46767b 100644 --- a/nym-wallet/src/requests/validators.ts +++ b/nym-wallet/src/requests/validators.ts @@ -1,11 +1,11 @@ -//TODO once merge branch feature/wallet-expose-validator-urls +import { invoke } from '@tauri-apps/api'; +import { Network } from '../types'; -// import { -// ValidatorUrls -// } from '../types'; +import { + ValidatorUrls +} from '../types'; -// export const getValidators = async (): Promise => { -// const ValidatorUrls: ValidatorUrls = await invoke('locked_coins'); -// const major = await minorToMajor(coin.amount); -// return major; -// }; \ No newline at end of file +export const getValidatorUrls = async (network: Network): Promise => { + const res: ValidatorUrls = await invoke('get_validator_nymd_urls', { network }); + return res; +}; diff --git a/nym-wallet/src/types/rust/index.ts b/nym-wallet/src/types/rust/index.ts index d82e3f75c5..b7554d077d 100644 --- a/nym-wallet/src/types/rust/index.ts +++ b/nym-wallet/src/types/rust/index.ts @@ -23,3 +23,4 @@ export * from './vestingperiod'; export * from './pendingundelegate'; export * from './delegationevent'; export * from './epoch'; +export * from './validatorurls'; diff --git a/nym-wallet/src/utils/index.ts b/nym-wallet/src/utils/index.ts index 6d60282783..924a45eb2b 100644 --- a/nym-wallet/src/utils/index.ts +++ b/nym-wallet/src/utils/index.ts @@ -2,7 +2,7 @@ import { invoke } from '@tauri-apps/api'; import { appWindow } from '@tauri-apps/api/window'; import bs58 from 'bs58'; import { minor, valid } from 'semver'; -import { userBalance, majorToMinor, getLockedCoins, getSpendableCoins } from '../requests'; +import { userBalance, majorToMinor, getLockedCoins, getSpendableCoins, getValidatorUrls } from '../requests'; import { Coin, Network, TCurrency } from '../types'; import { Console } from './console'; @@ -150,3 +150,13 @@ export const maximizeWindow = async () => { export function removeObjectDuplicates(arr: T[], id: K) { return arr.filter((v, i, a) => a.findIndex((v2) => v2[id] === v[id]) === i); } + +export const validatorUrls = async (network: Network) => { + try { + const urls = await getValidatorUrls(network); + return urls; + } catch (e) { + Console.error(e as string); + } + return null; +}; \ No newline at end of file