From 8198f9a1a0d6487eaa0d4e7ad4d04258d9ca66e6 Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 21 Nov 2024 19:17:30 +0100 Subject: [PATCH] fix: fallback to public rpcs (#35) --- dist/action.js | 25 +++++++++++++++++++++++++ dist/action.mjs | 25 +++++++++++++++++++++++++ src/lib.ts | 11 +++++++++++ src/public.ts | 15 +++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 src/public.ts diff --git a/dist/action.js b/dist/action.js index 29a5e4b..f272805 100644 --- a/dist/action.js +++ b/dist/action.js @@ -23205,6 +23205,21 @@ var ChainId = { zksync: zksync.id }; +// src/public.ts +var publicRPCs = { + [ChainId.mainnet]: "https://eth.llamarpc.com", + [ChainId.polygon]: "https://polygon.llamarpc.com", + [ChainId.arbitrum]: "https://polygon.llamarpc.com", + [ChainId.base]: "https://base.llamarpc.com", + [ChainId.bnb]: "https://binance.llamarpc.com", + [ChainId.metis]: "https://andromeda.metis.io/?owner=1088", + [ChainId.gnosis]: "https://rpc.ankr.com/gnosis", + [ChainId.scroll]: "https://rpc.scroll.io", + [ChainId.zksync]: "https://mainnet.era.zksync.io", + [ChainId.fantom]: "https://rpc.ftm.tools", + [ChainId.avalanche]: "https://api.avax.network/ext/bc/C/rpc" +}; + // src/lib.ts Object.values(ChainId).filter( (id) => networkMap[id] @@ -23240,6 +23255,12 @@ function getAlchemyRPC(chainId, alchemyKey2) { } return `https://${alchemyId}.g.alchemy.com/v2/${alchemyKey2}`; } +function getPublicRpc(chainId) { + const publicRpc = publicRPCs[chainId]; + if (!publicRpc) + throw new Error(`No default public rpc for '${chainId}' configured.`); + return publicRpc; +} var getRPCUrl = (chainId, options) => { if (!Object.values(ChainId).includes(chainId)) { throw new Error( @@ -23256,6 +23277,10 @@ var getRPCUrl = (chainId, options) => { } catch (e) { } } + try { + return getPublicRpc(chainId); + } catch (e) { + } }; // src/action.ts diff --git a/dist/action.mjs b/dist/action.mjs index 778c9b4..e8348c0 100644 --- a/dist/action.mjs +++ b/dist/action.mjs @@ -23203,6 +23203,21 @@ var ChainId = { zksync: zksync.id }; +// src/public.ts +var publicRPCs = { + [ChainId.mainnet]: "https://eth.llamarpc.com", + [ChainId.polygon]: "https://polygon.llamarpc.com", + [ChainId.arbitrum]: "https://polygon.llamarpc.com", + [ChainId.base]: "https://base.llamarpc.com", + [ChainId.bnb]: "https://binance.llamarpc.com", + [ChainId.metis]: "https://andromeda.metis.io/?owner=1088", + [ChainId.gnosis]: "https://rpc.ankr.com/gnosis", + [ChainId.scroll]: "https://rpc.scroll.io", + [ChainId.zksync]: "https://mainnet.era.zksync.io", + [ChainId.fantom]: "https://rpc.ftm.tools", + [ChainId.avalanche]: "https://api.avax.network/ext/bc/C/rpc" +}; + // src/lib.ts Object.values(ChainId).filter( (id) => networkMap[id] @@ -23238,6 +23253,12 @@ function getAlchemyRPC(chainId, alchemyKey2) { } return `https://${alchemyId}.g.alchemy.com/v2/${alchemyKey2}`; } +function getPublicRpc(chainId) { + const publicRpc = publicRPCs[chainId]; + if (!publicRpc) + throw new Error(`No default public rpc for '${chainId}' configured.`); + return publicRpc; +} var getRPCUrl = (chainId, options) => { if (!Object.values(ChainId).includes(chainId)) { throw new Error( @@ -23254,6 +23275,10 @@ var getRPCUrl = (chainId, options) => { } catch (e) { } } + try { + return getPublicRpc(chainId); + } catch (e) { + } }; // src/action.ts diff --git a/src/lib.ts b/src/lib.ts index 3b81825..295889a 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -1,5 +1,6 @@ import { networkMap } from "./alchemyIds"; import { ChainId, ChainList } from "./chainIds"; +import { publicRPCs } from "./public"; type SupportedChainIds = (typeof ChainId)[keyof typeof ChainId]; @@ -53,6 +54,13 @@ export function getAlchemyRPC(chainId: SupportedChainIds, alchemyKey: string) { return `https://${alchemyId}.g.alchemy.com/v2/${alchemyKey}`; } +export function getPublicRpc(chainId: SupportedChainIds) { + const publicRpc = publicRPCs[chainId as keyof typeof publicRPCs]; + if (!publicRpc) + throw new Error(`No default public rpc for '${chainId}' configured.`); + return publicRpc; +} + type GetRPCUrlOptions = { alchemyKey?: string; }; @@ -89,6 +97,9 @@ export const getRPCUrl = ( return getAlchemyRPC(chainId, options?.alchemyKey); } catch (e) {} } + try { + return getPublicRpc(chainId); + } catch (e) {} }; export { ChainId, ChainList }; diff --git a/src/public.ts b/src/public.ts new file mode 100644 index 0000000..7a1f158 --- /dev/null +++ b/src/public.ts @@ -0,0 +1,15 @@ +import { ChainId } from "./chainIds"; + +export const publicRPCs = { + [ChainId.mainnet]: "https://eth.llamarpc.com", + [ChainId.polygon]: "https://polygon.llamarpc.com", + [ChainId.arbitrum]: "https://polygon.llamarpc.com", + [ChainId.base]: "https://base.llamarpc.com", + [ChainId.bnb]: "https://binance.llamarpc.com", + [ChainId.metis]: "https://andromeda.metis.io/?owner=1088", + [ChainId.gnosis]: "https://rpc.ankr.com/gnosis", + [ChainId.scroll]: "https://rpc.scroll.io", + [ChainId.zksync]: "https://mainnet.era.zksync.io", + [ChainId.fantom]: "https://rpc.ftm.tools", + [ChainId.avalanche]: "https://api.avax.network/ext/bc/C/rpc", +} as const;