fix undelegating with vesting tokens
This commit is contained in:
@@ -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<TransactionExecuteResult>;
|
||||
undelegate: (
|
||||
mix_id: number,
|
||||
usesVestingContractTokens: boolean,
|
||||
fee?: FeeDetails,
|
||||
) => Promise<TransactionExecuteResult[]>;
|
||||
undelegate: (mix_id: number, fee?: Fee) => Promise<TransactionExecuteResult>;
|
||||
undelegateVesting: (mix_id: number) => Promise<TransactionExecuteResult>;
|
||||
};
|
||||
|
||||
export type TDelegationTransaction = {
|
||||
@@ -51,7 +54,10 @@ export const DelegationContext = createContext<TDelegationContext>({
|
||||
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],
|
||||
);
|
||||
|
||||
@@ -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<TransactionExecuteResult[]> => {
|
||||
const undelegate = async (mix_id: number, _fee?: Fee): Promise<TransactionExecuteResult> => {
|
||||
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],
|
||||
);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user