Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mliu committed Nov 4, 2024
1 parent 5f314e1 commit a52e292
Show file tree
Hide file tree
Showing 11 changed files with 1,162 additions and 42 deletions.
1 change: 1 addition & 0 deletions packages/huma-web-shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './hooks'
export * from './solana'
export * from './utils'
1 change: 1 addition & 0 deletions packages/huma-web-shared/src/solana/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './hooks'
export * from './types'
export * from './utils'
1 change: 1 addition & 0 deletions packages/huma-web-shared/src/solana/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './notEnabledAutoRedeem'
14 changes: 14 additions & 0 deletions packages/huma-web-shared/src/solana/utils/notEnabledAutoRedeem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Account } from '@solana/spl-token'
import { PublicKey } from '@solana/web3.js'

export const notEnabledAutoRedeem = (
tokenAccount: Account | undefined,
poolAuthorityPubkey: PublicKey,
amount: bigint | undefined,
): boolean =>
!!tokenAccount &&
!!amount &&
tokenAccount.amount > 0 &&
(tokenAccount.delegate == null ||
!poolAuthorityPubkey.equals(tokenAccount.delegate) ||
tokenAccount.delegatedAmount < amount)
21 changes: 21 additions & 0 deletions packages/huma-web-shared/src/utils/getLenderLockupDates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const getLenderLockupDates = (
withdrawalLockupPeriodDays: number,
): { lockupEndTimeUnix: number; withdrawTimeUnix: number } => {
const now = new Date()
const lockupEndTime = new Date(now.getFullYear(), now.getMonth(), 1)
lockupEndTime.setDate(
lockupEndTime.getDate() + (withdrawalLockupPeriodDays ?? 0),
)

const withdrawTime = new Date(
lockupEndTime.getFullYear(),
lockupEndTime.getMonth() + 1,
lockupEndTime.getDate(),
)

// Get Unix timestamps in seconds
const lockupEndTimeUnix = Math.floor(lockupEndTime.getTime() / 1000)
const withdrawTimeUnix = Math.floor(withdrawTime.getTime() / 1000)

return { lockupEndTimeUnix, withdrawTimeUnix }
}
1 change: 1 addition & 0 deletions packages/huma-web-shared/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './getLenderLockupDates'
1,071 changes: 1,071 additions & 0 deletions packages/huma-widget/API.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
convertToShares,
getSentinelAddress,
getTokenAccounts,
SolanaPoolInfo,
} from '@huma-finance/shared'
import React, { useCallback, useEffect, useState } from 'react'

