|
| 1 | +// @flow |
| 2 | +import React from 'react'; |
| 3 | +import classnames from 'classnames'; |
| 4 | +import BigNumber from 'bignumber.js'; |
| 5 | +import { |
| 6 | + defineMessages, |
| 7 | + intlShape, |
| 8 | + injectIntl, |
| 9 | + FormattedHTMLMessage, |
| 10 | +} from 'react-intl'; |
| 11 | +import { PopOver } from 'react-polymorph/lib/components/PopOver'; |
| 12 | +import { observer } from 'mobx-react'; |
| 13 | +import SVGInline from 'react-svg-inline'; |
| 14 | +import questionMarkIcon from '../../assets/images/question-mark.inline.svg'; |
| 15 | +import styles from './AssetTransactionConfirmation.scss'; |
| 16 | +import type { AssetToken } from '../../api/assets/types'; |
| 17 | +import Asset from './Asset'; |
| 18 | +import { formattedTokenWalletAmount } from '../../utils/formatters'; |
| 19 | + |
| 20 | +const messages = defineMessages({ |
| 21 | + assetLabel: { |
| 22 | + id: 'asset.transactionConfirmation.assetLabel', |
| 23 | + defaultMessage: '!!!Token #{assetNumber}', |
| 24 | + description: '"assetLabel" item on AssetTransactionConfirmation.', |
| 25 | + }, |
| 26 | + unformattedAmountLabel: { |
| 27 | + id: 'asset.transactionConfirmation.unformattedAmountLabel', |
| 28 | + defaultMessage: '!!!unformatted amount', |
| 29 | + description: |
| 30 | + '"unformattedAmountLabel" item on AssetTransactionConfirmation.', |
| 31 | + }, |
| 32 | + unformattedAmountMessageForHardwareWallets: { |
| 33 | + id: |
| 34 | + 'asset.transactionConfirmation.unformattedAmountMessageForHardwareWallets', |
| 35 | + defaultMessage: |
| 36 | + '!!!Native assets may specify a number of decimal places, as defined in the Cardano token registry. Daedalus uses this information to format the amount that is being sent in the transaction.<br /><br />The native token unformatted amount is the amount without these decimal places. Please ensure that you verify both amounts, as some wallet software may not yet use the Cardano token registry.', |
| 37 | + description: |
| 38 | + '"unformattedAmountMessageForHardwareWallets" item on AssetTransactionConfirmation.', |
| 39 | + }, |
| 40 | + unformattedAmountMessageForSoftwareWallets: { |
| 41 | + id: |
| 42 | + 'asset.transactionConfirmation.unformattedAmountMessageForSoftwareWallets', |
| 43 | + defaultMessage: |
| 44 | + '!!!Native assets may specify a number of decimal places, as defined in the Cardano token registry. Daedalus uses this information to format the amount that is being sent in the transaction.<br /><br />The native token unformatted amount is the amount without these decimal places. Please ensure that you verify both amounts, as some wallet software may not yet use the Cardano token registry.<br /><br />The native token unformatted amount will be displayed on the hardware wallet device during transaction confirmation.', |
| 45 | + description: |
| 46 | + '"unformattedAmountMessageForSoftwareWallets" item on AssetTransactionConfirmation.', |
| 47 | + }, |
| 48 | + missingToken: { |
| 49 | + id: 'asset.transactionConfirmation.missingToken', |
| 50 | + defaultMessage: '!!!There is no such token in this wallet', |
| 51 | + description: '"missingToken" item on AssetTransactionConfirmation.', |
| 52 | + }, |
| 53 | + insufficientBalance: { |
| 54 | + id: 'asset.transactionConfirmation.insufficientBalance', |
| 55 | + defaultMessage: |
| 56 | + '!!!Insufficient funds. The balance of the token in this wallet is {formattedBalance} (Unformatted: {unformattedBalance})', |
| 57 | + description: '"insufficientBalance" item on AssetTransactionConfirmation.', |
| 58 | + }, |
| 59 | +}); |
| 60 | + |
| 61 | +type Props = { |
| 62 | + asset: AssetToken, |
| 63 | + assetNumber: number, |
| 64 | + intl: intlShape.isRequired, |
| 65 | + isHardwareWallet: boolean, |
| 66 | + tokenIsMissing?: boolean, |
| 67 | + insufficientBalance?: boolean, |
| 68 | + amount: BigNumber, |
| 69 | +}; |
| 70 | + |
| 71 | +const onCopyAssetItem = () => {}; |
| 72 | + |
| 73 | +const AssetTransactionConfirmation = observer((props: Props) => { |
| 74 | + const { |
| 75 | + assetNumber, |
| 76 | + asset, |
| 77 | + intl, |
| 78 | + isHardwareWallet, |
| 79 | + tokenIsMissing, |
| 80 | + insufficientBalance, |
| 81 | + amount, |
| 82 | + } = props; |
| 83 | + const hasError = tokenIsMissing || insufficientBalance; |
| 84 | + const { metadata, decimals } = asset; |
| 85 | + const formattedAmount = formattedTokenWalletAmount( |
| 86 | + amount, |
| 87 | + metadata, |
| 88 | + decimals |
| 89 | + ); |
| 90 | + const unformattedAmount = formattedTokenWalletAmount(amount, null, 0); |
| 91 | + |
| 92 | + const formattedBalance = formattedTokenWalletAmount( |
| 93 | + asset.quantity, |
| 94 | + metadata, |
| 95 | + decimals |
| 96 | + ); |
| 97 | + const unformattedBalance = formattedTokenWalletAmount( |
| 98 | + asset.quantity, |
| 99 | + null, |
| 100 | + 0 |
| 101 | + ); |
| 102 | + |
| 103 | + const componentStyles = classnames(styles.component, { |
| 104 | + [styles.error]: hasError, |
| 105 | + }); |
| 106 | + |
| 107 | + const content = ( |
| 108 | + <> |
| 109 | + <div className={styles.assetsContainer}> |
| 110 | + <h3> |
| 111 | + <span className={styles.assetLabel}> |
| 112 | + {intl.formatMessage(messages.assetLabel, { assetNumber })}{' '} |
| 113 | + </span> |
| 114 | + <Asset |
| 115 | + asset={asset} |
| 116 | + onCopyAssetItem={onCopyAssetItem} |
| 117 | + hasError={hasError} |
| 118 | + /> |
| 119 | + </h3> |
| 120 | + <div className={styles.amountFeesWrapper}> |
| 121 | + <div className={styles.amount}>{formattedAmount}</div> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + <div className={styles.assetsContainer}> |
| 125 | + <div className={styles.unformattedAmountLine} /> |
| 126 | + <div className={styles.unformattedAmountLabel}> |
| 127 | + {intl.formatMessage(messages.unformattedAmountLabel)} |
| 128 | + <PopOver |
| 129 | + content={ |
| 130 | + <FormattedHTMLMessage |
| 131 | + {...messages[ |
| 132 | + isHardwareWallet |
| 133 | + ? 'unformattedAmountMessageForHardwareWallets' |
| 134 | + : 'unformattedAmountMessageForSoftwareWallets' |
| 135 | + ]} |
| 136 | + tagName="div" |
| 137 | + /> |
| 138 | + } |
| 139 | + > |
| 140 | + <div className={styles.questionMark}> |
| 141 | + <SVGInline svg={questionMarkIcon} /> |
| 142 | + </div> |
| 143 | + </PopOver> |
| 144 | + {':'} |
| 145 | + </div> |
| 146 | + <div className={styles.unformattedAmount}>{unformattedAmount}</div> |
| 147 | + </div> |
| 148 | + </> |
| 149 | + ); |
| 150 | + |
| 151 | + if (tokenIsMissing) { |
| 152 | + return ( |
| 153 | + <div className={componentStyles}> |
| 154 | + <PopOver |
| 155 | + content={intl.formatMessage(messages.missingToken)} |
| 156 | + appendTo="parent" |
| 157 | + placement="bottom" |
| 158 | + > |
| 159 | + {content} |
| 160 | + </PopOver> |
| 161 | + </div> |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + if (insufficientBalance) { |
| 166 | + return ( |
| 167 | + <div className={componentStyles}> |
| 168 | + <PopOver |
| 169 | + content={intl.formatMessage(messages.insufficientBalance, { |
| 170 | + formattedBalance, |
| 171 | + unformattedBalance, |
| 172 | + })} |
| 173 | + appendTo="parent" |
| 174 | + placement="bottom" |
| 175 | + > |
| 176 | + {content} |
| 177 | + </PopOver> |
| 178 | + </div> |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + return <div className={componentStyles}>{content}</div>; |
| 183 | +}); |
| 184 | + |
| 185 | +export default injectIntl(AssetTransactionConfirmation); |
0 commit comments