Skip to content

Commit

Permalink
Merge pull request #3224 from OlympusDAO/finiteCoolerApprovals
Browse files Browse the repository at this point in the history
add finite consolidation approvals, support cross clearing house consolidation
  • Loading branch information
brightiron authored Dec 23, 2024
2 parents 9b7d68d + dbea4e9 commit 7a4df8d
Show file tree
Hide file tree
Showing 10 changed files with 786 additions and 273 deletions.
631 changes: 388 additions & 243 deletions src/abi/CoolerConsolidation.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/components/TokenAllowanceGuard/TokenAllowanceGuard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const TokenAllowanceGuard: React.FC<{
loading={approveMutation.isLoading}
fullWidth
className=""
onClick={() => approveMutation.mutate({ spenderAddressMap })}
onClick={() => approveMutation.mutate({ spenderAddressMap, spendAmount })}
disabled={approveMutation.isLoading}
>
{approveMutation.isLoading ? `${approvalPendingText}` : `${approvalText}`}
Expand Down
7 changes: 4 additions & 3 deletions src/components/TokenAllowanceGuard/hooks/useApproveToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ContractReceipt } from "@ethersproject/contracts";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import toast from "react-hot-toast";
import { AddressMap } from "src/constants/addresses";
import { DecimalBigNumber } from "src/helpers/DecimalBigNumber/DecimalBigNumber";
import { useDynamicTokenContract } from "src/hooks/useContract";
import { contractAllowanceQueryKey } from "src/hooks/useContractAllowance";
import { EthersError } from "src/lib/EthersTypes";
Expand All @@ -15,14 +16,14 @@ export const useApproveToken = (tokenAddressMap: AddressMap) => {
const { chain = { id: 1 } } = useNetwork();
const token = useDynamicTokenContract(tokenAddressMap, true);

return useMutation<ContractReceipt, EthersError, { spenderAddressMap: AddressMap }>(
async ({ spenderAddressMap }) => {
return useMutation<ContractReceipt, EthersError, { spenderAddressMap: AddressMap; spendAmount?: DecimalBigNumber }>(
async ({ spenderAddressMap, spendAmount }) => {
const contractAddress = spenderAddressMap[chain.id as keyof typeof spenderAddressMap];

if (!token) throw new Error("Token doesn't exist on current network. Please switch networks.");
if (!contractAddress) throw new Error("Contract doesn't exist on current network. Please switch networks.");

const transaction = await token.approve(contractAddress, MaxUint256);
const transaction = await token.approve(contractAddress, spendAmount?.toBigNumber() || MaxUint256);

return transaction.wait();
},
Expand Down
2 changes: 1 addition & 1 deletion src/constants/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export const OLYMPUS_GOVERNANCE_ADDRESSES = {
};

export const COOLER_CONSOLIDATION_ADDRESSES = {
[NetworkId.MAINNET]: "0xB15bcb1b6593d85890f5287Baa2245B8A29F464a",
[NetworkId.MAINNET]: "0x784cA0C006b8651BAB183829A99fA46BeCe50dBc",
[NetworkId.TESTNET_GOERLI]: "",
};

Expand Down
5 changes: 4 additions & 1 deletion src/hooks/wagmi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export const { chains, provider, webSocketProvider } = configureChains(
[
{
...mainnet,
rpcUrls: { default: { http: ["https://rpc.ankr.com/eth"] }, public: { http: ["https://rpc.ankr.com/eth"] } },
rpcUrls: {
default: { http: ["https://rpc.ankr.com/eth"] },
public: { http: ["https://rpc.ankr.com/eth"] },
},
},
{
...polygon,
Expand Down
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;
},
});
};
25 changes: 18 additions & 7 deletions src/views/Lending/Cooler/hooks/useConsolidateCooler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,31 @@ export const useConsolidateCooler = () => {

return useMutation(
async ({
coolerAddress,
clearingHouseAddress,
fromCoolerAddress,
toCoolerAddress,
fromClearingHouseAddress,
toClearingHouseAddress,
loanIds,
}: {
coolerAddress: string;
clearingHouseAddress: string;
fromCoolerAddress: string;
toCoolerAddress: string;
fromClearingHouseAddress: string;
toClearingHouseAddress: string;
loanIds: number[];
}) => {
if (!signer) throw new Error(`Please connect a wallet`);
const contractAddress = COOLER_CONSOLIDATION_CONTRACT.addresses[networks.MAINNET];
const contract = CoolerConsolidation__factory.connect(contractAddress, signer);
const cooler = await contract.consolidateWithFlashLoan(clearingHouseAddress, coolerAddress, loanIds, 0, false, {
gasLimit: loanIds.length <= 30 ? loanIds.length * 1000000 : 30000000,
});
const cooler = await contract.consolidate(
fromClearingHouseAddress,
toClearingHouseAddress,
fromCoolerAddress,
toCoolerAddress,
loanIds,
{
gasLimit: loanIds.length <= 15 ? loanIds.length * 2000000 : 30000000,
},
);
const receipt = await cooler.wait();
return receipt;
},
Expand Down
44 changes: 44 additions & 0 deletions src/views/Lending/Cooler/hooks/useGetConsolidationAllowances.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useQuery } from "@tanstack/react-query";
import { COOLER_CONSOLIDATION_CONTRACT } from "src/constants/contracts";
import { DecimalBigNumber } from "src/helpers/DecimalBigNumber/DecimalBigNumber";
import { useTestableNetworks } from "src/hooks/useTestableNetworks";
import { CoolerConsolidation__factory } from "src/typechain";
import { useProvider } from "wagmi";

export const useGetConsolidationAllowances = ({
clearingHouseAddress,
coolerAddress,
loanIds,
}: {
clearingHouseAddress: string;
coolerAddress: string;
loanIds: number[];
}) => {
const provider = useProvider();
const networks = useTestableNetworks();

console.log("useGetConsolidationAllowances", clearingHouseAddress, coolerAddress, loanIds);

const { data, isFetched, isLoading } = useQuery(
["useGetConsolidationAllowances", clearingHouseAddress, coolerAddress],
async () => {
try {
const contractAddress = COOLER_CONSOLIDATION_CONTRACT.addresses[networks.MAINNET];
const contract = CoolerConsolidation__factory.connect(contractAddress, provider);
const requiredApprovals = await contract.requiredApprovals(clearingHouseAddress, coolerAddress, loanIds);
const totalDebtWithFee = requiredApprovals[3].add(requiredApprovals[4]);
return {
consolidatedLoanCollateral: new DecimalBigNumber(requiredApprovals[1], 18),
totalDebtWithFee: new DecimalBigNumber(totalDebtWithFee, 18),
};
} catch {
return {
consolidatedLoanCollateral: new DecimalBigNumber("0", 18),
totalDebtWithFee: new DecimalBigNumber("0", 18),
};
}
},
{ enabled: !!coolerAddress },
);
return { data, isFetched, isLoading };
};
Loading

0 comments on commit 7a4df8d

Please sign in to comment.