From f6ec12db94140f3f3c92e0a705f161bb156f1410 Mon Sep 17 00:00:00 2001 From: fmtabbara Date: Wed, 9 Mar 2022 12:15:29 +0000 Subject: [PATCH] allow delegation of vested tokens --- nym-wallet/src/index.tsx | 4 +- nym-wallet/src/pages/bond/BondForm.tsx | 5 +- .../src/pages/delegate/DelegateForm.tsx | 59 +++++++++++-------- .../src/pages/delegate/validationSchema.ts | 24 +++++++- nym-wallet/src/types/global.ts | 6 ++ 5 files changed, 66 insertions(+), 32 deletions(-) diff --git a/nym-wallet/src/index.tsx b/nym-wallet/src/index.tsx index 0ac5588621..13e782df80 100644 --- a/nym-wallet/src/index.tsx +++ b/nym-wallet/src/index.tsx @@ -1,4 +1,4 @@ -import React, { useContext, useEffect, useLayoutEffect } from 'react' +import React, { useContext, useEffect } from 'react' import ReactDOM from 'react-dom' import { ErrorBoundary } from 'react-error-boundary' import { BrowserRouter as Router } from 'react-router-dom' @@ -15,7 +15,7 @@ import { maximizeWindow } from './utils' const App = () => { const { clientDetails } = useContext(ClientContext) - useLayoutEffect(() => { + useEffect(() => { maximizeWindow() }, []) diff --git a/nym-wallet/src/pages/bond/BondForm.tsx b/nym-wallet/src/pages/bond/BondForm.tsx index d5a9bf0177..13f0b3622e 100644 --- a/nym-wallet/src/pages/bond/BondForm.tsx +++ b/nym-wallet/src/pages/bond/BondForm.tsx @@ -12,11 +12,10 @@ import { } from '@mui/material' import { yupResolver } from '@hookform/resolvers/yup' import { useForm } from 'react-hook-form' -import { EnumNodeType } from '../../types/global' import { NodeTypeSelector } from '../../components/NodeTypeSelector' import { bond, vestingBond, majorToMinor } from '../../requests' import { validationSchema } from './validationSchema' -import { Gateway, MixNode, TBondArgs } from '../../types' +import { Gateway, MixNode, TBondArgs, EnumNodeType } from '../../types' import { ClientContext } from '../../context/main' import { Fee, TokenPoolSelector } from '../../components' @@ -107,7 +106,7 @@ export const BondForm = ({ const watchNodeType = watch('nodeType', defaultValues.nodeType) const watchAdvancedOptions = watch('withAdvancedOptions', defaultValues.withAdvancedOptions) - const onSubmit = async (data: TBondFormFields, cb: (data: TBondArgs) => Promise) => { + const onSubmit = async (data: TBondFormFields, cb: (data: TBondArgs) => Promise) => { const formattedData = formatData(data) const pledge = await majorToMinor(data.amount) diff --git a/nym-wallet/src/pages/delegate/DelegateForm.tsx b/nym-wallet/src/pages/delegate/DelegateForm.tsx index 469e885ccb..bfe934a3bb 100644 --- a/nym-wallet/src/pages/delegate/DelegateForm.tsx +++ b/nym-wallet/src/pages/delegate/DelegateForm.tsx @@ -1,24 +1,26 @@ -import React, { useContext } from 'react' +import React, { useEffect, useContext } from 'react' import { Box, Button, CircularProgress, FormControl, Grid, InputAdornment, TextField, Typography } from '@mui/material' import { yupResolver } from '@hookform/resolvers/yup' import { useForm } from 'react-hook-form' -import { EnumNodeType } from '../../types' +import { DelegationResult, EnumNodeType, TDelegateArgs } from '../../types' import { validationSchema } from './validationSchema' import { ClientContext } from '../../context/main' -import { delegate, majorToMinor } from '../../requests' +import { delegate, majorToMinor, vestingDelegateToMixnode } from '../../requests' import { checkHasEnoughFunds } from '../../utils' -import { Fee } from '../../components' +import { Fee, TokenPoolSelector } from '../../components' type TDelegateForm = { - nodeType: EnumNodeType identity: string amount: string + tokenPool: string + type: EnumNodeType } const defaultValues: TDelegateForm = { - nodeType: EnumNodeType.mixnode, identity: '', amount: '', + tokenPool: 'balance', + type: EnumNodeType.mixnode, } export const DelegateForm = ({ @@ -30,37 +32,36 @@ export const DelegateForm = ({ }) => { const { register, - watch, handleSubmit, - setError, + setValue, + reset, formState: { errors, isSubmitting }, } = useForm({ defaultValues, resolver: yupResolver(validationSchema), }) - const watchNodeType = watch('nodeType', defaultValues.nodeType) + const { userBalance, currency, clientDetails } = useContext(ClientContext) - const { userBalance, currency } = useContext(ClientContext) - - const onSubmit = async (data: TDelegateForm) => { - const hasEnoughFunds = await checkHasEnoughFunds(data.amount) - if (!hasEnoughFunds) { - return setError('amount', { - message: 'Not enough funds in wallet', - }) - } + useEffect(() => { + reset() + }, [clientDetails]) + const onSubmit = async (data: TDelegateForm, cb: (data: TDelegateArgs) => Promise) => { const amount = await majorToMinor(data.amount) - await delegate({ - type: data.nodeType, + await cb({ + type: data.type, identity: data.identity, amount, }) - .then((res) => { + .then(async (res) => { + if (data.tokenPool === 'balance') { + await userBalance.fetchBalance() + } else { + await userBalance.fetchTokenAllocation() + } onSuccess({ amount: data.amount, address: res.target_address }) - userBalance.fetchBalance() }) .catch((e) => { console.log(e) @@ -86,7 +87,13 @@ export const DelegateForm = ({ /> - + {userBalance.originalVesting && ( + + setValue('tokenPool', pool)} /> + + )} + + - + @@ -117,7 +124,9 @@ export const DelegateForm = ({ }} >