Skip to content

Commit

Permalink
hide ClearinghouseV1 for users without positions (#3001)
Browse files Browse the repository at this point in the history
  • Loading branch information
brightiron authored Nov 1, 2023
1 parent 9cf33e8 commit ae230e6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 57 deletions.
38 changes: 21 additions & 17 deletions src/views/Lending/Cooler/hooks/useGetCoolerLoans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,32 @@ export const useGetCoolerLoans = ({
const { data: signer } = useSigner();

const { data, isFetched, isLoading } = useQuery(
["getCoolerLoans", networks.MAINNET, factoryAddress, collateralAddress, debtAddress],
["getCoolerLoans", networks.MAINNET, factoryAddress, collateralAddress, debtAddress, walletAddress],
async () => {
if (!walletAddress || !factoryAddress || !collateralAddress || !debtAddress || !signer) return [];
const contract = CoolerFactory__factory.connect(factoryAddress, signer);
try {
if (!walletAddress || !factoryAddress || !collateralAddress || !debtAddress || !signer) return [];
const contract = CoolerFactory__factory.connect(factoryAddress, signer);

const coolerAddress = await contract.callStatic.generateCooler(collateralAddress, debtAddress);
const coolerContract = Cooler__factory.connect(coolerAddress, Providers.getStaticProvider(networks.MAINNET));
const coolerAddress = await contract.callStatic.generateCooler(collateralAddress, debtAddress);
const coolerContract = Cooler__factory.connect(coolerAddress, Providers.getStaticProvider(networks.MAINNET));

const loans = [];
let loanId = 0;
while (true) {
try {
const loanData = await coolerContract.loans(loanId);
// const newCollateralAmount = await coolerContract.newCollateralFor(loanId);
loans.push({ ...loanData, loanId });
loanId++;
} catch (e) {
break;
const loans = [];
let loanId = 0;
while (true) {
try {
const loanData = await coolerContract.loans(loanId);
// const newCollateralAmount = await coolerContract.newCollateralFor(loanId);
loans.push({ ...loanData, loanId });
loanId++;
} catch (e) {
break;
}
}
}

return loans.filter(loan => !loan.collateral.isZero() && !loan.principal.isZero());
return loans.filter(loan => !loan.collateral.isZero() && !loan.principal.isZero());
} catch (e) {
return [];
}
},
{ enabled: !!walletAddress && !!factoryAddress && !!collateralAddress && !!debtAddress && !!signer },
);
Expand Down
105 changes: 65 additions & 40 deletions src/views/Lending/Cooler/positions/Positions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,45 @@ export const CoolerPositions = () => {
const [currentClearingHouse, setCurrentClearingHouse] = useState<"clearingHouseV1" | "clearingHouseV2">(
"clearingHouseV2",
);
const { data: clearingHouse } = useGetClearingHouse({ clearingHouse: currentClearingHouse });
const { data: clearingHouseV1 } = useGetClearingHouse({ clearingHouse: "clearingHouseV1" });
const { data: clearingHouseV2 } = useGetClearingHouse({ clearingHouse: "clearingHouseV2" });

const [createLoanModalOpen, setCreateLoanModalOpen] = useState(false);
const { data: loans, isFetched: isFetchedLoans } = useGetCoolerLoans({
const { data: loansV1, isFetched: isFetchedLoansV1 } = useGetCoolerLoans({
walletAddress: address,
factoryAddress: clearingHouseV1?.factory,
collateralAddress: clearingHouseV1?.collateralAddress,
debtAddress: clearingHouseV1?.debtAddress,
});

const { data: coolerAddressV1 } = useGetCoolerForWallet({
walletAddress: address,
factoryAddress: clearingHouseV1?.factory,
collateralAddress: clearingHouseV1?.collateralAddress,
debtAddress: clearingHouseV1?.debtAddress,
clearingHouseVersion: "clearingHouseV1",
});

const { data: loansV2, isFetched: isFetchedLoansV2 } = useGetCoolerLoans({
walletAddress: address,
factoryAddress: clearingHouse?.factory,
collateralAddress: clearingHouse?.collateralAddress,
debtAddress: clearingHouse?.debtAddress,
factoryAddress: clearingHouseV2?.factory,
collateralAddress: clearingHouseV2?.collateralAddress,
debtAddress: clearingHouseV2?.debtAddress,
});

const { data: coolerAddress } = useGetCoolerForWallet({
const { data: coolerAddressV2 } = useGetCoolerForWallet({
walletAddress: address,
factoryAddress: clearingHouse?.factory,
collateralAddress: clearingHouse?.collateralAddress,
debtAddress: clearingHouse?.debtAddress,
clearingHouseVersion: currentClearingHouse,
factoryAddress: clearingHouseV2?.factory,
collateralAddress: clearingHouseV2?.collateralAddress,
debtAddress: clearingHouseV2?.debtAddress,
clearingHouseVersion: "clearingHouseV2",
});

const coolerAddress = currentClearingHouse === "clearingHouseV1" ? coolerAddressV1 : coolerAddressV2;
const clearingHouse = currentClearingHouse === "clearingHouseV1" ? clearingHouseV1 : clearingHouseV2;
const loans = currentClearingHouse === "clearingHouseV1" ? loansV1 : loansV2;
const isFetchedLoans = currentClearingHouse === "clearingHouseV1" ? isFetchedLoansV1 : isFetchedLoansV2;

const { data: delegationAddress } = useCheckDelegation({ coolerAddress });

const [extendLoan, setExtendLoan] = useState<any>(null);
Expand All @@ -69,37 +92,39 @@ export const CoolerPositions = () => {
<OutstandingPrincipal />
</Grid>
</Grid>
<Box display="flex" mt="16px" justifyContent="right">
<Select
value={currentClearingHouse}
label="ClearingHouse"
onChange={e => {
setCurrentClearingHouse(e.target.value as "clearingHouseV1" | "clearingHouseV2");
}}
sx={{
width: "200px",
height: "44px",
backgroundColor: theme.colors.gray[700],
border: "none",
".MuiOutlinedInput-notchedOutline": {
border: "none",
},
"&.Mui-focused .MuiOutlinedInput-notchedOutline": {
border: "none",
},
"&:hover .MuiOutlinedInput-notchedOutline": {
{clearingHouseV1 && loansV1 && loansV1.length > 0 && (
<Box display="flex" mt="16px" justifyContent="right">
<Select
value={currentClearingHouse}
label="ClearingHouse"
onChange={e => {
setCurrentClearingHouse(e.target.value as "clearingHouseV1" | "clearingHouseV2");
}}
sx={{
width: "200px",
height: "44px",
backgroundColor: theme.colors.gray[700],
border: "none",
},
"& .MuiSelect-select": {
display: "flex",
alignItems: "center",
},
}}
>
<MenuItem value="clearingHouseV1">ClearingHouse V1</MenuItem>
<MenuItem value="clearingHouseV2">ClearingHouse V2</MenuItem>
</Select>
</Box>
".MuiOutlinedInput-notchedOutline": {
border: "none",
},
"&.Mui-focused .MuiOutlinedInput-notchedOutline": {
border: "none",
},
"&:hover .MuiOutlinedInput-notchedOutline": {
border: "none",
},
"& .MuiSelect-select": {
display: "flex",
alignItems: "center",
},
}}
>
<MenuItem value="clearingHouseV1">ClearingHouse V1</MenuItem>
<MenuItem value="clearingHouseV2">ClearingHouse V2</MenuItem>
</Select>
</Box>
)}

<Box mb="21px" mt="66px">
<Typography variant="h1">Your Positions</Typography>
Expand Down

0 comments on commit ae230e6

Please sign in to comment.