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

fix(mfi-v2-ui): handle missing referral data #417

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const AccountSummary = () => {

return (
<div className={cn("flex flex-col py-[10px] px-4 lg:flex-row w-full justify-between items-center")}>
<div className="font-[500] lg:block w-full h-[118px]">
<div className="font-[500] lg:block w-full">
<div className="h-full rounded-xl">
<span className="w-full flex justify-start text-xl">Global stats</span>
<GlobalStats
Expand All @@ -37,7 +37,7 @@ const AccountSummary = () => {

<div className="w-full">
{connected && (
<div className="font-[500] h-full rounded-xl">
<div className="font-[500] rounded-xl">
<span className="w-full h-full flex justify-start text-xl text-white">Your account</span>
<UserStats
accountSummary={isStoreInitialized && selectedAccount ? accountSummary : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export const PointsOverview: FC<PointsOverviewProps> = ({ userPointsData }) => {
<div className="grid grid-cols-2 gap-5 max-w-[800px] mx-auto w-full mt-2">
<Card className="bg-[#131619] h-24 rounded-xl" elevation={0}>
<CardContent>
<Typography color="#868E95" className="font-aeonik font-[300] text-base flex gap-1" gutterBottom>
<Typography
color="#868E95"
className="font-aeonik font-[300] text-base flex gap-1"
gutterBottom
component="div"
>
Total Points
<div className="self-center">
<MrgnTooltip
Expand Down Expand Up @@ -64,7 +69,12 @@ export const PointsOverview: FC<PointsOverviewProps> = ({ userPointsData }) => {
<div className="grid grid-cols-2 sm:grid-cols-3 gap-5 max-w-[800px] mx-auto w-full">
<Card className="bg-[#131619] h-24 rounded-xl" elevation={0}>
<CardContent>
<Typography color="#868E95" className="font-aeonik font-[300] text-base flex gap-1" gutterBottom>
<Typography
color="#868E95"
className="font-aeonik font-[300] text-base flex gap-1"
gutterBottom
component="div"
>
Lending Points
<div className="self-center">
<MrgnTooltip
Expand Down Expand Up @@ -93,7 +103,12 @@ export const PointsOverview: FC<PointsOverviewProps> = ({ userPointsData }) => {
</Card>
<Card className="bg-[#131619] h-24 rounded-xl" elevation={0}>
<CardContent>
<Typography color="#868E95" className="font-aeonik font-[300] text-base flex gap-1" gutterBottom>
<Typography
color="#868E95"
className="font-aeonik font-[300] text-base flex gap-1"
gutterBottom
component="div"
>
Borrowing Points
<div className="self-center">
<MrgnTooltip
Expand Down Expand Up @@ -122,7 +137,12 @@ export const PointsOverview: FC<PointsOverviewProps> = ({ userPointsData }) => {
</Card>
<Card className="bg-[#131619] h-24 rounded-xl" elevation={0}>
<CardContent>
<Typography color="#868E95" className="font-aeonik font-[300] text-base flex gap-1" gutterBottom>
<Typography
color="#868E95"
className="font-aeonik font-[300] text-base flex gap-1"
gutterBottom
component="div"
>
Referral Points
<div className="self-center">
<MrgnTooltip
Expand Down
45 changes: 30 additions & 15 deletions packages/marginfi-v2-ui-state/src/lib/points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,32 +155,47 @@ const getPointsDataForUser = async (wallet: string | undefined): Promise<UserPoi
const userPointsDoc = doc(firebaseApi.db, "points", wallet);
const userPublicProfileDoc = doc(firebaseApi.db, "users_public", wallet);

const [userPointsSnap, userPublicProfileSnap] = await Promise.all([
getDoc(userPointsDoc),
getDoc(userPublicProfileDoc),
]);
let userPointsSnap;
let userPublicProfileSnap;
let userReferralData;
let userReferralCode = "";
let isCustomReferralLink = false;

if (!userPointsSnap.exists() || !userPublicProfileSnap.exists()) {
try {
userPointsSnap = await getDoc(userPointsDoc);
} catch (e) {
return {
...DEFAULT_USER_POINTS_DATA,
owner: wallet,
};
}

const userReferralData = userPublicProfileSnap.data();
let userReferralCode = "";
let isCustomReferralLink;
if (userReferralData && Array.isArray(userReferralData?.referralCode)) {
const uuidPattern = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-\b[0-9a-fA-F]{12}$/;
userReferralCode = userReferralData.referralCode.find((code) => !uuidPattern.test(code));
isCustomReferralLink = true;
} else {
userReferralCode = userReferralData?.referralCode || "";
isCustomReferralLink = false;
try {
userPublicProfileSnap = await getDoc(userPublicProfileDoc);
userReferralData = userPublicProfileSnap.data();
if (userReferralData && Array.isArray(userReferralData?.referralCode)) {
const uuidPattern = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-\b[0-9a-fA-F]{12}$/;
userReferralCode = userReferralData.referralCode.find((code) => !uuidPattern.test(code));
isCustomReferralLink = true;
} else {
userReferralCode = userReferralData?.referralCode || "";
isCustomReferralLink = false;
}
} catch (e) {
console.log("error", e);
}

const pointsData = userPointsSnap.data();

if (!pointsData) {
return {
...DEFAULT_USER_POINTS_DATA,
owner: wallet,
};
}

console.log("pointsData", pointsData);

const depositPoints = pointsData.total_deposit_points.toFixed(4);
const borrowPoints = pointsData.total_borrow_points.toFixed(4);
const referralPoints = (pointsData.total_referral_deposit_points + pointsData.total_referral_borrow_points).toFixed(
Expand Down