Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: thorchain lp ledger open app ack #7632

Merged
merged 7 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/components/Modals/Send/hooks/useFormSend/useFormSend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -19,14 +20,20 @@ export const useFormSend = () => {
state: { wallet },
} = useWallet()

const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp({ isSigning: true })

const handleFormSend = useCallback(
async (sendInput: SendInput) => {
try {
const asset = selectAssetById(store.getState(), sendInput.assetId)
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({
Expand Down Expand Up @@ -72,7 +79,7 @@ export const useFormSend = () => {
send.close()
}
},
[qrCode, send, toast, translate, wallet],
[checkLedgerAppOpenIfLedgerConnected, qrCode, send, toast, translate, wallet],
)

return {
Expand Down
12 changes: 8 additions & 4 deletions src/components/Modals/Send/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,21 @@ export const estimateFees = ({
export const handleSend = async ({
sendInput,
wallet,
checkLedgerAppOpenIfLedgerConnected,
}: {
sendInput: SendInput
wallet: HDWallet
checkLedgerAppOpenIfLedgerConnected: (chainId: ChainId) => Promise<void>
}): Promise<string> => {
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
kaladinlight marked this conversation as resolved.
Show resolved Hide resolved
await checkLedgerAppOpenIfLedgerConnected(chainId)
const supportedEvmChainIds = getSupportedEvmChainIds()

const acccountMetadataFilter = { accountId: sendInput.accountId }
const accountMetadata = selectPortfolioAccountMetadataByAccountId(state, acccountMetadataFilter)
const isMetaMaskDesktop = await checkIsMetaMaskDesktop(wallet)
Expand All @@ -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)
Expand Down
14 changes: 12 additions & 2 deletions src/components/Sweep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -36,6 +37,8 @@ export const Sweep = ({
const [isSweepPending, setIsSweepPending] = useState(false)
const [txId, setTxId] = useState<string | null>(null)

const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp({ isSigning: true })

const {
state: { wallet },
} = useWallet()
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 4 additions & 3 deletions src/lib/utils/thorchain/hooks/useSendThorTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,18 @@ 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
kaladinlight marked this conversation as resolved.
Show resolved Hide resolved

if (
action !== 'withdrawRunepool' &&
!shouldUseDustAmount &&
!bn(amountOrDustCryptoBaseUnit).gt(0)
)
throw new Error('invalid amount specified')

await checkLedgerAppOpenIfLedgerConnected(asset.chainId)

const { account } = fromAccountId(accountId)

const { _txId, _serializedTxIndex } = await (async () => {
Expand Down Expand Up @@ -374,6 +374,7 @@ export const useSendThorTx = ({
const _txId = await handleSend({
sendInput,
wallet,
checkLedgerAppOpenIfLedgerConnected,
})

return {
Expand Down
8 changes: 6 additions & 2 deletions src/pages/Lending/Pool/components/Borrow/BorrowConfirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand All @@ -324,7 +327,6 @@ export const BorrowConfirm = ({
return maybeTxId
}, [
confirmedQuote,
mixpanel,
isQuoteExpired,
loanTxStatus,
collateralAssetId,
Expand All @@ -336,6 +338,7 @@ export const BorrowConfirm = ({
collateralAccountMetadata,
borrowAsset,
collateralAsset,
mixpanel,
eventData,
collateralAccountId,
estimatedFeesData,
Expand All @@ -345,6 +348,7 @@ export const BorrowConfirm = ({
setDepositAmount,
history,
selectedCurrency,
checkLedgerAppOpenIfLedgerConnected,
])

// Quote expiration interval
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -148,6 +149,8 @@ export const AddLiquidityInput: React.FC<AddLiquidityInputProps> = ({
currentAccountIdByChainId,
onAccountIdChange: handleAccountIdChange,
}) => {
const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp({ isSigning: true })

const mixpanel = getMixPanel()
const greenColor = useColorModeValue('green.600', 'green.200')
const dispatch = useAppDispatch()
Expand Down Expand Up @@ -873,7 +876,11 @@ export const AddLiquidityInput: React.FC<AddLiquidityInputProps> = ({
runeTxFeeCryptoBaseUnit,
])

const handleApprove = useCallback(() => mutate(undefined), [mutate])
const handleApprove = useCallback(async () => {
if (!assetId) return
await checkLedgerAppOpenIfLedgerConnected(fromAssetId(assetId).chainId)
kaladinlight marked this conversation as resolved.
Show resolved Hide resolved
mutate(undefined)
}, [assetId, checkLedgerAppOpenIfLedgerConnected, mutate])

const handleSubmit = useCallback(() => {
if (isApprovalRequired) return handleApprove()
Expand Down
Loading