Skip to content

Commit 2f803cc

Browse files
committed
update
1 parent d05ca9b commit 2f803cc

File tree

6 files changed

+119
-79
lines changed

6 files changed

+119
-79
lines changed

apps/api-reference/src/components/EvmApi/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Label,
1111
} from "@headlessui/react";
1212
import { ArrowPathIcon } from "@heroicons/react/24/outline";
13+
import { getEvmContractAddress } from "@pythnetwork/contract-manager/utils/utils";
1314
import PythAbi from "@pythnetwork/pyth-sdk-solidity/abis/IPyth.json";
1415
import PythErrorsAbi from "@pythnetwork/pyth-sdk-solidity/abis/PythErrors.json";
1516
import { ChainIcon } from "connectkit";
@@ -29,7 +30,6 @@ import { ParameterInput } from "./parameter-input";
2930
import type { EvmApiType } from "./run-button";
3031
import { RunButton } from "./run-button";
3132
import { getLogger } from "../../browser-logger";
32-
import { getContractAddress } from "../../evm-networks";
3333
import { useIsMounted } from "../../use-is-mounted";
3434
import type { SupportedLanguage } from "../Code";
3535
import { Code } from "../Code";
@@ -251,7 +251,7 @@ export const EvmApi = <ParameterName extends string>({
251251
? {
252252
name: currentChain.name,
253253
rpcUrl: currentChain.rpcUrls.default.http[0] ?? "",
254-
contractAddress: getContractAddress(chainId) ?? "",
254+
contractAddress: getEvmContractAddress(chainId, "priceFeed"),
255255
}
256256
: { name: "", rpcUrl: "", contractAddress: "" },
257257
paramValues,
@@ -294,7 +294,7 @@ const Example = <ParameterName extends string>({
294294
const updateValues = useCallback(() => {
295295
if (typeof example.parameters === "function") {
296296
setError(undefined);
297-
const address = getContractAddress(config.state.chainId);
297+
const address = getEvmContractAddress(config.state.chainId, "priceFeed");
298298
if (!address) {
299299
throw new Error(
300300
`No contract for chain id: ${config.state.chainId.toString()}`,

apps/api-reference/src/components/EvmApi/run-button.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import { ArrowPathIcon } from "@heroicons/react/24/outline";
4+
import { getEvmContractAddress } from "@pythnetwork/contract-manager/utils/utils";
45
import PythAbi from "@pythnetwork/pyth-sdk-solidity/abis/IPyth.json";
56
import PythErrorsAbi from "@pythnetwork/pyth-sdk-solidity/abis/PythErrors.json";
67
import { ConnectKitButton, Avatar } from "connectkit";
@@ -9,9 +10,9 @@ import { ContractFunctionExecutionError } from "viem";
910
import { useAccount, useConfig } from "wagmi";
1011
import { readContract, simulateContract, writeContract } from "wagmi/actions";
1112

13+
1214
import type { Parameter } from "./parameter";
1315
import { TRANSFORMS } from "./parameter";
14-
import { getContractAddress } from "../../evm-networks";
1516
import { useIsMounted } from "../../use-is-mounted";
1617
import { Button } from "../Button";
1718
import { Code } from "../Code";
@@ -166,7 +167,7 @@ const useRunButton = <ParameterName extends string>({
166167
if (args === undefined) {
167168
setStatus(ErrorStatus(new Error("Invalid parameters!")));
168169
} else {
169-
const address = getContractAddress(config.state.chainId);
170+
const address = getEvmContractAddress(config.state.chainId, "priceFeed");
170171
if (!address) {
171172
throw new Error(
172173
`No contract for chain id: ${config.state.chainId.toString()}`,

apps/api-reference/src/components/EvmProvider/index.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
"use client";
22

3+
import { getEvmChainRpcUrl, getAllEvmChainsIds } from "@pythnetwork/contract-manager/utils/utils";
34
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
45
import { ConnectKitProvider, getDefaultConfig } from "connectkit";
56
import { useTheme } from "next-themes";
67
import type { ReactNode } from "react";
78
import * as chains from "viem/chains";
89
import { WagmiProvider, createConfig, http, useChainId } from "wagmi";
910

10-
import { NETWORK_IDS, getRpcUrl } from "../../evm-networks";
11+
1112
import { metadata } from "../../metadata";
1213

13-
const CHAINS = NETWORK_IDS.map((id) =>
14+
const CHAINS = getAllEvmChainsIds().map((id) =>
1415
Object.values(chains).find((chain) => chain.id === id),
1516
).filter((chain) => chain !== undefined) as unknown as readonly [
1617
chains.Chain,
1718
...chains.Chain[],
1819
];
1920

2021
const TRANSPORTS = Object.fromEntries(
21-
CHAINS.map((chain) => {
22-
const rpcUrl = getRpcUrl(chain.id);
23-
if (rpcUrl) {
24-
return [chain.id, http(rpcUrl)];
25-
} else if (chain.rpcUrls.default.http[0]) {
26-
return [chain.id, http(chain.rpcUrls.default.http[0])];
27-
} else {
28-
throw new Error(`No rpc url found for ${chain.name}`);
29-
}
30-
}),
22+
CHAINS.map((chain) => [chain.id, http(getEvmChainRpcUrl(chain.id))]),
3123
);
3224

25+
// TODO: Remove this once we have a way to get the TRANSPORTS from the contract manager
26+
// const chainEntries = await Promise.all(
27+
// CHAINS.map(async (chain) => {
28+
// const url = await getEvmChainRpcUrl(chain.id);
29+
// return url ? [chain.id, http(url)] : undefined;
30+
// }),
31+
// );
32+
33+
// const TRANSPORTS = Object.fromEntries(
34+
// chainEntries.filter((entry): entry is [number, ReturnType<typeof http>] => entry !== undefined)
35+
// ) as Record<number, ReturnType<typeof http>>;
36+
37+
3338
type EvmProviderProps = {
3439
children: ReactNode;
3540
walletConnectProjectId?: string | undefined;

apps/api-reference/src/evm-networks.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.

contract_manager/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/contract-manager",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Set of tools to manage pyth contracts",
55
"private": true,
66
"exports": {
@@ -23,6 +23,10 @@
2323
},
2424
"./data/vaults/*.json": {
2525
"default": "./store/vaults/*.json"
26+
},
27+
"./utils/*": {
28+
"types": "./lib/utils/*.d.ts",
29+
"default": "./lib/utils/*.js"
2630
}
2731
},
2832
"files": [
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import Web3 from 'web3';
2+
import evmChainsData from '../../store/chains/EvmChains.json';
3+
import evmPriceFeedContractsData from '../../store/contracts/EvmPriceFeedContracts.json';
4+
import evmWormholeContractsData from '../../store/contracts/EvmWormholeContracts.json';
5+
import * as chains from "viem/chains";
6+
7+
export const getEvmContractAddress = (chainId: number, contractType: string): `0x${string}` => {
8+
const chain = evmChainsData.find((c) => c.networkId === chainId);
9+
if (!chain) {
10+
throw new Error(`Chain with network ID ${chainId} not found`);
11+
}
12+
13+
if (contractType === 'priceFeed') {
14+
const contract = evmPriceFeedContractsData.find((c) => c.chain === chain.id);
15+
if (!contract) {
16+
throw new Error(`Price feed contract not found for chain ${chainId}`);
17+
}
18+
return contract.address as `0x${string}`;
19+
}
20+
21+
if (contractType === 'wormhole') {
22+
const contract = evmWormholeContractsData.find((c) => c.chain === chain.id);
23+
if (!contract) {
24+
throw new Error(`Wormhole contract not found for chain ${chainId}`);
25+
}
26+
return contract.address as `0x${string}`;
27+
}
28+
29+
throw new Error(`Unknown contract type: ${contractType}`);
30+
}
31+
32+
export const getAllEvmChainsIds = () => evmChainsData.map((c) => c.networkId);
33+
34+
35+
// TODO: The below method should be used to get the RPC URL for a chain, but it is not working as expecte
36+
// export const getEvmChainRpcUrl = async (chainId: number) => {
37+
// const chain = evmChainsData.find((c) => c.networkId === chainId);
38+
// if (!chain) {
39+
// throw new Error(`Chain with network ID ${chainId} not found`);
40+
// }
41+
42+
// // Try JSON RPC URL first
43+
// if (await checkRpcUrl(chain.rpcUrl)) {
44+
// return chain.rpcUrl;
45+
// }
46+
47+
// // Fallback to viem chains
48+
// const viemChain = Object.values(chains).find((c: any) => c.id === chain.id);
49+
// if (viemChain && viemChain.rpcUrls && viemChain.rpcUrls.default) {
50+
// const viemRpcUrl = viemChain.rpcUrls.default.http[0];
51+
// if (await checkRpcUrl(viemRpcUrl)) {
52+
// return viemRpcUrl;
53+
// }
54+
// }
55+
56+
// throw new Error(`No working RPC URL found for chain ${chainId}`);
57+
// }
58+
59+
60+
export const getEvmChainRpcUrl = (chainId: number) => {
61+
const chain = evmChainsData.find((c) => c.networkId === chainId);
62+
if (!chain) {
63+
throw new Error(`Chain with network ID ${chainId} not found`);
64+
}
65+
66+
67+
// Let's try to use the viem chains without checking if they are working
68+
const viemChain = Object.values(chains).find((c: any) => c.id === chain.id);
69+
if (viemChain && viemChain.rpcUrls && viemChain.rpcUrls.default) {
70+
const viemRpcUrl = viemChain.rpcUrls.default.http[0];
71+
return viemRpcUrl;
72+
}
73+
74+
// Now let's try to use the json rpc url
75+
if (chain.rpcUrl) {
76+
return chain.rpcUrl;
77+
}
78+
79+
throw new Error(`No working RPC URL found for chain ${chainId}`);
80+
}
81+
82+
83+
// const checkRpcUrl = (rpcUrl: string) => {
84+
// const web3 = new Web3(rpcUrl);
85+
// return web3.eth.getBlockNumber().then((blockNumber) => {
86+
// return true;
87+
// }).catch((error) => {
88+
// console.error(`Error checking RPC URL ${rpcUrl}: ${error}`);
89+
// return false;
90+
// });
91+
// }

0 commit comments

Comments
 (0)