From 0cdb68bcf37a29bd7ce895fa2b9b9ff650fe31ca Mon Sep 17 00:00:00 2001 From: Fouad Tabbara Date: Mon, 14 Feb 2022 11:47:42 +0000 Subject: [PATCH] fix breaking changes + add vesting total period --- nym-wallet/src/hooks/useGetBalance.ts | 18 +++++++++++------- nym-wallet/src/pages/balance/vesting.tsx | 8 ++++---- nym-wallet/src/requests/vesting.ts | 9 +++++---- nym-wallet/src/types/rust/index.ts | 1 + 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/nym-wallet/src/hooks/useGetBalance.ts b/nym-wallet/src/hooks/useGetBalance.ts index 243d1b36c9..f589a03d66 100644 --- a/nym-wallet/src/hooks/useGetBalance.ts +++ b/nym-wallet/src/hooks/useGetBalance.ts @@ -1,16 +1,17 @@ import { useCallback, useEffect, useState } from 'react' import { invoke } from '@tauri-apps/api' -import { Balance, Coin } from '../types' -import { getVestingCoins, getVestedCoins, getLockedCoins, getSpendableCoins, originalVesting } from '../requests' +import { Balance, Coin, OriginalVestingResponse } from '../types' +import { getVestingCoins, getVestedCoins, getLockedCoins, getSpendableCoins, getOriginalVesting } from '../requests' type TTokenAllocation = { - [key in 'vesting' | 'vested' | 'locked' | 'spendable' | 'original']: Coin['amount'] + [key in 'vesting' | 'vested' | 'locked' | 'spendable' ]: Coin['amount'] } export type TUseuserBalance = { error?: string balance?: Balance tokenAllocation?: TTokenAllocation + originalVesting?: OriginalVestingResponse isLoading: boolean fetchBalance: () => void clearBalance: () => void @@ -21,6 +22,7 @@ export const useGetBalance = (address?: string): TUseuserBalance => { const [balance, setBalance] = useState() const [error, setError] = useState() const [tokenAllocation, setTokenAllocation] = useState() + const [originalVesting, setOriginalVesting] = useState() const [isLoading, setIsLoading] = useState(false) const fetchBalance = useCallback(async () => { @@ -43,25 +45,25 @@ export const useGetBalance = (address?: string): TUseuserBalance => { if (address) { try { const [originalVestingValue, vestingCoins, vestedCoins, lockedCoins, spendableCoins] = await Promise.all([ - Promise.resolve({amount: "100"}), + getOriginalVesting(address), getVestingCoins(address), getVestedCoins(address), getLockedCoins(address), getSpendableCoins(address), ]) - console.log(originalVestingValue, vestingCoins) + setOriginalVesting(originalVestingValue) setTokenAllocation({ - original: originalVestingValue.amount, vesting: vestingCoins.amount, vested: vestedCoins.amount, locked: lockedCoins.amount, spendable: spendableCoins.amount, }) } catch (e) { - console.log(e) clearTokenAllocation() + clearOriginalVesting() + console.log(e) } } setIsLoading(false) @@ -69,6 +71,7 @@ export const useGetBalance = (address?: string): TUseuserBalance => { const clearBalance = () => setBalance(undefined) const clearTokenAllocation = () => setTokenAllocation(undefined) + const clearOriginalVesting = () => setOriginalVesting(undefined) useEffect(() => { handleRefresh(address) @@ -89,6 +92,7 @@ export const useGetBalance = (address?: string): TUseuserBalance => { isLoading, balance, tokenAllocation, + originalVesting, fetchBalance, clearBalance, fetchTokenAllocation, diff --git a/nym-wallet/src/pages/balance/vesting.tsx b/nym-wallet/src/pages/balance/vesting.tsx index cdb136116c..64bc1c7fac 100644 --- a/nym-wallet/src/pages/balance/vesting.tsx +++ b/nym-wallet/src/pages/balance/vesting.tsx @@ -123,9 +123,9 @@ const VestingTable = () => { const [vestedPercentage, setVestedPercentage] = useState(0) const calculatPercentage = () => { - const { tokenAllocation } = userBalance - if (tokenAllocation?.vesting && tokenAllocation.vested && tokenAllocation.vested !== '0') { - const percentage = Math.round((+tokenAllocation.vested / +tokenAllocation.original) * 100) + const { tokenAllocation, originalVesting } = userBalance + if (tokenAllocation?.vesting && tokenAllocation.vested && tokenAllocation.vested !== '0' && originalVesting) { + const percentage = Math.round((+tokenAllocation.vested / +originalVesting?.amount.amount) * 100) setVestedPercentage(percentage) } else { setVestedPercentage(0) @@ -151,7 +151,7 @@ const VestingTable = () => { {userBalance.tokenAllocation?.vesting || 'n/a'} {currency?.major} - + {userBalance.originalVesting?.number_of_periods} => { const res: Coin = await invoke('locked_coins', { address }) @@ -21,9 +21,10 @@ export const getVestedCoins = async (vestingAccountAddress: string): Promise => { - const res: Coin = await invoke('original_vesting', { vestingAccountAddress }) - return await minorToMajor(res.amount) +export const getOriginalVesting = async (vestingAccountAddress: string): Promise => { + const res: OriginalVestingResponse = await invoke('original_vesting', { vestingAccountAddress }) + const majorValue = await minorToMajor(res.amount.amount) + return {...res, amount: majorValue} } export const withdrawVestedCoins = async (amount: string) => { diff --git a/nym-wallet/src/types/rust/index.ts b/nym-wallet/src/types/rust/index.ts index 6123c263cd..1faa19d09a 100644 --- a/nym-wallet/src/types/rust/index.ts +++ b/nym-wallet/src/types/rust/index.ts @@ -17,3 +17,4 @@ export * from './mixnodestatus' export * from './mixnodestatusresponse' export * from './inclusionprobabilityresponse' export * from './network' +export * from './originalvestingresponse'