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: defi section ledger open app gate #7628

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 33 additions & 4 deletions src/features/defi/providers/fox-farming/hooks/useFoxFarming.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethAssetId, fromAccountId, fromAssetId } from '@shapeshiftoss/caip'
import { ethAssetId, ethChainId, fromAccountId } from '@shapeshiftoss/caip'
import { CONTRACT_INTERACTION } from '@shapeshiftoss/chain-adapters'
import { supportsETH } from '@shapeshiftoss/hdwallet-core'
import { getFees } from '@shapeshiftoss/utils/dist/evm'
Expand All @@ -7,6 +7,7 @@ import { getOrCreateContractByAddress } from 'contracts/contractManager'
import { useCallback, useMemo } from 'react'
import { encodeFunctionData, getAddress, maxUint256 } from 'viem'
import { useFoxEth } from 'context/FoxEthProvider/FoxEthProvider'
import { useLedgerOpenApp } from 'hooks/useLedgerOpenApp/useLedgerOpenApp'
import { useWallet } from 'hooks/useWallet/useWallet'
import { toBaseUnit } from 'lib/math'
import { isValidAccountNumber } from 'lib/utils/accounts'
Expand Down Expand Up @@ -36,6 +37,8 @@ export const useFoxFarming = (
contractAddress: FoxEthStakingContractAddress,
{ skip }: UseFoxFarmingOptions = {},
) => {
const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp({ isSigning: true })

const { farmingAccountId } = useFoxEth()
const ethAsset = useAppSelector(state => selectAssetById(state, ethAssetId))
const lpAsset = useAppSelector(state => selectAssetById(state, foxEthLpAssetId))
Expand All @@ -49,7 +52,7 @@ export const useFoxFarming = (

const wallet = useWallet().state.wallet

const adapter = useMemo(() => assertGetEvmChainAdapter(fromAssetId(ethAssetId).chainId), [])
const adapter = useMemo(() => assertGetEvmChainAdapter(ethChainId), [])

const foxFarmingContract = useMemo(
() => getOrCreateContractByAddress(contractAddress),
Expand Down Expand Up @@ -82,6 +85,8 @@ export const useFoxFarming = (
wallet,
})

await checkLedgerAppOpenIfLedgerConnected(ethChainId)

const txid = await buildAndBroadcast({
adapter,
buildCustomTxInput,
Expand All @@ -102,6 +107,7 @@ export const useFoxFarming = (
lpAsset.precision,
adapter,
contractAddress,
checkLedgerAppOpenIfLedgerConnected,
],
)

Expand All @@ -126,6 +132,8 @@ export const useFoxFarming = (
wallet,
})

await checkLedgerAppOpenIfLedgerConnected(ethChainId)

const txid = await buildAndBroadcast({
adapter,
buildCustomTxInput,
Expand All @@ -146,6 +154,7 @@ export const useFoxFarming = (
lpAsset.precision,
adapter,
contractAddress,
checkLedgerAppOpenIfLedgerConnected,
],
)

Expand Down Expand Up @@ -271,6 +280,8 @@ export const useFoxFarming = (
const fees = await getApproveFees()
if (!fees) return

await checkLedgerAppOpenIfLedgerConnected(ethChainId)

const txid = await buildAndBroadcast({
adapter,
receiverAddress: CONTRACT_INTERACTION, // no receiver for this contract call
Expand All @@ -285,7 +296,14 @@ export const useFoxFarming = (
})

return txid
}, [accountNumber, adapter, contractAddress, getApproveFees, wallet])
}, [
accountNumber,
adapter,
checkLedgerAppOpenIfLedgerConnected,
contractAddress,
getApproveFees,
wallet,
])

const claimRewards = useCallback(async () => {
if (skip || !isValidAccountNumber(accountNumber) || !wallet || !userAddress) return
Expand All @@ -305,14 +323,25 @@ export const useFoxFarming = (
wallet,
})

await checkLedgerAppOpenIfLedgerConnected(ethChainId)

const txid = await buildAndBroadcast({
adapter,
buildCustomTxInput,
receiverAddress: await adapter.getAddress({ accountNumber, wallet }),
})

return txid
}, [accountNumber, adapter, contractAddress, foxFarmingContract.abi, skip, userAddress, wallet])
}, [
accountNumber,
adapter,
checkLedgerAppOpenIfLedgerConnected,
contractAddress,
foxFarmingContract.abi,
skip,
userAddress,
wallet,
])

