more ui refactoring after views removed

This commit is contained in:
jordansexton 2021-07-01 20:59:37 -05:00
parent 944758b7d7
commit dde74ae889
5 changed files with 32 additions and 54 deletions

View File

@ -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';

View File

@ -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),

View File

@ -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<string, TokenAccount[]>());
}, [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,
);
};

View File

@ -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),

View File

@ -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';