Skip to content

Commit 415b15d

Browse files
committed
feat: only use fastnear, ft_metadata
1 parent 6dd16f1 commit 415b15d

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

src/all-token-balance-history.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,38 @@ export async function getAllTokenBalanceHistory(
3131
return cachedData;
3232
}
3333

34+
const token = tokens[token_id as keyof typeof tokens];
35+
let decimals = token?.decimals || 24;
36+
37+
try {
38+
if (!token?.decimals) {
39+
console.log(`Fetching token details for ${token_id}`);
40+
const tokenDetails = await fetchFromRPC({
41+
jsonrpc: "2.0",
42+
id: "dontcare",
43+
method: "query",
44+
params: {
45+
request_type: "call_function",
46+
account_id: token_id,
47+
"finality": "final",
48+
method_name: "ft_metadata",
49+
args_base64: btoa(JSON.stringify({})),
50+
},
51+
}, false, false);
52+
53+
const decodedResult = tokenDetails.result.result
54+
.map((c: number) => String.fromCharCode(c))
55+
.join("");
56+
57+
const decodedResultObject = JSON.parse(decodedResult);
58+
decimals = parseInt(decodedResultObject.decimals, 10);
59+
60+
console.log(`Decimals: ${decimals}`);
61+
}
62+
} catch (error) {
63+
console.log(`Error fetching token details for ${token_id}: ${error}`);
64+
}
65+
3466
try {
3567
const blockData = await fetchFromRPC({
3668
jsonrpc: "2.0",
@@ -128,12 +160,12 @@ export async function getAllTokenBalanceHistory(
128160
balance = String.fromCharCode(...balanceData.result.result);
129161
balance = balance ? balance.replace(/"/g, "") : balanceMinimum;
130162
}
131-
163+
132164
return {
133165
timestamp,
134166
date: formatDate(timestamp, value),
135167
balance: balance
136-
? convertFTBalance(balance, tokens[token_id as keyof typeof tokens].decimals)
168+
? convertFTBalance(balance, decimals)
137169
: balanceMinimum,
138170
};
139171
});

src/constants/tokens.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ export const tokens: Record<string, TokenMetadata> = {
3131
"decimals": 24,
3232
"id": "wrap.near"
3333
},
34+
"itlx_2.intellex_agents_owner_1.near": {
35+
"id": "itlx_2.intellex_agents_owner_1.near",
36+
"decimals": 24,
37+
"icon": "data:image/svg+xml,%3Csvg version='1.0' xmlns='http://www.w3.org/2000/svg' width='721.000000pt' height='399.000000pt' viewBox='0 0 721.000000 399.000000' preserveAspectRatio='xMidYMid meet'%3E%3Cg transform='translate(0.000000,399.000000) scale(0.100000,-0.100000)' fill='%23000000' stroke='none'%3E%3Cpath d='M0 1995 l0 -1995 3605 0 3605 0 0 1995 0 1995 -3605 0 -3605 0 0 -1995z m2888 1200 c110 -22 190 -64 252 -132 183 -200 178 -507 -15 -830 -75 -126 -101 -152 -50 -49 163 327 192 597 83 769 -58 91 -160 160 -277 187 -81 19 -231 15 -351 -10 -134 -27 -260 -74 -438 -161 l-143 -71 46 -50 c57 -63 109 -151 137 -231 32 -89 32 -263 1 -362 -70 -221 -249 -381 -473 -421 -129 -23 -268 -7 -325 38 -34 27 -65 92 -65 138 0 83 188 426 362 660 l33 45 -64 -50 c-342 -266 -660 -644 -817 -970 -168 -350 -171 -585 -9 -734 65 -59 135 -87 243 -100 307 -34 733 104 1261 408 60 34 45 14 -42 -57 -438 -358 -1180 -536 -1521 -365 -69 34 -140 111 -167 181 -34 85 -32 269 4 405 66 249 202 520 394 786 9 12 8 31 -3 81 -18 85 -17 229 1 309 38 159 150 298 298 370 178 87 378 93 570 16 l68 -28 97 46 c345 161 680 228 910 182z'/%3E%3C/g%3E%3C/svg%3E",
38+
"name": "Intellex AI Protocol Token (TESTING)",
39+
"reference": "https://raw.githubusercontent.com/brainstems/itlx_nep141_token/refs/heads/master/metadata.json",
40+
"reference_hash": "K29udivYwweOUnCZPFt/KhcMmm0DQLvzYoVdKXN41P8=",
41+
"spec": "ft-1.0.0",
42+
"symbol": "ITLX2"
43+
},
3444
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.factory.bridge.near": {
3545
"spec": "ft-1.0.0",
3646
"name": "Bridged USDC",

src/utils/fetch-from-rpc.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@ import NodeCache from "node-cache";
66

77
const RPC_ENDPOINTS = [
88
"https://rpc.mainnet.fastnear.com/",
9-
"https://rpc.mainnet.near.org",
10-
"https://free.rpc.fastnear.com",
11-
"https://near.lava.build",
9+
// "https://rpc.mainnet.near.org",
10+
// "https://free.rpc.fastnear.com",
11+
// "https://near.lava.build",
1212
];
1313

1414
const ARCHIVAL_RPC_ENDPOINTS = [
15-
"https://archival-rpc.mainnet.near.org",
16-
"https://archival-rpc.mainnet.pagoda.co",
1715
"https://archival-rpc.mainnet.fastnear.com",
18-
"https://rpc.mainnet.near.org",
16+
// "https://archival-rpc.mainnet.near.org",
17+
// "https://archival-rpc.mainnet.pagoda.co",
18+
// "https://rpc.mainnet.near.org",
1919
];
2020

21-
const CACHE_EXPIRATION = 1000 * 10; // 10 seconds
21+
const CACHE_EXPIRATION = 10; // 10 seconds
2222
const cache = new NodeCache({
2323
stdTTL: CACHE_EXPIRATION,
2424
checkperiod: CACHE_EXPIRATION / 2,
2525
});
2626

2727
export async function fetchFromRPC(body: any, disableCache: boolean = false, archival: boolean = false): Promise<any> {
2828
const requestHash = crypto.createHash('sha256').update(JSON.stringify(body)).digest('hex')
29-
29+
3030
// Extract account ID and block height from the request body if present
3131
let accountId: string | undefined;
3232
let blockHeight: number | undefined;
@@ -144,6 +144,8 @@ export async function fetchFromRPC(body: any, disableCache: boolean = false, arc
144144
exists: false
145145
}
146146
});
147+
148+
return 0;
147149
}
148150

149151
if (error.response?.status === 429) {

0 commit comments

Comments
 (0)