From 85852086ea7dbde404ef5ec24065db1cd50d4d78 Mon Sep 17 00:00:00 2001 From: fmtabbara Date: Thu, 17 Nov 2022 15:14:06 +0000 Subject: [PATCH] fix undelegating with vesting tokens --- nym-wallet/src/context/delegations.tsx | 25 ++++++---- nym-wallet/src/context/mocks/delegations.tsx | 49 +++++++++++++------- nym-wallet/src/pages/delegation/index.tsx | 24 +++++++--- 3 files changed, 65 insertions(+), 33 deletions(-) diff --git a/nym-wallet/src/context/delegations.tsx b/nym-wallet/src/context/delegations.tsx index 79aeb70d94..678b60c305 100644 --- a/nym-wallet/src/context/delegations.tsx +++ b/nym-wallet/src/context/delegations.tsx @@ -1,14 +1,20 @@ import React, { createContext, FC, useCallback, useContext, useEffect, useMemo, useState } from 'react'; -import { getDelegationSummary, undelegateAllFromMixnode } from 'src/requests/delegation'; +import { getDelegationSummary, undelegateAllFromMixnode, undelegateFromMixnode } from 'src/requests/delegation'; import { DelegationWithEverything, FeeDetails, DecCoin, TransactionExecuteResult, WrappedDelegationEvent, + Fee, } from '@nymproject/types'; import type { Network } from 'src/types'; -import { delegateToMixnode, getAllPendingDelegations, vestingDelegateToMixnode } from 'src/requests'; +import { + delegateToMixnode, + getAllPendingDelegations, + vestingDelegateToMixnode, + vestingUndelegateFromMixnode, +} from 'src/requests'; import { TPoolOption } from 'src/components'; import { decCoinToDisplay } from 'src/utils'; @@ -25,11 +31,8 @@ export type TDelegationContext = { tokenPool: TPoolOption, fee?: FeeDetails, ) => Promise; - undelegate: ( - mix_id: number, - usesVestingContractTokens: boolean, - fee?: FeeDetails, - ) => Promise; + undelegate: (mix_id: number, fee?: Fee) => Promise; + undelegateVesting: (mix_id: number) => Promise; }; export type TDelegationTransaction = { @@ -51,7 +54,10 @@ export const DelegationContext = createContext({ addDelegation: async () => { throw new Error('Not implemented'); }, - undelegate: async () => { + undelegate: () => { + throw new Error('Not implemented'); + }, + undelegateVesting: () => { throw new Error('Not implemented'); }, }); @@ -135,7 +141,8 @@ export const DelegationContextProvider: FC<{ totalRewards, refresh, addDelegation, - undelegate: undelegateAllFromMixnode, + undelegate: undelegateFromMixnode, + undelegateVesting: vestingUndelegateFromMixnode, }), [isLoading, error, delegations, pendingDelegations, totalDelegations], ); diff --git a/nym-wallet/src/context/mocks/delegations.tsx b/nym-wallet/src/context/mocks/delegations.tsx index 3674dc1494..bc216fd7bf 100644 --- a/nym-wallet/src/context/mocks/delegations.tsx +++ b/nym-wallet/src/context/mocks/delegations.tsx @@ -1,5 +1,12 @@ import React, { FC, useCallback, useEffect, useMemo, useState } from 'react'; -import { DelegationWithEverything, DecCoin, TransactionExecuteResult, FeeDetails } from '@nymproject/types'; +import { + DelegationWithEverything, + DecCoin, + TransactionExecuteResult, + FeeDetails, + Fee, + CurrencyDenom, +} from '@nymproject/types'; import { DelegationContext, TDelegationTransaction } from '../delegations'; import { mockSleep } from './utils'; @@ -154,11 +161,7 @@ export const MockDelegationContextProvider: FC<{}> = ({ children }) => { }; }; - const undelegate = async ( - mix_id: number, - _usesVestingContractTokens: boolean, - _fee?: FeeDetails, - ): Promise => { + const undelegate = async (mix_id: number, _fee?: Fee): Promise => { await mockSleep(SLEEP_MS); mockDelegations = mockDelegations.map((d) => { if (d.mix_id === mix_id) { @@ -175,18 +178,29 @@ export const MockDelegationContextProvider: FC<{}> = ({ children }) => { triggerStateUpdate(); }, 3000); - return [ - { - logs_json: '', - data_json: '', - transaction_hash: '', - gas_info: { - gas_wanted: { gas_units: BigInt(1) }, - gas_used: { gas_units: BigInt(1) }, - }, - fee: { amount: '1', denom: 'nym' }, + return { + logs_json: '', + data_json: '', + transaction_hash: '', + gas_info: { + gas_wanted: { gas_units: BigInt(1) }, + gas_used: { gas_units: BigInt(1) }, }, - ]; + fee: { amount: '1', denom: 'nym' as CurrencyDenom }, + }; + }; + + const undelegateVesting = async (mix_id: number, _fee?: FeeDetails) => { + return { + logs_json: '', + data_json: '', + transaction_hash: '', + gas_info: { + gas_wanted: { gas_units: BigInt(1) }, + gas_used: { gas_units: BigInt(1) }, + }, + fee: { amount: '1', denom: 'nym' as CurrencyDenom }, + }; }; const resetState = () => { @@ -226,6 +240,7 @@ export const MockDelegationContextProvider: FC<{}> = ({ children }) => { addDelegation, updateDelegation, undelegate, + undelegateVesting, }), [isLoading, error, delegations, totalDelegations, trigger], ); diff --git a/nym-wallet/src/pages/delegation/index.tsx b/nym-wallet/src/pages/delegation/index.tsx index 62418f0463..a70300388f 100644 --- a/nym-wallet/src/pages/delegation/index.tsx +++ b/nym-wallet/src/pages/delegation/index.tsx @@ -52,6 +52,7 @@ export const Delegation: FC<{ isStorybook?: boolean }> = ({ isStorybook }) => { isLoading, addDelegation, undelegate, + undelegateVesting, refresh: refreshDelegations, } = useDelegationContext(); @@ -206,7 +207,8 @@ export const Delegation: FC<{ isStorybook?: boolean }> = ({ isStorybook }) => { const handleUndelegate = async ( mixId: number, - identityKey: string, + // identityKey is no longer used + _: string, usesVestingContractTokens: boolean, fee?: FeeDetails, ) => { @@ -216,19 +218,27 @@ export const Delegation: FC<{ isStorybook?: boolean }> = ({ isStorybook }) => { }); setShowUndelegateModal(false); setCurrentDelegationListActionItem(undefined); - + let tx; try { - const txs = await undelegate(mixId, usesVestingContractTokens, fee); + if (usesVestingContractTokens) { + tx = await undelegateVesting(mixId); + } else { + tx = await undelegate(mixId, fee?.fee); + } + + // const txs = await undelegate(mixId, usesVestingContractTokens, fee); const balances = await getAllBalances(); setConfirmationModalProps({ status: 'success', action: 'undelegate', ...balances, - transactions: txs.map((tx) => ({ - url: `${urls(network).blockExplorer}/transaction/${tx.transaction_hash}`, - hash: tx.transaction_hash, - })), + transactions: [ + { + url: `${urls(network).blockExplorer}/transaction/${tx.transaction_hash}`, + hash: tx.transaction_hash, + }, + ], }); } catch (e) { Console.error('Failed to undelegate', e);