return {
allowance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useCallback, useContext, useMemo } from 'react'
import { useTranslate } from 'react-polyglot'
import { useHistory } from 'react-router-dom'
import type { StepComponentProps } from 'components/DeFi/components/Steps'
import { useLedgerOpenApp } from 'hooks/useLedgerOpenApp/useLedgerOpenApp'
import { usePoll } from 'hooks/usePoll/usePoll'
import { useWallet } from 'hooks/useWallet/useWallet'
import { bn, bnOrZero } from 'lib/bignumber/bignumber'
Expand All @@ -27,6 +28,7 @@ import { DepositContext } from '../DepositContext'
type ApproveProps = StepComponentProps & { accountId: AccountId | undefined }

export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp({ isSigning: true })
const { poll } = usePoll()
const foxyApi = getFoxyApi()
const { state, dispatch } = useContext(DepositContext)
Expand Down Expand Up @@ -99,7 +101,8 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
foxyApi &&
dispatch &&
bip44Params &&
state
state &&
feeAsset
)
)
return
Expand All @@ -109,6 +112,8 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
if (!supportsETH(walletState.wallet))
throw new Error(`handleApprove: wallet does not support ethereum`)

await checkLedgerAppOpenIfLedgerConnected(feeAsset.chainId)

await foxyApi.approve({
tokenContractAddress: assetReference,
contractAddress,
Expand Down Expand Up @@ -163,6 +168,8 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
dispatch,
bip44Params,
state,
feeAsset,
checkLedgerAppOpenIfLedgerConnected,
contractAddress,
asset.precision,
poll,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { StepComponentProps } from 'components/DeFi/components/Steps'
import { Row } from 'components/Row/Row'
import { RawText, Text } from 'components/Text'
import type { TextPropTypes } from 'components/Text/Text'
import { useLedgerOpenApp } from 'hooks/useLedgerOpenApp/useLedgerOpenApp'
import { usePoll } from 'hooks/usePoll/usePoll'
import { useWallet } from 'hooks/useWallet/useWallet'
import { bn, bnOrZero } from 'lib/bignumber/bignumber'
Expand All @@ -32,6 +33,7 @@ import { DepositContext } from '../DepositContext'
type ConfirmProps = StepComponentProps & { accountId: AccountId | undefined }

export const Confirm: React.FC<ConfirmProps> = ({ onNext, accountId }) => {
const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp({ isSigning: true })
const { poll } = usePoll<TransactionReceipt | null>()
const foxyApi = getFoxyApi()
const { state, dispatch } = useContext(DepositContext)
Expand Down Expand Up @@ -73,7 +75,8 @@ export const Confirm: React.FC<ConfirmProps> = ({ onNext, accountId }) => {
walletState.wallet &&
foxyApi &&
bip44Params &&
dispatch
dispatch &&
feeAsset
)
)
return
Expand All @@ -83,6 +86,8 @@ export const Confirm: React.FC<ConfirmProps> = ({ onNext, accountId }) => {
if (!supportsETH(walletState.wallet))
throw new Error(`handleDeposit: wallet does not support ethereum`)

await checkLedgerAppOpenIfLedgerConnected(feeAsset.chainId)

const txid = await foxyApi.deposit({
amountDesired: bnOrZero(state?.deposit.cryptoAmount)
.times(bn(10).pow(asset.precision))
Expand Down Expand Up @@ -131,6 +136,8 @@ export const Confirm: React.FC<ConfirmProps> = ({ onNext, accountId }) => {
foxyApi,
bip44Params,
dispatch,
feeAsset,
checkLedgerAppOpenIfLedgerConnected,
state?.deposit.cryptoAmount,
asset.precision,
contractAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useFoxyQuery } from 'features/defi/providers/foxy/components/FoxyManage
import { useCallback, useContext, useMemo } from 'react'
import { useTranslate } from 'react-polyglot'
import type { StepComponentProps } from 'components/DeFi/components/Steps'
import { useLedgerOpenApp } from 'hooks/useLedgerOpenApp/useLedgerOpenApp'
import { usePoll } from 'hooks/usePoll/usePoll'
import { useWallet } from 'hooks/useWallet/useWallet'
import { bn, bnOrZero } from 'lib/bignumber/bignumber'
Expand All @@ -26,6 +27,7 @@ import { WithdrawContext } from '../WithdrawContext'
type ApproveProps = StepComponentProps & { accountId: AccountId | undefined }

export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp({ isSigning: true })
const { poll } = usePoll()
const foxyApi = getFoxyApi()
const { state, dispatch } = useContext(WithdrawContext)
Expand Down Expand Up @@ -115,7 +117,8 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
state?.withdraw &&
foxyApi &&
dispatch &&
bip44Params
bip44Params &&
feeAsset
)
)
return
Expand All @@ -124,6 +127,9 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
if (!supportsETH(walletState.wallet))
throw new Error(`handleApprove: wallet does not support ethereum`)
dispatch({ type: FoxyWithdrawActionType.SET_LOADING, payload: true })

await checkLedgerAppOpenIfLedgerConnected(feeAsset.chainId)

await foxyApi.approve({
tokenContractAddress: rewardId,
contractAddress,
Expand Down Expand Up @@ -167,9 +173,11 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
}, [
asset.precision,
bip44Params,
checkLedgerAppOpenIfLedgerConnected,
contractAddress,
dispatch,
estimatedGasCryptoBaseUnit,
feeAsset,
foxyApi,
getWithdrawGasEstimate,
onNext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { StepComponentProps } from 'components/DeFi/components/Steps'
import { Row } from 'components/Row/Row'
import { RawText, Text } from 'components/Text'
import type { TextPropTypes } from 'components/Text/Text'
import { useLedgerOpenApp } from 'hooks/useLedgerOpenApp/useLedgerOpenApp'
import { usePoll } from 'hooks/usePoll/usePoll'
import { useWallet } from 'hooks/useWallet/useWallet'
import { bn, bnOrZero } from 'lib/bignumber/bignumber'
Expand All @@ -34,6 +35,7 @@ export const Confirm: React.FC<StepComponentProps & { accountId?: AccountId | un
onNext,
accountId,
}) => {
const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp({ isSigning: true })
const { poll } = usePoll<TransactionReceipt | null>()
const foxyApi = getFoxyApi()
const { state, dispatch } = useContext(WithdrawContext)
Expand Down Expand Up @@ -78,7 +80,8 @@ export const Confirm: React.FC<StepComponentProps & { accountId?: AccountId | un
walletState.wallet &&
foxyApi &&
dispatch &&
bip44Params
bip44Params &&
feeAsset
)
)
return
Expand All @@ -87,6 +90,8 @@ export const Confirm: React.FC<StepComponentProps & { accountId?: AccountId | un
if (!supportsETH(walletState.wallet))
throw new Error(`handleConfirm: wallet does not support ethereum`)

await checkLedgerAppOpenIfLedgerConnected(feeAsset.chainId)

const txid = await foxyApi.withdraw({
tokenContractAddress: rewardId,
userAddress: accountAddress,
Expand Down Expand Up @@ -131,6 +136,8 @@ export const Confirm: React.FC<StepComponentProps & { accountId?: AccountId | un
foxyApi,
dispatch,
bip44Params,
feeAsset,
checkLedgerAppOpenIfLedgerConnected,
contractAddress,
underlyingAsset.precision,
onNext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { encodeFunctionData, getAddress } from 'viem'
import type { StepComponentProps } from 'components/DeFi/components/Steps'
import { useBrowserRouter } from 'hooks/useBrowserRouter/useBrowserRouter'
import { useErrorHandler } from 'hooks/useErrorToast/useErrorToast'
import { useLedgerOpenApp } from 'hooks/useLedgerOpenApp/useLedgerOpenApp'
import { usePoll } from 'hooks/usePoll/usePoll'
import { useWallet } from 'hooks/useWallet/useWallet'
import { bnOrZero } from 'lib/bignumber/bignumber'
Expand Down Expand Up @@ -54,6 +55,7 @@ import { DepositContext } from '../DepositContext'
type ApproveProps = StepComponentProps & { accountId: AccountId | undefined }

export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp({ isSigning: true })
const { poll } = usePoll()
const { state, dispatch } = useContext(DepositContext)
const estimatedGasCryptoPrecision = state?.approve.estimatedGasCryptoPrecision
Expand Down Expand Up @@ -156,6 +158,9 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
})

const adapter = assertGetEvmChainAdapter(chainId)

await checkLedgerAppOpenIfLedgerConnected(asset.chainId)

const buildCustomTxInput = await createBuildCustomTxInput({
accountNumber,
from: fromAccountId(accountId).account,
Expand Down Expand Up @@ -205,10 +210,13 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
}, [
accountId,
accountNumber,
asset,
asset.assetId,
asset.chainId,
asset.precision,
assetId,
assets,
chainId,
checkLedgerAppOpenIfLedgerConnected,
dispatch,
inboundAddress,
onNext,
Expand Down
Loading
Loading