Skip to content

Commit

Permalink
Merge pull request #2968 from OlympusDAO/develop
Browse files Browse the repository at this point in the history
Release: Delegate coolers & repay calc
  • Loading branch information
appleseed-iii authored Sep 22, 2023
2 parents f5d144e + 01561c4 commit 6b5e473
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 167 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"husky": "^8.0.3",
"jsdom": "^21.1.1",
"less-plugin-npm-import": "^2.1.0",
"lint-staged": "^13.2.3",
"lint-staged": "^14.0.1",
"node-watch": "^0.7.1",
"orval": "^6.17.0",
"os-browserify": "^0.3.0",
Expand Down
25 changes: 25 additions & 0 deletions src/views/Lending/Cooler/hooks/useDelegateVoting.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useMutation } from "@tanstack/react-query";
import toast from "react-hot-toast";
import { Cooler__factory } from "src/typechain";
import { useSigner } from "wagmi";

export const useDelegateVoting = () => {
const { data: signer } = useSigner();

return useMutation(
async ({ coolerAddress, delegationAddress }: { coolerAddress: string; delegationAddress: string }) => {
if (!signer) throw new Error(`Please connect a wallet`);
const coolerContract = Cooler__factory.connect(coolerAddress, signer);
const receipt = await coolerContract.delegateVoting(delegationAddress);
return receipt;
},
{
onError: (error: Error) => {
toast.error(error.message);
},
onSuccess: async tx => {
toast(`Successfully Delegated Voting`);
},
},
);
};
12 changes: 5 additions & 7 deletions src/views/Lending/Cooler/positions/CreateOrRepayLoan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,11 @@ export const CreateOrRepayLoan = ({
onChange={(e: { target: { value: DecimalBigNumber | string } }) => {
const value = typeof e.target.value === "string" ? new DecimalBigNumber(e.target.value) : e.target.value;
setPaymentAmount(value);
setCollateralAmount(
value.div(
loan && !interestRepaid
? new DecimalBigNumber(loanToCollateral).add(new DecimalBigNumber(loan.interestDue, 18))
: new DecimalBigNumber(loanToCollateral),
),
);
const collateralToReceive = value
.sub(loan && !interestRepaid ? new DecimalBigNumber(loan.interestDue, 18) : new DecimalBigNumber("0", 18))
.div(new DecimalBigNumber(loanToCollateral));

setCollateralAmount(collateralToReceive);
}}
loanBalance={loan?.principal.add(loan?.interestDue)}
/>
Expand Down
71 changes: 71 additions & 0 deletions src/views/Lending/Cooler/positions/DelegateVoting.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Box, SvgIcon } from "@mui/material";
import { Input, Modal, PrimaryButton } from "@olympusdao/component-library";
import { useState } from "react";
import { ReactComponent as lendAndBorrowIcon } from "src/assets/icons/lendAndBorrow.svg";
import { WalletConnectedGuard } from "src/components/WalletConnectedGuard";
import { useDelegateVoting } from "src/views/Lending/Cooler/hooks/useDelegateVoting";

export const DelegateVoting = ({
coolerAddress,
open,
setOpen,
}: {
coolerAddress?: string;
open: boolean;
setOpen: (open: boolean) => void;
}) => {
const [delegationAddress, setDelegationAddress] = useState("");
const delegateVoting = useDelegateVoting();

return (
<Modal
maxWidth="476px"
minHeight="200px"
open={open}
headerContent={
<Box display="flex" alignItems="center" gap="6px">
<SvgIcon component={lendAndBorrowIcon} /> <Box fontWeight="500">Delegate Voting</Box>
</Box>
}
onClose={() => setOpen(false)}
>
{coolerAddress ? (
<>
<Box display="flex" flexDirection="row" justifyContent="space-between" alignItems="center"></Box>
<Box mt={"16px"} mb="16px">
<Input
id="delegateAddress"
placeholder="Address"
value={delegationAddress}
onChange={e => {
setDelegationAddress(e.target.value);
}}
/>
</Box>

<WalletConnectedGuard fullWidth>
<PrimaryButton
fullWidth
disabled={delegateVoting.isLoading}
onClick={() => {
delegateVoting.mutate(
{ coolerAddress, delegationAddress },
{
onSuccess: () => {
setOpen(false);
},
},
);
}}
loading={delegateVoting.isLoading}
>
Delegate Voting
</PrimaryButton>
</WalletConnectedGuard>
</>
) : (
<></>
)}
</Modal>
);
};
6 changes: 6 additions & 0 deletions src/views/Lending/Cooler/positions/Positions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useGetClearingHouse } from "src/views/Lending/Cooler/hooks/useGetCleari
import { useGetCoolerForWallet } from "src/views/Lending/Cooler/hooks/useGetCoolerForWallet";
import { useGetCoolerLoans } from "src/views/Lending/Cooler/hooks/useGetCoolerLoans";
import { CreateOrRepayLoan } from "src/views/Lending/Cooler/positions/CreateOrRepayLoan";
import { DelegateVoting } from "src/views/Lending/Cooler/positions/DelegateVoting";
import { ExtendLoan } from "src/views/Lending/Cooler/positions/ExtendLoan";
import { LiquidityCTA } from "src/views/Liquidity/LiquidityCTA";
import { useAccount } from "wagmi";
Expand All @@ -43,6 +44,7 @@ export const CoolerPositions = () => {

const [extendLoan, setExtendLoan] = useState<any>(null);
const [repayLoan, setRepayLoan] = useState<any>(null);
const [delegateVoting, setDelegateVoting] = useState<any>(null);

return (
<div id="cooler-positions">
Expand Down Expand Up @@ -182,6 +184,10 @@ export const CoolerPositions = () => {
</Box>
</>
)}
<Box display="flex" justifyContent={"center"}>
<PrimaryButton onClick={() => setDelegateVoting(true)}>Delegate Voting</PrimaryButton>
</Box>
<DelegateVoting coolerAddress={coolerAddress} open={delegateVoting} setOpen={setDelegateVoting} />
</>
)}

Expand Down
Loading

0 comments on commit 6b5e473

Please sign in to comment.