Skip to content

Commit

Permalink
Approve both lenders with retry (#356)
Browse files Browse the repository at this point in the history
* approve both lenders with retry

* update approve lenders

---------

Co-authored-by: shan <[email protected]>
  • Loading branch information
shan-57blocks and shan57blocks authored Nov 15, 2024
1 parent baee362 commit 7dbd9d5
Show file tree
Hide file tree
Showing 20 changed files with 209 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -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<string, unknown>
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 (
<LoadingModal
title='Lender Approval'
description='Checking your verification status...'
/>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
IdentityVerificationStatusV2,
KYCCopy,
KYCType,
TrancheType,
VerificationStatusResultV2,
} from '@huma-finance/shared'
import { useAuthErrorHandling, useChainInfo } from '@huma-finance/web-shared'
Expand All @@ -34,22 +33,16 @@ type Props = {
seniorTrancheVault: string
}
chainType: CHAIN_TYPE
isUniTranche: boolean
pointsTestnetExperience: boolean
campaign?: Campaign
chainSpecificData?: Record<string, unknown>
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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -247,7 +191,7 @@ export function PersonaEvaluation({
}

case IdentityVerificationStatusV2.CONSENTED_TO_SUBSCRIPTION: {
await approveLender()
dispatch(setStep(WIDGET_STEP.ApproveLender))
break
}

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
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'
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 (
<PersonaEvaluation
poolInfo={{
Expand All @@ -37,15 +26,9 @@ export function Evaluation({
seniorTrancheVault: poolInfo.seniorTrancheMint,
}}
handleClose={handleClose}
isUniTranche={isUniTranche}
pointsTestnetExperience={pointsTestnetExperience}
campaign={campaign}
chainSpecificData={{
huma_program_id: solanChainInfo.poolProgram,
pool_id: poolInfo.poolId,
}}
chainType={CHAIN_TYPE.SOLANA}
changeTranche={changeTranche}
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
<ApproveLenderBase
juniorTrancheVault={poolInfo.juniorTrancheMint}
seniorTrancheVault={poolInfo.seniorTrancheMint}
isUniTranche={isUniTranche}
chainType={CHAIN_TYPE.SOLANA}
chainSpecificData={{
huma_program_id: solanaChainInfo.poolProgram,
pool_id: poolInfo.poolId,
}}
changeTranche={changeTranche}
/>
)
}
24 changes: 18 additions & 6 deletions packages/huma-widget/src/components/Lend/solanaSupply/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -97,6 +103,7 @@ export function SolanaLendSupply({
juniorLenderApproved,
seniorLenderApproved,
isLoadingTokenAccount,
isUniTranche,
])

if (isLoadingLenderAccounts || isLoadingTokenAccount) {
Expand All @@ -121,10 +128,15 @@ export function SolanaLendSupply({
{step === WIDGET_STEP.Evaluation && (
<Evaluation
poolInfo={poolInfo}
isUniTranche={!!isUniTranche}
pointsTestnetExperience={pointsTestnetExperience}
campaign={poolState.campaign}
handleClose={handleClose}
/>
)}
{step === WIDGET_STEP.ApproveLender && (
<ApproveLender
poolInfo={poolInfo}
isUniTranche={!!isUniTranche}
changeTranche={setSelectedTranche}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 '.'
Expand All @@ -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) {
Expand All @@ -37,11 +28,9 @@ export function Evaluation({
<PersonaEvaluation
poolInfo={poolInfo}
handleClose={handleClose}
isUniTranche={isUniTranche}
pointsTestnetExperience={pointsTestnetExperience}
campaign={campaign}
chainType={CHAIN_TYPE.EVM}
changeTranche={changeTranche}
/>
)
}
Expand Down
Loading

0 comments on commit 7dbd9d5

Please sign in to comment.