From 06c492dba8cd62a4094d095bce324f507f0ed250 Mon Sep 17 00:00:00 2001 From: gromxyz Date: Thu, 7 Oct 2021 12:24:23 +0200 Subject: [PATCH 1/2] Add custom recipient field --- .../wallet/txs/upgrade/Upgrade.stories.tsx | 1 + .../components/wallet/txs/upgrade/Upgrade.tsx | 33 ++++++++++++++++--- .../views/wallet/upgrade/UpgradeView.tsx | 4 +++ src/renderer/views/wallet/upgrade/types.ts | 3 +- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/renderer/components/wallet/txs/upgrade/Upgrade.stories.tsx b/src/renderer/components/wallet/txs/upgrade/Upgrade.stories.tsx index 9116d66a4..c3f4557dd 100644 --- a/src/renderer/components/wallet/txs/upgrade/Upgrade.stories.tsx +++ b/src/renderer/components/wallet/txs/upgrade/Upgrade.stories.tsx @@ -68,6 +68,7 @@ const defaultProps: UpgradeProps = { }, runeNativeAddress: 'rune-native-address', walletAddress: 'bnb12312312312123123123123', + addressValidation: () => true, walletType: 'keystore', walletIndex: 0, reloadOnError: () => {}, diff --git a/src/renderer/components/wallet/txs/upgrade/Upgrade.tsx b/src/renderer/components/wallet/txs/upgrade/Upgrade.tsx index d4b6f8118..b0d86ec2c 100644 --- a/src/renderer/components/wallet/txs/upgrade/Upgrade.tsx +++ b/src/renderer/components/wallet/txs/upgrade/Upgrade.tsx @@ -23,7 +23,7 @@ import { useSubscriptionState } from '../../../../hooks/useSubscriptionState' import { INITIAL_UPGRADE_RUNE_STATE } from '../../../../services/chain/const' import { UpgradeRuneParams, UpgradeRuneTxState, UpgradeRuneTxState$ } from '../../../../services/chain/types' import { FeeRD } from '../../../../services/chain/types' -import { OpenExplorerTxUrl } from '../../../../services/clients' +import { AddressValidation, OpenExplorerTxUrl } from '../../../../services/clients' import { PoolAddressRD } from '../../../../services/midgard/types' import { NonEmptyWalletBalances, ValidatePasswordHandler, WalletType } from '../../../../services/wallet/types' import { AssetWithDecimal } from '../../../../types/asgardex' @@ -31,6 +31,7 @@ import { PasswordModal } from '../../../modal/password' import { MaxBalanceButton } from '../../../uielements/button/MaxBalanceButton' import { ViewTxButton } from '../../../uielements/button/ViewTxButton' import { UIFeesRD } from '../../../uielements/fees' +import { Input } from '../../../uielements/input' import { InputBigNumber } from '../../../uielements/input/InputBigNumber' import { AccountSelector } from '../../account' import * as Styled from '../TxForm.styles' @@ -49,6 +50,7 @@ export type Props = { upgrade$: (_: UpgradeRuneParams) => UpgradeRuneTxState$ balances: O.Option reloadFeeHandler: (params: TxParams) => void + addressValidation: AddressValidation successActionHandler: OpenExplorerTxUrl reloadBalancesHandler: FP.Lazy network: Network @@ -57,10 +59,9 @@ export type Props = { type FormValues = { amount: BigNumber + address: Address } -const INITIAL_FORM_VALUES: FormValues = { amount: ZERO_BN } - export const Upgrade: React.FC = (props): JSX.Element => { const { runeAsset, @@ -72,6 +73,7 @@ export const Upgrade: React.FC = (props): JSX.Element => { balances: oBalances, successActionHandler, reloadFeeHandler, + addressValidation, reloadBalancesHandler, network, walletAddress, @@ -358,6 +360,18 @@ export const Upgrade: React.FC = (props): JSX.Element => { return false }, [targetPoolAddressRD, reloadFeeHandler, runeAsset.asset, amountToUpgrade]) + const addressValidator = useCallback( + async (_: unknown, value: string) => { + if (!value) { + return Promise.reject(intl.formatMessage({ id: 'wallet.errors.address.empty' })) + } + if (!addressValidation(value.toLowerCase())) { + return Promise.reject(intl.formatMessage({ id: 'wallet.errors.address.invalid' })) + } + }, + [addressValidation, intl] + ) + const renderUpgradeForm = useMemo( () => ( @@ -372,7 +386,11 @@ export const Upgrade: React.FC = (props): JSX.Element => { walletBalances={[]} network={network} /> - + {intl.formatMessage({ id: 'common.amount' })} = (props): JSX.Element => { disabled={isLoading} /> + {intl.formatMessage({ id: 'common.address' })} + + + + {renderFeeError} @@ -410,6 +433,7 @@ export const Upgrade: React.FC = (props): JSX.Element => { runeAsset.asset, network, form, + runeNativeAddress, onSubmit, intl, amountValidator, @@ -417,6 +441,7 @@ export const Upgrade: React.FC = (props): JSX.Element => { onChangeInput, maxAmount, addMaxAmountHandler, + addressValidator, uiFeesRD, reloadFees, renderFeeError, diff --git a/src/renderer/views/wallet/upgrade/UpgradeView.tsx b/src/renderer/views/wallet/upgrade/UpgradeView.tsx index 64c6d2d44..464d2a109 100644 --- a/src/renderer/views/wallet/upgrade/UpgradeView.tsx +++ b/src/renderer/views/wallet/upgrade/UpgradeView.tsx @@ -22,6 +22,7 @@ import { useWalletContext } from '../../../contexts/WalletContext' import { isNonNativeRuneAsset } from '../../../helpers/assetHelper' import { eqOAsset } from '../../../helpers/fp/eq' import { useOpenExplorerTxUrl } from '../../../hooks/useOpenExplorerTxUrl' +import { useValidateAddress } from '../../../hooks/useValidateAddress' import { AssetDetailsParams } from '../../../routes/wallet' import { AssetWithDecimalLD, AssetWithDecimalRD } from '../../../services/chain/types' import { OpenExplorerTxUrl } from '../../../services/clients' @@ -43,6 +44,8 @@ export const UpgradeView: React.FC = (): JSX.Element => { const { network$ } = useAppContext() const network = useObservableState(network$, DEFAULT_NETWORK) + const { validateAddress } = useValidateAddress(THORChain) + const { addressByChain$, upgradeRuneToNative$, assetWithDecimal$ } = useChainContext() // Accept [CHAIN].Rune only @@ -183,6 +186,7 @@ export const UpgradeView: React.FC = (): JSX.Element => { () => renderDataError(Error('Could not get address from asset')), (runeNativeAddress) => { return renderUpgradeComponent(runeAsset, { + addressValidation: validateAddress, walletAddress, walletType, walletIndex: parseInt(walletIndex), diff --git a/src/renderer/views/wallet/upgrade/types.ts b/src/renderer/views/wallet/upgrade/types.ts index 2cfb18dfe..f468d216e 100644 --- a/src/renderer/views/wallet/upgrade/types.ts +++ b/src/renderer/views/wallet/upgrade/types.ts @@ -4,7 +4,7 @@ import * as O from 'fp-ts/Option' import { Network } from '../../../../shared/api/types' import { UpgradeRuneParams, UpgradeRuneTxState$ } from '../../../services/chain/types' -import { OpenExplorerTxUrl } from '../../../services/clients' +import { AddressValidation, OpenExplorerTxUrl } from '../../../services/clients' import { PoolAddressRD } from '../../../services/midgard/types' import { NonEmptyWalletBalances, ValidatePasswordHandler, WalletType } from '../../../services/wallet/types' import { AssetWithDecimal } from '../../../types/asgardex' @@ -16,6 +16,7 @@ export type CommonUpgradeProps = { walletIndex: number runeNativeAddress: Address targetPoolAddressRD: PoolAddressRD + addressValidation: AddressValidation validatePassword$: ValidatePasswordHandler upgrade$: (_: UpgradeRuneParams) => UpgradeRuneTxState$ balances: O.Option From dd7244593873f3945150a6984e6fae1e0596cdbf Mon Sep 17 00:00:00 2001 From: Veado Date: Thu, 7 Oct 2021 17:08:06 +0200 Subject: [PATCH 2/2] Fix memo --- src/renderer/components/wallet/txs/upgrade/Upgrade.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/renderer/components/wallet/txs/upgrade/Upgrade.tsx b/src/renderer/components/wallet/txs/upgrade/Upgrade.tsx index b0d86ec2c..9309292c4 100644 --- a/src/renderer/components/wallet/txs/upgrade/Upgrade.tsx +++ b/src/renderer/components/wallet/txs/upgrade/Upgrade.tsx @@ -201,7 +201,7 @@ export const Upgrade: React.FC = (props): JSX.Element => { poolAddress, amount: amountToUpgrade, asset: runeAsset.asset, - memo: getSwitchMemo(runeNativeAddress), + memo: getSwitchMemo(form.getFieldValue('address')), network, walletAddress, walletIndex, @@ -218,7 +218,7 @@ export const Upgrade: React.FC = (props): JSX.Element => { upgrade$, amountToUpgrade, runeAsset.asset, - runeNativeAddress, + form, network, walletAddress, walletIndex, @@ -502,7 +502,7 @@ export const Upgrade: React.FC = (props): JSX.Element => { targetPoolAddressRD, RD.chain( RD.fromPredicate( - FP.not(({ halted }) => halted), + ({ halted }) => !halted, () => new Error(intl.formatMessage({ id: 'pools.halted.chain' }, { chain: runeAsset.asset.chain })) ) ),