From 6016809c57fd76581389f3e016a34ed670712176 Mon Sep 17 00:00:00 2001 From: gomes <17035424+gomesalexandre@users.noreply.github.com> Date: Tue, 27 Aug 2024 18:10:11 +0200 Subject: [PATCH 1/4] feat: thorchain lp ledger open app ack --- src/lib/utils/thorchain/hooks/useSendThorTx.tsx | 6 +++--- .../components/AddLiquidity/AddLiquidityInput.tsx | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/lib/utils/thorchain/hooks/useSendThorTx.tsx b/src/lib/utils/thorchain/hooks/useSendThorTx.tsx index 96e3193a0e5..26c13256189 100644 --- a/src/lib/utils/thorchain/hooks/useSendThorTx.tsx +++ b/src/lib/utils/thorchain/hooks/useSendThorTx.tsx @@ -270,9 +270,11 @@ export const useSendThorTx = ({ if (!accountId) return if (!transactionType) return if (!estimateFeesArgs) return - if (accountNumber === undefined) return if (isToken(asset.assetId) && !inboundAddressData) return + await checkLedgerAppOpenIfLedgerConnected(asset.chainId) + if (accountNumber === undefined) return + if ( action !== 'withdrawRunepool' && !shouldUseDustAmount && @@ -280,8 +282,6 @@ export const useSendThorTx = ({ ) throw new Error('invalid amount specified') - await checkLedgerAppOpenIfLedgerConnected(asset.chainId) - const { account } = fromAccountId(accountId) const { _txId, _serializedTxIndex } = await (async () => { diff --git a/src/pages/ThorChainLP/components/AddLiquidity/AddLiquidityInput.tsx b/src/pages/ThorChainLP/components/AddLiquidity/AddLiquidityInput.tsx index 21e6c05a761..9ceb939395e 100644 --- a/src/pages/ThorChainLP/components/AddLiquidity/AddLiquidityInput.tsx +++ b/src/pages/ThorChainLP/components/AddLiquidity/AddLiquidityInput.tsx @@ -51,6 +51,7 @@ import { useBrowserRouter } from 'hooks/useBrowserRouter/useBrowserRouter' import { useFeatureFlag } from 'hooks/useFeatureFlag/useFeatureFlag' import { useIsSmartContractAddress } from 'hooks/useIsSmartContractAddress/useIsSmartContractAddress' import { useIsSnapInstalled } from 'hooks/useIsSnapInstalled/useIsSnapInstalled' +import { useLedgerOpenApp } from 'hooks/useLedgerOpenApp/useLedgerOpenApp' import { useModal } from 'hooks/useModal/useModal' import { useToggle } from 'hooks/useToggle/useToggle' import { useWallet } from 'hooks/useWallet/useWallet' @@ -148,6 +149,8 @@ export const AddLiquidityInput: React.FC = ({ currentAccountIdByChainId, onAccountIdChange: handleAccountIdChange, }) => { + const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp({ isSigning: true }) + const mixpanel = getMixPanel() const greenColor = useColorModeValue('green.600', 'green.200') const dispatch = useAppDispatch() @@ -873,7 +876,11 @@ export const AddLiquidityInput: React.FC = ({ runeTxFeeCryptoBaseUnit, ]) - const handleApprove = useCallback(() => mutate(undefined), [mutate]) + const handleApprove = useCallback(async () => { + if (!assetId) return + await checkLedgerAppOpenIfLedgerConnected(fromAssetId(assetId).chainId) + mutate(undefined) + }, [assetId, checkLedgerAppOpenIfLedgerConnected, mutate]) const handleSubmit = useCallback(() => { if (isApprovalRequired) return handleApprove() From 39154e5e40b47fe39c34452d15b07b9c43282cc6 Mon Sep 17 00:00:00 2001 From: gomes <17035424+gomesalexandre@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:45:14 +0200 Subject: [PATCH 2/4] feat: handleSend too --- .../Modals/Send/hooks/useFormSend/useFormSend.tsx | 11 +++++++++-- src/components/Modals/Send/utils.ts | 12 ++++++++---- src/components/Sweep.tsx | 14 ++++++++++++-- src/lib/utils/thorchain/hooks/useSendThorTx.tsx | 1 + .../Pool/components/Borrow/BorrowConfirm.tsx | 8 ++++++-- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/components/Modals/Send/hooks/useFormSend/useFormSend.tsx b/src/components/Modals/Send/hooks/useFormSend/useFormSend.tsx index 138e220a58f..9cc4c35cbda 100644 --- a/src/components/Modals/Send/hooks/useFormSend/useFormSend.tsx +++ b/src/components/Modals/Send/hooks/useFormSend/useFormSend.tsx @@ -2,6 +2,7 @@ import { ExternalLinkIcon } from '@chakra-ui/icons' import { Link, Text, useToast } from '@chakra-ui/react' import { useCallback } from 'react' import { useTranslate } from 'react-polyglot' +import { useLedgerOpenApp } from 'hooks/useLedgerOpenApp/useLedgerOpenApp' import { useModal } from 'hooks/useModal/useModal' import { useWallet } from 'hooks/useWallet/useWallet' import { selectAssetById } from 'state/slices/selectors' @@ -19,6 +20,8 @@ export const useFormSend = () => { state: { wallet }, } = useWallet() + const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp({ isSigning: true }) + const handleFormSend = useCallback( async (sendInput: SendInput) => { try { @@ -26,7 +29,11 @@ export const useFormSend = () => { if (!asset) throw new Error(`No asset found for assetId ${sendInput.assetId}`) if (!wallet) throw new Error('No wallet connected') - const broadcastTXID = await handleSend({ wallet, sendInput }) + const broadcastTXID = await handleSend({ + wallet, + sendInput, + checkLedgerAppOpenIfLedgerConnected, + }) setTimeout(() => { toast({ @@ -72,7 +79,7 @@ export const useFormSend = () => { send.close() } }, - [qrCode, send, toast, translate, wallet], + [checkLedgerAppOpenIfLedgerConnected, qrCode, send, toast, translate, wallet], ) return { diff --git a/src/components/Modals/Send/utils.ts b/src/components/Modals/Send/utils.ts index ed44f255959..2251d5ec14e 100644 --- a/src/components/Modals/Send/utils.ts +++ b/src/components/Modals/Send/utils.ts @@ -98,15 +98,21 @@ export const estimateFees = ({ export const handleSend = async ({ sendInput, wallet, + checkLedgerAppOpenIfLedgerConnected, }: { sendInput: SendInput wallet: HDWallet + checkLedgerAppOpenIfLedgerConnected: (chainId: ChainId) => Promise }): Promise => { - const supportedEvmChainIds = getSupportedEvmChainIds() - const state = store.getState() const asset = selectAssetById(state, sendInput.assetId ?? '') if (!asset) return '' + + const chainId = asset.chainId + // The double check here and in broadcastTXID isn't a mistake - getAddress() does on-device address derivation + await checkLedgerAppOpenIfLedgerConnected(chainId) + const supportedEvmChainIds = getSupportedEvmChainIds() + const acccountMetadataFilter = { accountId: sendInput.accountId } const accountMetadata = selectPortfolioAccountMetadataByAccountId(state, acccountMetadataFilter) const isMetaMaskDesktop = await checkIsMetaMaskDesktop(wallet) @@ -126,8 +132,6 @@ export const handleSend = async ({ .times(bn(10).exponentiatedBy(asset.precision)) .toFixed(0) - const chainId = asset.chainId - const { estimatedFees, feeType, to, memo, from } = sendInput if (!accountMetadata) diff --git a/src/components/Sweep.tsx b/src/components/Sweep.tsx index 2fae6ae2e8d..cd4c32d56a8 100644 --- a/src/components/Sweep.tsx +++ b/src/components/Sweep.tsx @@ -4,6 +4,7 @@ import { FeeDataKey } from '@shapeshiftoss/chain-adapters' import { useCallback, useEffect, useState } from 'react' import { useTranslate } from 'react-polyglot' import { Row } from 'components/Row/Row' +import { useLedgerOpenApp } from 'hooks/useLedgerOpenApp/useLedgerOpenApp' import { useWallet } from 'hooks/useWallet/useWallet' import { fromBaseUnit } from 'lib/math' import { sleep } from 'lib/poll/poll' @@ -36,6 +37,8 @@ export const Sweep = ({ const [isSweepPending, setIsSweepPending] = useState(false) const [txId, setTxId] = useState(null) + const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp({ isSigning: true }) + const { state: { wallet }, } = useWallet() @@ -83,12 +86,19 @@ export const Sweep = ({ fiatSymbol: '', } - const txId = await handleSend({ wallet, sendInput }) + const txId = await handleSend({ wallet, sendInput, checkLedgerAppOpenIfLedgerConnected }) setTxId(txId) } catch (e) { console.error(e) } - }, [accountId, assetId, estimatedFeesData, fromAddress, wallet]) + }, [ + accountId, + assetId, + checkLedgerAppOpenIfLedgerConnected, + estimatedFeesData, + fromAddress, + wallet, + ]) const adapter = assertGetUtxoChainAdapter(fromAssetId(assetId).chainId) diff --git a/src/lib/utils/thorchain/hooks/useSendThorTx.tsx b/src/lib/utils/thorchain/hooks/useSendThorTx.tsx index 26c13256189..a6c9b6e1c4f 100644 --- a/src/lib/utils/thorchain/hooks/useSendThorTx.tsx +++ b/src/lib/utils/thorchain/hooks/useSendThorTx.tsx @@ -374,6 +374,7 @@ export const useSendThorTx = ({ const _txId = await handleSend({ sendInput, wallet, + checkLedgerAppOpenIfLedgerConnected, }) return { diff --git a/src/pages/Lending/Pool/components/Borrow/BorrowConfirm.tsx b/src/pages/Lending/Pool/components/Borrow/BorrowConfirm.tsx index f7d9301442e..ae2bf9f9ac4 100644 --- a/src/pages/Lending/Pool/components/Borrow/BorrowConfirm.tsx +++ b/src/pages/Lending/Pool/components/Borrow/BorrowConfirm.tsx @@ -39,6 +39,7 @@ import { RawText, Text } from 'components/Text' import { getChainAdapterManager } from 'context/PluginProvider/chainAdapterSingleton' import { queryClient } from 'context/QueryClientProvider/queryClient' import { useInterval } from 'hooks/useInterval/useInterval' +import { useLedgerOpenApp } from 'hooks/useLedgerOpenApp/useLedgerOpenApp' import { useWallet } from 'hooks/useWallet/useWallet' import { bn, bnOrZero } from 'lib/bignumber/bignumber' import { getMaybeCompositeAssetSymbol } from 'lib/mixpanel/helpers' @@ -91,6 +92,8 @@ export const BorrowConfirm = ({ state: { wallet }, } = useWallet() + const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp({ isSigning: true }) + const borrowAssetId = borrowAsset?.assetId ?? '' const history = useHistory() const translate = useTranslate() @@ -312,7 +315,7 @@ export const BorrowConfirm = ({ if (!sendInput) throw new Error('Error building send input') - return handleSend({ sendInput, wallet }) + return handleSend({ sendInput, wallet, checkLedgerAppOpenIfLedgerConnected }) })() if (!maybeTxId) { @@ -324,7 +327,6 @@ export const BorrowConfirm = ({ return maybeTxId }, [ confirmedQuote, - mixpanel, isQuoteExpired, loanTxStatus, collateralAssetId, @@ -336,6 +338,7 @@ export const BorrowConfirm = ({ collateralAccountMetadata, borrowAsset, collateralAsset, + mixpanel, eventData, collateralAccountId, estimatedFeesData, @@ -345,6 +348,7 @@ export const BorrowConfirm = ({ setDepositAmount, history, selectedCurrency, + checkLedgerAppOpenIfLedgerConnected, ]) // Quote expiration interval From 4ffc42229bd227e051eb00550215367f156f4a89 Mon Sep 17 00:00:00 2001 From: gomes <17035424+gomesalexandre@users.noreply.github.com> Date: Wed, 28 Aug 2024 19:17:13 +0200 Subject: [PATCH 3/4] feat: leverage onMutate --- .../components/AddLiquidity/AddLiquidityInput.tsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pages/ThorChainLP/components/AddLiquidity/AddLiquidityInput.tsx b/src/pages/ThorChainLP/components/AddLiquidity/AddLiquidityInput.tsx index 9ceb939395e..8ca7fa3c132 100644 --- a/src/pages/ThorChainLP/components/AddLiquidity/AddLiquidityInput.tsx +++ b/src/pages/ThorChainLP/components/AddLiquidity/AddLiquidityInput.tsx @@ -608,6 +608,10 @@ export const AddLiquidityInput: React.FC = ({ from: poolAssetAccountId ? fromAccountId(poolAssetAccountId).account : undefined, accountNumber: poolAssetAccountNumber, }), + onMutate: async () => { + if (!poolAsset) return + await checkLedgerAppOpenIfLedgerConnected(poolAsset.chainId) + }, onSuccess: (txId: string) => { setApprovalTxId(txId) }, @@ -876,11 +880,7 @@ export const AddLiquidityInput: React.FC = ({ runeTxFeeCryptoBaseUnit, ]) - const handleApprove = useCallback(async () => { - if (!assetId) return - await checkLedgerAppOpenIfLedgerConnected(fromAssetId(assetId).chainId) - mutate(undefined) - }, [assetId, checkLedgerAppOpenIfLedgerConnected, mutate]) + const handleApprove = useCallback(() => mutate(undefined), [mutate]) const handleSubmit = useCallback(() => { if (isApprovalRequired) return handleApprove() From 93bf620d28f96958a39b20414dadf6fa533cca3c Mon Sep 17 00:00:00 2001 From: gomes <17035424+gomesalexandre@users.noreply.github.com> Date: Wed, 28 Aug 2024 19:22:41 +0200 Subject: [PATCH 4/4] fet: rm comment --- src/components/Modals/Send/utils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/Modals/Send/utils.ts b/src/components/Modals/Send/utils.ts index 2251d5ec14e..ad10b9843a0 100644 --- a/src/components/Modals/Send/utils.ts +++ b/src/components/Modals/Send/utils.ts @@ -109,7 +109,6 @@ export const handleSend = async ({ if (!asset) return '' const chainId = asset.chainId - // The double check here and in broadcastTXID isn't a mistake - getAddress() does on-device address derivation await checkLedgerAppOpenIfLedgerConnected(chainId) const supportedEvmChainIds = getSupportedEvmChainIds()