Skip to content

Commit 711cdc2

Browse files
committed
Update caching functions to accept environment as a parameter. Implement better error handling for getNodeLicenseDetails.
1 parent 0440ef6 commit 711cdc2

File tree

6 files changed

+74
-35
lines changed

6 files changed

+74
-35
lines changed

app/accounts/page.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getServerConfig } from '@/config/serverConfig';
12
import { getSSURL } from '@/lib/actions';
23
import * as types from '@/typedefs/blockchain';
34
import { LicenseItem } from '@/typedefs/general';
@@ -16,8 +17,8 @@ export async function generateMetadata() {
1617
};
1718
}
1819

19-
const fetchCachedLicenseHolders = cache(async () => {
20-
const url = await getSSURL('license-holders');
20+
const fetchCachedLicenseHolders = cache(async (environment: 'mainnet' | 'testnet' | 'devnet') => {
21+
const url = await getSSURL(`license-holders?env=${environment}`);
2122

2223
const res = await fetch(url, {
2324
next: { revalidate: 3600 }, // Cache for 1 hour
@@ -44,6 +45,8 @@ export default async function AccountsPage(props: {
4445
const searchParams = await props.searchParams;
4546
const currentPage = Number(searchParams?.page) || 1;
4647

48+
const { config } = await getServerConfig();
49+
4750
let ndHolders: {
4851
ethAddress: types.EthAddress;
4952
licenseId: number;
@@ -63,7 +66,7 @@ export default async function AccountsPage(props: {
6366
}[];
6467

6568
try {
66-
({ ndHolders, mndHolders } = await fetchCachedLicenseHolders());
69+
({ ndHolders, mndHolders } = await fetchCachedLicenseHolders(config.environment));
6770

6871
ndHolders.forEach((holder) => {
6972
if (!holders[holder.ethAddress]) {

app/license/[licenseType]/[licenseId]/page.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import LicensePageNodeCardWrapper from '@/app/server-components/LicensePage/LicensePageNodeCardWrapper';
22
import LicensePageNodePerformanceCardWrapper from '@/app/server-components/LicensePage/LicensePageNodePerformanceCardWrapper';
33
import LicenseCard from '@/app/server-components/main-cards/LicenseCard';
4+
import { getServerConfig } from '@/config/serverConfig';
45
import { getNodeAvailability } from '@/lib/actions';
56
import { getLicense } from '@/lib/api/blockchain';
67
import { isEmptyETHAddr } from '@/lib/utils';
@@ -11,6 +12,7 @@ import { cache, Suspense } from 'react';
1112

1213
export async function generateMetadata({ params }) {
1314
const { licenseType, licenseId } = await params;
15+
const { config } = await getServerConfig();
1416

1517
if (!licenseType || !['ND', 'MND', 'GND'].includes(licenseType)) {
1618
return {
@@ -33,7 +35,7 @@ export async function generateMetadata({ params }) {
3335
}
3436

3537
try {
36-
await cachedGetLicense(licenseType, licenseId);
38+
await cachedGetLicense(licenseType, licenseId, config.environment);
3739
} catch (error) {
3840
return {
3941
title: 'Error',
@@ -51,12 +53,15 @@ export async function generateMetadata({ params }) {
5153
};
5254
}
5355

54-
const cachedGetLicense = cache(async (licenseType: 'ND' | 'MND' | 'GND', licenseId: string) => {
55-
return await getLicense(licenseType, licenseId);
56-
});
56+
const cachedGetLicense = cache(
57+
async (licenseType: 'ND' | 'MND' | 'GND', licenseId: string, _environment: 'mainnet' | 'testnet' | 'devnet') => {
58+
return await getLicense(licenseType, licenseId);
59+
},
60+
);
5761

5862
export default async function LicensePage({ params }) {
5963
const { licenseType, licenseId } = await params;
64+
const { config } = await getServerConfig();
6065

6166
if (!licenseType || !['ND', 'MND', 'GND'].includes(licenseType)) {
6267
console.log(`[License Page] Invalid license type: ${licenseType}`);
@@ -73,7 +78,7 @@ export default async function LicensePage({ params }) {
7378
let license: types.License;
7479

7580
try {
76-
license = await cachedGetLicense(licenseType, licenseId);
81+
license = await cachedGetLicense(licenseType, licenseId, config.environment);
7782
} catch (error) {
7883
console.error(error);
7984
console.log(`[License Page] Failed to fetch license: ${licenseType}-${licenseId}`);

app/node/[nodeEthAddr]/page.tsx

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import LicenseCard from '@/app/server-components/main-cards/LicenseCard';
22
import NodeCard from '@/app/server-components/main-cards/NodeCard';
33
import NodePerformanceCard from '@/app/server-components/main-cards/NodePerformanceCard';
44
import { DetailedAlert } from '@/app/server-components/shared/DetailedAlert';
5+
import { getServerConfig } from '@/config/serverConfig';
56
import { getNodeAvailability } from '@/lib/actions';
67
import { getNodeLicenseDetails } from '@/lib/api/blockchain';
78
import { isEmptyETHAddr } from '@/lib/utils';
@@ -13,6 +14,7 @@ import { isAddress } from 'viem';
1314

1415
export async function generateMetadata({ params }) {
1516
const { nodeEthAddr } = await params;
17+
const { config } = await getServerConfig();
1618

1719
if (!nodeEthAddr || !isAddress(nodeEthAddr) || isEmptyETHAddr(nodeEthAddr)) {
1820
console.log(`[Node Page] Invalid node address: ${nodeEthAddr}`);
@@ -22,7 +24,7 @@ export async function generateMetadata({ params }) {
2224
let nodeResponse: types.OraclesAvailabilityResult & types.OraclesDefaultResult;
2325

2426
try {
25-
({ nodeResponse } = await getCachedLicenseDetailsAndNodeAvailability(nodeEthAddr));
27+
({ nodeResponse } = await getCachedLicenseDetailsAndNodeAvailability(nodeEthAddr, config.environment));
2628

2729
if (!nodeResponse) {
2830
console.log(`[Node Page] No node response found for address: ${nodeEthAddr}`);
@@ -49,25 +51,42 @@ export async function generateMetadata({ params }) {
4951
const getCachedLicenseDetailsAndNodeAvailability = cache(
5052
async (
5153
nodeEthAddr: types.EthAddress,
54+
_environment: 'mainnet' | 'testnet' | 'devnet',
5255
): Promise<{
5356
license: types.License;
5457
licenseId: bigint;
5558
licenseType: 'ND' | 'MND' | 'GND';
5659
owner: types.EthAddress;
5760
nodeResponse: types.OraclesAvailabilityResult & types.OraclesDefaultResult;
5861
}> => {
59-
const {
60-
nodeAddress,
61-
totalAssignedAmount,
62-
totalClaimedAmount,
63-
lastClaimEpoch,
64-
assignTimestamp,
65-
lastClaimOracle,
66-
isBanned,
67-
licenseId,
68-
licenseType,
69-
owner,
70-
} = await getNodeLicenseDetails(nodeEthAddr);
62+
let nodeAddress: types.EthAddress,
63+
totalAssignedAmount: bigint,
64+
totalClaimedAmount: bigint,
65+
lastClaimEpoch: bigint,
66+
assignTimestamp: bigint,
67+
lastClaimOracle: types.EthAddress,
68+
isBanned: boolean,
69+
licenseId: bigint,
70+
licenseType: 'ND' | 'MND' | 'GND' | undefined,
71+
owner: types.EthAddress;
72+
73+
try {
74+
({
75+
nodeAddress,
76+
totalAssignedAmount,
77+
totalClaimedAmount,
78+
lastClaimEpoch,
79+
assignTimestamp,
80+
lastClaimOracle,
81+
isBanned,
82+
licenseId,
83+
licenseType,
84+
owner,
85+
} = await getNodeLicenseDetails(nodeEthAddr));
86+
} catch (error) {
87+
console.error(error);
88+
throw new Error('Failed to get node license details.');
89+
}
7190

7291
if (!licenseId || !licenseType) {
7392
throw new Error('License not found.');
@@ -98,6 +117,7 @@ const getCachedLicenseDetailsAndNodeAvailability = cache(
98117

99118
export default async function NodePage({ params }) {
100119
const { nodeEthAddr } = await params;
120+
const { config } = await getServerConfig();
101121

102122
if (!nodeEthAddr || !isAddress(nodeEthAddr) || isEmptyETHAddr(nodeEthAddr)) {
103123
console.log(`[Node Page] Invalid node address in page component: ${nodeEthAddr}`);
@@ -111,8 +131,10 @@ export default async function NodePage({ params }) {
111131
let nodeResponse: types.OraclesAvailabilityResult & types.OraclesDefaultResult;
112132

113133
try {
114-
({ license, licenseId, licenseType, owner, nodeResponse } =
115-
await getCachedLicenseDetailsAndNodeAvailability(nodeEthAddr));
134+
({ license, licenseId, licenseType, owner, nodeResponse } = await getCachedLicenseDetailsAndNodeAvailability(
135+
nodeEthAddr,
136+
config.environment,
137+
));
116138
} catch (error: any) {
117139
console.error(error);
118140

app/server-components/Nodes/Node.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ import { BorderedCard } from '../shared/cards/BorderedCard';
1111
import LicenseSmallCard from '../shared/Licenses/LicenseSmallCard';
1212

1313
export default async function Node({ ratio1Addr, node }: { ratio1Addr: R1Address; node: NodeState }) {
14-
const { licenseId, licenseType, owner, totalAssignedAmount, totalClaimedAmount, isBanned } = await getNodeLicenseDetails(
15-
node.eth_addr,
16-
);
14+
let licenseId: bigint | undefined,
15+
licenseType: 'ND' | 'MND' | 'GND' | undefined,
16+
owner: string | undefined,
17+
totalAssignedAmount: bigint | undefined,
18+
totalClaimedAmount: bigint | undefined,
19+
isBanned: boolean | undefined;
1720

18-
// The node will soon be disconnected by dAuth
19-
if (!licenseId) {
21+
try {
22+
({ licenseId, licenseType, owner, totalAssignedAmount, totalClaimedAmount, isBanned } = await getNodeLicenseDetails(
23+
node.eth_addr,
24+
));
25+
} catch (error) {
26+
console.error(error);
2027
return null;
2128
}
2229

app/server-components/R1MintedLastEpoch.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { getNextEpochTimestamp } from '@/config';
1+
import { Config, getNextEpochTimestamp } from '@/config';
22
import { getServerConfig } from '@/config/serverConfig';
33
import { getSSURL } from '@/lib/actions';
44
import { differenceInSeconds } from 'date-fns';
5+
import { cache } from 'react';
56
import { formatUnits } from 'viem';
67

7-
const fetchCachedR1MintedLastEpoch = async () => {
8-
const url = await getSSURL('r1-minted-last-epoch');
9-
const { config } = await getServerConfig();
8+
const fetchCachedR1MintedLastEpoch = cache(async (config: Config) => {
9+
const url = await getSSURL(`r1-minted-last-epoch?env=${config.environment}`);
1010

1111
const res = await fetch(url, {
1212
next: { revalidate: differenceInSeconds(getNextEpochTimestamp(config), new Date()) + 1 },
@@ -17,13 +17,15 @@ const fetchCachedR1MintedLastEpoch = async () => {
1717
} = await res.json();
1818

1919
return data.value;
20-
};
20+
});
2121

2222
export default async function R1MintedLastEpoch() {
23+
const { config } = await getServerConfig();
24+
2325
let value: string | undefined;
2426

2527
try {
26-
value = await fetchCachedR1MintedLastEpoch();
28+
value = await fetchCachedR1MintedLastEpoch(config);
2729
} catch (error) {
2830
console.error(error);
2931
return <div className="text-lg md:text-xl"></div>;

config/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,5 @@ export const getEnvironment = (hostname: string | null): 'mainnet' | 'testnet' |
134134
? ('testnet' as const)
135135
: hostname === domains.devnet
136136
? ('devnet' as const)
137-
: ('devnet' as const);
137+
: ('mainnet' as const);
138138
};

0 commit comments

Comments
 (0)