Skip to content

Commit

Permalink
hide button if consolidator inactive. show button for users to migrat…
Browse files Browse the repository at this point in the history
…e v1/v2 positions)
  • Loading branch information
brightiron committed Dec 21, 2024
1 parent 18a7af3 commit dbea4e9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
19 changes: 19 additions & 0 deletions src/views/Lending/Cooler/hooks/useCheckConsolidatorActive.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useQuery } from "@tanstack/react-query";
import { COOLER_CONSOLIDATION_ADDRESSES } from "src/constants/addresses";
import { useTestableNetworks } from "src/hooks/useTestableNetworks";
import { CoolerConsolidation__factory } from "src/typechain";
import { useProvider } from "wagmi";

export const useCheckConsolidatorActive = () => {
const networks = useTestableNetworks();
const provider = useProvider();

return useQuery({
queryKey: ["consolidatorActive"],
queryFn: async () => {
const contract = CoolerConsolidation__factory.connect(COOLER_CONSOLIDATION_ADDRESSES[networks.MAINNET], provider);
const isActive = await contract.isActive();
return isActive;
},
});
};
12 changes: 8 additions & 4 deletions src/views/Lending/Cooler/positions/ConsolidateLoan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { formatNumber } from "src/helpers";
import { useBalance } from "src/hooks/useBalance";
import { useTestableNetworks } from "src/hooks/useTestableNetworks";
import { NetworkId } from "src/networkDetails";
import { useCheckConsolidatorActive } from "src/views/Lending/Cooler/hooks/useCheckConsolidatorActive";
import { useConsolidateCooler } from "src/views/Lending/Cooler/hooks/useConsolidateCooler";
import { useCreateCooler } from "src/views/Lending/Cooler/hooks/useCreateCooler";
import { useGetClearingHouse } from "src/views/Lending/Cooler/hooks/useGetClearingHouse";
Expand Down Expand Up @@ -41,6 +42,7 @@ export const ConsolidateLoans = ({
factoryAddress: string;
}) => {
const coolerMutation = useConsolidateCooler();
const { data: consolidatorActive } = useCheckConsolidatorActive();
const createCooler = useCreateCooler();
const networks = useTestableNetworks();
const [open, setOpen] = useState(false);
Expand Down Expand Up @@ -84,7 +86,7 @@ export const ConsolidateLoans = ({
: selectedVersion === "v2"
? clearingHouseAddresses.v2
: clearingHouseAddresses.v3;
const duration = 121; // Standard duration for consolidated loans
const duration = clearingHouseAddresses.v3.duration; // Standard duration for consolidated loans
const debtAddress = clearingHouseAddresses.v3.debtAddress;

// Show button only if there are loans that can be consolidated
Expand Down Expand Up @@ -149,11 +151,13 @@ export const ConsolidateLoans = ({

if (!showConsolidateButton) return null;

console.log(allowances, "allowances");

return (
<>
<PrimaryButton onClick={() => setOpen(!open)}>Consolidate Loans</PrimaryButton>
{consolidatorActive && (
<PrimaryButton onClick={() => setOpen(!open)}>
Consolidate Loans to {clearingHouseAddresses.v3.debtAssetName}
</PrimaryButton>
)}
<Modal
maxWidth="476px"
minHeight="200px"
Expand Down
36 changes: 20 additions & 16 deletions src/views/Lending/Cooler/positions/Positions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,22 +274,26 @@ export const CoolerPositions = () => {
>
Borrow {activeClearingHouse.debtAssetName} & Open Position
</PrimaryButton>
{allLoans.length > 1 && clearingHouses.v1 && clearingHouses.v2 && clearingHouses.v3 && (
<ConsolidateLoans
v3CoolerAddress={coolerAddressV3 || ""}
v2CoolerAddress={coolerAddressV2 || ""}
v1CoolerAddress={coolerAddressV1 || ""}
clearingHouseAddresses={{
v1: clearingHouses.v1,
v2: clearingHouses.v2,
v3: clearingHouses.v3,
}}
v1Loans={loansV1}
v2Loans={loansV2}
v3Loans={loansV3}
factoryAddress={clearingHouses.v3.factory}
/>
)}
{allLoans.length > 1 ||
(((loansV1 && loansV1.length > 0) || (loansV2 && loansV2.length > 0)) &&
clearingHouses.v1 &&
clearingHouses.v2 &&
clearingHouses.v3 && (
<ConsolidateLoans
v3CoolerAddress={coolerAddressV3 || ""}
v2CoolerAddress={coolerAddressV2 || ""}
v1CoolerAddress={coolerAddressV1 || ""}
clearingHouseAddresses={{
v1: clearingHouses.v1,
v2: clearingHouses.v2,
v3: clearingHouses.v3,
}}
v1Loans={loansV1}
v2Loans={loansV2}
v3Loans={loansV3}
factoryAddress={clearingHouses.v3.factory}
/>
))}
</Box>
)}
</>
Expand Down

0 comments on commit dbea4e9

Please sign in to comment.