Skip to content

Commit

Permalink
fix: handle missing referral data
Browse files Browse the repository at this point in the history
  • Loading branch information
chambaz committed Dec 9, 2023
1 parent 46eb4bc commit 710c5a0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
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

3 comments on commit 710c5a0

@vercel
Copy link

@vercel vercel bot commented on 710c5a0 Dec 10, 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:

marginfi-landing-page – ./apps/marginfi-landing-page

marginfi-landing-page-mrgn.vercel.app
marginfi-landing-page-git-production-mrgn.vercel.app
marginfi-landing-page.vercel.app
marginfi.com
www.marginfi.com

@vercel
Copy link

@vercel vercel bot commented on 710c5a0 Dec 10, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 710c5a0 Dec 10, 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:

omni – ./apps/omni

omni-git-production-mrgn.vercel.app
omni-mrgn.vercel.app
omni.marginfi.com
omni-one.vercel.app

Please sign in to comment.