Skip to content

Commit

Permalink
feat: fetch .sol domains (favorite > first > pub key)
Browse files Browse the repository at this point in the history
  • Loading branch information
chambaz committed Oct 13, 2023
1 parent 5160a79 commit 7a1eb84
Showing 1 changed file with 52 additions and 18 deletions.
70 changes: 52 additions & 18 deletions packages/marginfi-v2-ui-state/src/lib/points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ import {
where,
QueryDocumentSnapshot,
} from "firebase/firestore";
import { FavouriteDomain, NAME_OFFERS_ID, reverseLookupBatch } from "@bonfida/spl-name-service";
import {
FavouriteDomain,
NAME_OFFERS_ID,
reverseLookupBatch,
reverseLookup,
getAllDomains,
getFavoriteDomain,
} from "@bonfida/spl-name-service";
import { Connection, PublicKey } from "@solana/web3.js";
import { firebaseApi } from ".";

Expand All @@ -29,6 +36,8 @@ type LeaderboardRow = {
socialPoints: number;
};

const shortAddress = (address: string) => `${address.slice(0, 4)}...${address.slice(-4)}`;

async function fetchLeaderboardData({
connection,
queryCursor,
Expand Down Expand Up @@ -61,25 +70,50 @@ async function fetchLeaderboardData({

const leaderboardFinalSlice: LeaderboardRow[] = [...leaderboardSlice];

if (connection) {
const publicKeys = leaderboardFinalSlice.map((value) => {
const [favoriteDomains] = FavouriteDomain.getKeySync(NAME_OFFERS_ID, new PublicKey(value.id));
return favoriteDomains;
});
const favoriteDomainsInfo = (await connection.getMultipleAccountsInfo(publicKeys)).map((accountInfo, idx) =>
accountInfo ? FavouriteDomain.deserialize(accountInfo.data).nameAccount : publicKeys[idx]
);
const reverseLookup = await reverseLookupBatch(connection, favoriteDomainsInfo);

leaderboardFinalSlice.map(
(value, idx) =>
(value.id = reverseLookup[idx]
? `${reverseLookup[idx]}.sol`
: `${value.id.slice(0, 5)}...${value.id.slice(-5)}`)
);
if (!connection) {
return leaderboardFinalSlice;
}

return leaderboardFinalSlice;
const leaderboardFinalSliceWithDomains: LeaderboardRow[] = await Promise.all(
leaderboardFinalSlice.map(async (value) => {
// attempt to get favorite domain
try {
const { reverse } = await getFavoriteDomain(connection, new PublicKey(value.id));
if (reverse) {
return {
...value,
id: `${reverse}.sol`,
};
}
} catch (e) {
// attempt to get all domains
try {
const domains = await getAllDomains(connection, new PublicKey(value.id));
if (domains.length > 0) {
const reverse = await reverseLookup(connection, domains[0]);
if (reverse) {
return {
...value,
id: `${reverse}.sol`,
};
}
}
} catch (e) {
return {
...value,
id: shortAddress(value.id),
};
}
}

return {
...value,
id: shortAddress(value.id),
};
})
);

return leaderboardFinalSliceWithDomains;
}

// Firebase query is very constrained, so we calculate the number of users with more points
Expand Down

0 comments on commit 7a1eb84

Please sign in to comment.