Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cooler consolidation #3185

Merged
merged 13 commits into from
Sep 13, 2024
125 changes: 125 additions & 0 deletions src/abi/CoolerConsolidation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"abi": [
{
"type": "constructor",
"inputs": [
{ "name": "gohm_", "type": "address", "internalType": "address" },
{ "name": "sdai_", "type": "address", "internalType": "address" },
{ "name": "dai_", "type": "address", "internalType": "address" },
{ "name": "owner_", "type": "address", "internalType": "address" },
{ "name": "lender_", "type": "address", "internalType": "address" },
{ "name": "collector_", "type": "address", "internalType": "address" },
{ "name": "feePercentage_", "type": "uint256", "internalType": "uint256" }
],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "collector",
"inputs": [],
"outputs": [{ "name": "", "type": "address", "internalType": "address" }],
"stateMutability": "view"
},
{
"type": "function",
"name": "consolidateWithFlashLoan",
"inputs": [
{ "name": "clearinghouse_", "type": "address", "internalType": "address" },
{ "name": "cooler_", "type": "address", "internalType": "address" },
{ "name": "ids_", "type": "uint256[]", "internalType": "uint256[]" },
{ "name": "useFunds_", "type": "uint256", "internalType": "uint256" },
{ "name": "sdai_", "type": "bool", "internalType": "bool" }
],
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "dai",
"inputs": [],
"outputs": [{ "name": "", "type": "address", "internalType": "contract IERC20" }],
"stateMutability": "view"
},
{
"type": "function",
"name": "feePercentage",
"inputs": [],
"outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }],
"stateMutability": "view"
},
{
"type": "function",
"name": "gohm",
"inputs": [],
"outputs": [{ "name": "", "type": "address", "internalType": "contract IERC20" }],
"stateMutability": "view"
},
{
"type": "function",
"name": "lender",
"inputs": [],
"outputs": [{ "name": "", "type": "address", "internalType": "contract IERC3156FlashLender" }],
"stateMutability": "view"
},
{
"type": "function",
"name": "onFlashLoan",
"inputs": [
{ "name": "initiator_", "type": "address", "internalType": "address" },
{ "name": "token_", "type": "address", "internalType": "address" },
{ "name": "amount_", "type": "uint256", "internalType": "uint256" },
{ "name": "fee_", "type": "uint256", "internalType": "uint256" },
{ "name": "params_", "type": "bytes", "internalType": "bytes" }
],
"outputs": [{ "name": "", "type": "bytes32", "internalType": "bytes32" }],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "owner",
"inputs": [],
"outputs": [{ "name": "", "type": "address", "internalType": "address" }],
"stateMutability": "view"
},
{
"type": "function",
"name": "requiredApprovals",
"inputs": [
{ "name": "cooler_", "type": "address", "internalType": "address" },
{ "name": "ids_", "type": "uint256[]", "internalType": "uint256[]" }
],
"outputs": [
{ "name": "", "type": "address", "internalType": "address" },
{ "name": "", "type": "uint256", "internalType": "uint256" },
{ "name": "", "type": "uint256", "internalType": "uint256" },
{ "name": "", "type": "uint256", "internalType": "uint256" }
],
"stateMutability": "view"
},
{
"type": "function",
"name": "sdai",
"inputs": [],
"outputs": [{ "name": "", "type": "address", "internalType": "contract IERC4626" }],
"stateMutability": "view"
},
{
"type": "function",
"name": "setCollector",
"inputs": [{ "name": "collector_", "type": "address", "internalType": "address" }],
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "setFeePercentage",
"inputs": [{ "name": "feePercentage_", "type": "uint256", "internalType": "uint256" }],
"outputs": [],
"stateMutability": "nonpayable"
},
{ "type": "error", "name": "OnlyCoolerOwner", "inputs": [] },
{ "type": "error", "name": "OnlyLender", "inputs": [] },
{ "type": "error", "name": "OnlyOwner", "inputs": [] },
{ "type": "error", "name": "OnlyThis", "inputs": [] }
]
}
5 changes: 5 additions & 0 deletions src/constants/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,8 @@ export const COOLER_CLEARING_HOUSE_V2_ADDRESSES = {
[NetworkId.MAINNET]: "0xE6343ad0675C9b8D3f32679ae6aDbA0766A2ab4c",
[NetworkId.TESTNET_GOERLI]: "0xbfe14B5950a530A5CE572Cd2FaC6d44c718A3C47",
};

