Skip to content

Commit

Permalink
fix: grants
Browse files Browse the repository at this point in the history
  • Loading branch information
MrX-SNX committed Sep 24, 2024
1 parent 0c7a134 commit bbf7b6b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 40 deletions.
99 changes: 65 additions & 34 deletions frontend/components/Home/UserGrants/GrantStatus.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { Heading, Progress, Flex, Box, Text, LightMode } from '@chakra-ui/react'
import { Icon } from '@chakra-ui/icons'
import { BsClockHistory } from 'react-icons/bs'
import { ethers } from 'ethers'
import { useRecoilState } from 'recoil'
import { format, formatDistanceToNowStrict, formatDistance } from 'date-fns'
import { getGrant } from '../../../lib/store/grants'
import {
Heading,
Progress,
Flex,
Box,
Text,
LightMode,
} from "@chakra-ui/react";
import { Icon } from "@chakra-ui/icons";
import { BsClockHistory } from "react-icons/bs";
import { ethers } from "ethers";
import { useRecoilState } from "recoil";
import { format, formatDistanceToNowStrict, formatDistance } from "date-fns";
import { getGrant } from "../../../lib/store/grants";

export default function GrantStatus({ tokenId, cancelled }) {
const [grant] = useRecoilState(getGrant(tokenId));
const [[grant]] = useRecoilState(getGrant(tokenId));

const amountVested = parseFloat(ethers.utils.formatUnits(grant.amountVested, 18));
const totalAmount = parseFloat(ethers.utils.formatUnits(grant.totalAmount, 18));
const amountVested = parseFloat(
ethers.utils.formatUnits(grant.amountVested, 18)
);
const totalAmount = parseFloat(
ethers.utils.formatUnits(grant.totalAmount, 18)
);
const vestAmount = parseFloat(ethers.utils.formatUnits(grant.vestAmount, 18));
const startTimestamp = parseInt(grant.startTimestamp);
const cliffTimestamp = parseInt(grant.cliffTimestamp);
Expand All @@ -26,44 +37,64 @@ export default function GrantStatus({ tokenId, cancelled }) {
}

const nextVest = formatDistanceToNowStrict(
new Date(Math.max(
cliffTimestamp * 1000,
nextIntervalVest * 1000
))
new Date(Math.max(cliffTimestamp * 1000, nextIntervalVest * 1000))
);

const intervalInWords = formatDistance(new Date(0), new Date(vestInterval * 1000))
const intervalInWords = formatDistance(
new Date(0),
new Date(vestInterval * 1000)
);

const descriptionText = <div>{
cliffTimestamp > Date.now() / 1000 ?
<>Your grant will vest {vestAmount.toLocaleString()} {grant.tokenSymbol} every {intervalInWords} starting {format(new Date(startTimestamp * 1000), 'M/dd/yyyy')} with a cliff on {format(new Date(cliffTimestamp * 1000), 'M/dd/yyyy')}.</>
:
amountVested == totalAmount ?
const descriptionText = (
<div>
{cliffTimestamp > Date.now() / 1000 ? (
<>
Your grant will vest {vestAmount.toLocaleString()} {grant.tokenSymbol}{" "}
every {intervalInWords} starting{" "}
{format(new Date(startTimestamp * 1000), "M/dd/yyyy")} with a cliff on{" "}
{format(new Date(cliffTimestamp * 1000), "M/dd/yyyy")}.
</>
) : amountVested == totalAmount ? (
<Text>Your grant has completely vested.</Text>
:
<Text>Your grant vests every {intervalInWords}. Your next {vestAmount.toLocaleString()} {grant.tokenSymbol} will vest in {nextVest}.</Text>
} </div>
) : (
<Text>
Your grant vests every {intervalInWords}. Your next{" "}
{vestAmount.toLocaleString()} {grant.tokenSymbol} will vest in{" "}
{nextVest}.
</Text>
)}{" "}
</div>
);

return (
<Box
mb={5}
borderRadius="md"
background="gray.900"
py={5}
px={6}>
<Heading size="lg" fontWeight="light"><Icon as={BsClockHistory} boxSize={5} mr={2} />Grant Status</Heading>
<Box mb={5} borderRadius="md" background="gray.900" py={5} px={6}>
<Heading size="lg" fontWeight="light">
<Icon as={BsClockHistory} boxSize={5} mr={2} />
Grant Status
</Heading>
<Flex align="center">
<Box width="55%" pr={4}>
{!cancelled ? descriptionText : <Text>Cancelled.</Text>}
</Box>
<Box width="45%">
<Heading size="md" fontWeight="medium" mb={1}>{(amountVested / totalAmount * 100).toLocaleString()}% Vested</Heading>
<Heading size="md" fontWeight="medium" mb={1}>
{((amountVested / totalAmount) * 100).toLocaleString()}% Vested
</Heading>
<LightMode>
<Progress colorScheme='green' size='sm' background="gray.700" borderRadius={8} value={amountVested / totalAmount * 100} />
<Progress
colorScheme="green"
size="sm"
background="gray.700"
borderRadius={8}
value={(amountVested / totalAmount) * 100}
/>
</LightMode>
<Text opacity={0.8} fontSize="sm">{amountVested && amountVested.toLocaleString()} {grant.tokenSymbol} of {totalAmount && totalAmount.toLocaleString()} {grant.tokenSymbol}</Text>
<Text opacity={0.8} fontSize="sm">
{amountVested && amountVested.toLocaleString()} {grant.tokenSymbol}{" "}
of {totalAmount && totalAmount.toLocaleString()} {grant.tokenSymbol}
</Text>
</Box>
</Flex>
</Box>
)
);
}
2 changes: 1 addition & 1 deletion frontend/components/Home/UserGrants/RedeemTokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useRecoilState } from "recoil";
import { getGrant, redeemGrant } from "../../../lib/store/grants";

export default function RedeemTokens({ tokenId }) {
const [grant, setGrant] = useRecoilState(getGrant(tokenId));
const [[grant], setGrant] = useRecoilState(getGrant(tokenId));

const [exchangeMode, setExchangeMode] = useState(false);
const [exchangeTokenAmount, setExchangeTokenAmount] = useState(0);
Expand Down
10 changes: 7 additions & 3 deletions frontend/components/Home/UserGrants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ export default function UserGrants() {

useEffect(() => {
if (address && networkId) {
fetchGrantsByUser(address).finally(() => {
setLoadingData(false);
});
fetchGrantsByUser(address)
.then((grants) => {
setGrant(grants);
})
.finally(() => {
setLoadingData(false);
});
}
}, [address, networkId, setGrant]);

Expand Down
12 changes: 10 additions & 2 deletions frontend/lib/store/grants.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export const getGrant = selectorFamily({
get:
(tokenId) =>
({ get }) => {
return get(grantsState)[tokenId];
return Object.values(get(grantsState))
.flat()
.filter((g) => {
return g.tokenId.toString() == tokenId.toString();
});
},
set:
(tokenId) =>
Expand All @@ -53,7 +57,11 @@ export const getGrantsByUser = selectorFamily({
get:
(address) =>
({ get }) => {
return Object.values(get(grantsState)).filter((g) => g.owner == address);
return Object.values(get(grantsState))
.flat()
.filter((g) => {
return g.owner == address;
});
},
set:
() =>
Expand Down

0 comments on commit bbf7b6b

Please sign in to comment.