diff --git a/nym-wallet/src/hooks/useGetBalance.ts b/nym-wallet/src/hooks/useGetBalance.ts index f589a03d66..ec756b6d63 100644 --- a/nym-wallet/src/hooks/useGetBalance.ts +++ b/nym-wallet/src/hooks/useGetBalance.ts @@ -1,10 +1,17 @@ import { useCallback, useEffect, useState } from 'react' import { invoke } from '@tauri-apps/api' -import { Balance, Coin, OriginalVestingResponse } from '../types' -import { getVestingCoins, getVestedCoins, getLockedCoins, getSpendableCoins, getOriginalVesting } from '../requests' +import { Balance, Coin, OriginalVestingResponse, Period } from '../types' +import { + getVestingCoins, + getVestedCoins, + getLockedCoins, + getSpendableCoins, + getOriginalVesting, + getCurrentVestingPeriod, +} from '../requests' type TTokenAllocation = { - [key in 'vesting' | 'vested' | 'locked' | 'spendable' ]: Coin['amount'] + [key in 'vesting' | 'vested' | 'locked' | 'spendable']: Coin['amount'] } export type TUseuserBalance = { @@ -12,6 +19,7 @@ export type TUseuserBalance = { balance?: Balance tokenAllocation?: TTokenAllocation originalVesting?: OriginalVestingResponse + currentVestingPeriod?: Period isLoading: boolean fetchBalance: () => void clearBalance: () => void @@ -23,6 +31,7 @@ export const useGetBalance = (address?: string): TUseuserBalance => { const [error, setError] = useState() const [tokenAllocation, setTokenAllocation] = useState() const [originalVesting, setOriginalVesting] = useState() + const [currentVestingPeriod, setCurrentVestingPeriod] = useState() const [isLoading, setIsLoading] = useState(false) const fetchBalance = useCallback(async () => { @@ -44,22 +53,26 @@ export const useGetBalance = (address?: string): TUseuserBalance => { setIsLoading(true) if (address) { try { - const [originalVestingValue, vestingCoins, vestedCoins, lockedCoins, spendableCoins] = await Promise.all([ - getOriginalVesting(address), - getVestingCoins(address), - getVestedCoins(address), - getLockedCoins(address), - getSpendableCoins(address), - ]) + const [originalVestingValue, vestingCoins, vestedCoins, lockedCoins, spendableCoins, currentVestingPeriod] = + await Promise.all([ + getOriginalVesting(address), + getVestingCoins(address), + getVestedCoins(address), + getLockedCoins(address), + getSpendableCoins(address), + getCurrentVestingPeriod(address), + ]) setOriginalVesting(originalVestingValue) - + setCurrentVestingPeriod(currentVestingPeriod) setTokenAllocation({ vesting: vestingCoins.amount, vested: vestedCoins.amount, locked: lockedCoins.amount, spendable: spendableCoins.amount, }) + + console.log(currentVestingPeriod) } catch (e) { clearTokenAllocation() clearOriginalVesting() @@ -93,6 +106,7 @@ export const useGetBalance = (address?: string): TUseuserBalance => { balance, tokenAllocation, originalVesting, + currentVestingPeriod, fetchBalance, clearBalance, fetchTokenAllocation, diff --git a/nym-wallet/src/pages/balance/vesting.tsx b/nym-wallet/src/pages/balance/vesting.tsx index 64bc1c7fac..3e7af935f2 100644 --- a/nym-wallet/src/pages/balance/vesting.tsx +++ b/nym-wallet/src/pages/balance/vesting.tsx @@ -18,6 +18,7 @@ import { useSnackbar } from 'notistack' import { NymCard } from '../../components' import { ClientContext } from '../../context/main' import { withdrawVestedCoins } from '../../requests' +import { Period } from '../../types' export const VestingCard = () => { const { userBalance, currency } = useContext(ClientContext) @@ -151,7 +152,8 @@ const VestingTable = () => { {userBalance.tokenAllocation?.vesting || 'n/a'} {currency?.major} - {userBalance.originalVesting?.number_of_periods} + + {vestingPeriod(userBalance.currentVestingPeriod, userBalance.originalVesting?.number_of_periods)} { ) } + + +const vestingPeriod = (current?:Period, original?: number ) => { + + if (current === "After") return "Complete" + + if (typeof current === "object" && typeof original === "number") return `${current.In + 1}/${original}` + + return "N/A" +} \ No newline at end of file diff --git a/nym-wallet/src/requests/index.ts b/nym-wallet/src/requests/index.ts index 05f4b051a0..f342dc3765 100644 --- a/nym-wallet/src/requests/index.ts +++ b/nym-wallet/src/requests/index.ts @@ -2,6 +2,6 @@ export * from './account' export * from './actions' export * from './coin' export * from './contract' -export * from './network' -export * from './queries' export * from './vesting' +export * from './network' +export * from './queries' \ No newline at end of file diff --git a/nym-wallet/src/requests/vesting.ts b/nym-wallet/src/requests/vesting.ts index 8c01f67a2f..e77fd2a923 100644 --- a/nym-wallet/src/requests/vesting.ts +++ b/nym-wallet/src/requests/vesting.ts @@ -1,6 +1,6 @@ import { invoke } from '@tauri-apps/api' import { majorToMinor, minorToMajor } from '.' -import { Coin, OriginalVestingResponse } from '../types' +import { Coin, OriginalVestingResponse, Period } from '../types' export const getLockedCoins = async (address: string): Promise => { const res: Coin = await invoke('locked_coins', { address }) @@ -31,3 +31,5 @@ export const withdrawVestedCoins = async (amount: string) => { const minor = await majorToMinor(amount) await invoke('withdraw_vested_coins', { amount: { amount: minor.amount, denom: 'Minor' } }) } + +export const getCurrentVestingPeriod = async (address: string): Promise => await invoke('get_current_vesting_period', {address}) diff --git a/nym-wallet/src/types/rust/index.ts b/nym-wallet/src/types/rust/index.ts index 1faa19d09a..2113d69d71 100644 --- a/nym-wallet/src/types/rust/index.ts +++ b/nym-wallet/src/types/rust/index.ts @@ -18,3 +18,4 @@ export * from './mixnodestatusresponse' export * from './inclusionprobabilityresponse' export * from './network' export * from './originalvestingresponse' +export * from './period' \ No newline at end of file diff --git a/nym-wallet/src/types/rust/period.ts b/nym-wallet/src/types/rust/period.ts index 86a901216f..fec990e0fa 100644 --- a/nym-wallet/src/types/rust/period.ts +++ b/nym-wallet/src/types/rust/period.ts @@ -1 +1 @@ -export type Period = "Before" | number | "After"; \ No newline at end of file +export type Period = "Before" | {In: number} | "After"; \ No newline at end of file diff --git a/nym-wallet/src/utils/index.ts b/nym-wallet/src/utils/index.ts index 6bc81a81a4..1bb1ec46dc 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 bs58 from 'bs58' import { minor, valid } from 'semver' import { userBalance, majorToMinor, getGasFee } from '../requests' -import { Coin, Network, TCurrency } from '../types' +import { Coin, Network, Period, TCurrency } from '../types' export const validateKey = (key: string, bytesLength: number): boolean => { // it must be a valid base58 key