Skip to content

Commit

Permalink
fix unstakeAll issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-1979 committed Nov 11, 2023
1 parent 2bf3a28 commit 4326d6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@
* this component opens unstake review page
* */

import type { ApiPromise } from '@polkadot/api';
import type { SubmittableExtrinsicFunction } from '@polkadot/api/types';
import type { AnyTuple } from '@polkadot/types/types';

import { Container, Grid } from '@mui/material';
import React, { useCallback, useContext, useEffect, useState } from 'react';

import { Chain } from '@polkadot/extension-chains/types';
import { Balance } from '@polkadot/types/interfaces';
import keyring from '@polkadot/ui-keyring';
import { BN } from '@polkadot/util';

import { AccountHolderWithProxy, ActionContext, AmountFee, Motion, PasswordUseProxyConfirm, Popup, ShowBalance2, WrongPasswordAlert } from '../../../../components';
import { useAccountDisplay, useDecimal, useProxies, useToken, useTranslation } from '../../../../hooks';
import { useAccountDisplay, useApi, useChain, useDecimal, useFormatted, useProxies, useToken, useTranslation } from '../../../../hooks';
import { HeaderBrand, SubTitle, WaitScreen } from '../../../../partials';
import Confirmation from '../../../../partials/Confirmation';
import { signAndSend } from '../../../../util/api';
Expand All @@ -32,13 +30,9 @@ import TxDetail from './partials/TxDetail';
interface Props {
address: string;
amount: string;
api: ApiPromise;
chain: Chain;
chilled: SubmittableExtrinsicFunction<'promise', AnyTuple> | undefined
estimatedFee: Balance | undefined;
formatted: string;
hasNominator: boolean;
ç: boolean;
maxUnlockingChunks: number
redeem: SubmittableExtrinsicFunction<'promise', AnyTuple> | undefined;
redeemDate: string | undefined;
Expand All @@ -47,11 +41,14 @@ interface Props {
total: BN | undefined;
unlockingLen: number;
unbonded: SubmittableExtrinsicFunction<'promise', AnyTuple> | undefined;
staked: BN;
unstakeAllAmount: boolean;
}

export default function Review({ address, amount, api, chain, chilled, estimatedFee, formatted, hasNominator, maxUnlockingChunks, redeem, redeemDate, setShow, show, staked, total, unbonded, unlockingLen }: Props): React.ReactElement {
export default function Review({ address, amount, chilled, estimatedFee, hasNominator, maxUnlockingChunks, redeem, redeemDate, setShow, show, total, unbonded, unlockingLen, unstakeAllAmount }: Props): React.ReactElement {
const { t } = useTranslation();
const formatted = useFormatted(address);
const chain = useChain(address);
const api = useApi(address);
const proxies = useProxies(api, formatted);
const name = useAccountDisplay(address);
const onAction = useContext(ActionContext);
Expand Down Expand Up @@ -89,7 +86,7 @@ export default function Review({ address, amount, api, chain, chilled, estimated

const unstake = useCallback(async () => {
try {
if (!formatted || !unbonded || !redeem || !chilled || hasNominator === undefined) {
if (!api || !chain || !formatted || !unbonded || !redeem || !chilled || hasNominator === undefined) {
return;
}

Expand All @@ -108,7 +105,7 @@ export default function Review({ address, amount, api, chain, chilled, estimated
txs.push(redeem(spanCount));
}

if (amountAsBN.eq(staked) && hasNominator) {
if (unstakeAllAmount && hasNominator) {
txs.push(chilled());
}

Expand All @@ -124,7 +121,7 @@ export default function Review({ address, amount, api, chain, chilled, estimated
date: Date.now(),
failureText,
fee: fee || String(estimatedFee || 0),
from: { address: formatted, name },
from: { address: String(formatted), name },
subAction: 'Unstake',
success,
throughProxy: selectedProxyAddress ? { address: selectedProxyAddress, name: selectedProxyName } : undefined,
Expand All @@ -141,7 +138,7 @@ export default function Review({ address, amount, api, chain, chilled, estimated
console.log('error:', e);
setIsPasswordError(true);
}
}, [amount, api, chain, chilled, decimal, estimatedFee, formatted, hasNominator, maxUnlockingChunks, name, password, redeem, selectedProxy, selectedProxyAddress, selectedProxyName, staked, unbonded, unlockingLen]);
}, [amount, api, chain, chilled, decimal, estimatedFee, formatted, hasNominator, maxUnlockingChunks, name, password, redeem, selectedProxy, selectedProxyAddress, selectedProxyName, unbonded, unlockingLen, unstakeAllAmount]);

const _onBackClick = useCallback(() => {
setShow(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { BN, BN_ONE, BN_ZERO } from '@polkadot/util';
import { AmountWithOptions, Motion, PButton, Warning } from '../../../../components';
import { useApi, useChain, useDecimal, useFormatted, useStakingAccount, useStakingConsts, useToken, useTranslation, useUnSupportedNetwork } from '../../../../hooks';
import { HeaderBrand, SubTitle } from '../../../../partials';
import Asset from '../../../../partials/Asset';
import { DATE_OPTIONS, MAX_AMOUNT_LENGTH, STAKING_CHAINS } from '../../../../util/constants';
import { amountToHuman, amountToMachine } from '../../../../util/utils';
import Asset from '../../../../partials/Asset';
import Review from './Review';

interface State {
Expand Down Expand Up @@ -50,8 +50,8 @@ export default function Index(): React.ReactElement {
const [showReview, setShowReview] = useState<boolean>(false);
const [unstakeAllAmount, setUnstakeAllAmount] = useState<boolean>(false);

const staked = useMemo(() => stakingAccount && stakingAccount.stakingLedger.active, [stakingAccount]);
const totalAfterUnstake = useMemo(() => staked && decimal && staked.sub(amountToMachine(amount, decimal)) as BN | undefined, [amount, decimal, staked]);
const staked = useMemo(() => stakingAccount && stakingAccount.stakingLedger.active as unknown as BN, [stakingAccount]);
const totalAfterUnstake = useMemo(() => staked && decimal ? staked.sub(amountToMachine(amount, decimal)) : undefined, [amount, decimal, staked]);
const unlockingLen = stakingAccount?.stakingLedger?.unlocking?.length;
const maxUnlockingChunks = api && api.consts.staking.maxUnlockingChunks?.toNumber() as unknown as number;
const amountAsBN = useMemo(() => amountToMachine(amount, decimal), [amount, decimal]);
Expand Down Expand Up @@ -104,7 +104,7 @@ export default function Index(): React.ReactElement {
txs.push(redeem(...dummyParams));
}

if (amountAsBN.eq(staked)) {
if (unstakeAllAmount) {
txs.push(chilled());
}

Expand All @@ -114,7 +114,7 @@ export default function Index(): React.ReactElement {

setEstimatedFee(api?.createType('Balance', partialFee));
}
}, [amountAsBN, api, chilled, formatted, maxUnlockingChunks, redeem, staked, unbonded, unlockingLen]);
}, [amountAsBN, api, chilled, formatted, maxUnlockingChunks, redeem, staked, unbonded, unlockingLen, unstakeAllAmount]);

useEffect(() => {
if (amountAsBN && redeem && chilled && maxUnlockingChunks && unlockingLen !== undefined && unbonded && formatted && staked) {
Expand Down Expand Up @@ -219,21 +219,18 @@ export default function Index(): React.ReactElement {
<Review
address={address}
amount={amount}
api={api}
chain={chain}
chilled={chilled}
estimatedFee={estimatedFee}
formatted={formatted}
hasNominator={!!stakingAccount?.nominators?.length}
maxUnlockingChunks={maxUnlockingChunks}
redeem={redeem}
redeemDate={redeemDate}
setShow={setShowReview}
show={showReview}
staked={staked}
total={totalAfterUnstake}
unbonded={unbonded}
unlockingLen={unlockingLen ?? 0}
unstakeAllAmount={unstakeAllAmount}
/>
}
</Motion>
Expand Down

0 comments on commit 4326d6d

Please sign in to comment.