From 530500a433ea04fcc29d80613c4b700505f9faaf Mon Sep 17 00:00:00 2001 From: shanejonas Date: Thu, 5 Sep 2019 09:58:56 -0700 Subject: [PATCH] fix(configuration): add query param configuration for rpc url fixes #56 --- README.md | 7 +++++++ src/hooks/useMultiGeth.ts | 5 +++-- src/hooks/useQueryParams.ts | 27 +++++++++++++++++++++++++++ src/stores/useMultiGethStore.ts | 4 +++- 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/hooks/useQueryParams.ts diff --git a/README.md b/README.md index 10254c4d..8bc69d8a 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,13 @@ By default, multi-geth service will run ETC mainnet. Jade Service runner conveni └── keystore ``` +## Configuration + +### Configure rpc via url + +https://explorer.jade.builders/?rpcUrl=https://services.jade.builders/multi-geth/kotti/1.9.2 + + ### Configure default urls via environment variables Override eth url diff --git a/src/hooks/useMultiGeth.ts b/src/hooks/useMultiGeth.ts index e99ef5aa..01ccdaa5 100644 --- a/src/hooks/useMultiGeth.ts +++ b/src/hooks/useMultiGeth.ts @@ -9,6 +9,7 @@ function useMultiGeth( serviceRunnerUrl: string, version: string, env: string, + queryUrlOverride?: string, ): [ERPC, Dispatch] { const [erpc, setErpc] = React.useState(); const [urlOverride, setUrlOverride] = useState(process.env.REACT_APP_ETH_RPC_URL); @@ -24,7 +25,7 @@ function useMultiGeth( await serviceRunner.startService(serviceName, version, env); let parsedUrl; try { - parsedUrl = new URL(urlOverride || `${serviceRunnerUrl}/${serviceName}/${env}/${version}`); + parsedUrl = new URL(queryUrlOverride || urlOverride || `${serviceRunnerUrl}/${serviceName}/${env}/${version}`); } catch (e) { return; } @@ -49,7 +50,7 @@ function useMultiGeth( } }; runAsync(); - }, [serviceRunner, serviceRunnerUrl, version, env, urlOverride]); + }, [serviceRunner, serviceRunnerUrl, version, env, urlOverride, queryUrlOverride]); return [erpc, setUrlOverride]; } diff --git a/src/hooks/useQueryParams.ts b/src/hooks/useQueryParams.ts new file mode 100644 index 00000000..8e81e517 --- /dev/null +++ b/src/hooks/useQueryParams.ts @@ -0,0 +1,27 @@ +import { useState } from "react"; +import * as qs from "qs"; + +const useQueryParams = () => { + const parse = () => { + return qs.parse(window.location.search, { + ignoreQueryPrefix: true, + depth: 100, + decoder(str) { + if (/^(\d+|\d*\.\d+)$/.test(str)) { + return parseFloat(str); + } + if (str === "false") { + return false; + } + if (str === "true") { + return true; + } + return decodeURIComponent(str); + }, + }); + }; + const [query] = useState(parse()); + return [query]; +}; + +export default useQueryParams; diff --git a/src/stores/useMultiGethStore.ts b/src/stores/useMultiGethStore.ts index bee0be2b..deabb985 100644 --- a/src/stores/useMultiGethStore.ts +++ b/src/stores/useMultiGethStore.ts @@ -1,8 +1,10 @@ import { createStore } from "reusable"; import useMultiGeth from "../hooks/useMultiGeth"; import useServiceRunnerStore from "./useServiceRunnerStore"; +import useQueryParams from "../hooks/useQueryParams"; export default createStore(() => { const [serviceRunner, serviceRunnerUrl] = useServiceRunnerStore(); - return useMultiGeth(serviceRunner, serviceRunnerUrl, "1.9.2", "mainnet"); + const [queryParams] = useQueryParams(); + return useMultiGeth(serviceRunner, serviceRunnerUrl, "1.9.2", "mainnet", queryParams.rpcUrl); });