Skip to content

Commit

Permalink
📈 vesting
Browse files Browse the repository at this point in the history
  • Loading branch information
sebipap committed Nov 23, 2023
1 parent 367a4ef commit eb891d1
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 28 deletions.
49 changes: 33 additions & 16 deletions components/ActiveStream/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -335,20 +335,6 @@ const ActiveStream: FC<ActiveStreamProps> = ({
}
}, [escrowedEXA, opts, refetch, tokenId]);

const withdraw = useCallback(async () => {
if (!escrowedEXA || !opts) return;
setLoading(true);
try {
const tx = await escrowedEXA.write.withdrawMax([[BigInt(tokenId)]], opts);
await waitForTransaction({ hash: tx });
} catch {
// if request fails, don't do anything
} finally {
setLoading(false);
refetch();
}
}, [escrowedEXA, opts, refetch, tokenId]);

const elapsed = useMemo(() => {
const now = Math.floor(Date.now() / 1000);
return now - startTime;
Expand All @@ -363,6 +349,37 @@ const ActiveStream: FC<ActiveStreamProps> = ({
return (elapsed / duration) * 100;
}, [elapsed, duration]);

const handleWithdrawClick = useCallback(async () => {
track('Button Clicked', {
name: 'withdraw',
location: 'Active Stream',
value: progress,
});

if (!escrowedEXA || !opts) return;
setLoading(true);
try {
const tx = await escrowedEXA.write.withdrawMax([[BigInt(tokenId)]], opts);
track('TX Signed', {
contractName: 'EscrowedEXA',
method: 'withdrawMax',
hash: tx,
});
const { status } = await waitForTransaction({ hash: tx });
track('TX Completed', {
contractName: 'EscrowedEXA',
method: 'withdrawMax',
hash: tx,
status,
});
} catch {
// if request fails, don't do anything
} finally {
setLoading(false);
refetch();
}
}, [escrowedEXA, opts, progress, refetch, tokenId]);

const timeLeft = useMemo(() => {
const now = Math.floor(Date.now() / 1000);
const secondsLeft = endTime - now;
Expand Down Expand Up @@ -455,7 +472,7 @@ const ActiveStream: FC<ActiveStreamProps> = ({
data-testid={
progress === 100 ? `vesting-stream-${tokenId}-withdraw` : `vesting-stream-${tokenId}-cancel`
}
onClick={() => (progress === 100 ? withdraw() : setCancelModalOpen(true))}
onClick={() => (progress === 100 ? handleWithdrawClick() : setCancelModalOpen(true))}
sx={{ cursor: 'pointer' }}
>
<Typography fontFamily="IBM Plex Mono" fontSize={12} fontWeight={500} textTransform="uppercase">
Expand Down Expand Up @@ -519,7 +536,7 @@ const ActiveStream: FC<ActiveStreamProps> = ({
<LoadingButton
fullWidth
variant="contained"
onClick={withdraw}
onClick={handleWithdrawClick}
loading={loading}
data-testid={`vesting-stream-${tokenId}-claim`}
>
Expand Down
47 changes: 41 additions & 6 deletions components/VestingInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,15 @@ function VestingInput({ refetch }: Props) {
}
}, [walletAddress, reserveRatio, vestingPeriod, escrowedEXA, exa, opts, qty, isContract, sign]);

const setMaxBalance = useCallback(() => {
const handleMaxClick = useCallback(() => {
if (balance) {
setQty(formatEther(balance));
}
track('Button Clicked', {
location: 'Vesting',
name: 'max',
value: formatNumber(formatEther(balance || 0n)),
});
}, [balance]);

const onClose = useCallback(() => {
Expand Down Expand Up @@ -358,7 +363,7 @@ function VestingInput({ refetch }: Props) {
{t('Available')}: {formatNumber(formatEther(balance || 0n))}
</Typography>
<Button
onClick={setMaxBalance}
onClick={handleMaxClick}
sx={{
textTransform: 'uppercase',
borderRadius: 1,
Expand Down Expand Up @@ -402,7 +407,13 @@ function VestingInput({ refetch }: Props) {
components={{
1: (
<button
onClick={openGetEXA}
onClick={() => {
openGetEXA();
track('Button Clicked', {
location: 'Vesting',
name: 'get EXA',
});
}}
style={{
fontWeight: 700,
textDecoration: 'underline',
Expand Down Expand Up @@ -440,15 +451,31 @@ function VestingInput({ refetch }: Props) {

<Box mt={0} display="flex" flexDirection="column" gap={1}>
{impersonateActive ? (
<Button fullWidth variant="contained">
<Button
fullWidth
variant="contained"
onClick={() =>
track('Button Clicked', {
location: 'Vesting',
name: 'exit read-only mode',
})
}
>
{t('Exit Read-Only Mode')}
</Button>
) : displayNetwork.id !== chain?.id ? (
<LoadingButton
fullWidth
variant="contained"
loading={switchIsLoading}
onClick={() => switchNetwork?.(displayNetwork.id)}
onClick={() => {
switchNetwork?.(displayNetwork.id);
track('Button Clicked', {
location: 'Vesting',
name: 'switch network',
value: displayNetwork.name,
});
}}
>
{t('Please switch to {{network}} network', { network: displayNetwork.name })}
</LoadingButton>
Expand All @@ -457,7 +484,15 @@ function VestingInput({ refetch }: Props) {
fullWidth
variant="contained"
loading={isLoading}
onClick={submit}
onClick={() => {
submit();
track('Button Clicked', {
location: 'Vesting',
name: 'vest',
value: qty,
text: insufficientFunds ? t('Insufficient esEXA balance') : t('Vest esEXA'),
});
}}
data-testid="vesting-submit"
disabled={insufficientFunds}
>
Expand Down
34 changes: 32 additions & 2 deletions pages/vesting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useModal } from 'contexts/ModalContext';
import formatNumber from 'utils/formatNumber';
import { formatEther } from 'viem';
import { toPercentage } from 'utils/utils';
import { track } from 'utils/segment';

const Vesting: NextPage = () => {
const { t } = useTranslation();
Expand All @@ -36,11 +37,27 @@ const Vesting: NextPage = () => {
}, [rewards]);

const handleClaimAll = useCallback(async () => {
track('Button Clicked', {
location: 'Vesting',
name: 'withdraw all',
value: activeStreams.length,
});
if (!activeStreams || !escrowedEXA || !opts) return;
setLoading(true);
try {
const tx = await escrowedEXA.write.withdrawMax([activeStreams.map(({ tokenId }) => BigInt(tokenId))], opts);
await waitForTransaction({ hash: tx });
track('TX Signed', {
contractName: 'EscrowedEXA',
method: 'withdrawMax',
hash: tx,
});
const { status } = await waitForTransaction({ hash: tx });
track('TX Completed', {
contractName: 'EscrowedEXA',
method: 'withdrawMax',
hash: tx,
status,
});
} catch {
// if request fails, don't do anything
} finally {
Expand All @@ -66,6 +83,13 @@ const Vesting: NextPage = () => {
target="_blank"
rel="noreferrer noopener"
href="https://docs.exact.ly/governance/exactly-token-exa/escrowedexa-esexa"
onClick={() =>
track('Button Clicked', {
location: 'Vesting',
name: 'learn more ',
href: 'https://docs.exact.ly/governance/exactly-token-exa/escrowedexa-esexa',
})
}
>
{t('Learn more about the esEXA Vesting Program.')}
</a>
Expand Down Expand Up @@ -115,7 +139,13 @@ const Vesting: NextPage = () => {
components={{
1: (
<button
onClick={openGetEXA}
onClick={() => {
openGetEXA();
track('Button Clicked', {
location: 'Vesting',
name: 'get EXA',
});
}}
style={{
fontWeight: 700,
textDecoration: 'underline',
Expand Down
10 changes: 6 additions & 4 deletions utils/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ type TrackEvent = {
contractName: string;
method: string;
hash: Hash;
amount: string;
usdAmount: string;
amount?: string;
usdAmount?: string;
};
'TX Completed': {
contractName?: string;
method?: string;
status: TransactionReceipt['status'];
hash: Hash;
amount: string;
usdAmount: string;
amount?: string;
usdAmount?: string;
};
};

Expand Down

1 comment on commit eb891d1

@vercel
Copy link

@vercel vercel bot commented on eb891d1 Nov 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

app – ./

exactly-development.vercel.app
app.exactly.app
app-git-main.exactly.app
app.exact.ly

Please sign in to comment.