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
149 changes: 121 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"helmet": "^8.0.0",
"js-sha256": "^0.11.0",
"node-cache": "^5.1.2",
"node-cron": "^4.2.1",
"react": ">=16"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ model RpcRequest {

model FTToken {
id String @id @default(uuid())
account_id String
account_id String @unique
totalCumulativeAmt Float
fts Json
timestamp DateTime @default(now())
Expand Down
44 changes: 24 additions & 20 deletions src/all-token-balance-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,30 +290,34 @@ export async function getAllTokenBalanceHistory(
);

if (prev) {
await prisma.tokenBalanceHistory.update({
where: {
account_id_token_id_period: {
prisma.tokenBalanceHistory
.update({
where: {
account_id_token_id_period: {
account_id,
token_id,
period,
},
},
data: {
balance_history: finalHistory,
toBlock: currentBlock,
},
})
.catch((e) => console.error("DB write failed:", e.message));
} else {
prisma.tokenBalanceHistory
.create({
data: {
account_id,
token_id,
period,
balance_history: finalHistory,
fromBlock: blockHeights[0],
toBlock: currentBlock,
},
},
data: {
balance_history: finalHistory,
toBlock: currentBlock,
},
});
} else {
await prisma.tokenBalanceHistory.create({
data: {
account_id,
token_id,
period,
balance_history: finalHistory,
fromBlock: blockHeights[0],
toBlock: currentBlock,
},
});
})
.catch((e) => console.error("DB write failed:", e.message));
}

return { period, data: finalHistory };
Expand Down
54 changes: 32 additions & 22 deletions src/ft-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,30 @@ export async function getFTTokens(account_id: string, cache: FTCache) {
const nearblocksFts = nearblocksRes?.data?.inventory?.fts || [];
const fastnearFts = fastnearRes?.data?.tokens || [];

const nearblocksMap = new Map((nearblocksFts as FtsToken[]).map((ft) => [ft.contract, ft]));

const mergedFts = await Promise.all(fastnearFts.map(async (ft: any) => {
const meta = nearblocksMap.get(ft.contract_id) as FtsToken | undefined;
if (meta && meta.ft_meta) {
return {
contract: ft.contract_id,
amount: ft.balance, // use FastNear balance
ft_meta: meta.ft_meta,
} as FtsToken;
} else {
// Fetch metadata if not found in Nearblocks
const fetched = await fetchFtMeta(ft.contract_id);
if (fetched) {
fetched.amount = ft.balance;
return fetched;
const nearblocksMap = new Map(
(nearblocksFts as FtsToken[]).map((ft) => [ft.contract, ft])
);

const mergedFts = (await Promise.all(
fastnearFts.map(async (ft: any) => {
const meta = nearblocksMap.get(ft.contract_id) as FtsToken | undefined;
if (meta && meta.ft_meta) {
return {
contract: ft.contract_id,
amount: ft.balance, // use FastNear balance
ft_meta: meta.ft_meta,
} as FtsToken;
} else {
// Fetch metadata if not found in Nearblocks
const fetched = await fetchFtMeta(ft.contract_id);
if (fetched) {
fetched.amount = ft.balance;
return fetched;
}
return null;
}
return null;
}
})) as FtsToken[];
})
)) as FtsToken[];

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

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

// Save to DB
prisma.fTToken
.create({
data: {
.upsert({
where: { account_id },
update: {
totalCumulativeAmt: parseFloat(total.toFixed(2)),
fts: finalFts as any,
timestamp: new Date(),
},
create: {
account_id,
totalCumulativeAmt: parseFloat(total.toFixed(2)),
fts: finalFts as any,
timestamp: new Date(),
},
})
.catch((e) => console.error("DB write failed:", e.message));
Expand All @@ -163,4 +174,3 @@ export async function getFTTokens(account_id: string, cache: FTCache) {
throw new Error("Failed to fetch FT tokens");
}
}

Loading