export const COOLER_CONSOLIDATION_ADDRESSES = {
[NetworkId.MAINNET]: "0xD24fAba2506b5B42Be42a6E6C1BA1Dd6856A412d",
[NetworkId.TESTNET_GOERLI]: "",
};
8 changes: 8 additions & 0 deletions src/constants/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BOND_FIXED_TERM_TELLER_ADDRESSES,
COOLER_CLEARING_HOUSE_V1_ADDRESSES,
COOLER_CLEARING_HOUSE_V2_ADDRESSES,
COOLER_CONSOLIDATION_ADDRESSES,
CROSS_CHAIN_BRIDGE_ADDRESSES,
CROSS_CHAIN_BRIDGE_ADDRESSES_TESTNET,
DEV_FAUCET,
Expand All @@ -27,6 +28,7 @@ import {
BondFixedExpiryTeller__factory,
BondFixedTermTeller__factory,
CoolerClearingHouse__factory,
CoolerConsolidation__factory,
CrossChainBridge__factory,
CrossChainBridgeTestnet__factory,
CrossChainMigrator__factory,
Expand Down Expand Up @@ -162,3 +164,9 @@ export const COOLER_CLEARING_HOUSE_CONTRACT_V2 = new Contract({
name: "Cooler Clearing House Contract V2",
addresses: COOLER_CLEARING_HOUSE_V2_ADDRESSES,
});

export const COOLER_CONSOLIDATION_CONTRACT = new Contract({
factory: CoolerConsolidation__factory,
name: "Cooler Consolidation Utils",
addresses: COOLER_CONSOLIDATION_ADDRESSES,
});
2 changes: 1 addition & 1 deletion src/helpers/environment/Environment/Environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class Environment {
case NetworkId.MAINNET:
return this._get({
key: `VITE_ETHEREUM_NODE_URL`,
fallback: "https://rpc.ankr.com/eth",
fallback: "https://virtual.mainnet.rpc.tenderly.co/31652316-ab0d-4eea-bbd1-0c6bb5cf4702",
brightiron marked this conversation as resolved.
Show resolved Hide resolved
});
case NetworkId.TESTNET_GOERLI:
return this._get({
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://virtual.mainnet.rpc.tenderly.co/31652316-ab0d-4eea-bbd1-0c6bb5cf4702"] },
public: { http: ["https://virtual.mainnet.rpc.tenderly.co/31652316-ab0d-4eea-bbd1-0c6bb5cf4702"] },
},
brightiron marked this conversation as resolved.
Show resolved Hide resolved
},
{
...polygon,
Expand Down
62 changes: 62 additions & 0 deletions src/views/Lending/Cooler/hooks/useConsolidateCooler.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import toast from "react-hot-toast";
import { COOLER_CONSOLIDATION_CONTRACT } from "src/constants/contracts";
import { trackGAEvent, trackGtagEvent } from "src/helpers/analytics/trackGAEvent";
import { balanceQueryKey } from "src/hooks/useBalance";
import { contractAllowanceQueryKey } from "src/hooks/useContractAllowance";
import { useTestableNetworks } from "src/hooks/useTestableNetworks";
import { CoolerConsolidation__factory } from "src/typechain";
import { useSigner } from "wagmi";

export const useConsolidateCooler = () => {
const { data: signer } = useSigner();
const queryClient = useQueryClient();
const networks = useTestableNetworks();

return useMutation(
async ({
coolerAddress,
clearingHouseAddress,
loanIds,
}: {
coolerAddress: string;
clearingHouseAddress: 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 receipt = await cooler.wait();
return receipt;
},
{
onError: (error: Error) => {
toast.error(error.message);
},
onSuccess: async tx => {
queryClient.invalidateQueries({ queryKey: ["getCoolerLoans"] });
queryClient.invalidateQueries({ queryKey: [balanceQueryKey()] });
queryClient.invalidateQueries({ queryKey: [contractAllowanceQueryKey()] });
if (tx.transactionHash) {
trackGAEvent({
category: "Cooler",
action: "Consolidate Cooler",
dimension1: tx.transactionHash,
dimension2: tx.from, // the signer, not necessarily the receipient
});

trackGtagEvent("Cooler", {
event_category: "Consolidate Cooler",
address: tx.from.slice(2), // the signer, not necessarily the receipient
txHash: tx.transactionHash.slice(2),
});
}

toast(`Coolers Consolidated Successfully`);
},
},
);
};
Loading
Loading