Skip to content

Commit cb6f126

Browse files
committed
switch to fastnear for token balance in whitelist-tokens
1 parent d3aec3c commit cb6f126

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

src/constants/tokens.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const tokens: Record<string, TokenMetadata> = {
1414
spec: "ft-1.0.0",
1515
name: "NEAR",
1616
symbol: "NEAR",
17-
icon: "https://near.org/_next/static/media/near-icon.2e682d59.svg",
17+
icon: "https://pages.near.org/wp-content/uploads/2023/11/NEAR_token.png",
1818
reference: null,
1919
reference_hash: null,
2020
decimals: 24,

src/ft-tokens.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ export async function getFTTokens(account_id: string, cache: FTCache) {
8282
},
8383
}
8484
),
85-
axios.get(`https://api.fastnear.com/v1/account/${account_id}/full`),
85+
axios.get(`https://api.fastnear.com/v1/account/${account_id}/full`, {
86+
headers: {
87+
Authorization: `Bearer ${process.env.FASTNEAR_API_KEY}`,
88+
},
89+
}),
8690
]);
8791

8892
const nearblocksFts = nearblocksRes?.data?.inventory?.fts || [];

src/utils/interface.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { TokenMetadata } from "./search-token";
22

33
export interface BalanceResp {
4-
amount: number;
5-
contract: string;
6-
symbol: string;
4+
balance: number;
5+
contract_id: string;
76
}
87

98
export interface Token {

src/whitelist-tokens.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,11 @@ export async function getWhitelistTokens(
2222
return cachedData;
2323
}
2424

25-
if (!process.env.PIKESPEAK_KEY) {
26-
throw new Error("PIKESPEAK_KEY is not set");
27-
}
28-
2925
const fetchBalancesPromise = account
3026
? axios
31-
.get(`https://api.pikespeak.ai/account/balance/${account}`, {
27+
.get(`https://api.fastnear.com/v1/account/${account}/full`, {
3228
headers: {
33-
"Content-Type": "application/json",
34-
"x-api-key": process.env.PIKESPEAK_KEY || "",
29+
Authorization: `Bearer ${process.env.FASTNEAR_API_KEY}`,
3530
},
3631
})
3732
.then((res) => res.data)
@@ -59,22 +54,23 @@ export async function getWhitelistTokens(
5954
Promise.all(fetchTokenPricePromises),
6055
]);
6156

62-
const filteredBalances = userBalances.filter(
63-
(i: any) => i.symbol !== "NEAR [Storage]"
64-
);
65-
6657
// Map over tokens to include only the required fields
6758
const simplifiedTokens = Object.keys(tokens).map((id, index) => {
6859
const token = tokens[id];
6960
const priceData = tokenPrices[index];
7061

71-
const parsedBalance =
72-
filteredBalances
73-
.find((i: BalanceResp) => i.contract.toLowerCase() === id)
74-
?.amount.toString() || "0";
75-
76-
const balance = Big(parsedBalance)
77-
.mul(Big(10).pow(token.decimals))
62+
let balance = "0";
63+
64+
if (id === "near") {
65+
balance = userBalances.state.balance;
66+
} else {
67+
balance =
68+
userBalances.tokens
69+
.find((i: BalanceResp) => i.contract_id.toLowerCase() === id)
70+
?.balance?.toString() || "0";
71+
}
72+
const parsedBalance = Big(balance)
73+
.div(Big(10).pow(token.decimals))
7874
.toFixed(4);
7975

8076
return {

0 commit comments

Comments
 (0)