PaymentInfo.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2017-2020 @polkadot/react-signer authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { SubmittableExtrinsic } from '@polkadot/api/promise/types';
  5. import { RuntimeDispatchInfo } from '@polkadot/types/interfaces';
  6. import BN from 'bn.js';
  7. import React, { useState, useEffect } from 'react';
  8. import { Trans } from 'react-i18next';
  9. import { Expander } from '@polkadot/react-components';
  10. import { useApi, useIsMountedRef } from '@polkadot/react-hooks';
  11. import { formatBalance, isFunction } from '@polkadot/util';
  12. interface Props {
  13. accountId?: string | null;
  14. className?: string;
  15. extrinsic?: SubmittableExtrinsic | null;
  16. isSendable: boolean;
  17. onChange?: (hasAvailable: boolean) => void;
  18. tip?: BN;
  19. }
  20. function PaymentInfo ({ accountId, className = '', extrinsic }: Props): React.ReactElement<Props> | null {
  21. const { api } = useApi();
  22. const [dispatchInfo, setDispatchInfo] = useState<RuntimeDispatchInfo | null>(null);
  23. const mountedRef = useIsMountedRef();
  24. useEffect((): void => {
  25. // eslint-disable-next-line @typescript-eslint/unbound-method
  26. accountId && extrinsic && isFunction(extrinsic.paymentInfo) && isFunction(api.rpc.payment?.queryInfo) &&
  27. setTimeout((): void => {
  28. try {
  29. extrinsic
  30. .paymentInfo(accountId)
  31. .then((info) => mountedRef.current && setDispatchInfo(info))
  32. .catch(console.error);
  33. } catch (error) {
  34. console.error((error as Error).message);
  35. }
  36. }, 0);
  37. }, [api, accountId, extrinsic, mountedRef]);
  38. if (!dispatchInfo) {
  39. return null;
  40. }
  41. return (
  42. <Expander
  43. className={className}
  44. summary={
  45. <Trans i18nKey='feesForSubmission'>
  46. Fees of <span className='highlight'>{formatBalance(dispatchInfo.partialFee, { withSiFull: true })}</span> will be applied to the submission
  47. </Trans>
  48. }
  49. withDot
  50. />
  51. );
  52. }
  53. export default React.memo(PaymentInfo);