Skip to content

Commit 1514a45

Browse files
committed
requested changes
1 parent 96bcb66 commit 1514a45

File tree

8 files changed

+105
-110
lines changed

8 files changed

+105
-110
lines changed

components/EntropyApiDataFetcher.tsx

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import { useEffect, useState } from "react";
22
import * as chains from "viem-chains/chains";
33
import { EntropyDeploymentsConfig } from "./EntropyDeploymentsConfig";
4+
import { z } from "zod";
45

5-
interface ApiChainConfig {
6-
name: string;
7-
network_id: number;
8-
contract_addr: string;
9-
reveal_delay_blocks: number;
10-
gas_limit: number;
11-
default_fee: number;
12-
}
6+
const ApiChainConfigSchema = z.object({
7+
name: z.string(),
8+
network_id: z.number(),
9+
contract_addr: z.string(),
10+
reveal_delay_blocks: z.number(),
11+
gas_limit: z.number(),
12+
default_fee: z.number(),
13+
});
14+
15+
type ApiChainConfig = z.infer<typeof ApiChainConfigSchema>;
16+
17+
const entropyDeploymentsSchema = z.array(ApiChainConfigSchema);
1318

1419
export interface EntropyDeployment {
1520
address: string;
@@ -21,71 +26,58 @@ export interface EntropyDeployment {
2126
nativeCurrency?: string;
2227
}
2328

29+
const getChainData = (network_id: number) => {
30+
return Object.values(chains).find((chain) => chain.id === network_id);
31+
};
32+
33+
const transformChainData = (chain: ApiChainConfig): [string, EntropyDeployment] => {
34+
const chainData = getChainData(chain.network_id);
35+
const configOverride = EntropyDeploymentsConfig[chain.network_id];
36+
37+
const deployment: EntropyDeployment = {
38+
address: chain.contract_addr,
39+
delay: `${chain.reveal_delay_blocks} block${
40+
chain.reveal_delay_blocks !== 1 ? "s" : ""
41+
}`,
42+
gasLimit: chain.gas_limit.toLocaleString(),
43+
default_fee: chain.default_fee,
44+
// Use config override first, then viem data, then undefined
45+
rpc: configOverride?.rpc ?? chainData?.rpcUrls?.default?.http?.[0],
46+
explorer:
47+
configOverride?.explorer ?? chainData?.blockExplorers?.default?.url,
48+
nativeCurrency:
49+
configOverride?.nativeCurrency ?? chainData?.nativeCurrency?.symbol,
50+
};
51+
52+
return [chain.name, deployment];
53+
};
54+
2455
const fetchEntropyDeployments = async (
2556
url: string
26-
): Promise<ApiChainConfig[]> => {
57+
): Promise<Record<string, EntropyDeployment>> => {
2758
try {
2859
const response = await fetch(url);
29-
const data = await response.json();
30-
return data as ApiChainConfig[];
60+
const apiData = entropyDeploymentsSchema.parse(await response.json());
61+
62+
return Object.fromEntries(apiData.map(transformChainData));
3163
} catch (error) {
3264
console.error("Error fetching entropy deployments:", error);
33-
return [];
65+
return {};
3466
}
3567
};
3668

37-
export const getChainData = (network_id: number) => {
38-
for (const chain of Object.values(chains)) {
39-
if (chain.id === network_id) {
40-
return chain;
41-
}
42-
}
43-
return null;
44-
};
45-
46-
interface EntropyApiDataFetcherProps {
47-
url: string;
48-
children: (
49-
data: Record<string, EntropyDeployment>,
50-
isLoading: boolean,
51-
error: string | null
52-
) => React.ReactNode;
53-
}
54-
55-
export function EntropyApiDataFetcher({
56-
url,
57-
children,
58-
}: EntropyApiDataFetcherProps) {
69+
export function useEntropyApiData(url: string) {
5970
const [data, setData] = useState<Record<string, EntropyDeployment>>({});
6071
const [isLoading, setIsLoading] = useState(true);
6172
const [error, setError] = useState<string | null>(null);
6273

6374
useEffect(() => {
64-
fetchEntropyDeployments(url)
65-
.then((apiData: ApiChainConfig[]) => {
66-
const transformedData = apiData.reduce((acc, chain) => {
67-
const chainData = getChainData(chain.network_id);
68-
const configOverride = EntropyDeploymentsConfig[chain.network_id];
69-
70-
acc[chain.name] = {
71-
address: chain.contract_addr,
72-
delay: `${chain.reveal_delay_blocks} block${
73-
chain.reveal_delay_blocks !== 1 ? "s" : ""
74-
}`,
75-
gasLimit: chain.gas_limit.toLocaleString(),
76-
default_fee: chain.default_fee,
77-
// Use config override first, then viem data, then undefined
78-
rpc: configOverride?.rpc || chainData?.rpcUrls?.default?.http?.[0],
79-
explorer:
80-
configOverride?.explorer ||
81-
chainData?.blockExplorers?.default?.url,
82-
nativeCurrency:
83-
configOverride?.nativeCurrency ||
84-
chainData?.nativeCurrency?.symbol,
85-
};
86-
return acc;
87-
}, {} as Record<string, EntropyDeployment>);
75+
setData({});
76+
setIsLoading(true);
77+
setError(null);
8878

79+
fetchEntropyDeployments(url)
80+
.then((transformedData) => {
8981
setData(transformedData);
9082
setIsLoading(false);
9183
})
@@ -95,5 +87,5 @@ export function EntropyApiDataFetcher({
9587
});
9688
}, [url]);
9789

98-
return <>{children(data, isLoading, error)}</>;
90+
return { data, isLoading, error };
9991
}

components/EntropyDeployments.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useEntropyApiData } from "./EntropyApiDataFetcher";
2+
import EntropyDeploymentTable from "./EntropyDeploymentTable";
3+
4+
interface EntropyDeploymentsProps {
5+
networkName: string;
6+
url: string;
7+
showReveal?: boolean;
8+
}
9+
10+
export function EntropyDeployments({ networkName, url, showReveal = false }: EntropyDeploymentsProps) {
11+
const { isLoading, error, data } = useEntropyApiData(url);
12+
13+
if (isLoading) {
14+
return <p>Loading {networkName} data...</p>;
15+
}
16+
17+
if (error) {
18+
return <p>Error loading {networkName} data: {error}</p>;
19+
}
20+
21+
return <EntropyDeploymentTable deployments={data} showReveal={showReveal} />;
22+
}

components/EntropyFeeTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const FeeTable = ({
7171
"Loading..."
7272
) : (
7373
<>
74-
{fees[name]} <b>{deployment.nativeCurrency || "ETH"}</b>
74+
{fees[name]} <b>{deployment.nativeCurrency ?? "ETH"}</b>
7575
</>
7676
)}
7777
</StyledTd>

components/EntropyFees.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useEntropyApiData } from "./EntropyApiDataFetcher";
2+
import EntropyFeeTable from "./EntropyFeeTable";
3+
4+
interface EntropyFeesProps {
5+
networkName: string;
6+
url: string;
7+
}
8+
9+
export function EntropyFees({ networkName, url }: EntropyFeesProps) {
10+
const { isLoading, error, data } = useEntropyApiData(url);
11+
12+
if (isLoading) {
13+
return <p>Loading {networkName} fees...</p>;
14+
}
15+
16+
if (error) {
17+
return <p>Error loading {networkName} fees: {error}</p>;
18+
}
19+
20+
return <EntropyFeeTable deployments={data} />;
21+
}

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"toml": "^3.0.0",
3939
"viem": "^0.3.50",
4040
"viem-chains": "npm:viem@^2.32.1",
41-
"wagmi": "^1.2.0"
41+
"wagmi": "^1.2.0",
42+
"zod": "^3.25.76"
4243
},
4344
"devDependencies": {
4445
"@pythnetwork/pyth-sdk-solidity": "^2.3.0",

pages/entropy/contract-addresses.mdx

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
import { EntropyApiDataFetcher } from "../../components/EntropyApiDataFetcher";
2-
import EntropyDeploymentTable from "../../components/EntropyDeploymentTable";
1+
import EntropyDeployments from "../../components/EntropyDeployments";
32
import { FORTUNA_API_URLS } from "../../components/constants";
43

54
# Entropy Contract Addresses on EVM
65

76
## Mainnets
87

9-
<EntropyApiDataFetcher url={FORTUNA_API_URLS.MAINNET}>
10-
{(data, isLoading, error) =>
11-
isLoading ? (
12-
<p>Loading mainnet data...</p>
13-
) : error ? (
14-
<p>Error loading mainnet data: {error}</p>
15-
) : (
16-
<EntropyDeploymentTable deployments={data} showReveal={true} />
17-
)
18-
}
19-
</EntropyApiDataFetcher>
8+
<EntropyDeployments networkName="mainnet" url={FORTUNA_API_URLS.MAINNET} showReveal={true} />
209

2110
**The default provider for above mainnet chains is `0x52DeaA1c84233F7bb8C8A45baeDE41091c616506`.**
2211

@@ -27,17 +16,7 @@ The default provider fulfills the request by sending a transaction with a gas li
2716

2817
## Testnets
2918

30-
<EntropyApiDataFetcher url={FORTUNA_API_URLS.TESTNET}>
31-
{(data, isLoading, error) =>
32-
isLoading ? (
33-
<p>Loading testnet data...</p>
34-
) : error ? (
35-
<p>Error loading testnet data: {error}</p>
36-
) : (
37-
<EntropyDeploymentTable deployments={data} showReveal={true} />
38-
)
39-
}
40-
</EntropyApiDataFetcher>
19+
<EntropyDeployments networkName="testnet" url={FORTUNA_API_URLS.TESTNET} showReveal={true} />
4120

4221
**The default provider for above testnet chains is `0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344`.**
4322

pages/entropy/current-fees.mdx

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import EntropyFeeTable from "../../components/EntropyFeeTable";
2-
import { EntropyApiDataFetcher } from "../../components/EntropyApiDataFetcher";
1+
import EntropyFees from "../../components/EntropyFees";
32
import { FORTUNA_API_URLS } from "../../components/constants";
43
import { Callout } from "nextra/components";
54

@@ -12,17 +11,7 @@ Note that the fees shown below will vary over time with prevailing gas prices on
1211
The fees for mainnet are dynamically set. Always use the on-chain method `entropy.getFeeV2(){:solidity}` to get the current fee.
1312
</Callout>
1413

15-
<EntropyApiDataFetcher url={FORTUNA_API_URLS.MAINNET}>
16-
{(data, isLoading, error) =>
17-
isLoading ? (
18-
<p>Loading mainnet fees...</p>
19-
) : error ? (
20-
<p>Error loading mainnet fees: {error}</p>
21-
) : (
22-
<EntropyFeeTable deployments={data} />
23-
)
24-
}
25-
</EntropyApiDataFetcher>
14+
<EntropyFees networkName="mainnet" url={FORTUNA_API_URLS.MAINNET} />
2615

2716
## Testnet
2817

@@ -31,14 +20,4 @@ Note that the fees shown below will vary over time with prevailing gas prices on
3120
fees.
3221
</Callout>
3322

34-
<EntropyApiDataFetcher url={FORTUNA_API_URLS.TESTNET}>
35-
{(data, isLoading, error) =>
36-
isLoading ? (
37-
<p>Loading testnet fees...</p>
38-
) : error ? (
39-
<p>Error loading testnet fees: {error}</p>
40-
) : (
41-
<EntropyFeeTable deployments={data} />
42-
)
43-
}
44-
</EntropyApiDataFetcher>
23+
<EntropyFees networkName="testnet" url={FORTUNA_API_URLS.TESTNET} />

0 commit comments

Comments
 (0)