Skip to content

Commit a8e950b

Browse files
authored
Merge pull request #19 from NEAR-DevHub/fixes
Improvements with db space
2 parents 7565c4d + 0dce3a6 commit a8e950b

File tree

9 files changed

+249
-116
lines changed

9 files changed

+249
-116
lines changed

package-lock.json

Lines changed: 121 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"helmet": "^8.0.0",
3030
"js-sha256": "^0.11.0",
3131
"node-cache": "^5.1.2",
32+
"node-cron": "^4.2.1",
3233
"react": ">=16"
3334
},
3435
"devDependencies": {

prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ model RpcRequest {
4141

4242
model FTToken {
4343
id String @id @default(uuid())
44-
account_id String
44+
account_id String @unique
4545
totalCumulativeAmt Float
4646
fts Json
4747
timestamp DateTime @default(now())

src/all-token-balance-history.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -290,30 +290,34 @@ export async function getAllTokenBalanceHistory(
290290
);
291291

292292
if (prev) {
293-
await prisma.tokenBalanceHistory.update({
294-
where: {
295-
account_id_token_id_period: {
293+
prisma.tokenBalanceHistory
294+
.update({
295+
where: {
296+
account_id_token_id_period: {
297+
account_id,
298+
token_id,
299+
period,
300+
},
301+
},
302+
data: {
303+
balance_history: finalHistory,
304+
toBlock: currentBlock,
305+
},
306+
})
307+
.catch((e) => console.error("DB write failed:", e.message));
308+
} else {
309+
prisma.tokenBalanceHistory
310+
.create({
311+
data: {
296312
account_id,
297313
token_id,
298314
period,
315+
balance_history: finalHistory,
316+
fromBlock: blockHeights[0],
317+
toBlock: currentBlock,
299318
},
300-
},
301-
data: {
302-
balance_history: finalHistory,
303-
toBlock: currentBlock,
304-
},
305-
});
306-
} else {
307-
await prisma.tokenBalanceHistory.create({
308-
data: {
309-
account_id,
310-
token_id,
311-
period,
312-
balance_history: finalHistory,
313-
fromBlock: blockHeights[0],
314-
toBlock: currentBlock,
315-
},
316-
});
319+
})
320+
.catch((e) => console.error("DB write failed:", e.message));
317321
}
318322

319323
return { period, data: finalHistory };

src/ft-tokens.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,30 @@ export async function getFTTokens(account_id: string, cache: FTCache) {
8888
const nearblocksFts = nearblocksRes?.data?.inventory?.fts || [];
8989
const fastnearFts = fastnearRes?.data?.tokens || [];
9090

91-
const nearblocksMap = new Map((nearblocksFts as FtsToken[]).map((ft) => [ft.contract, ft]));
92-
93-
const mergedFts = await Promise.all(fastnearFts.map(async (ft: any) => {
94-
const meta = nearblocksMap.get(ft.contract_id) as FtsToken | undefined;
95-
if (meta && meta.ft_meta) {
96-
return {
97-
contract: ft.contract_id,
98-
amount: ft.balance, // use FastNear balance
99-
ft_meta: meta.ft_meta,
100-
} as FtsToken;
101-
} else {
102-
// Fetch metadata if not found in Nearblocks
103-
const fetched = await fetchFtMeta(ft.contract_id);
104-
if (fetched) {
105-
fetched.amount = ft.balance;
106-
return fetched;
91+
const nearblocksMap = new Map(
92+
(nearblocksFts as FtsToken[]).map((ft) => [ft.contract, ft])
93+
);
94+
95+
const mergedFts = (await Promise.all(
96+
fastnearFts.map(async (ft: any) => {
97+
const meta = nearblocksMap.get(ft.contract_id) as FtsToken | undefined;
98+
if (meta && meta.ft_meta) {
99+
return {
100+
contract: ft.contract_id,
101+
amount: ft.balance, // use FastNear balance
102+
ft_meta: meta.ft_meta,
103+
} as FtsToken;
104+
} else {
105+
// Fetch metadata if not found in Nearblocks
106+
const fetched = await fetchFtMeta(ft.contract_id);
107+
if (fetched) {
108+
fetched.amount = ft.balance;
109+
return fetched;
110+
}
111+
return null;
107112
}
108-
return null;
109-
}
110-
})) as FtsToken[];
113+
})
114+
)) as FtsToken[];
111115

112116
const updatedFts = mergedFts.filter(Boolean) as FtsToken[];
113117

@@ -142,11 +146,18 @@ export async function getFTTokens(account_id: string, cache: FTCache) {
142146

143147
// Save to DB
144148
prisma.fTToken
145-
.create({
146-
data: {
149+
.upsert({
150+
where: { account_id },
151+
update: {
152+
totalCumulativeAmt: parseFloat(total.toFixed(2)),
153+
fts: finalFts as any,
154+
timestamp: new Date(),
155+
},
156+
create: {
147157
account_id,
148158
totalCumulativeAmt: parseFloat(total.toFixed(2)),
149159
fts: finalFts as any,
160+
timestamp: new Date(),
150161
},
151162
})
152163
.catch((e) => console.error("DB write failed:", e.message));
@@ -163,4 +174,3 @@ export async function getFTTokens(account_id: string, cache: FTCache) {
163174
throw new Error("Failed to fetch FT tokens");
164175
}
165176
}
166-

0 commit comments

Comments
 (0)