import {
notEnabledAutoRedeem,
SolanaPoolState,
useHumaProgram,
useLenderAccounts,
Expand All @@ -18,7 +18,6 @@ import {
} from '@solana/spl-token'
import { useWallet } from '@solana/wallet-adapter-react'
import { PublicKey, Transaction } from '@solana/web3.js'
import { BN } from '@coral-xyz/anchor'
import { useAppDispatch } from '../../../hooks/useRedux'
import { setError, setStep } from '../../../store/widgets.reducers'
import { WIDGET_STEP } from '../../../store/widgets.store'
Expand Down Expand Up @@ -84,49 +83,39 @@ export function Transfer({
return
}
if (
seniorTokenAccount &&
seniorTokenAccount.amount > 0 &&
(seniorTokenAccount.delegate == null ||
!poolAuthorityPubkey.equals(seniorTokenAccount.delegate) ||
seniorTokenAccount.delegatedAmount < seniorTokenAccount.amount)
) {
const sharesAmount = convertToShares(
new BN(poolState.seniorTrancheAssets ?? 0),
seniorTrancheMintSupply ?? new BN(0),
new BN(seniorTokenAccount.amount.toString()),
notEnabledAutoRedeem(
seniorTokenAccount,
poolAuthorityPubkey,
seniorTokenAccount?.amount,
)
) {
tx.add(
createApproveCheckedInstruction(
seniorTrancheATA,
new PublicKey(poolInfo.seniorTrancheMint),
poolAuthorityPubkey, // delegate
publicKey, // owner of the wallet
BigInt(sharesAmount.muln(1.1).toString()), // amount
BigInt(seniorTokenAccount?.amount.toString() ?? 0), // amount
poolInfo.trancheDecimals,
undefined, // multiSigners
TOKEN_2022_PROGRAM_ID,
),
)
}
if (
juniorTokenAccount &&
juniorTokenAccount.amount > 0 &&
(juniorTokenAccount.delegate == null ||
!poolAuthorityPubkey.equals(juniorTokenAccount.delegate) ||
juniorTokenAccount.delegatedAmount < juniorTokenAccount.amount)
) {
const sharesAmount = convertToShares(
new BN(poolState.juniorTrancheAssets ?? 0),
juniorTrancheMintSupply ?? new BN(0),
new BN(juniorTokenAccount.amount.toString()),
notEnabledAutoRedeem(
juniorTokenAccount,
poolAuthorityPubkey,
juniorTokenAccount?.amount,
)
) {
tx.add(
createApproveCheckedInstruction(
juniorTrancheATA,
new PublicKey(poolInfo.juniorTrancheMint),
poolAuthorityPubkey, // delegate
publicKey, // owner of the wallet
BigInt(sharesAmount.muln(1.1).toString()), // amount
BigInt(juniorTokenAccount?.amount.toString() ?? 0), // amount
poolInfo.trancheDecimals,
undefined, // multiSigners
TOKEN_2022_PROGRAM_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {
SolanaPoolInfo,
timeUtil,
} from '@huma-finance/shared'
import { SolanaPoolState } from '@huma-finance/web-shared'
import dayjs from 'dayjs'
import { getLenderLockupDates, SolanaPoolState } from '@huma-finance/web-shared'
import React from 'react'
import { useAppSelector } from '../../../hooks/useRedux'
import { selectWidgetState } from '../../../store/widgets.selectors'
Expand All @@ -23,15 +22,14 @@ export function Success({
}: Props): React.ReactElement {
const { solanaSignature } = useAppSelector(selectWidgetState)

const lockupEndTime = dayjs()
.add(poolState.withdrawalLockupPeriodDays ?? 0, 'day')
.date(1)
const withdrawTime = lockupEndTime.add(1, 'month')
const { lockupEndTimeUnix, withdrawTimeUnix } = getLenderLockupDates(
poolState.withdrawalLockupPeriodDays ?? 0,
)
const content = [
`Redemption request will be automatically submitted on ${timeUtil.timestampToLL(
lockupEndTime.unix(),
lockupEndTimeUnix,
)}. Your deposit can be redeemed and yield rewards will stop on ${timeUtil.timestampToLL(
withdrawTime.unix(),
withdrawTimeUnix,
)}.`,
]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useCallback } from 'react'
import dayjs from 'dayjs'
import { Box, css, useTheme } from '@mui/material'
import { SolanaPoolState } from '@huma-finance/web-shared'
import { getLenderLockupDates, SolanaPoolState } from '@huma-finance/web-shared'
import { timeUtil } from '@huma-finance/shared'
import { useAppDispatch } from '../../../hooks/useRedux'
import { WrapperModal } from '../../WrapperModal'
Expand Down Expand Up @@ -38,10 +37,9 @@ export function ApproveAllowance({ poolState }: Props): React.ReactElement {
`,
}

const lockupEndTime = dayjs()
.add(poolState.withdrawalLockupPeriodDays ?? 0, 'day')
.date(1)

const { lockupEndTimeUnix } = getLenderLockupDates(
poolState.withdrawalLockupPeriodDays ?? 0,
)
return (
<WrapperModal title='Auto-Redemption'>
<Box css={styles.iconWrapper}>
Expand All @@ -51,7 +49,7 @@ export function ApproveAllowance({ poolState }: Props): React.ReactElement {
This transaction will also enable auto-redemption for your tranche
shares by approving our automation account as a delegate. Redemption
requests will be automatically submitted on{' '}
{timeUtil.timestampToLL(lockupEndTime.unix())}.
{timeUtil.timestampToLL(lockupEndTimeUnix)}.
</Box>
<BottomButton variant='contained' onClick={handleNext}>
SUPPLY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
SolanaPoolState,
useHumaProgram,
useLenderAccounts,
useTrancheTokenAccounts,
} from '@huma-finance/web-shared'
import {
createApproveCheckedInstruction,
Expand Down Expand Up @@ -62,6 +63,11 @@ export function Transfer({
juniorTrancheMintSupply,
loading: isLoadingLenderAccounts,
} = useLenderAccounts(poolInfo.chainId, poolInfo.poolName)
const {
seniorTokenAccount,
juniorTokenAccount,
loading: isLoadingTrancheTokenAccounts,
} = useTrancheTokenAccounts(poolInfo)
const program = useHumaProgram(poolInfo.chainId)

const handleSuccess = useCallback(
Expand Down Expand Up @@ -94,7 +100,12 @@ export function Transfer({

useEffect(() => {
async function getTx() {
if (!publicKey || transaction || isLoadingLenderAccounts) {
if (
!publicKey ||
transaction ||
isLoadingLenderAccounts ||
isLoadingTrancheTokenAccounts
) {
return
}

Expand Down Expand Up @@ -164,6 +175,17 @@ export function Transfer({
: juniorTrancheMintSupply ?? new BN(0),
supplyBigNumber,
)
const existingShares = convertToShares(
selectedTranche === 'senior'
? new BN(poolState.seniorTrancheAssets ?? 0)
: new BN(poolState.juniorTrancheAssets ?? 0),
selectedTranche === 'senior'
? seniorTrancheMintSupply ?? new BN(0)
: juniorTrancheMintSupply ?? new BN(0),
selectedTranche === 'senior'
? new BN(seniorTokenAccount?.amount.toString() ?? '0')
: new BN(juniorTokenAccount?.amount.toString() ?? '0'),
)
tx.add(
createApproveCheckedInstruction(
selectedTranche === 'senior' ? seniorTrancheATA : juniorTrancheATA,
Expand All @@ -174,7 +196,7 @@ export function Transfer({
),
new PublicKey(poolInfo.poolAuthority), // delegate
publicKey, // owner of the wallet
BigInt(sharesAmount.muln(1.1).toString()), // amount
BigInt(sharesAmount.muln(1.1).add(existingShares).toString()), // amount
poolInfo.trancheDecimals,
undefined, // multiSigners
TOKEN_2022_PROGRAM_ID,
Expand All @@ -186,8 +208,10 @@ export function Transfer({
getTx()
}, [
isLoadingLenderAccounts,
isLoadingTrancheTokenAccounts,
juniorLenderApprovedAccountPDA,
juniorLenderStateAccount,
juniorTokenAccount?.amount,
juniorTrancheMintSupply,
poolInfo,
poolState.juniorTrancheAssets,
Expand All @@ -197,12 +221,13 @@ export function Transfer({
selectedTranche,
seniorLenderApprovedAccountPDA,
seniorLenderStateAccount,
seniorTokenAccount?.amount,
seniorTrancheMintSupply,
supplyBigNumber,
transaction,
])

if (isLoadingLenderAccounts) {
if (isLoadingLenderAccounts || isLoadingTrancheTokenAccounts) {
return <LoadingModal title='Supply' />
}

Expand Down

0 comments on commit a52e292

Please sign in to comment.