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

feat(vesting): total amount counts #567

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 97 additions & 14 deletions apps/vesting/src/components/VestingAccountStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,11 @@ function LockupPeriods({
</div>

<div className="p-4 pt-0">
<div className="flex flex-col gap-2">
<div>
<div className="text-xs leading-normal text-[#8E8E8E] md:text-sm">
Start date
</div>
<div>{formatDate(new Date(startTime))}</div>
</div>
<div>
<div className="text-xs leading-normal text-[#8E8E8E] md:text-sm">
End date
</div>
<div>{formatDate(new Date(endTime))}</div>
</div>
</div>
<VestingStatsGraph
endTime={endTime}
startTime={startTime}
lockupPeriods={lockupPeriods}
/>
</div>

<div className="divide-y border-t">
Expand Down Expand Up @@ -238,3 +229,95 @@ function LockupTimelineListItem({
</div>
);
}

function VestingStatsGraph({
startTime,
endTime,
lockupPeriods,
}: {
startTime: string;
endTime: string;
lockupPeriods: VestingPeriod[];
}) {
const { lockedAmount, totalAmount, unlockedAmount } = useMemo(() => {
const now = new Date();
const bigIntLockupPeriods = lockupPeriods.reduce(
(acc, el) => {
const prevDateStr = acc.lastDate;

const prevDate = new Date(prevDateStr);
const offset = Number.parseInt(el.length ?? '0') * 1000;
const unlockDate = new Date(prevDate.getTime() + offset);
const nowTime = now.getTime();
const past = unlockDate.getTime() < nowTime;

const nextUnlockedAmount = past
? acc.unlockedAmount + BigInt(el.amount?.[0].amount ?? '0')
: acc.unlockedAmount;

const nextLockedAmount = past
? acc.lockedAmount
: acc.lockedAmount + BigInt(el.amount?.[0].amount ?? '0');

return {
totalAmount: acc.totalAmount + BigInt(el.amount?.[0].amount ?? '0'),
unlockedAmount: nextUnlockedAmount,
lockedAmount: nextLockedAmount,
lastDate: unlockDate,
};
},
{
totalAmount: 0n,
unlockedAmount: 0n,
lockedAmount: 0n,
lastDate: new Date(startTime),
},
);

return {
totalAmount: Number.parseFloat(
formatUnits(bigIntLockupPeriods.totalAmount, 18),
),
unlockedAmount: Number.parseFloat(
formatUnits(bigIntLockupPeriods.unlockedAmount, 18),
),
lockedAmount: Number.parseFloat(
formatUnits(bigIntLockupPeriods.lockedAmount, 18),
),
};
}, [lockupPeriods, startTime]);
return (
<div className="flex flex-col gap-2">
<div>
<div className="text-xs leading-normal text-[#8E8E8E] md:text-sm">
Start date
</div>
<div>{formatDate(new Date(startTime))}</div>
</div>
<div>
<div className="text-xs leading-normal text-[#8E8E8E] md:text-sm">
End date
</div>
<div>{formatDate(new Date(endTime))}</div>
</div>
<div>
<div className="text-xs leading-normal text-[#8E8E8E] md:text-sm">
Total lockups
</div>
<div>{totalAmount} ISLM</div>
</div>
<div>
<div className="text-xs leading-normal text-[#8E8E8E] md:text-sm">
Already unlocked
</div>
<div>{unlockedAmount} ISLM</div>
</div>
<div>
<div className="text-xs leading-normal text-[#8E8E8E] md:text-sm">
Locked
</div>
<div>{lockedAmount} ISLM</div>
</div>
</div>
);
}
Loading