From 7dbd9d525b7a09d5cfa422d5a81b938383fb82d0 Mon Sep 17 00:00:00 2001 From: shan-57blocks <115970472+shan-57blocks@users.noreply.github.com> Date: Fri, 15 Nov 2024 11:23:12 +0800 Subject: [PATCH] Approve both lenders with retry (#356) * approve both lenders with retry * update approve lenders --------- Co-authored-by: shan --- .../Lend/components/ApproveLenderBase.tsx | 103 ++++++++++++++++++ .../Lend/components/PersonaEvaluation.tsx | 60 +--------- .../Lend/solanaSupply/1-Evaluation.tsx | 19 +--- .../Lend/solanaSupply/2-ApproveLender.tsx | 37 +++++++ ...-ChooseTranche.tsx => 3-ChooseTranche.tsx} | 0 ...{3-ChooseAmount.tsx => 4-ChooseAmount.tsx} | 0 ...veAllowance.tsx => 5-ApproveAllowance.tsx} | 0 .../{4-Transfer.tsx => 6-Transfer.tsx} | 0 .../{5-Success.tsx => 7-Success.tsx} | 0 ...{6-PointsEarned.tsx => 8-PointsEarned.tsx} | 0 .../components/Lend/solanaSupply/index.tsx | 24 +++- .../components/Lend/supplyV2/2-Evaluation.tsx | 13 +-- .../Lend/supplyV2/3-ApproveLender.tsx | 26 +++++ ...{3-ChooseAmount.tsx => 4-ChooseAmount.tsx} | 0 ...veAllowance.tsx => 5-ApproveAllowance.tsx} | 0 .../{5-Transfer.tsx => 6-Transfer.tsx} | 0 .../supplyV2/{6-Success.tsx => 7-Success.tsx} | 0 ...-Notifications.tsx => 8-Notifications.tsx} | 0 .../src/components/Lend/supplyV2/index.tsx | 42 ++++--- .../huma-widget/src/store/widgets.store.ts | 1 + 20 files changed, 209 insertions(+), 116 deletions(-) create mode 100644 packages/huma-widget/src/components/Lend/components/ApproveLenderBase.tsx create mode 100644 packages/huma-widget/src/components/Lend/solanaSupply/2-ApproveLender.tsx rename packages/huma-widget/src/components/Lend/solanaSupply/{2-ChooseTranche.tsx => 3-ChooseTranche.tsx} (100%) rename packages/huma-widget/src/components/Lend/solanaSupply/{3-ChooseAmount.tsx => 4-ChooseAmount.tsx} (100%) rename packages/huma-widget/src/components/Lend/solanaSupply/{4-ApproveAllowance.tsx => 5-ApproveAllowance.tsx} (100%) rename packages/huma-widget/src/components/Lend/solanaSupply/{4-Transfer.tsx => 6-Transfer.tsx} (100%) rename packages/huma-widget/src/components/Lend/solanaSupply/{5-Success.tsx => 7-Success.tsx} (100%) rename packages/huma-widget/src/components/Lend/solanaSupply/{6-PointsEarned.tsx => 8-PointsEarned.tsx} (100%) create mode 100644 packages/huma-widget/src/components/Lend/supplyV2/3-ApproveLender.tsx rename packages/huma-widget/src/components/Lend/supplyV2/{3-ChooseAmount.tsx => 4-ChooseAmount.tsx} (100%) rename packages/huma-widget/src/components/Lend/supplyV2/{4-ApproveAllowance.tsx => 5-ApproveAllowance.tsx} (100%) rename packages/huma-widget/src/components/Lend/supplyV2/{5-Transfer.tsx => 6-Transfer.tsx} (100%) rename packages/huma-widget/src/components/Lend/supplyV2/{6-Success.tsx => 7-Success.tsx} (100%) rename packages/huma-widget/src/components/Lend/supplyV2/{7-Notifications.tsx => 8-Notifications.tsx} (100%) diff --git a/packages/huma-widget/src/components/Lend/components/ApproveLenderBase.tsx b/packages/huma-widget/src/components/Lend/components/ApproveLenderBase.tsx new file mode 100644 index 0000000..5299097 --- /dev/null +++ b/packages/huma-widget/src/components/Lend/components/ApproveLenderBase.tsx @@ -0,0 +1,103 @@ +/* eslint-disable no-await-in-loop */ +import { + CHAIN_TYPE, + checkIsDev, + IdentityServiceV2, + timeUtil, + TrancheType, +} from '@huma-finance/shared' +import { useChainInfo } from '@huma-finance/web-shared' +import React, { useCallback, useEffect } from 'react' + +import { useAppDispatch } from '../../../hooks/useRedux' +import { setError, setStep } from '../../../store/widgets.reducers' +import { WIDGET_STEP } from '../../../store/widgets.store' +import { LoadingModal } from '../../LoadingModal' + +type Props = { + juniorTrancheVault: string + seniorTrancheVault: string + chainType: CHAIN_TYPE + isUniTranche: boolean + chainSpecificData?: Record + changeTranche: (tranche: TrancheType) => void +} + +export function ApproveLenderBase({ + juniorTrancheVault, + seniorTrancheVault, + isUniTranche, + chainType, + chainSpecificData, + changeTranche, +}: Props): React.ReactElement | null { + const isDev = checkIsDev() + const dispatch = useAppDispatch() + const { account, chainId } = useChainInfo(isDev, chainType) + + const approveLender = useCallback( + async (trancheVault: string) => { + let tryAttempts = 2 + + while (tryAttempts > 0) { + try { + tryAttempts -= 1 + await IdentityServiceV2.approveLender( + account!, + chainId!, + trancheVault, + isDev, + chainSpecificData, + ) + tryAttempts = 0 + } catch (e: unknown) { + if (tryAttempts === 0) { + dispatch( + setError({ + errorMessage: 'Something went wrong, please try again later.', + }), + ) + } else { + await timeUtil.sleep(3000) + console.error(e) + } + } + } + }, + [account, chainId, chainSpecificData, dispatch, isDev], + ) + + useEffect(() => { + const approveLenderAsync = async () => { + if (account && chainId) { + if (isUniTranche) { + await approveLender(juniorTrancheVault) + changeTranche('junior') + dispatch(setStep(WIDGET_STEP.ChooseAmount)) + } else { + await approveLender(juniorTrancheVault) + await approveLender(seniorTrancheVault) + dispatch(setStep(WIDGET_STEP.ChooseTranche)) + } + } + } + + approveLenderAsync() + }, [ + account, + approveLender, + chainId, + changeTranche, + dispatch, + isUniTranche, + juniorTrancheVault, + seniorTrancheVault, + ]) + + return ( + + ) +} diff --git a/packages/huma-widget/src/components/Lend/components/PersonaEvaluation.tsx b/packages/huma-widget/src/components/Lend/components/PersonaEvaluation.tsx index 405040d..835a03f 100644 --- a/packages/huma-widget/src/components/Lend/components/PersonaEvaluation.tsx +++ b/packages/huma-widget/src/components/Lend/components/PersonaEvaluation.tsx @@ -8,7 +8,6 @@ import { IdentityVerificationStatusV2, KYCCopy, KYCType, - TrancheType, VerificationStatusResultV2, } from '@huma-finance/shared' import { useAuthErrorHandling, useChainInfo } from '@huma-finance/web-shared' @@ -34,22 +33,16 @@ type Props = { seniorTrancheVault: string } chainType: CHAIN_TYPE - isUniTranche: boolean pointsTestnetExperience: boolean campaign?: Campaign - chainSpecificData?: Record - changeTranche: (tranche: TrancheType) => void handleClose: (options?: CloseModalOptions) => void } export function PersonaEvaluation({ poolInfo, - isUniTranche, campaign, pointsTestnetExperience, chainType, - chainSpecificData, - changeTranche, handleClose, }: Props): React.ReactElement | null { const theme = useTheme() @@ -95,55 +88,6 @@ export function PersonaEvaluation({ pointsTestnetExperience, ]) - const approveLender = useCallback(async () => { - isActionOngoingRef.current = true - setLoadingType('approveLender') - try { - await IdentityServiceV2.approveLender( - account!, - chainId!, - poolInfo.juniorTrancheVault, - isDev, - chainSpecificData, - ) - } catch (e: unknown) { - console.error(e) - } - - if (!isUniTranche) { - try { - await IdentityServiceV2.approveLender( - account!, - chainId!, - poolInfo.seniorTrancheVault, - isDev, - chainSpecificData, - ) - } catch (e: unknown) { - console.error(e) - } - } - if (isUniTranche) { - changeTranche('junior') - dispatch(setStep(WIDGET_STEP.ChooseAmount)) - } else { - dispatch(setStep(WIDGET_STEP.ChooseTranche)) - } - - isActionOngoingRef.current = false - setLoadingType(undefined) - }, [ - account, - chainId, - chainSpecificData, - changeTranche, - dispatch, - isDev, - isUniTranche, - poolInfo.juniorTrancheVault, - poolInfo.seniorTrancheVault, - ]) - const checkVerificationStatus = useCallback(async () => { if (isActionOngoingRef.current || isKYCResumedRef.current) { return @@ -247,7 +191,7 @@ export function PersonaEvaluation({ } case IdentityVerificationStatusV2.CONSENTED_TO_SUBSCRIPTION: { - await approveLender() + dispatch(setStep(WIDGET_STEP.ApproveLender)) break } @@ -260,7 +204,7 @@ export function PersonaEvaluation({ } finally { isActionOngoingRef.current = false } - }, [KYCCopies, account, approveLender, chainId, isDev, setAuthError]) + }, [KYCCopies, account, chainId, dispatch, isDev, setAuthError]) useEffect(() => { checkVerificationStatus() diff --git a/packages/huma-widget/src/components/Lend/solanaSupply/1-Evaluation.tsx b/packages/huma-widget/src/components/Lend/solanaSupply/1-Evaluation.tsx index 12db7f2..67f7a4e 100644 --- a/packages/huma-widget/src/components/Lend/solanaSupply/1-Evaluation.tsx +++ b/packages/huma-widget/src/components/Lend/solanaSupply/1-Evaluation.tsx @@ -1,9 +1,4 @@ -import { - CHAIN_TYPE, - SOLANA_CHAIN_INFO, - SolanaPoolInfo, - TrancheType, -} from '@huma-finance/shared' +import { CHAIN_TYPE, SolanaPoolInfo } from '@huma-finance/shared' import React from 'react' import { PersonaEvaluation } from '../components/PersonaEvaluation' @@ -11,24 +6,18 @@ import { Campaign } from '../supplyV2' type Props = { poolInfo: SolanaPoolInfo - isUniTranche: boolean pointsTestnetExperience: boolean campaign?: Campaign - changeTranche: (tranche: TrancheType) => void handleClose: () => void } export function Evaluation({ poolInfo, - isUniTranche, pointsTestnetExperience, campaign, - changeTranche, handleClose, }: Props): React.ReactElement | null { if (poolInfo.KYC?.Persona) { - const solanChainInfo = SOLANA_CHAIN_INFO[poolInfo.chainId] - return ( ) } diff --git a/packages/huma-widget/src/components/Lend/solanaSupply/2-ApproveLender.tsx b/packages/huma-widget/src/components/Lend/solanaSupply/2-ApproveLender.tsx new file mode 100644 index 0000000..ad0368b --- /dev/null +++ b/packages/huma-widget/src/components/Lend/solanaSupply/2-ApproveLender.tsx @@ -0,0 +1,37 @@ +import { + CHAIN_TYPE, + SOLANA_CHAIN_INFO, + SolanaPoolInfo, + TrancheType, +} from '@huma-finance/shared' +import React from 'react' + +import { ApproveLenderBase } from '../components/ApproveLenderBase' + +type Props = { + poolInfo: SolanaPoolInfo + isUniTranche: boolean + changeTranche: (tranche: TrancheType) => void +} + +export function ApproveLender({ + poolInfo, + isUniTranche, + changeTranche, +}: Props): React.ReactElement | null { + const solanaChainInfo = SOLANA_CHAIN_INFO[poolInfo.chainId] + + return ( + + ) +} diff --git a/packages/huma-widget/src/components/Lend/solanaSupply/2-ChooseTranche.tsx b/packages/huma-widget/src/components/Lend/solanaSupply/3-ChooseTranche.tsx similarity index 100% rename from packages/huma-widget/src/components/Lend/solanaSupply/2-ChooseTranche.tsx rename to packages/huma-widget/src/components/Lend/solanaSupply/3-ChooseTranche.tsx diff --git a/packages/huma-widget/src/components/Lend/solanaSupply/3-ChooseAmount.tsx b/packages/huma-widget/src/components/Lend/solanaSupply/4-ChooseAmount.tsx similarity index 100% rename from packages/huma-widget/src/components/Lend/solanaSupply/3-ChooseAmount.tsx rename to packages/huma-widget/src/components/Lend/solanaSupply/4-ChooseAmount.tsx diff --git a/packages/huma-widget/src/components/Lend/solanaSupply/4-ApproveAllowance.tsx b/packages/huma-widget/src/components/Lend/solanaSupply/5-ApproveAllowance.tsx similarity index 100% rename from packages/huma-widget/src/components/Lend/solanaSupply/4-ApproveAllowance.tsx rename to packages/huma-widget/src/components/Lend/solanaSupply/5-ApproveAllowance.tsx diff --git a/packages/huma-widget/src/components/Lend/solanaSupply/4-Transfer.tsx b/packages/huma-widget/src/components/Lend/solanaSupply/6-Transfer.tsx similarity index 100% rename from packages/huma-widget/src/components/Lend/solanaSupply/4-Transfer.tsx rename to packages/huma-widget/src/components/Lend/solanaSupply/6-Transfer.tsx diff --git a/packages/huma-widget/src/components/Lend/solanaSupply/5-Success.tsx b/packages/huma-widget/src/components/Lend/solanaSupply/7-Success.tsx similarity index 100% rename from packages/huma-widget/src/components/Lend/solanaSupply/5-Success.tsx rename to packages/huma-widget/src/components/Lend/solanaSupply/7-Success.tsx diff --git a/packages/huma-widget/src/components/Lend/solanaSupply/6-PointsEarned.tsx b/packages/huma-widget/src/components/Lend/solanaSupply/8-PointsEarned.tsx similarity index 100% rename from packages/huma-widget/src/components/Lend/solanaSupply/6-PointsEarned.tsx rename to packages/huma-widget/src/components/Lend/solanaSupply/8-PointsEarned.tsx diff --git a/packages/huma-widget/src/components/Lend/solanaSupply/index.tsx b/packages/huma-widget/src/components/Lend/solanaSupply/index.tsx index c730b3f..69387f3 100644 --- a/packages/huma-widget/src/components/Lend/solanaSupply/index.tsx +++ b/packages/huma-widget/src/components/Lend/solanaSupply/index.tsx @@ -19,11 +19,12 @@ import { ErrorModal } from '../../ErrorModal' import { PointsEarned } from '../../PointsEarned' import { WidgetWrapper } from '../../WidgetWrapper' import { Evaluation } from './1-Evaluation' -import { ChooseTranche } from './2-ChooseTranche' -import { ChooseAmount } from './3-ChooseAmount' -import { Transfer } from './4-Transfer' -import { Success } from './5-Success' -import { ApproveAllowance } from './4-ApproveAllowance' +import { ApproveLender } from './2-ApproveLender' +import { ChooseTranche } from './3-ChooseTranche' +import { ChooseAmount } from './4-ChooseAmount' +import { ApproveAllowance } from './5-ApproveAllowance' +import { Transfer } from './6-Transfer' +import { Success } from './7-Success' export interface Campaign { id: string @@ -72,6 +73,11 @@ export function SolanaLendSupply({ return } + if (!isUniTranche && (!juniorLenderApproved || !seniorLenderApproved)) { + dispatch(setStep(WIDGET_STEP.ApproveLender)) + return + } + if (juniorLenderApproved && !seniorLenderApproved) { setSelectedTranche('junior') dispatch(setStep(WIDGET_STEP.ChooseAmount)) @@ -97,6 +103,7 @@ export function SolanaLendSupply({ juniorLenderApproved, seniorLenderApproved, isLoadingTokenAccount, + isUniTranche, ]) if (isLoadingLenderAccounts || isLoadingTokenAccount) { @@ -121,10 +128,15 @@ export function SolanaLendSupply({ {step === WIDGET_STEP.Evaluation && ( + )} + {step === WIDGET_STEP.ApproveLender && ( + )} diff --git a/packages/huma-widget/src/components/Lend/supplyV2/2-Evaluation.tsx b/packages/huma-widget/src/components/Lend/supplyV2/2-Evaluation.tsx index e32b775..8317596 100644 --- a/packages/huma-widget/src/components/Lend/supplyV2/2-Evaluation.tsx +++ b/packages/huma-widget/src/components/Lend/supplyV2/2-Evaluation.tsx @@ -1,9 +1,4 @@ -import { - CHAIN_TYPE, - CloseModalOptions, - PoolInfoV2, - TrancheType, -} from '@huma-finance/shared' +import { CHAIN_TYPE, CloseModalOptions, PoolInfoV2 } from '@huma-finance/shared' import React from 'react' import { Campaign } from '.' @@ -12,19 +7,15 @@ import { SecuritizeEvaluation } from '../components/SecuritizeEvaluation' type Props = { poolInfo: PoolInfoV2 - isUniTranche: boolean pointsTestnetExperience: boolean campaign?: Campaign - changeTranche: (tranche: TrancheType) => void handleClose: (options?: CloseModalOptions) => void } export function Evaluation({ poolInfo, - isUniTranche, campaign, pointsTestnetExperience, - changeTranche, handleClose, }: Props): React.ReactElement | null { if (poolInfo.KYC?.Securitize) { @@ -37,11 +28,9 @@ export function Evaluation({ ) } diff --git a/packages/huma-widget/src/components/Lend/supplyV2/3-ApproveLender.tsx b/packages/huma-widget/src/components/Lend/supplyV2/3-ApproveLender.tsx new file mode 100644 index 0000000..e1b496f --- /dev/null +++ b/packages/huma-widget/src/components/Lend/supplyV2/3-ApproveLender.tsx @@ -0,0 +1,26 @@ +import { CHAIN_TYPE, PoolInfoV2, TrancheType } from '@huma-finance/shared' +import React from 'react' + +import { ApproveLenderBase } from '../components/ApproveLenderBase' + +type Props = { + poolInfo: PoolInfoV2 + isUniTranche: boolean + changeTranche: (tranche: TrancheType) => void +} + +export function ApproveLender({ + poolInfo, + isUniTranche, + changeTranche, +}: Props): React.ReactElement { + return ( + + ) +} diff --git a/packages/huma-widget/src/components/Lend/supplyV2/3-ChooseAmount.tsx b/packages/huma-widget/src/components/Lend/supplyV2/4-ChooseAmount.tsx similarity index 100% rename from packages/huma-widget/src/components/Lend/supplyV2/3-ChooseAmount.tsx rename to packages/huma-widget/src/components/Lend/supplyV2/4-ChooseAmount.tsx diff --git a/packages/huma-widget/src/components/Lend/supplyV2/4-ApproveAllowance.tsx b/packages/huma-widget/src/components/Lend/supplyV2/5-ApproveAllowance.tsx similarity index 100% rename from packages/huma-widget/src/components/Lend/supplyV2/4-ApproveAllowance.tsx rename to packages/huma-widget/src/components/Lend/supplyV2/5-ApproveAllowance.tsx diff --git a/packages/huma-widget/src/components/Lend/supplyV2/5-Transfer.tsx b/packages/huma-widget/src/components/Lend/supplyV2/6-Transfer.tsx similarity index 100% rename from packages/huma-widget/src/components/Lend/supplyV2/5-Transfer.tsx rename to packages/huma-widget/src/components/Lend/supplyV2/6-Transfer.tsx diff --git a/packages/huma-widget/src/components/Lend/supplyV2/6-Success.tsx b/packages/huma-widget/src/components/Lend/supplyV2/7-Success.tsx similarity index 100% rename from packages/huma-widget/src/components/Lend/supplyV2/6-Success.tsx rename to packages/huma-widget/src/components/Lend/supplyV2/7-Success.tsx diff --git a/packages/huma-widget/src/components/Lend/supplyV2/7-Notifications.tsx b/packages/huma-widget/src/components/Lend/supplyV2/8-Notifications.tsx similarity index 100% rename from packages/huma-widget/src/components/Lend/supplyV2/7-Notifications.tsx rename to packages/huma-widget/src/components/Lend/supplyV2/8-Notifications.tsx diff --git a/packages/huma-widget/src/components/Lend/supplyV2/index.tsx b/packages/huma-widget/src/components/Lend/supplyV2/index.tsx index 7cd7b50..05f49ac 100644 --- a/packages/huma-widget/src/components/Lend/supplyV2/index.tsx +++ b/packages/huma-widget/src/components/Lend/supplyV2/index.tsx @@ -23,11 +23,12 @@ import { PointsEarned } from '../../PointsEarned' import { WidgetWrapper } from '../../WidgetWrapper' import { ChooseTranche } from './1-ChooseTranche' import { Evaluation } from './2-Evaluation' -import { ChooseAmount } from './3-ChooseAmount' -import { ApproveAllowance } from './4-ApproveAllowance' -import { Transfer } from './5-Transfer' -import { Success } from './6-Success' -import { Notifications } from './7-Notifications' +import { ApproveLender } from './3-ApproveLender' +import { ChooseAmount } from './4-ChooseAmount' +import { ApproveAllowance } from './5-ApproveAllowance' +import { Transfer } from './6-Transfer' +import { Success } from './7-Success' +import { Notifications } from './8-Notifications' export interface Campaign { id: string @@ -87,11 +88,11 @@ export function LendSupplyV2({ useEffect(() => { if (!step && poolInfo && lenderApproveStatusFetched && lpConfig) { - if ( - campaign && - !isUniTranche && - (!lenderApprovedJunior || !lenderApprovedSenior) - ) { + const uniTrancheNotEvaluated = isUniTranche && !lenderApprovedJunior + const tranchesNotEvaluated = + !isUniTranche && !lenderApprovedJunior && !lenderApprovedSenior + + if (uniTrancheNotEvaluated || tranchesNotEvaluated) { if (poolInfo.KYC) { dispatch(setStep(WIDGET_STEP.Evaluation)) } else if (poolInfo.supplyLink) { @@ -101,15 +102,8 @@ export function LendSupplyV2({ return } - if (lenderApprovedJunior && !lenderApprovedSenior) { - setSelectedTranche('junior') - dispatch(setStep(WIDGET_STEP.ChooseAmount)) - return - } - - if (lenderApprovedSenior && !lenderApprovedJunior) { - setSelectedTranche('senior') - dispatch(setStep(WIDGET_STEP.ChooseAmount)) + if (!isUniTranche && (!lenderApprovedJunior || !lenderApprovedSenior)) { + dispatch(setStep(WIDGET_STEP.ApproveLender)) return } @@ -137,7 +131,6 @@ export function LendSupplyV2({ lenderApprovedSenior, lpConfig, poolInfo, - campaign, step, ]) @@ -178,12 +171,17 @@ export function LendSupplyV2({ )} + {step === WIDGET_STEP.ApproveLender && ( + + )} {step === WIDGET_STEP.ChooseAmount && (