Skip to content

Commit cae5351

Browse files
committed
fix: fee issue utilizing fee hook
1 parent 3eb62e8 commit cae5351

File tree

43 files changed

+230
-844
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+230
-844
lines changed

packages/extension-polkagate/src/fullscreen/accountDetails/unlock/Review.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import type { ApiPromise } from '@polkadot/api';
1212
import type { SubmittableExtrinsic } from '@polkadot/api/types';
13-
import type { Balance } from '@polkadot/types/interfaces';
1413
import type { ISubmittableResult } from '@polkadot/types/types';
1514
import type { BN } from '@polkadot/util';
1615
import type { Lock } from '../../../hooks/useAccountLocks';
@@ -20,10 +19,10 @@ import { faLockOpen, faUserAstronaut } from '@fortawesome/free-solid-svg-icons';
2019
import { Container, Grid, useTheme } from '@mui/material';
2120
import React, { useCallback, useEffect, useMemo, useState } from 'react';
2221

23-
import { BN_ONE, isBn } from '@polkadot/util';
22+
import { isBn } from '@polkadot/util';
2423

2524
import { AccountHolderWithProxy, AmountFee, SignArea2, Warning, WrongPasswordAlert } from '../../../components';
26-
import { useInfo, useProxies, useTranslation } from '../../../hooks';
25+
import { useEstimatedFee, useInfo, useProxies, useTranslation } from '../../../hooks';
2726
import { SubTitle } from '../../../partials';
2827
import { PROXY_TYPE } from '../../../util/constants';
2928
import { amountToHuman } from '../../../util/utils';
@@ -56,7 +55,6 @@ export default function Review ({ address, api, classToUnlock, setDisplayPopup,
5655
const [selectedProxy, setSelectedProxy] = useState<Proxy | undefined>();
5756
const [proxyItems, setProxyItems] = useState<ProxyItem[]>();
5857
const [txInfo, setTxInfo] = useState<TxInfo | undefined>();
59-
const [estimatedFee, setEstimatedFee] = useState<Balance>();
6058
const [params, setParams] = useState<SubmittableExtrinsic<'promise', ISubmittableResult>[]>();
6159
const [step, setStep] = useState<number>(STEPS.INDEX);
6260

@@ -67,6 +65,8 @@ export default function Review ({ address, api, classToUnlock, setDisplayPopup,
6765
const unlockClass = api.tx['convictionVoting']['unlock']; // (class)
6866
const batchAll = api.tx['utility']['batchAll'];
6967

68+
const estimatedFee = useEstimatedFee(address, batchAll(params));
69+
7070
const extraInfo = useMemo(() => ({
7171
action: 'Unlock Referenda',
7272
amount,
@@ -93,12 +93,6 @@ export default function Review ({ address, api, classToUnlock, setDisplayPopup,
9393
const params = [...removes, ...unlocks];
9494

9595
setParams(params);
96-
97-
if (!api?.call?.['transactionPaymentApi']) {
98-
return setEstimatedFee(api?.createType('Balance', BN_ONE) as Balance | undefined);
99-
}
100-
101-
batchAll(params).paymentInfo(formatted).then((i) => setEstimatedFee(i?.partialFee)).catch(console.error);
10296
}, [api, batchAll, formatted, classToUnlock, remove, unlockClass]);
10397

10498
useEffect((): void => {

packages/extension-polkagate/src/fullscreen/governance/delegate/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export function Delegate ({ address, open, setOpen, showDelegationNote }: Props)
217217

218218
batch(txList)
219219
.paymentInfo(delegateInformation.delegateeAddress)
220-
.then((i) => setEstimatedFee(i?.partialFee))
220+
.then((i) => setEstimatedFee(api?.createType('Balance', i?.partialFee) as Balance))
221221
.catch(console.error);
222222
} else {
223223
const tx = delegate(...[
@@ -229,7 +229,7 @@ export function Delegate ({ address, open, setOpen, showDelegationNote }: Props)
229229

230230
tx
231231
.paymentInfo(delegateInformation.delegateeAddress)
232-
.then((i) => setEstimatedFee(i?.partialFee))
232+
.then((i) => setEstimatedFee(api?.createType('Balance', i?.partialFee) as Balance))
233233
.catch(console.error);
234234
}
235235
}, [api, batch, delegate, delegateInformation, delegateInformation?.delegateeAddress]);

packages/extension-polkagate/src/fullscreen/governance/delegate/modify/ModifyDelegate.tsx

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
3-
// @ts-nocheck
43

4+
//@ts-nocheck
55
/* eslint-disable react/jsx-max-props-per-line */
66

77
/**
88
* @description
99
* this component opens Modify Delegate review page
1010
* */
1111

12+
import type { SubmittableExtrinsic } from '@polkadot/api/types';
1213
import type { Balance } from '@polkadot/types/interfaces';
14+
import type { ISubmittableResult } from '@polkadot/types/types';
15+
import type { BN } from '@polkadot/util';
16+
import type { Lock } from '../../../../hooks/useAccountLocks';
1317
import type { BalancesInfo, Proxy, TxInfo } from '../../../../util/types';
18+
import type { AlreadyDelegateInformation, DelegateInformation } from '..';
1419

1520
import { Divider, Grid, Typography } from '@mui/material';
1621
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
1722

18-
import { SubmittableExtrinsic } from '@polkadot/api/types';
19-
import { ISubmittableResult } from '@polkadot/types/types';
20-
import { BN, BN_ONE, BN_ZERO } from '@polkadot/util';
23+
import { BN_ONE, BN_ZERO } from '@polkadot/util';
2124

2225
import { Identity, Motion, ShowValue, SignArea2, WrongPasswordAlert } from '../../../../components';
2326
import { useCurrentBlockNumber, useIdentity, useInfo, useTracks, useTranslation } from '../../../../hooks';
24-
import { Lock } from '../../../../hooks/useAccountLocks';
2527
import { ThroughProxy } from '../../../../partials';
2628
import { PROXY_TYPE } from '../../../../util/constants';
2729
import { amountToHuman, amountToMachine } from '../../../../util/utils';
2830
import DisplayValue from '../../post/castVote/partial/DisplayValue';
2931
import TracksList from '../partial/TracksList';
30-
import { AlreadyDelegateInformation, DelegateInformation, STEPS } from '..';
32+
import { STEPS } from '..';
3133
import Modify from './Modify';
3234

3335
interface Props {
@@ -51,7 +53,7 @@ interface Props {
5153

5254
export type ModifyModes = 'Modify' | 'ReviewModify';
5355

54-
export default function ModifyDelegate({ accountLocks, address, balances, classicDelegateInformation, formatted, lockedAmount, mixedDelegateInformation, mode, otherDelegatedTracks, selectedProxy, setDelegateInformation, setModalHeight, setMode, setStep, setTxInfo, step }: Props): React.ReactElement<Props> {
56+
export default function ModifyDelegate ({ accountLocks, address, balances, classicDelegateInformation, formatted, lockedAmount, mixedDelegateInformation, mode, otherDelegatedTracks, selectedProxy, setDelegateInformation, setModalHeight, setMode, setStep, setTxInfo, step }: Props): React.ReactElement<Props> {
5557
const { t } = useTranslation();
5658
const { api, chain, decimal, genesisHash, token } = useInfo(address);
5759
const { tracks } = useTracks(address);
@@ -76,11 +78,19 @@ export default function ModifyDelegate({ accountLocks, address, balances, classi
7678
const [newConviction, setNewConviction] = useState<number | undefined>();
7779

7880
const selectedProxyAddress = selectedProxy?.delegate as unknown as string;
79-
const undelegate = api && api.tx['convictionVoting']['undelegate'];
80-
const delegate = api && api.tx['convictionVoting']['delegate'];
81-
const batch = api && api.tx['utility']['batchAll'];
82-
83-
const acceptableConviction = useMemo(() => newConviction !== undefined ? newConviction === 0.1 ? 0 : newConviction : conviction === 0.1 ? 0 : conviction, [conviction, newConviction]);
81+
const undelegate = api?.tx['convictionVoting']['undelegate'];
82+
const delegate = api?.tx['convictionVoting']['delegate'];
83+
const batch = api?.tx['utility']['batchAll'];
84+
85+
const acceptableConviction = useMemo(() =>
86+
newConviction !== undefined
87+
? newConviction === 0.1
88+
? 0
89+
: newConviction
90+
: conviction === 0.1
91+
? 0
92+
: conviction
93+
, [conviction, newConviction]);
8494

8595
const delegateAmountBN = useMemo(() => newDelegateAmount ? amountToMachine(newDelegateAmount, decimal) : classicDelegateInformation ? classicDelegateInformation.delegateAmountBN : BN_ZERO, [classicDelegateInformation, decimal, newDelegateAmount]);
8696
const delegatePower = useMemo(() => {
@@ -109,6 +119,7 @@ export default function ModifyDelegate({ accountLocks, address, balances, classi
109119

110120
useEffect(() => {
111121
if (ref) {
122+
// @ts-ignore
112123
setModalHeight(ref.current?.offsetHeight as number);
113124
}
114125
}, [setModalHeight]);
@@ -185,8 +196,8 @@ export default function ModifyDelegate({ accountLocks, address, balances, classi
185196
}
186197

187198
txList.length === 1
188-
? txList[0].paymentInfo(formatted).then((i) => setEstimatedFee(i?.partialFee)).catch(console.error)
189-
: batch(txList).paymentInfo(formatted).then((i) => setEstimatedFee(i?.partialFee)).catch(console.error);
199+
? txList[0].paymentInfo(formatted).then((i) => setEstimatedFee(api?.createType('Balance', i?.partialFee))).catch(console.error)
200+
: batch(txList).paymentInfo(formatted).then((i) => setEstimatedFee(api?.createType('Balance', i?.partialFee))).catch(console.error);
190201
}, [api, batch, formatted, txList, undelegate]);
191202

192203
const extraInfo = useMemo(() => ({

packages/extension-polkagate/src/fullscreen/governance/delegate/remove/RemoveDelegate.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ export default function RemoveDelegate ({ address, classicDelegateInformation, f
100100
}
101101

102102
params.length === 1
103-
? undelegate(BN_ZERO).paymentInfo(formatted).then((i) => setEstimatedFee(i?.partialFee)).catch(console.error)
104-
: batch(params.map((param) => undelegate(param))).paymentInfo(formatted).then((i) => setEstimatedFee(i?.partialFee)).catch(console.error);
103+
? undelegate(BN_ZERO).paymentInfo(formatted).then((i) => setEstimatedFee(api?.createType('Balance', i?.partialFee) as Balance)).catch(console.error)
104+
: batch(params.map((param) => undelegate(param))).paymentInfo(formatted).then((i) => setEstimatedFee(api?.createType('Balance', i?.partialFee) as Balance)).catch(console.error);
105105
}, [api, batch, formatted, params, setSelectedTracksLength, undelegate]);
106106

107107
const extraInfo = useMemo(() => ({

packages/extension-polkagate/src/fullscreen/governance/post/castVote/Cast.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export default function Cast ({ address, notVoted, previousVote, refIndex, setSt
170170
const dummyVote = undefined;
171171
const feeDummyParams = ['1', dummyVote];
172172

173-
tx(...feeDummyParams).paymentInfo(formatted).then((i) => setEstimatedFee(i?.partialFee)).catch(console.error);
173+
tx(...feeDummyParams).paymentInfo(formatted).then((i) => setEstimatedFee(api?.createType('Balance', i?.partialFee))).catch(console.error);
174174
}, [api, formatted, tx]);
175175

176176
useEffect(() => {

packages/extension-polkagate/src/fullscreen/governance/post/castVote/index.tsx

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
/* eslint-disable react/jsx-max-props-per-line */
55

6-
import type { Balance } from '@polkadot/types/interfaces';
76
import type { Proxy, ProxyItem, TxInfo } from '../../../../util/types';
87
import type { Vote } from '../myVote/util';
98

@@ -12,10 +11,10 @@ import React, { useCallback, useEffect, useMemo, useState } from 'react';
1211

1312
import { AccountsStore } from '@polkadot/extension-base/stores';
1413
import keyring from '@polkadot/ui-keyring';
15-
import { BN, BN_ONE, noop } from '@polkadot/util';
14+
import { BN, noop } from '@polkadot/util';
1615
import { cryptoWaitReady } from '@polkadot/util-crypto';
1716

18-
import { useInfo, useProxies, useTranslation } from '../../../../hooks';
17+
import { useEstimatedFee, useInfo, useProxies, useTranslation } from '../../../../hooks';
1918
import { PROXY_TYPE } from '../../../../util/constants';
2019
import { amountToHuman, amountToMachine } from '../../../../util/utils';
2120
import { DraggableModalWithTitle } from '../../components/DraggableModalWithTitle';
@@ -76,7 +75,6 @@ export default function Index ({ address, cantModify, hasVoted, myVote, notVoted
7675
const [selectedProxy, setSelectedProxy] = useState<Proxy | undefined>();
7776
const [proxyItems, setProxyItems] = useState<ProxyItem[]>();
7877
const [txInfo, setTxInfo] = useState<TxInfo | undefined>();
79-
const [estimatedFee, setEstimatedFee] = useState<Balance>();
8078
const [voteInformation, setVoteInformation] = useState<VoteInformation | undefined>();
8179
const [step, setStep] = useState<number>(showAbout ? STEPS.ABOUT : STEPS.CHECK_SCREEN);
8280
const [alterType, setAlterType] = useState<'modify' | 'remove'>();
@@ -85,6 +83,8 @@ export default function Index ({ address, cantModify, hasVoted, myVote, notVoted
8583
const voteTx = api?.tx['convictionVoting']['vote'];
8684
const removeTx = api?.tx['convictionVoting']['removeVote'];
8785

86+
const estimatedFee = useEstimatedFee(address, voteTx, ['1', undefined]);
87+
8888
useEffect((): void => {
8989
const fetchedProxyItems = proxies?.map((p: Proxy) => ({ proxy: p, status: 'current' })) as ProxyItem[];
9090

@@ -149,22 +149,6 @@ export default function Index ({ address, cantModify, hasVoted, myVote, notVoted
149149
}
150150
}, [step, hasVoted, alterType, t]);
151151

152-
useEffect(() => {
153-
if (!formatted || !voteTx) {
154-
return;
155-
}
156-
157-
if (!api?.call?.['transactionPaymentApi']) {
158-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
159-
return setEstimatedFee(api?.createType('Balance', BN_ONE) as Balance);
160-
}
161-
162-
const dummyVote = undefined;
163-
const feeDummyParams = ['1', dummyVote];
164-
165-
voteTx(...feeDummyParams).paymentInfo(formatted).then((i) => setEstimatedFee(i?.partialFee)).catch(console.error);
166-
}, [api, formatted, voteTx]);
167-
168152
const handleClose = useCallback(() => {
169153
if (step === STEPS.PROXY) {
170154
setStep(alterType === 'remove' ? STEPS.REMOVE : STEPS.REVIEW);

packages/extension-polkagate/src/fullscreen/governance/post/decisionDeposit/index.tsx

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
3-
// @ts-nocheck
43

54
/* eslint-disable react/jsx-max-props-per-line */
65

7-
import type { Balance } from '@polkadot/types/interfaces';
6+
import type { Proxy, ProxyItem, TxInfo } from '../../../../util/types';
87

98
import { Close as CloseIcon } from '@mui/icons-material';
109
import { Grid, Typography, useTheme } from '@mui/material';
1110
import React, { useCallback, useEffect, useMemo, useState } from 'react';
1211

1312
import { AccountsStore } from '@polkadot/extension-base/stores';
1413
import keyring from '@polkadot/ui-keyring';
15-
import { BN_ONE } from '@polkadot/util';
1614
import { cryptoWaitReady } from '@polkadot/util-crypto';
1715

1816
import { Identity, ShowBalance, SignArea2, Warning } from '../../../../components';
19-
import { useAccountDisplay, useBalances, useInfo, useProxies, useTranslation } from '../../../../hooks';
17+
import { useAccountDisplay, useBalances, useEstimatedFee, useInfo, useProxies, useTranslation } from '../../../../hooks';
2018
import { ThroughProxy } from '../../../../partials';
2119
import { getValue } from '../../../../popup/account/util';
22-
import { Proxy, ProxyItem, TxInfo } from '../../../../util/types';
2320
import { PROXY_TYPE } from '../../../../util/constants';
24-
import type { Proxy, ProxyItem, TxInfo } from '../../../../util/types';
2521
import { DraggableModal } from '../../components/DraggableModal';
2622
import SelectProxyModal2 from '../../components/SelectProxyModal2';
2723
import WaitScreen from '../../partials/WaitScreen';
@@ -61,27 +57,12 @@ export default function DecisionDeposit ({ address, open, refIndex, setOpen, tra
6157
const [step, setStep] = useState<number>(STEPS.REVIEW);
6258
const [txInfo, setTxInfo] = useState<TxInfo | undefined>();
6359
const [isPasswordError, setIsPasswordError] = useState(false);
64-
const [estimatedFee, setEstimatedFee] = useState<Balance>();
6560
const [selectedProxy, setSelectedProxy] = useState<Proxy | undefined>();
6661

67-
const tx = api && api.tx['referenda']['placeDecisionDeposit'];
62+
const tx = api?.tx['referenda']['placeDecisionDeposit'];
6863
const amount = track?.[1]?.decisionDeposit;
6964
const selectedProxyAddress = selectedProxy?.delegate as unknown as string;
70-
71-
useEffect(() => {
72-
if (!formatted || !tx) {
73-
return;
74-
}
75-
76-
if (!api?.call?.['transactionPaymentApi']) {
77-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
78-
return setEstimatedFee(api?.createType('Balance', BN_ONE));
79-
}
80-
81-
const feeDummyParams = [1];
82-
83-
tx(...feeDummyParams).paymentInfo(formatted).then((i) => setEstimatedFee(i?.partialFee)).catch(console.error);
84-
}, [api, formatted, tx]);
65+
const estimatedFee = useEstimatedFee(address, tx, [1]);
8566

8667
useEffect(() => {
8768
cryptoWaitReady().then(() => keyring.loadAll({ store: new AccountsStore() })).catch(() => null);
@@ -121,6 +102,8 @@ export default function DecisionDeposit ({ address, open, refIndex, setOpen, tra
121102
if (step === STEPS.CONFIRM) {
122103
return t('Paying Confirmation');
123104
}
105+
106+
return undefined;
124107
}, [step, t]);
125108

126109
const HEIGHT = 550;
@@ -193,7 +176,7 @@ export default function DecisionDeposit ({ address, open, refIndex, setOpen, tra
193176
</Grid>
194177
<Grid container item sx={{ bottom: '20px', left: '4%', position: 'absolute', width: '92%' }}>
195178
<SignArea2
196-
address={address as string}
179+
address={address!}
197180
call={tx}
198181
disabled={notEnoughBalance}
199182
extraInfo={extraInfo}

0 commit comments

Comments
 (0)