Skip to content
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
2 changes: 1 addition & 1 deletion src/constants/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const tokens: Record<string, TokenMetadata> = {
spec: "ft-1.0.0",
name: "NEAR",
symbol: "NEAR",
icon: "https://ipfs.near.social/ipfs/bafkreifnyxk6cssapw7j5vc6zuzwl7vt6o5ddspoo5lcmbvtdrcmfozqyu",
icon: "https://near.org/_next/static/media/near-icon.2e682d59.svg",
reference: null,
reference_hash: null,
decimals: 24,
Expand Down
5 changes: 2 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ app.get("/api/token-metadata", async (req: Request, res: Response) => {

app.get("/api/whitelist-tokens", async (req: Request, res: Response) => {
try {
const { account } = req.query as { account?: string | string[] };
const tokens = await getWhitelistTokens(account);
const { account } = req.query as { account: string };
const tokens = await getWhitelistTokens(account, cache);
return res.json(tokens);
} catch (error) {
console.error(error);
Expand Down Expand Up @@ -218,7 +218,6 @@ app.get(
account_id,
token_id
);

return res.json(result);
} catch (error) {
console.error("Error fetching all balance history:", error);
Expand Down
4 changes: 3 additions & 1 deletion src/utils/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export const unWrapNear = async ({ amountIn }: { amountIn: string }) => {
amount: toNonDivisibleNumber(24, amountIn),
},
amount: ONE_YOCTO_NEAR,
gas: "30000000000000",
},
],
};
Expand Down Expand Up @@ -392,6 +393,7 @@ export async function getUserStakeBalances(

// Return cached balance if available
if (balanceCache[cacheKey] !== undefined) {
console.log("Cached response for key:", cacheKey);
return balanceCache[cacheKey];
}

Expand All @@ -417,7 +419,7 @@ export async function getUserStakeBalances(
useArchival
);

const balance = response?.result.result
const balance = response?.result?.result
? parseInt(
response.result.result
.map((c: any) => String.fromCharCode(c))
Expand Down
26 changes: 23 additions & 3 deletions src/whitelist-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@ import axios from "axios";
import { BalanceResp } from "./utils/interface";
import { tokens } from "./constants/tokens";

export async function getWhitelistTokens(account?: string | string[]) {
type WhitelistTokensCache = {
get: (key: string) => any;
set: (key: string, value: any, ttl: number) => void;
};

export async function getWhitelistTokens(
account: string,
cache: WhitelistTokensCache
) {
// Fetch prices and balances concurrently
const cacheKey = `${account}-whitelist-tokens`;

const cachedData = cache.get(cacheKey);

if (cachedData) {
console.log(`Cached response for key: ${cacheKey}`);
return cachedData;
}

if (!process.env.PIKESPEAK_KEY) {
throw new Error("PIKESPEAK_KEY is not set");
Expand Down Expand Up @@ -72,12 +88,16 @@ export async function getWhitelistTokens(account?: string | string[]) {
: priceData.price,
symbol: token.symbol,
name: token.name,
icon: token.icon,
icon: id === "near" ? tokens?.[id]?.icon : token.icon,
};
});

// Return sorted tokens based on balance
return simplifiedTokens.sort(

const result = simplifiedTokens.sort(
(a, b) => parseFloat(b.parsedBalance) - parseFloat(a.parsedBalance)
);

cache.set(cacheKey, result, 600);
return result;
}