-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Approve both lenders with retry (#356)
* approve both lenders with retry * update approve lenders --------- Co-authored-by: shan <[email protected]>
- Loading branch information
1 parent
baee362
commit 7dbd9d5
Showing
20 changed files
with
209 additions
and
116 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
packages/huma-widget/src/components/Lend/components/ApproveLenderBase.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...' | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/huma-widget/src/components/Lend/solanaSupply/2-ApproveLender.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
/> | ||
) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.