diff --git a/packages/lending/src/hooks/index.ts b/packages/lending/src/hooks/index.ts index 8f6439f..bfd998e 100644 --- a/packages/lending/src/hooks/index.ts +++ b/packages/lending/src/hooks/index.ts @@ -1,7 +1,6 @@ export * from './useBorrowedAmount'; export * from './useBorrowingPower'; export * from './useCollateralBalance'; -export * from './useEnrichedLendingObligations'; export * from './useLendingMarket'; export * from './useLendingObligations'; export * from './useLendingReserves'; diff --git a/packages/lending/src/hooks/useBorrowedAmount.ts b/packages/lending/src/hooks/useBorrowedAmount.ts index 992780e..afd9b76 100644 --- a/packages/lending/src/hooks/useBorrowedAmount.ts +++ b/packages/lending/src/hooks/useBorrowedAmount.ts @@ -13,7 +13,7 @@ export function useBorrowedAmount(address?: string | PublicKey) { const [borrowedInfo, setBorrowedInfo] = useState({ borrowedLamports: 0, borrowedInUSD: 0, - colateralInUSD: 0, + collateralInUSD: 0, ltv: 0, health: 0, }); @@ -24,43 +24,35 @@ export function useBorrowedAmount(address?: string | PublicKey) { setBorrowedInfo({ borrowedLamports: 0, borrowedInUSD: 0, - colateralInUSD: 0, + collateralInUSD: 0, ltv: 0, health: 0, }); (async () => { const result = { - borrowedLamports: 0, + borrowedLamports: wadToLamports(reserve.info.liquidity.borrowedAmountWads).toNumber(), borrowedInUSD: 0, - colateralInUSD: 0, + collateralInUSD: 0, ltv: 0, health: 0, }; - let liquidationThreshold = 0; + let liquidationThreshold = reserve.info.config.liquidationThreshold; + // @FIXME: see if this requires obligations userObligationsByReserve.forEach(item => { - // @FIXME: support multiple borrows, and decimals may be different than lamports - const borrowedLamports = wadToLamports( - item.obligation.info.borrows[0].borrowedAmountWads, - ).toNumber(); - - // @FIXME: obligation tokens - result.borrowedLamports += borrowedLamports; - result.borrowedInUSD += item.obligation.info.borrowedInQuote; - result.colateralInUSD += item.obligation.info.collateralInQuote; - // @FIXME: BigNumber - liquidationThreshold = item.obligation.info.liquidationThreshold; + result.borrowedInUSD += item.obligation.info.borrowedValue; + result.collateralInUSD += item.obligation.info.depositedValue; }, 0); if (userObligationsByReserve.length === 1) { result.ltv = userObligationsByReserve[0].obligation.info.ltv; result.health = userObligationsByReserve[0].obligation.info.health; } else { - result.ltv = (100 * result.borrowedInUSD) / result.colateralInUSD; + result.ltv = (100 * result.borrowedInUSD) / result.collateralInUSD; result.health = - (result.colateralInUSD * liquidationThreshold) / + (result.collateralInUSD * liquidationThreshold) / 100 / result.borrowedInUSD; result.health = Number.isFinite(result.health) ? result.health : 0; @@ -68,7 +60,7 @@ export function useBorrowedAmount(address?: string | PublicKey) { setBorrowedInfo(result); })(); - }, [connection, userObligationsByReserve, setBorrowedInfo]); + }, [connection, reserve, userObligationsByReserve, setBorrowedInfo]); return { borrowed: fromLamports(borrowedInfo.borrowedLamports, liquidityMint), diff --git a/packages/lending/src/hooks/useUserObligations.ts b/packages/lending/src/hooks/useUserObligations.ts index dbcfd76..3e24ed7 100644 --- a/packages/lending/src/hooks/useUserObligations.ts +++ b/packages/lending/src/hooks/useUserObligations.ts @@ -1,46 +1,37 @@ -import { hooks, TokenAccount } from '@oyster/common'; +import { useWallet } from '@oyster/common'; import { useMemo } from 'react'; -import { useEnrichedLendingObligations } from './useEnrichedLendingObligations'; - -const { useUserAccounts } = hooks; +import { useLendingObligations } from './useLendingObligations'; export function useUserObligations() { - const { userAccounts } = useUserAccounts(); - const { obligations } = useEnrichedLendingObligations(); - - // @FIXME: obligation tokens were removed, simplify this - const accountsByMint = useMemo(() => { - return userAccounts.reduce((res, acc) => { - const id = acc.info.mint.toBase58(); - res.set(id, [...(res.get(id) || []), acc]); - return res; - }, new Map()); - }, [userAccounts]); + const { wallet } = useWallet(); + const { obligations } = useLendingObligations(); const userObligations = useMemo(() => { - if (accountsByMint.size === 0) { - return []; - } - - // @FIXME: obligation tokens were removed, simplify this return obligations - .map(ob => { - return { - obligation: ob, - userAccounts: [], - }; - }) + .filter( + obligation => + obligation.info.owner.toBase58() === wallet?.publicKey?.toBase58(), + ) + .map(obligation => ({ obligation })) .sort( (a, b) => - b.obligation.info.borrowedInQuote - a.obligation.info.borrowedInQuote, + b.obligation.info.borrowedValue.toNumber() - + a.obligation.info.borrowedValue.toNumber(), ); - }, [accountsByMint, obligations]); + }, [obligations]); return { userObligations, totalInQuote: userObligations.reduce( - (result, item) => result + item.obligation.info.borrowedInQuote, + (result, item) => result + item.obligation.info.borrowedValue.toNumber(), 0, ), }; } + +export const useUserObligation = (address: string) => { + const userObligations = useUserObligations(); + return userObligations.userObligations.find( + obligation => obligation.obligation.pubkey.toBase58() === address, + ); +}; diff --git a/packages/lending/src/views/home/index.tsx b/packages/lending/src/views/home/index.tsx index a7c7efd..3ba0907 100644 --- a/packages/lending/src/views/home/index.tsx +++ b/packages/lending/src/views/home/index.tsx @@ -57,7 +57,7 @@ export const HomeView = () => { marketSize: fromLamports(marketCapLamports, liquidityMint.info) * price, borrowed: fromLamports( - wadToLamports(item.info.liquidity.borrowedAmountWads).toNumber(), + wadToLamports(item.info.liquidity.borrowedAmountWads), liquidityMint.info, ) * price, name: getTokenName(tokenMap, mint), diff --git a/packages/lending/src/views/index.tsx b/packages/lending/src/views/index.tsx index f082c79..f7a0a73 100644 --- a/packages/lending/src/views/index.tsx +++ b/packages/lending/src/views/index.tsx @@ -1,14 +1,10 @@ export { HomeView } from './home'; export { BorrowView } from './borrow'; -export { BorrowReserveView } from './borrowReserve'; export { DashboardView } from './dashboard'; export { DepositView } from './deposit'; export { DepositReserveView } from './depositReserve'; export { ReserveView } from './reserve'; export { WithdrawView } from './withdraw'; export { FaucetView } from './faucet'; -export { RepayReserveView } from './repayReserve'; -export { LiquidateView } from './liquidate'; -export { LiquidateReserveView } from './liquidateReserve'; export { MarginTrading } from './margin'; export { ObligationsView } from './obligations';