From 3888801081809990f05c44a92373c5bae746f0c4 Mon Sep 17 00:00:00 2001 From: "plebeius.eth" Date: Tue, 11 Jul 2023 12:26:22 +0200 Subject: [PATCH] add chain providers to settings --- src/components/modals/SettingsModal.jsx | 146 +++++++++++++++++++++--- 1 file changed, 133 insertions(+), 13 deletions(-) diff --git a/src/components/modals/SettingsModal.jsx b/src/components/modals/SettingsModal.jsx index 144e847d..cb6e215d 100755 --- a/src/components/modals/SettingsModal.jsx +++ b/src/components/modals/SettingsModal.jsx @@ -32,6 +32,8 @@ const SettingsModal = ({ isOpen, closeModal }) => { const gatewayRef = useRef(); const ipfsRef = useRef(); const pubsubRef = useRef(); + const ethereumRpcRef = useRef(); + const polygonRpcRef = useRef(); const dataPathRef = useRef(); const importRef = useRef(); const nameRef = useRef(); @@ -42,11 +44,20 @@ const SettingsModal = ({ isOpen, closeModal }) => { 'https://cloudflare-ipfs.com', 'https://plebpubsub.live' ]; + const defaultPubsubHttpClientsOptions = [ 'https://pubsubprovider.xyz/api/v0', 'https://plebpubsub.live/api/v0' ]; + const defaultEthereumRpcUrls = [ + 'https://ethrpc.xyz', + ]; + + const defaultPolygonRpcUrls = [ + 'https://polygon-rpc.com', + ]; + const isValidURL = (url) => { try { new URL(url); @@ -56,6 +67,7 @@ const SettingsModal = ({ isOpen, closeModal }) => { } }; + const handleSavePlebbitOptions = async () => { let gatewayUrls = gatewayRef.current.value.split('\n').filter(url => url.trim()); let ipfsClientsOptions = ipfsRef.current.value.split('\n').filter(url => url.trim()); @@ -100,6 +112,55 @@ const SettingsModal = ({ isOpen, closeModal }) => { }; + const handleSaveChainProviders = async () => { + let ethereumRpcUrls = ethereumRpcRef.current.value.split('\\n').filter(url => url.trim()); + let polygonRpcUrls = polygonRpcRef.current.value.split('\\n').filter(url => url.trim()); + + if (!ethereumRpcUrls.length) { + ethereumRpcUrls = defaultEthereumRpcUrls; + } + + if (!polygonRpcUrls.length) { + polygonRpcUrls = defaultPolygonRpcUrls; + } + + const invalidUrls = [ + ...ethereumRpcUrls || defaultEthereumRpcUrls, + ...polygonRpcUrls || defaultPolygonRpcUrls, + ].filter((url) => !isValidURL(url)); + + if (invalidUrls.length > 0) { + setNewErrorMessage(`Invalid URL(s): ${invalidUrls.join(', ')}`); + return; + } + + const chainProviders = { + 'eth': { + urls: ethereumRpcUrls, + chainId: 1, + }, + 'matic': { + urls: polygonRpcUrls, + chainId: 137, + } + } + + try { + await setAccount({ + ...account, + plebbitOptions: { + ...account.plebbitOptions, + chainProviders, + }, + }); + localStorage.setItem("successToast", "Chain Providers Saved"); + window.location.reload(); + } catch (error) { + setNewErrorMessage(error.message); console.log(error); + } + }; + + const handleResetPlebbitOptions = async () => { setNewErrorMessage(null); setNewSuccessMessage(null); @@ -126,6 +187,41 @@ const SettingsModal = ({ isOpen, closeModal }) => { }; + const handleResetChainProviders = async () => { + setNewErrorMessage(null); + setNewSuccessMessage(null); + + ethereumRpcRef.current.value = defaultEthereumRpcUrls.join('\\n'); + polygonRpcRef.current.value = defaultPolygonRpcUrls.join('\\n'); + + const chainProviders = { + 'eth': { + urls: defaultEthereumRpcUrls, + chainId: 1, + }, + 'matic': { + urls: defaultPolygonRpcUrls, + chainId: 137, + } + } + + try { + await setAccount({ + ...account, + plebbitOptions: { + ...account.plebbitOptions, + chainProviders, + }, + }); + localStorage.setItem("successToast", "Chain Providers Reset"); + window.location.reload(); + } catch (error) { + setNewErrorMessage(error.message); console.log(error); + } + }; + + + const handleCloseModal = () => { setAccountJson(null); @@ -153,10 +249,10 @@ const SettingsModal = ({ isOpen, closeModal }) => { const expandAll = () => { - if (expanded.length === 3) { + if (expanded.length === 4) { setExpanded([]); } else { - setExpanded([0, 1, 2]); + setExpanded([0, 1, 2, 3]); } }; @@ -240,7 +336,7 @@ const SettingsModal = ({ isOpen, closeModal }) => { [ ] @@ -395,16 +491,40 @@ const SettingsModal = ({ isOpen, closeModal }) => { /> - {/* */} + +