diff --git a/src/views/Lending/Cooler/positions/Positions.tsx b/src/views/Lending/Cooler/positions/Positions.tsx index c944ee3cf..f69e0d1b8 100644 --- a/src/views/Lending/Cooler/positions/Positions.tsx +++ b/src/views/Lending/Cooler/positions/Positions.tsx @@ -1,10 +1,8 @@ import { Box, Grid, - MenuItem, Paper, - Select, - Skeleton, + SvgIcon, Table, TableBody, TableCell, @@ -12,11 +10,11 @@ import { TableHead, TableRow, Typography, - useTheme, } from "@mui/material"; import { OHMTokenProps, PrimaryButton, SecondaryButton, Token } from "@olympusdao/component-library"; import { ethers } from "ethers"; -import { useEffect, useState } from "react"; +import { useState } from "react"; +import usdsIcon from "src/assets/tokens/usds.svg?react"; import { BorrowRate, OutstandingPrincipal, WeeklyCapacityRemaining } from "src/views/Lending/Cooler/dashboard/Metrics"; import { useGetClearingHouse } from "src/views/Lending/Cooler/hooks/useGetClearingHouse"; import { useGetCoolerForWallet } from "src/views/Lending/Cooler/hooks/useGetCoolerForWallet"; @@ -27,8 +25,6 @@ import { useAccount } from "wagmi"; export const CoolerPositions = () => { const { address } = useAccount(); - const [currentClearingHouse, setCurrentClearingHouse] = useState<"v1" | "v2" | "v3">("v3"); - // Get clearing house data for all versions const clearingHouses = { v1: useGetClearingHouse({ clearingHouse: "clearingHouseV1" }).data, v2: useGetClearingHouse({ clearingHouse: "clearingHouseV2" }).data, @@ -81,46 +77,60 @@ export const CoolerPositions = () => { clearingHouseVersion: "clearingHouseV3", }); - // Organize version data - const versionData = { - v1: { - loans: { data: loansV1, isFetched: isFetchedLoansV1 }, - coolerAddress: { data: coolerAddressV1 }, - }, - v2: { - loans: { data: loansV2, isFetched: isFetchedLoansV2 }, - coolerAddress: { data: coolerAddressV2 }, - }, - v3: { - loans: { data: loansV3, isFetched: isFetchedLoansV3 }, - coolerAddress: { data: coolerAddressV3 }, - }, + const [extendLoan, setExtendLoan] = useState[number] | undefined>(undefined); + const [repayLoan, setRepayLoan] = useState[number] | undefined>(undefined); + + const getAllLoans = () => { + const allLoans = [ + ...(loansV1 || []).map(loan => ({ ...loan, version: "v1" })), + ...(loansV2 || []).map(loan => ({ ...loan, version: "v2" })), + ...(loansV3 || []).map(loan => ({ ...loan, version: "v3" })), + ]; + return allLoans; }; - const currentData = versionData[currentClearingHouse]; - const coolerAddress = currentData.coolerAddress.data; - const clearingHouse = clearingHouses[currentClearingHouse]; - const loans = currentData.loans.data; - const isFetchedLoans = currentData.loans.isFetched; + const getActiveClearingHouse = () => { + if (clearingHouses.v3?.isActive && clearingHouses.v3?.capacity.gt(0)) { + return { version: "v3", ...clearingHouses.v3 }; + } + if (clearingHouses.v2?.isActive && clearingHouses.v2?.capacity.gt(0)) { + return { version: "v2", ...clearingHouses.v2 }; + } + return null; + }; - const [extendLoan, setExtendLoan] = useState(null); - const [repayLoan, setRepayLoan] = useState(null); - const theme = useTheme(); + const activeClearingHouse = getActiveClearingHouse(); + const allLoans = getAllLoans(); - // Update the clearing house version when the data is available - useEffect(() => { - if (clearingHouses.v3?.isActive && clearingHouses.v3?.capacity.gt(0)) { - setCurrentClearingHouse("v3"); - } else if (clearingHouses.v2?.isActive && clearingHouses.v2?.capacity.gt(0)) { - setCurrentClearingHouse("v2"); + const getClearingHouseForLoan = (version: string) => { + const clearingHouse = clearingHouses[version as keyof typeof clearingHouses]; + if (!clearingHouse) throw new Error(`No clearing house found for version ${version}`); + return clearingHouse; + }; + + const getCoolerAddressForLoan = (version: string) => { + switch (version) { + case "v1": + return coolerAddressV1; + case "v2": + return coolerAddressV2; + case "v3": + return coolerAddressV3; + default: + return undefined; } - }, [clearingHouses.v2, clearingHouses.v3]); + }; + + console.log(allLoans, isFetchedLoansV1, isFetchedLoansV2, isFetchedLoansV3, address); return (
- + @@ -129,46 +139,6 @@ export const CoolerPositions = () => { - {([(loansV1 || []).length > 0, (loansV2 || []).length > 0].filter(Boolean).length > 0 || - (clearingHouses.v3?.isActive && - clearingHouses.v3?.capacity.gt(0) && - ((loansV1 && loansV1.length > 0) || (loansV2 && loansV2.length > 0)))) && ( - - - - )} Your Positions @@ -181,17 +151,11 @@ export const CoolerPositions = () => { )} - {address && !isFetchedLoans && ( - - - - )} - - {loans && loans.length == 0 && isFetchedLoans && ( + {allLoans.length === 0 && isFetchedLoansV1 && isFetchedLoansV2 && isFetchedLoansV3 && address && ( You currently have no Cooler loans - {clearingHouse?.isActive && clearingHouse.capacity.gt(0) && ( + {activeClearingHouse && ( <> Borrow against gOHM at a fixed rate and maturity @@ -201,7 +165,7 @@ export const CoolerPositions = () => { setCreateLoanModalOpen(true); }} > - Borrow {clearingHouse?.debtAssetName} & Open Position + Borrow {activeClearingHouse.debtAssetName} & Open Position @@ -210,132 +174,175 @@ export const CoolerPositions = () => { )} - {coolerAddress && ( + {address && (!isFetchedLoansV1 || !isFetchedLoansV2 || !isFetchedLoansV3) && ( + + Loading your positions... + + )} + + {allLoans.length > 0 && ( <> - {loans && loans.length > 0 && ( - <> - - - - - Collateral - - Interest Rate + +
+ + + Collateral + + Interest Rate + + + Repayment + + + Maturity Date + + + + + + {allLoans.map((loan, index) => { + const principalAndInterest = loan.principal.add(loan.interestDue || 0) || 0; + return ( + + + + {loan.collateral && Number(ethers.utils.formatUnits(loan.collateral.toString())).toFixed(4)}{" "} + gOHM + - - Repayment + + {loan.request?.interest && ( + {Number(ethers.utils.formatUnits(loan.request.interest.toString())) * 100}% + )} - - Maturity Date + + {principalAndInterest && ( + + {Number(ethers.utils.formatUnits(principalAndInterest.toString())).toFixed(2)}{" "} + {loan.debtAssetName}{" "} + {loan.debtAssetName === "USDS" ? ( + + ) : ( + + )} + + )} + + + {loan.expiry && ( + + {new Date(Number(loan.expiry.toString()) * 1000).toLocaleString([], { + month: "long", + day: "numeric", + year: "numeric", + }) || ""} + + )} + + + + { + setRepayLoan(loan); + setCreateLoanModalOpen(true); + }} + > + Repay + + setExtendLoan(loan)}>Extend + - - - - {loans?.map((loan, index) => { - const principalAndInterest = loan.principal.add(loan.interestDue || 0) || 0; - return ( - - - - {loan.collateral && - Number(ethers.utils.formatUnits(loan.collateral.toString())).toFixed(4)}{" "} - gOHM - - - - {loan.request?.interest && ( - {Number(ethers.utils.formatUnits(loan.request.interest.toString())) * 100}% - )} - - - {principalAndInterest && ( - - {Number(ethers.utils.formatUnits(principalAndInterest.toString())).toFixed(2)}{" "} - {loan.debtAssetName}{" "} - - - )} - - - {loan.expiry && ( - - {new Date(Number(loan.expiry.toString()) * 1000).toLocaleString([], { - month: "long", - day: "numeric", - year: "numeric", - }) || ""} - - )} - - - - { - setRepayLoan(loan); - setCreateLoanModalOpen(true); - }} - > - Repay - - setExtendLoan(loan)}>Extend - - - - ); - })} - -
-
- {clearingHouse?.isActive && clearingHouse.capacity.gt(0) && ( - - { - setRepayLoan(undefined); - setCreateLoanModalOpen(true); - }} - > - Borrow {clearingHouse.debtAssetName} & Open Position - - - )} - + ); + })} + + + + {activeClearingHouse && ( + + { + setRepayLoan(undefined); + setCreateLoanModalOpen(true); + }} + > + Borrow {activeClearingHouse.debtAssetName} & Open Position + + )} )} - {clearingHouse && ( + {activeClearingHouse && ( <> - {extendLoan && ( + {extendLoan && getClearingHouseForLoan(extendLoan.version) && getCoolerAddressForLoan(extendLoan.version) && ( )} )}