From 69b19e4b7b90b8dfd7420bdf5e09719b1fd896da Mon Sep 17 00:00:00 2001 From: botond <105208209+notbot00@users.noreply.github.com> Date: Wed, 22 Jun 2022 00:20:53 +0100 Subject: [PATCH] feat(#507): Node Discovery * feat: add network-switcher lib * feat: add env variables for some deployed app urls * feat: add network processing to environment hoook * refactor: network handling * refactor: remove dialog from provider and add env setter * feat: add network switcher dialog to the trading app * refactor: add network redirect to dialog connect callback * fix: lint * feat: add network configuration files to static app * feat: update environments to use config file instead of static node url * refactor: split out network switcher utils * refactor: split up environment hook * fix: jsonify env variable for possible networks * fix: add formatter file * feat: add network loader component * feat: add network loader to the trading app * fix: assign correct global state to network swicther * feat: add status modal * feat: add network-switcher lib * feat: add env variables for some deployed app urls * feat: add network processing to environment hoook * refactor: network handling * refactor: remove dialog from provider and add env setter * feat: add network switcher dialog to the trading app * refactor: add network redirect to dialog connect callback * fix: lint * fix: jsonify env variable for possible networks * fix: add formatter file * fix: assign correct global state to network swicther * fix: failing tests from UI changes * feat: add environment validation * feat: add runtime validation for network configs * fix: readd node urls to envs to avoid breaking the apps for now * chore: rename network swicther lib to environmnet * fix: lint * feat: add tests for config hook * feat: add environment hook tests * fix: lint * fix: lint * feat: add environment hook tests * feat: add storage tests * fix: formet * feat: improve loading states * fix: format * fix: use router instead of window location * fix: rearrange network loader props and components * fix: remove FC type * fix: env validation * fix: untangle returns in network loader * fix: add teardown for env and localstorage * fix: add custom to env networks * fix: lint * fix: format * fix: lint * fix: remove env provider from simple trading app * fix: remove failing promise hacks * fix: some leftover format files * fix: remove network switcher from tsconf * fix: move Networks to libs/environment * fix: add defaults for ether env vars * feat: add tests for default ether env vars * fix: remove chain id env var from web3 container * fix: remove chain id from the environment * fix: format * fix: lint token * fix: lint env * fix: add comment to callout hack * fix: lint token again * fix: remove skip * fix: move addresses to token app * fix: improve schema validation errors and fix token app * fix: lint * fix: format * fix: format * fix: add network loaders to apps * fix: format * fix: remove logs * fix: cypress process errors * fix: change network loader hierarchy in token * fix: remove stray console.log * fix: revert test changes in simple trading app * fix: prefix env vars with NX Co-authored-by: Dexter Edwards * fix: improve schema validation errors and fix token app * fix: format * fix: disable lint rules for catch block any types * fix: format again * fix: remove redundant process.platform injections * fix: format Co-authored-by: Joe Co-authored-by: Matthew Russell Co-authored-by: Dexter Edwards --- apps/explorer-e2e/.env | 2 +- apps/explorer-e2e/cypress.json | 2 +- apps/explorer/.env | 2 +- apps/explorer/.env.capsule | 2 +- apps/explorer/.env.devnet | 1 + apps/explorer/.env.mainnet | 1 + apps/explorer/.env.stagnet1 | 1 + apps/explorer/.env.stagnet2 | 1 + apps/explorer/.env.testnet | 1 + apps/explorer/src/app/app.tsx | 12 +- apps/simple-trading-app/.env | 4 + apps/simple-trading-app/.env.devnet | 1 + apps/simple-trading-app/.env.mainnet | 1 + apps/simple-trading-app/.env.stagnet1 | 1 + apps/simple-trading-app/.env.stagnet2 | 1 + apps/simple-trading-app/.env.testnet | 1 + apps/simple-trading-app/src/app/app.tsx | 12 +- .../src/app/config/index.tsx | 5 - apps/static/src/assets/devnet-network.json | 3 + apps/static/src/assets/mainnet-network.json | 17 + apps/static/src/assets/stagnet1-network.json | 3 + apps/static/src/assets/stagnet2-network.json | 3 + apps/static/src/assets/testnet-network.json | 3 + apps/stats/src/app.tsx | 27 +- apps/token-e2e/.env | 1 - apps/token-e2e/.env.devnet | 1 - apps/token-e2e/.env.mainnet | 1 - apps/token-e2e/.env.stagnet1 | 1 - apps/token-e2e/.env.stagnet2 | 1 - apps/token-e2e/.env.testnet | 1 - apps/token/.env | 2 +- apps/token/.env.devnet | 2 +- apps/token/.env.mainnet | 2 +- apps/token/.env.stagnet1 | 2 +- apps/token/.env.stagnet2 | 2 +- apps/token/.env.testnet | 2 +- apps/token/README.md | 14 +- apps/token/src/app.tsx | 91 ++--- .../add-locked-token/add-locked-token.tsx | 7 +- .../transaction-complete.tsx | 2 +- .../transaction-callout/transaction-error.tsx | 2 +- .../transaction-pending.tsx | 2 +- .../transactions-modal/transactions-modal.tsx | 2 +- .../web3-connector/web3-connector.tsx | 44 +-- apps/token/src/config/env.ts | 45 ++- apps/token/src/config/index.ts | 1 - apps/token/src/config/vega.ts | 176 ---------- .../contexts/contracts/contracts-provider.tsx | 71 ++-- .../src/hooks/use-add-asset-to-wallet.ts | 4 +- apps/token/src/hooks/use-tranches.ts | 5 +- apps/token/src/routes/claim/complete.tsx | 2 +- apps/token/src/routes/contracts/index.tsx | 7 +- .../home/token-details/token-details.tsx | 2 +- .../associate/associate-transaction.tsx | 2 +- apps/token/src/routes/staking/staking.tsx | 2 +- .../src/routes/staking/validator-table.tsx | 2 +- apps/token/src/routes/tranches/tranche.tsx | 2 +- apps/token/src/routes/withdrawals/index.tsx | 2 +- apps/trading-e2e/cypress.json | 1 - apps/trading/.env | 2 +- apps/trading/.env.devnet | 2 +- apps/trading/.env.mainnet | 2 +- apps/trading/.env.stagnet1 | 2 +- apps/trading/.env.stagnet2 | 2 +- apps/trading/.env.testnet | 2 +- apps/trading/README.md | 4 +- apps/trading/components/app-loader/index.tsx | 8 +- .../web3-container/web3-container.spec.tsx | 2 +- .../web3-container/web3-container.tsx | 17 +- apps/trading/pages/_app.page.tsx | 85 +++-- .../portfolio/deposit/deposit-container.tsx | 2 +- .../.babelrc | 0 .../.eslintrc.json | 0 libs/environment/README.md | 19 ++ .../jest.config.js | 4 +- libs/environment/package.json | 4 + .../project.json | 20 +- .../src/components/index.ts | 1 + .../src/components/network-loader/index.ts | 1 + .../network-loader/network-loader.tsx | 162 +++++++++ .../network-switcher-dialog/index.tsx | 0 .../network-switcher-dialog.tsx | 0 .../src/components/network-switcher/index.tsx | 0 .../network-switcher/network-switcher.tsx | 2 +- .../src/hooks/index.ts | 0 .../environment/src/hooks/use-config.spec.tsx | 311 ++++++++++++++++++ libs/environment/src/hooks/use-config.tsx | 122 +++++++ .../src/hooks/use-environment.spec.tsx | 228 +++++++++++++ .../environment/src/hooks/use-environment.tsx | 50 +++ .../src/index.ts | 3 + libs/environment/src/types.ts | 27 ++ .../src/utils/compile-environment.ts | 102 ++++++ libs/environment/src/utils/compile-errors.ts | 19 ++ .../src/utils/promise-race-success.ts | 22 ++ .../src/utils/validate-configuration.ts | 19 ++ .../src/utils/validate-environment.ts | 84 +++++ .../tsconfig.json | 0 .../tsconfig.lib.json | 0 .../tsconfig.spec.json | 0 libs/network-switcher/README.md | 17 - libs/network-switcher/package.json | 4 - .../src/hooks/use-environment.tsx | 183 ----------- libs/react-helpers/src/index.ts | 1 - libs/react-helpers/src/lib/environment.ts | 7 - .../async-renderer/async-renderer.tsx | 4 +- .../src/components/dialog/dialog.tsx | 4 +- .../src/components/icon/icon.stories.tsx | 2 +- .../lib/transaction-dialog/dialog-rows.tsx | 2 +- libs/withdraws/src/lib/withdraw-dialog.tsx | 2 +- libs/withdraws/src/lib/withdrawals-table.tsx | 2 +- package.json | 1 + tsconfig.base.json | 2 +- workspace.json | 2 +- yarn.lock | 5 + 114 files changed, 1522 insertions(+), 659 deletions(-) delete mode 100644 apps/simple-trading-app/src/app/config/index.tsx create mode 100644 apps/static/src/assets/devnet-network.json create mode 100644 apps/static/src/assets/mainnet-network.json create mode 100644 apps/static/src/assets/stagnet1-network.json create mode 100644 apps/static/src/assets/stagnet2-network.json create mode 100644 apps/static/src/assets/testnet-network.json delete mode 100644 apps/token/src/config/vega.ts rename libs/{network-switcher => environment}/.babelrc (100%) rename libs/{network-switcher => environment}/.eslintrc.json (100%) create mode 100644 libs/environment/README.md rename libs/{network-switcher => environment}/jest.config.js (63%) create mode 100644 libs/environment/package.json rename libs/{network-switcher => environment}/project.json (55%) rename libs/{network-switcher => environment}/src/components/index.ts (69%) create mode 100644 libs/environment/src/components/network-loader/index.ts create mode 100644 libs/environment/src/components/network-loader/network-loader.tsx rename libs/{network-switcher => environment}/src/components/network-switcher-dialog/index.tsx (100%) rename libs/{network-switcher => environment}/src/components/network-switcher-dialog/network-switcher-dialog.tsx (100%) rename libs/{network-switcher => environment}/src/components/network-switcher/index.tsx (100%) rename libs/{network-switcher => environment}/src/components/network-switcher/network-switcher.tsx (95%) rename libs/{network-switcher => environment}/src/hooks/index.ts (100%) create mode 100644 libs/environment/src/hooks/use-config.spec.tsx create mode 100644 libs/environment/src/hooks/use-config.tsx create mode 100644 libs/environment/src/hooks/use-environment.spec.tsx create mode 100644 libs/environment/src/hooks/use-environment.tsx rename libs/{network-switcher => environment}/src/index.ts (69%) create mode 100644 libs/environment/src/types.ts create mode 100644 libs/environment/src/utils/compile-environment.ts create mode 100644 libs/environment/src/utils/compile-errors.ts create mode 100644 libs/environment/src/utils/promise-race-success.ts create mode 100644 libs/environment/src/utils/validate-configuration.ts create mode 100644 libs/environment/src/utils/validate-environment.ts rename libs/{network-switcher => environment}/tsconfig.json (100%) rename libs/{network-switcher => environment}/tsconfig.lib.json (100%) rename libs/{network-switcher => environment}/tsconfig.spec.json (100%) delete mode 100644 libs/network-switcher/README.md delete mode 100644 libs/network-switcher/package.json delete mode 100644 libs/network-switcher/src/hooks/use-environment.tsx delete mode 100644 libs/react-helpers/src/lib/environment.ts diff --git a/apps/explorer-e2e/.env b/apps/explorer-e2e/.env index 3cd032624..5254f0d9d 100644 --- a/apps/explorer-e2e/.env +++ b/apps/explorer-e2e/.env @@ -2,7 +2,7 @@ NX_CHAIN_EXPLORER_URL=https://explorer.vega.trading/.netlify/functions/chain-exp NX_TENDERMINT_URL=http://localhost:26617 NX_TENDERMINT_WEBSOCKET_URL=wss://localhost:26617/websocket NX_VEGA_URL=http://localhost:3028/query -NX_VEGA_ENV=LOCAL +NX_VEGA_ENV=CUSTOM NX_VEGA_REST=http://localhost:3029 CYPRESS_VEGA_TENDERMINT_URL=http://localhost:26617 diff --git a/apps/explorer-e2e/cypress.json b/apps/explorer-e2e/cypress.json index fb459aadd..56edbee7e 100644 --- a/apps/explorer-e2e/cypress.json +++ b/apps/explorer-e2e/cypress.json @@ -14,7 +14,7 @@ "screenshotsFolder": "../../dist/cypress/apps/explorer-e2e/screenshots", "chromeWebSecurity": false, "env": { - "environment": "local", + "environment": "CUSTOM", "tsConfig": "tsconfig.json", "TAGS": "not @todo and not @ignore and not @manual" } diff --git a/apps/explorer/.env b/apps/explorer/.env index f71bdbc25..6715926fc 100644 --- a/apps/explorer/.env +++ b/apps/explorer/.env @@ -2,7 +2,7 @@ NX_CHAIN_EXPLORER_URL=https://explorer.vega.trading/.netlify/functions/chain-exp NX_TENDERMINT_URL=http://localhost:26617 NX_TENDERMINT_WEBSOCKET_URL=wss://localhost:26617/websocket NX_VEGA_URL=http://localhost:3028/query -NX_VEGA_ENV=LOCAL +NX_VEGA_ENV=CUSTOM NX_VEGA_REST=http://localhost:3029 NX_CHAIN_EXPLORER_URL=https://explorer.vega.trading/.netlify/functions/chain-explorer-api diff --git a/apps/explorer/.env.capsule b/apps/explorer/.env.capsule index ed2cf672b..4fa0a1a12 100644 --- a/apps/explorer/.env.capsule +++ b/apps/explorer/.env.capsule @@ -4,7 +4,7 @@ NX_TENDERMINT_URL=http://localhost:26617 NX_TENDERMINT_WEBSOCKET_URL=wss://localhost:26617/websocket NX_VEGA_URL=http://localhost:3028/query NX_VEGA_NETWORKS='{"TESTNET":"https://explorer.fairground.wtf","MAINNET":"https://explorer.vega.xyz"}' -NX_VEGA_ENV=LOCAL +NX_VEGA_ENV=CUSTOM NX_VEGA_REST=http://localhost:3029 # App flags diff --git a/apps/explorer/.env.devnet b/apps/explorer/.env.devnet index 598dd92e4..c5c245622 100644 --- a/apps/explorer/.env.devnet +++ b/apps/explorer/.env.devnet @@ -2,6 +2,7 @@ NX_CHAIN_EXPLORER_URL=https://explorer.vega.trading/.netlify/functions/chain-explorer-api NX_TENDERMINT_URL=https://n04.d.vega.xyz/tm NX_TENDERMINT_WEBSOCKET_URL=wss://n04.d.vega.xyz/tm/websocket +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/devnet-network.json NX_VEGA_URL=https://n04.d.vega.xyz/query NX_VEGA_NETWORKS='{"TESTNET":"https://explorer.fairground.wtf","MAINNET":"https://explorer.vega.xyz"}' NX_VEGA_ENV=DEVNET diff --git a/apps/explorer/.env.mainnet b/apps/explorer/.env.mainnet index 8215f8db1..721af8681 100644 --- a/apps/explorer/.env.mainnet +++ b/apps/explorer/.env.mainnet @@ -2,6 +2,7 @@ NX_CHAIN_EXPLORER_URL=https://explorer.vega.trading/.netlify/functions/chain-explorer-api NX_TENDERMINT_URL=https://mainnet-observer-proxy01.ops.vega.xyz/ NX_TENDERMINT_WEBSOCKET_URL=wss://mainnet-observer-proxy01.ops.vega.xyz/websocket +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/mainnet-network.json NX_VEGA_URL=https://api.token.vega.xyz/query NX_VEGA_NETWORKS='{"TESTNET":"https://explorer.fairground.wtf","MAINNET":"https://explorer.vega.xyz"}' NX_VEGA_ENV=MAINNET diff --git a/apps/explorer/.env.stagnet1 b/apps/explorer/.env.stagnet1 index e8073baa7..7a7359f9d 100644 --- a/apps/explorer/.env.stagnet1 +++ b/apps/explorer/.env.stagnet1 @@ -2,6 +2,7 @@ NX_CHAIN_EXPLORER_URL=https://explorer.vega.trading/.netlify/functions/chain-explorer-api NX_TENDERMINT_URL=https://n03.s.vega.xyz/tm NX_TENDERMINT_WEBSOCKET_URL=wss://n03.s.vega.xyz/tm/websocket +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/stagnet1-network.json NX_VEGA_URL=https://n03.s.vega.xyz/query NX_VEGA_NETWORKS='{"TESTNET":"https://explorer.fairground.wtf","MAINNET":"https://explorer.vega.xyz"}' NX_VEGA_ENV=STAGNET diff --git a/apps/explorer/.env.stagnet2 b/apps/explorer/.env.stagnet2 index 991030406..38baff594 100644 --- a/apps/explorer/.env.stagnet2 +++ b/apps/explorer/.env.stagnet2 @@ -2,6 +2,7 @@ NX_CHAIN_EXPLORER_URL=https://explorer.vega.trading/.netlify/functions/chain-explorer-api NX_TENDERMINT_URL=https://n03.stagnet2.vega.xyz/tm NX_TENDERMINT_WEBSOCKET_URL=wss://n03.stagnet2.vega.xyz/tm/websocket +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/stagnet2-network.json NX_VEGA_URL=https://n03.stagnet2.vega.xyz/query NX_VEGA_NETWORKS='{"TESTNET":"https://explorer.fairground.wtf","MAINNET":"https://explorer.vega.xyz"}' NX_VEGA_ENV=STAGNET2 diff --git a/apps/explorer/.env.testnet b/apps/explorer/.env.testnet index a6aca9546..750098142 100644 --- a/apps/explorer/.env.testnet +++ b/apps/explorer/.env.testnet @@ -2,6 +2,7 @@ NX_CHAIN_EXPLORER_URL=https://explorer.vega.trading/.netlify/functions/chain-explorer-api NX_TENDERMINT_URL=https://lb.testnet.vega.xyz/tm NX_TENDERMINT_WEBSOCKET_URL=wss://lb.testnet.vega.xyz/tm/websocket +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/testnet-network.json NX_VEGA_URL=https://lb.testnet.vega.xyz/query NX_VEGA_NETWORKS='{"TESTNET":"https://explorer.fairground.wtf","MAINNET":"https://explorer.vega.xyz"}' NX_VEGA_ENV=TESTNET diff --git a/apps/explorer/src/app/app.tsx b/apps/explorer/src/app/app.tsx index 5d025872f..088ae5594 100644 --- a/apps/explorer/src/app/app.tsx +++ b/apps/explorer/src/app/app.tsx @@ -1,13 +1,11 @@ -import { useState, useEffect, useMemo } from 'react'; +import { useState, useEffect } from 'react'; import { useLocation } from 'react-router-dom'; -import { ApolloProvider } from '@apollo/client'; import { ThemeContext, useThemeSwitcher } from '@vegaprotocol/react-helpers'; -import { EnvironmentProvider } from '@vegaprotocol/network-switcher'; +import { EnvironmentProvider, NetworkLoader } from '@vegaprotocol/environment'; import { createClient } from './lib/apollo-client'; import { Nav } from './components/nav'; import { Header } from './components/header'; import { Main } from './components/main'; -import { DATA_SOURCES } from './config'; import { TendermintWebsocketProvider } from './contexts/websocket/tendermint-websocket-provider'; function App() { @@ -20,13 +18,11 @@ function App() { setMenuOpen(false); }, [location]); - const client = useMemo(() => createClient(DATA_SOURCES.dataNodeUrl), []); - return ( - +
-
+
diff --git a/apps/simple-trading-app/.env b/apps/simple-trading-app/.env index e3e9ddb62..72bdc67a8 100644 --- a/apps/simple-trading-app/.env +++ b/apps/simple-trading-app/.env @@ -17,3 +17,7 @@ NX_INCOMING_HOOK_BODY=$INCOMING_HOOK_BODY NX_URL=$URL NX_DEPLOY_URL=$DEPLOY_URL NX_DEPLOY_PRIME_URL=$DEPLOY_PRIME_URL + +NX_VEGA_CONFIG_URL="https://static.vega.xyz/assets/testnet-network.json" +NX_VEGA_ENV = 'TESTNET' +NX_VEGA_URL="https://lb.testnet.vega.xyz/query" diff --git a/apps/simple-trading-app/.env.devnet b/apps/simple-trading-app/.env.devnet index e708febd6..eee3a007a 100644 --- a/apps/simple-trading-app/.env.devnet +++ b/apps/simple-trading-app/.env.devnet @@ -1,4 +1,5 @@ # App configuration variables +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/devnet-network.json NX_VEGA_URL=https://n04.d.vega.xyz/query NX_VEGA_ENV=DEVNET NX_VEGA_REST=https://n04.d.vega.xyz/datanode/rest diff --git a/apps/simple-trading-app/.env.mainnet b/apps/simple-trading-app/.env.mainnet index 59a12c1d3..f064d6d24 100644 --- a/apps/simple-trading-app/.env.mainnet +++ b/apps/simple-trading-app/.env.mainnet @@ -1,4 +1,5 @@ # App configuration variables +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/mainnet-network.json NX_VEGA_URL=https://api.token.vega.xyz/query NX_VEGA_ENV=MAINNET NX_VEGA_REST=https://api.token.vega.xyz/ diff --git a/apps/simple-trading-app/.env.stagnet1 b/apps/simple-trading-app/.env.stagnet1 index 75573c36a..df03774f2 100644 --- a/apps/simple-trading-app/.env.stagnet1 +++ b/apps/simple-trading-app/.env.stagnet1 @@ -1,4 +1,5 @@ # App configuration variables +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/stagnet1-network.json NX_VEGA_URL=https://n03.s.vega.xyz/query NX_VEGA_ENV=STAGNET NX_VEGA_REST=https://n03.s.vega.xyz/datanode/rest diff --git a/apps/simple-trading-app/.env.stagnet2 b/apps/simple-trading-app/.env.stagnet2 index e3ea4b625..665e0ad12 100644 --- a/apps/simple-trading-app/.env.stagnet2 +++ b/apps/simple-trading-app/.env.stagnet2 @@ -1,4 +1,5 @@ # App configuration variables +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/stagnet2-network.json NX_VEGA_URL=https://n03.stagnet2.vega.xyz/query NX_VEGA_ENV=STAGNET2 NX_VEGA_REST=https://n01.stagnet2.vega.xyz/datanode/rest diff --git a/apps/simple-trading-app/.env.testnet b/apps/simple-trading-app/.env.testnet index f0fd1afc0..1b572c593 100644 --- a/apps/simple-trading-app/.env.testnet +++ b/apps/simple-trading-app/.env.testnet @@ -1,4 +1,5 @@ # App configuration variables +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/testnet-network.json NX_VEGA_URL=https://lb.testnet.vega.xyz/query NX_VEGA_ENV=TESTNET NX_VEGA_REST=https://lb.testnet.vega.xyz/datanode/rest diff --git a/apps/simple-trading-app/src/app/app.tsx b/apps/simple-trading-app/src/app/app.tsx index bb8213af8..10bbe605e 100644 --- a/apps/simple-trading-app/src/app/app.tsx +++ b/apps/simple-trading-app/src/app/app.tsx @@ -1,15 +1,13 @@ -import React, { useState, useMemo, useEffect } from 'react'; -import { ApolloProvider } from '@apollo/client'; +import { useState, useEffect } from 'react'; import { ThemeContext } from '@vegaprotocol/react-helpers'; import { useThemeSwitcher } from '@vegaprotocol/react-helpers'; +import { EnvironmentProvider, NetworkLoader } from '@vegaprotocol/environment'; import { createClient } from './lib/apollo-client'; -import { DATA_SOURCES } from './config'; import { VegaConnectDialog, VegaManageDialog, VegaWalletProvider, } from '@vegaprotocol/wallet'; -import { EnvironmentProvider } from '@vegaprotocol/network-switcher'; import { VegaWalletConnectButton } from './components/vega-wallet-connect-button'; import { ThemeSwitcher } from '@vegaprotocol/ui-toolkit'; import { Connectors } from './lib/vega-connectors'; @@ -26,8 +24,6 @@ function App() { manage: false, }); - const client = useMemo(() => createClient(DATA_SOURCES.dataNodeUrl), []); - const [menuOpen, setMenuOpen] = useState(false); const onToggle = () => setMenuOpen(!menuOpen); @@ -40,7 +36,7 @@ function App() { return ( - +
@@ -82,7 +78,7 @@ function App() {
-
+
); diff --git a/apps/simple-trading-app/src/app/config/index.tsx b/apps/simple-trading-app/src/app/config/index.tsx deleted file mode 100644 index b309e30f9..000000000 --- a/apps/simple-trading-app/src/app/config/index.tsx +++ /dev/null @@ -1,5 +0,0 @@ -export const DATA_SOURCES = { - dataNodeUrl: process.env['NX_VEGA_URL'] as string, - envName: process.env['NX_VEGA_ENV'] as string, - restEndpoint: process.env['NX_VEGA_REST'] as string, -}; diff --git a/apps/static/src/assets/devnet-network.json b/apps/static/src/assets/devnet-network.json new file mode 100644 index 000000000..0ae00e51b --- /dev/null +++ b/apps/static/src/assets/devnet-network.json @@ -0,0 +1,3 @@ +{ + "hosts": ["https://n04.d.vega.xyz/query"] +} diff --git a/apps/static/src/assets/mainnet-network.json b/apps/static/src/assets/mainnet-network.json new file mode 100644 index 000000000..edc8eb86b --- /dev/null +++ b/apps/static/src/assets/mainnet-network.json @@ -0,0 +1,17 @@ +{ + "hosts": [ + "https://vega-data-graphql.chorus.one/query", + "https://vega.xprv.io/datanode/query", + "http://nala.mainnet.vega.community:3008/query", + "http://commodum.mainnet.vega.community:3008/query", + "http://lovali.mainnet.vega.community:3008/query", + "http://b-harvest.mainnet.vega.community:3008/query", + "http://staking-facilities.mainnet.vega.community:3008/query", + "http://figment.mainnet.vega.community:3008/query", + "http://nodes-guru.mainnet.vega.community:3008/query", + "http://p2p.mainnet.vega.community:3008/query", + "http://rockaway.mainnet.vega.community:3008/query", + "http://greenfield-one.mainnet.vega.community:3008/query", + "http://ryabina.mainnet.vega.community:3008/query" + ] +} diff --git a/apps/static/src/assets/stagnet1-network.json b/apps/static/src/assets/stagnet1-network.json new file mode 100644 index 000000000..c912e6233 --- /dev/null +++ b/apps/static/src/assets/stagnet1-network.json @@ -0,0 +1,3 @@ +{ + "hosts": ["https://n03.s.vega.xyz/query"] +} diff --git a/apps/static/src/assets/stagnet2-network.json b/apps/static/src/assets/stagnet2-network.json new file mode 100644 index 000000000..7d61d8ca1 --- /dev/null +++ b/apps/static/src/assets/stagnet2-network.json @@ -0,0 +1,3 @@ +{ + "hosts": ["https://n03.stagnet2.vega.xyz/query"] +} diff --git a/apps/static/src/assets/testnet-network.json b/apps/static/src/assets/testnet-network.json new file mode 100644 index 000000000..8e6e13295 --- /dev/null +++ b/apps/static/src/assets/testnet-network.json @@ -0,0 +1,3 @@ +{ + "hosts": ["https://lb.testnet.vega.xyz/query"] +} diff --git a/apps/stats/src/app.tsx b/apps/stats/src/app.tsx index 1b1ef0ab3..8fbdfafa5 100644 --- a/apps/stats/src/app.tsx +++ b/apps/stats/src/app.tsx @@ -3,7 +3,6 @@ import { DATA_SOURCES } from './config'; import { Header } from './components/header'; import { StatsManager } from '@vegaprotocol/network-stats'; import { ThemeContext } from '@vegaprotocol/react-helpers'; -import { EnvironmentProvider } from '@vegaprotocol/network-switcher'; import { useThemeSwitcher } from '@vegaprotocol/react-helpers'; const envName = DATA_SOURCES.envName; @@ -15,21 +14,19 @@ function App() { const [theme, toggleTheme] = useThemeSwitcher(); return ( - - -
-
-
- -
+ +
+
+
+
- - +
+
); } diff --git a/apps/token-e2e/.env b/apps/token-e2e/.env index dd881f4d0..cb95c70e4 100644 --- a/apps/token-e2e/.env +++ b/apps/token-e2e/.env @@ -21,7 +21,6 @@ REACT_APP_DEPLOY_PRIME_URL=$DEPLOY_PRIME_URL # App configuration variables NX_VEGA_ENV=TESTNET NX_VEGA_URL=https://lb.testnet.vega.xyz/query -NX_ETHEREUM_CHAIN_ID=3 NX_ETHEREUM_PROVIDER_URL=https://ropsten.infura.io/v3/4f846e79e13f44d1b51bbd7ed9edefb8 NX_ETHERSCAN_URL=https://ropsten.etherscan.io NX_FAIRGROUND=false diff --git a/apps/token-e2e/.env.devnet b/apps/token-e2e/.env.devnet index 1d2f6b979..1b684b256 100644 --- a/apps/token-e2e/.env.devnet +++ b/apps/token-e2e/.env.devnet @@ -1,6 +1,5 @@ # App configuration variables NX_VEGA_ENV=DEVNET NX_VEGA_URL=https://n04.d.vega.xyz/query -NX_ETHEREUM_CHAIN_ID=3 NX_ETHEREUM_PROVIDER_URL=https://ropsten.infura.io/v3/4f846e79e13f44d1b51bbd7ed9edefb8 NX_ETHERSCAN_URL=https://ropsten.etherscan.io diff --git a/apps/token-e2e/.env.mainnet b/apps/token-e2e/.env.mainnet index 92c3a7893..333947031 100644 --- a/apps/token-e2e/.env.mainnet +++ b/apps/token-e2e/.env.mainnet @@ -1,6 +1,5 @@ # App configuration variables NX_VEGA_ENV=MAINNET NX_VEGA_URL=https://api.token.vega.xyz/query -NX_ETHEREUM_CHAIN_ID=1 NX_ETHEREUM_PROVIDER_URL=https://mainnet.infura.io/v3/4f846e79e13f44d1b51bbd7ed9edefb8 NX_ETHERSCAN_URL=https://etherscan.io diff --git a/apps/token-e2e/.env.stagnet1 b/apps/token-e2e/.env.stagnet1 index 6a0b047bc..bc321d731 100644 --- a/apps/token-e2e/.env.stagnet1 +++ b/apps/token-e2e/.env.stagnet1 @@ -1,6 +1,5 @@ # App configuration variables NX_VEGA_ENV=STAGNET NX_VEGA_URL=https://n03.s.vega.xyz/query -NX_ETHEREUM_CHAIN_ID=3 NX_ETHEREUM_PROVIDER_URL=https://ropsten.infura.io/v3/4f846e79e13f44d1b51bbd7ed9edefb8 NX_ETHERSCAN_URL=https://ropsten.etherscan.io diff --git a/apps/token-e2e/.env.stagnet2 b/apps/token-e2e/.env.stagnet2 index 06904d1ed..813dce012 100644 --- a/apps/token-e2e/.env.stagnet2 +++ b/apps/token-e2e/.env.stagnet2 @@ -1,6 +1,5 @@ # App configuration variables NX_VEGA_ENV=STAGNET2 NX_VEGA_URL=https://n03.stagnet2.vega.xyz/query -NX_ETHEREUM_CHAIN_ID=3 NX_ETHEREUM_PROVIDER_URL=https://ropsten.infura.io/v3/4f846e79e13f44d1b51bbd7ed9edefb8 NX_ETHERSCAN_URL=https://ropsten.etherscan.io diff --git a/apps/token-e2e/.env.testnet b/apps/token-e2e/.env.testnet index 65316dde5..9da0c78ab 100644 --- a/apps/token-e2e/.env.testnet +++ b/apps/token-e2e/.env.testnet @@ -1,6 +1,5 @@ # App configuration variables NX_VEGA_ENV=TESTNET NX_VEGA_URL=https://lb.testnet.vega.xyz/query -NX_ETHEREUM_CHAIN_ID=3 NX_ETHEREUM_PROVIDER_URL=https://ropsten.infura.io/v3/4f846e79e13f44d1b51bbd7ed9edefb8 NX_ETHERSCAN_URL=https://ropsten.etherscan.io diff --git a/apps/token/.env b/apps/token/.env index ee3673919..a9843234a 100644 --- a/apps/token/.env +++ b/apps/token/.env @@ -20,8 +20,8 @@ REACT_APP_DEPLOY_PRIME_URL=$DEPLOY_PRIME_URL # App configuration variables NX_VEGA_ENV=TESTNET +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/testnet-network.json NX_VEGA_URL=https://lb.testnet.vega.xyz/query -NX_ETHEREUM_CHAIN_ID=3 NX_ETHEREUM_PROVIDER_URL=https://ropsten.infura.io/v3/4f846e79e13f44d1b51bbd7ed9edefb8 NX_ETHERSCAN_URL=https://ropsten.etherscan.io NX_FAIRGROUND=false diff --git a/apps/token/.env.devnet b/apps/token/.env.devnet index 23f0ec1f2..7e2b6866b 100644 --- a/apps/token/.env.devnet +++ b/apps/token/.env.devnet @@ -1,7 +1,7 @@ # App configuration variables NX_VEGA_ENV=DEVNET +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/devnet-network.json NX_VEGA_URL=https://n04.d.vega.xyz/query NX_VEGA_NETWORKS='{"DEVNET":"https://dev.token.vega.xyz","STAGNET":"https://dev.token.vega.xyz","STAGNET2":"staging2.token.vega.xyz","TESTNET":"token.fairground.wtf","MAINNET":"token.vega.xyz"}' -NX_ETHEREUM_CHAIN_ID=3 NX_ETHEREUM_PROVIDER_URL=https://ropsten.infura.io/v3/4f846e79e13f44d1b51bbd7ed9edefb8 NX_ETHERSCAN_URL=https://ropsten.etherscan.io diff --git a/apps/token/.env.mainnet b/apps/token/.env.mainnet index 910217e5c..830652c43 100644 --- a/apps/token/.env.mainnet +++ b/apps/token/.env.mainnet @@ -1,7 +1,7 @@ # App configuration variables NX_VEGA_ENV=MAINNET +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/mainnet-network.json NX_VEGA_URL=https://api.token.vega.xyz/query NX_VEGA_NETWORKS='{"DEVNET":"https://dev.token.vega.xyz","STAGNET":"https://dev.token.vega.xyz","STAGNET2":"staging2.token.vega.xyz","TESTNET":"token.fairground.wtf","MAINNET":"token.vega.xyz"}' -NX_ETHEREUM_CHAIN_ID=1 NX_ETHEREUM_PROVIDER_URL=https://mainnet.infura.io/v3/4f846e79e13f44d1b51bbd7ed9edefb8 NX_ETHERSCAN_URL=https://etherscan.io diff --git a/apps/token/.env.stagnet1 b/apps/token/.env.stagnet1 index 06c33fb72..b329754dc 100644 --- a/apps/token/.env.stagnet1 +++ b/apps/token/.env.stagnet1 @@ -1,7 +1,7 @@ # App configuration variables NX_VEGA_ENV=STAGNET +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/stagnet1-network.json NX_VEGA_URL=https://n03.s.vega.xyz/query NX_VEGA_NETWORKS='{"DEVNET":"https://dev.token.vega.xyz","STAGNET":"https://dev.token.vega.xyz","STAGNET2":"staging2.token.vega.xyz","TESTNET":"token.fairground.wtf","MAINNET":"token.vega.xyz"}' -NX_ETHEREUM_CHAIN_ID=3 NX_ETHEREUM_PROVIDER_URL=https://ropsten.infura.io/v3/4f846e79e13f44d1b51bbd7ed9edefb8 NX_ETHERSCAN_URL=https://ropsten.etherscan.io diff --git a/apps/token/.env.stagnet2 b/apps/token/.env.stagnet2 index c245d632b..bb04f26af 100644 --- a/apps/token/.env.stagnet2 +++ b/apps/token/.env.stagnet2 @@ -1,7 +1,7 @@ # App configuration variables NX_VEGA_ENV=STAGNET2 +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/stagnet2-network.json NX_VEGA_URL=https://n03.stagnet2.vega.xyz/query NX_VEGA_NETWORKS='{"DEVNET":"https://dev.token.vega.xyz","STAGNET":"https://dev.token.vega.xyz","STAGNET2":"staging2.token.vega.xyz","TESTNET":"token.fairground.wtf","MAINNET":"token.vega.xyz"}' -NX_ETHEREUM_CHAIN_ID=3 NX_ETHEREUM_PROVIDER_URL=https://ropsten.infura.io/v3/4f846e79e13f44d1b51bbd7ed9edefb8 NX_ETHERSCAN_URL=https://ropsten.etherscan.io diff --git a/apps/token/.env.testnet b/apps/token/.env.testnet index b4abf6a60..b9f668029 100644 --- a/apps/token/.env.testnet +++ b/apps/token/.env.testnet @@ -1,7 +1,7 @@ # App configuration variables NX_VEGA_ENV=TESTNET +NX_VEGA_CONFIG_URL=https://static.vega.xyz/assets/testnet-network.json NX_VEGA_URL=https://lb.testnet.vega.xyz/query NX_VEGA_NETWORKS='{"DEVNET":"https://dev.token.vega.xyz","STAGNET":"https://dev.token.vega.xyz","STAGNET2":"staging2.token.vega.xyz","TESTNET":"token.fairground.wtf","MAINNET":"token.vega.xyz"}' -NX_ETHEREUM_CHAIN_ID=3 NX_ETHEREUM_PROVIDER_URL=https://ropsten.infura.io/v3/4f846e79e13f44d1b51bbd7ed9edefb8 NX_ETHERSCAN_URL=https://ropsten.etherscan.io diff --git a/apps/token/README.md b/apps/token/README.md index ada33d46e..18d550969 100644 --- a/apps/token/README.md +++ b/apps/token/README.md @@ -48,13 +48,13 @@ There are a few different configuration options offered for this app: | `NX_APP_INFURA_ID` | Infura fallback for if the user does not have a web3 compatible browser | | `NX_APP_HOSTED_WALLET_ENABLED` | If the hosted wallet is enabled or not. If so then allow users to login using the hosted wallet | | `NX_APP_ENV` | Change network to connect to. When set to CUSTOM use CUSTOM\_\* vars for network parameters | -| `CUSTOM_URLS` | When NX_APP_ENV=CUSTOM use these Data Node REST URLs, optional if CUSTOM_URLS_WITH_GRAPHQL is used. | -| `CUSTOM_URLS_WITH_GRAPHQL` | When NX_APP_ENV=CUSTOM use these Data Node GraphQL URLs, optional if CUSTOM_URLS is used. | -| `CUSTOM_TOKEN_ADDRESS` | When NX_APP_ENV=CUSTOM specify Vega token address. | -| `CUSTOM_CLAIM_ADDRESS` | When NX_APP_ENV=CUSTOM specify Vega claim address. | -| `CUSTOM_LOCKED_ADDRESS` | When NX_APP_ENV=CUSTOM specify Vega locked address. | -| `CUSTOM_VESTING_ADDRESS` | When NX_APP_ENV=CUSTOM specify Vega vesting address. | -| `CUSTOM_STAKING_BRIDGE` | When NX_APP_ENV=CUSTOM specify Vega staking bridge address. | +| `NX_CUSTOM_URLS` | When NX_APP_ENV=CUSTOM use these Data Node REST URLs, optional if CUSTOM_URLS_WITH_GRAPHQL is used. | +| `NX_CUSTOM_URLS_WITH_GRAPHQL` | When NX_APP_ENV=CUSTOM use these Data Node GraphQL URLs, optional if CUSTOM_URLS is used. | +| `NX_CUSTOM_TOKEN_ADDRESS` | When NX_APP_ENV=CUSTOM specify Vega token address. | +| `NX_CUSTOM_CLAIM_ADDRESS` | When NX_APP_ENV=CUSTOM specify Vega claim address. | +| `NX_CUSTOM_LOCKED_ADDRESS` | When NX_APP_ENV=CUSTOM specify Vega locked address. | +| `NX_CUSTOM_VESTING_ADDRESS` | When NX_APP_ENV=CUSTOM specify Vega vesting address. | +| `NX_CUSTOM_STAKING_BRIDGE` | When NX_APP_ENV=CUSTOM specify Vega staking bridge address. | ## Example configs: diff --git a/apps/token/src/app.tsx b/apps/token/src/app.tsx index 7baef0696..1c4da6e53 100644 --- a/apps/token/src/app.tsx +++ b/apps/token/src/app.tsx @@ -17,60 +17,67 @@ import { AppRouter } from './routes'; import { Web3Provider } from '@vegaprotocol/web3'; import { VegaWalletDialogs } from './components/vega-wallet-dialogs'; import { VegaWalletProvider } from '@vegaprotocol/wallet'; -import { createConnectors } from './lib/web3-connectors'; -import { ApolloProvider } from '@apollo/client'; -import { createClient } from './lib/apollo-client'; +import { AsyncRenderer } from '@vegaprotocol/ui-toolkit'; +import { useEthereumConfig } from '@vegaprotocol/web3'; import { - EnvironmentProvider, useEnvironment, -} from '@vegaprotocol/network-switcher'; + EnvironmentProvider, + NetworkLoader, +} from '@vegaprotocol/environment'; +import { createClient } from './lib/apollo-client'; +import { createConnectors } from './lib/web3-connectors'; const AppContainer = () => { const sideBar = React.useMemo(() => [, ], []); - const { ETHEREUM_PROVIDER_URL, ETHEREUM_CHAIN_ID, VEGA_URL } = - useEnvironment(); - const Connectors = useMemo( - () => createConnectors(ETHEREUM_PROVIDER_URL, ETHEREUM_CHAIN_ID), - [ETHEREUM_CHAIN_ID, ETHEREUM_PROVIDER_URL] - ); - const client = useMemo(() => createClient(VEGA_URL), [VEGA_URL]); + const { config, loading, error } = useEthereumConfig(); + const { ETHEREUM_PROVIDER_URL } = useEnvironment(); + const Connectors = useMemo(() => { + if (config?.chain_id) { + return createConnectors(ETHEREUM_PROVIDER_URL, Number(config.chain_id)); + } + return undefined; + }, [config?.chain_id, ETHEREUM_PROVIDER_URL]); return ( - - - - - - - - - - <> -
- - - - - -
- - - -
-
-
-
-
-
-
-
-
+ + + + {Connectors && ( + + + + + + + <> +
+ + + + + +
+ + + +
+
+
+
+
+
+ )} +
+
+
); }; function App() { return ( - + + + ); } diff --git a/apps/token/src/components/add-locked-token/add-locked-token.tsx b/apps/token/src/components/add-locked-token/add-locked-token.tsx index a715e6bfa..771a699e9 100644 --- a/apps/token/src/components/add-locked-token/add-locked-token.tsx +++ b/apps/token/src/components/add-locked-token/add-locked-token.tsx @@ -1,15 +1,14 @@ import { useTranslation } from 'react-i18next'; -import { useEnvironment } from '@vegaprotocol/network-switcher'; import { useAddAssetSupported } from '../../hooks/use-add-asset-to-wallet'; import vegaVesting from '../../images/vega_vesting.png'; import { AddTokenButtonLink } from '../add-token-button/add-token-button'; import { Callout } from '@vegaprotocol/ui-toolkit'; +import { ENV } from '../../config/env'; export const AddLockedTokenAddress = () => { const { t } = useTranslation(); const addSupported = useAddAssetSupported(); - const { ADDRESSES } = useEnvironment(); return ( { <>

{ {t( 'The token address is {{address}}. Hit the add token button in your ERC20 wallet and enter this address.', { - address: ADDRESSES.lockedAddress, + address: ENV.addresses.lockedAddress, } )}

diff --git a/apps/token/src/components/transaction-callout/transaction-complete.tsx b/apps/token/src/components/transaction-callout/transaction-complete.tsx index b859ffbb9..079ca8004 100644 --- a/apps/token/src/components/transaction-callout/transaction-complete.tsx +++ b/apps/token/src/components/transaction-callout/transaction-complete.tsx @@ -1,7 +1,7 @@ import { Callout, Intent } from '@vegaprotocol/ui-toolkit'; import { useTranslation } from 'react-i18next'; import { Link } from '@vegaprotocol/ui-toolkit'; -import { useEnvironment } from '@vegaprotocol/network-switcher'; +import { useEnvironment } from '@vegaprotocol/environment'; import type { ReactElement } from 'react'; export const TransactionComplete = ({ diff --git a/apps/token/src/components/transaction-callout/transaction-error.tsx b/apps/token/src/components/transaction-callout/transaction-error.tsx index 2bbf4601d..393dc491c 100644 --- a/apps/token/src/components/transaction-callout/transaction-error.tsx +++ b/apps/token/src/components/transaction-callout/transaction-error.tsx @@ -2,7 +2,7 @@ import { Button, Callout, Intent } from '@vegaprotocol/ui-toolkit'; import { useTranslation } from 'react-i18next'; import { Link } from '@vegaprotocol/ui-toolkit'; -import { useEnvironment } from '@vegaprotocol/network-switcher'; +import { useEnvironment } from '@vegaprotocol/environment'; export interface TransactionErrorProps { error: Error | null; diff --git a/apps/token/src/components/transaction-callout/transaction-pending.tsx b/apps/token/src/components/transaction-callout/transaction-pending.tsx index 42192a124..07fecf1ec 100644 --- a/apps/token/src/components/transaction-callout/transaction-pending.tsx +++ b/apps/token/src/components/transaction-callout/transaction-pending.tsx @@ -2,7 +2,7 @@ import React from 'react'; import { Callout, Loader } from '@vegaprotocol/ui-toolkit'; import { useTranslation } from 'react-i18next'; import { Link } from '@vegaprotocol/ui-toolkit'; -import { useEnvironment } from '@vegaprotocol/network-switcher'; +import { useEnvironment } from '@vegaprotocol/environment'; export const TransactionPending = ({ hash, diff --git a/apps/token/src/components/transactions-modal/transactions-modal.tsx b/apps/token/src/components/transactions-modal/transactions-modal.tsx index 26e31a80b..066f1cffa 100644 --- a/apps/token/src/components/transactions-modal/transactions-modal.tsx +++ b/apps/token/src/components/transactions-modal/transactions-modal.tsx @@ -1,5 +1,5 @@ import { Dialog, Link } from '@vegaprotocol/ui-toolkit'; -import { useEnvironment } from '@vegaprotocol/network-switcher'; +import { useEnvironment } from '@vegaprotocol/environment'; import React from 'react'; import { useTranslation } from 'react-i18next'; diff --git a/apps/token/src/components/web3-connector/web3-connector.tsx b/apps/token/src/components/web3-connector/web3-connector.tsx index dd5d6e2ea..a0aa80562 100644 --- a/apps/token/src/components/web3-connector/web3-connector.tsx +++ b/apps/token/src/components/web3-connector/web3-connector.tsx @@ -1,5 +1,6 @@ -import { useEnvironment } from '@vegaprotocol/network-switcher'; -import { Button, Splash } from '@vegaprotocol/ui-toolkit'; +import { useEnvironment } from '@vegaprotocol/environment'; +import { useEthereumConfig } from '@vegaprotocol/web3'; +import { Button, Splash, AsyncRenderer } from '@vegaprotocol/ui-toolkit'; import { Web3ConnectDialog } from '@vegaprotocol/web3'; import { useWeb3React } from '@web3-react/core'; import type { ReactElement } from 'react'; @@ -16,30 +17,35 @@ interface Web3ConnectorProps { export function Web3Connector({ children }: Web3ConnectorProps) { const { appState, appDispatch } = useAppState(); - const { ETHEREUM_PROVIDER_URL, ETHEREUM_CHAIN_ID } = useEnvironment(); - const Connectors = useMemo( - () => createConnectors(ETHEREUM_PROVIDER_URL, ETHEREUM_CHAIN_ID), - [ETHEREUM_CHAIN_ID, ETHEREUM_PROVIDER_URL] - ); + const { ETHEREUM_PROVIDER_URL } = useEnvironment(); + const { config, loading, error } = useEthereumConfig(); + const Connectors = useMemo(() => { + if (config?.chain_id) { + return createConnectors(ETHEREUM_PROVIDER_URL, Number(config.chain_id)); + } + return undefined; + }, [config?.chain_id, ETHEREUM_PROVIDER_URL]); const setDialogOpen = useCallback( (isOpen: boolean) => { appDispatch({ type: AppStateActionType.SET_ETH_WALLET_OVERLAY, isOpen }); }, [appDispatch] ); - const appChainId = Number(ETHEREUM_CHAIN_ID); + const appChainId = Number(config?.chain_id); return ( - <> + {children} - - + {Connectors && ( + + )} + ); } @@ -49,11 +55,7 @@ interface Web3ContentProps { setDialogOpen: (isOpen: boolean) => void; } -export const Web3Content = ({ - children, - appChainId, - setDialogOpen, -}: Web3ContentProps) => { +export const Web3Content = ({ children, appChainId }: Web3ContentProps) => { const { error, connector, chainId } = useWeb3React(); useEffect(() => { diff --git a/apps/token/src/config/env.ts b/apps/token/src/config/env.ts index fc335b544..48df11587 100644 --- a/apps/token/src/config/env.ts +++ b/apps/token/src/config/env.ts @@ -1,3 +1,5 @@ +import type { Networks } from '@vegaprotocol/environment'; + const windowOrDefault = (key: string) => { if (window._env_ && window._env_[key]) { return window._env_[key]; @@ -7,10 +9,49 @@ const windowOrDefault = (key: string) => { const TRUTHY = ['1', 'true']; +interface VegaContracts { + claimAddress: string; + lockedAddress: string; +} + +const customClaimAddress = process.env['NX_CUSTOM_CLAIM_ADDRESS'] as string; +const customLockedAddress = process.env['NX_CUSTOM_LOCKED_ADDRESS'] as string; + +export const ContractAddresses: { + [key in Networks | 'CUSTOM']: VegaContracts; +} = { + CUSTOM: { + claimAddress: customClaimAddress ?? '0x0', + lockedAddress: customLockedAddress ?? '0x0', + }, + DEVNET: { + claimAddress: '0x8Cef746ab7C83B61F6461cC92882bD61AB65a994', + lockedAddress: '0x0', + }, + STAGNET: { + claimAddress: '0x8Cef746ab7C83B61F6461cC92882bD61AB65a994', // TODO not deployed to this env, but random address so app doesn't error + lockedAddress: '0x0', // TODO not deployed to this env + }, + STAGNET2: { + claimAddress: '0x8Cef746ab7C83B61F6461cC92882bD61AB65a994', // TODO not deployed to this env, but random address so app doesn't error + lockedAddress: '0x0', // TODO not deployed to this env + }, + TESTNET: { + claimAddress: '0x8Cef746ab7C83B61F6461cC92882bD61AB65a994', // TODO not deployed to this env, but random address so app doesn't error + lockedAddress: '0x0', // TODO not deployed to this env + }, + MAINNET: { + claimAddress: '0x0ee1fb382caf98e86e97e51f9f42f8b4654020f3', + lockedAddress: '0x78344c7305d73a7a0ac3c94cd9960f4449a1814e', + }, +}; + +const envName = windowOrDefault('NX_VEGA_ENV') ?? 'local'; + export const ENV = { // Environment dsn: windowOrDefault('NX_SENTRY_DSN'), - envName: windowOrDefault('NX_VEGA_ENV'), + envName, commit: windowOrDefault('NX_COMMIT_REF'), branch: windowOrDefault('NX_BRANCH'), vegaUrl: windowOrDefault('NX_VEGA_URL'), @@ -26,4 +67,6 @@ export const ENV = { process.env['NX_IS_NEW_BRIDGE_CONTRACT'] as string ), }, + addresses: + ContractAddresses[(envName === 'local' ? 'CUSTOM' : envName) as Networks], }; diff --git a/apps/token/src/config/index.ts b/apps/token/src/config/index.ts index 9df05ee55..095810f4f 100644 --- a/apps/token/src/config/index.ts +++ b/apps/token/src/config/index.ts @@ -1,4 +1,3 @@ export * from './flags'; export * from './links'; export * from './network-params'; -export * from './vega'; diff --git a/apps/token/src/config/vega.ts b/apps/token/src/config/vega.ts deleted file mode 100644 index 29752076f..000000000 --- a/apps/token/src/config/vega.ts +++ /dev/null @@ -1,176 +0,0 @@ -import { Networks } from '@vegaprotocol/react-helpers'; - -interface VegaNode { - url: string; - api: { - GraphQL: boolean; - }; -} - -type VegaNets = { - [N in Networks]: { - nodes: VegaNode[]; - }; -}; - -export type NetworkConfig = { - [N in Networks]: string[]; -}; - -export const VegaNetworks: VegaNets = { - [Networks.DEVNET]: { - nodes: [ - { - url: 'https://n01.d.vega.xyz', - api: { - GraphQL: false, - }, - }, - { - url: 'https://n02.d.vega.xyz', - api: { - GraphQL: false, - }, - }, - { - url: 'https://n03.d.vega.xyz', - api: { - GraphQL: false, - }, - }, - { - url: 'https://n04.d.vega.xyz', - api: { - GraphQL: true, - }, - }, - ], - }, - [Networks.STAGNET]: { - nodes: [ - { - url: 'https://n01.s.vega.xyz', - api: { - GraphQL: false, - }, - }, - { - url: 'https://n02.s.vega.xyz', - api: { - GraphQL: false, - }, - }, - { - url: 'https://n03.s.vega.xyz', - api: { - GraphQL: true, - }, - }, - { - url: 'https://n04.s.vega.xyz', - api: { - GraphQL: false, - }, - }, - { - url: 'https://n05.s.vega.xyz', - api: { - GraphQL: false, - }, - }, - ], - }, - [Networks.STAGNET2]: { - nodes: [ - { - url: 'https://n03.stagnet2.vega.xyz', - api: { - GraphQL: true, - }, - }, - ], - }, - [Networks.TESTNET]: { - nodes: [ - { - url: 'https://lb.testnet.vega.xyz', - api: { - GraphQL: true, - }, - }, - { - url: 'https://n01.testnet.vega.xyz', - api: { - GraphQL: false, - }, - }, - { - url: 'https://n02.testnet.vega.xyz', - api: { - GraphQL: false, - }, - }, - { - url: 'https://n03.testnet.vega.xyz', - api: { - GraphQL: false, - }, - }, - { - url: 'https://n04.testnet.vega.xyz', - api: { - GraphQL: false, - }, - }, - { - url: 'https://n05.testnet.vega.xyz', - api: { - GraphQL: false, - }, - }, - { - url: 'https://n06.testnet.vega.xyz', - api: { - GraphQL: true, - }, - }, - { - url: 'https://n07.testnet.vega.xyz', - api: { - GraphQL: true, - }, - }, - { - url: 'https://n08.testnet.vega.xyz', - api: { - GraphQL: true, - }, - }, - { - url: 'https://n09.testnet.vega.xyz', - api: { - GraphQL: true, - }, - }, - ], - }, - [Networks.MAINNET]: { - nodes: [], - }, -}; - -export const GraphQLNodes = Object.keys(VegaNetworks).reduce( - (obj: Record, network) => { - const rawNodes: VegaNode[] = VegaNetworks[network as Networks].nodes; - const nodesWithGraphQL = rawNodes - .filter((n) => n.api.GraphQL) - .map((n) => n.url); - obj[network] = nodesWithGraphQL; - return obj; - }, - {} -); - -export const EnvironmentNodes = GraphQLNodes[ - process.env['NX_VEGA_ENV'] as Networks -] as string[]; diff --git a/apps/token/src/contexts/contracts/contracts-provider.tsx b/apps/token/src/contexts/contracts/contracts-provider.tsx index d99d416fc..c3896dc3c 100644 --- a/apps/token/src/contexts/contracts/contracts-provider.tsx +++ b/apps/token/src/contexts/contracts/contracts-provider.tsx @@ -6,14 +6,15 @@ import { } from '@vegaprotocol/smart-contracts'; import { Splash } from '@vegaprotocol/ui-toolkit'; import { useWeb3React } from '@web3-react/core'; -import React, { useMemo } from 'react'; +import React from 'react'; import { SplashLoader } from '../../components/splash-loader'; import type { ContractsContextShape } from './contracts-context'; import { ContractsContext } from './contracts-context'; import { createDefaultProvider } from '../../lib/web3-connectors'; import { useEthereumConfig } from '@vegaprotocol/web3'; -import { useEnvironment } from '@vegaprotocol/network-switcher'; +import { useEnvironment } from '@vegaprotocol/environment'; +import { ENV } from '../../config/env'; /** * Provides Vega Ethereum contract instances to its children. @@ -21,14 +22,9 @@ import { useEnvironment } from '@vegaprotocol/network-switcher'; export const ContractsProvider = ({ children }: { children: JSX.Element }) => { const { provider: activeProvider, account } = useWeb3React(); const { config } = useEthereumConfig(); - const { VEGA_ENV, ADDRESSES, ETHEREUM_PROVIDER_URL, ETHEREUM_CHAIN_ID } = - useEnvironment(); + const { VEGA_ENV, ETHEREUM_PROVIDER_URL } = useEnvironment(); const [contracts, setContracts] = React.useState(null); - const defaultProvider = useMemo( - () => createDefaultProvider(ETHEREUM_PROVIDER_URL, ETHEREUM_CHAIN_ID), - [ETHEREUM_PROVIDER_URL, ETHEREUM_CHAIN_ID] - ); // Create instances of contract classes. If we have an account use a signer for the // contracts so that we can sign transactions, otherwise use the provider for just @@ -37,39 +33,46 @@ export const ContractsProvider = ({ children }: { children: JSX.Element }) => { const run = async () => { let signer = null; - const provider = activeProvider ? activeProvider : defaultProvider; - - if ( - account && - activeProvider && - typeof activeProvider.getSigner === 'function' - ) { - signer = provider.getSigner(); - } - - if (provider && config) { - const staking = new StakingBridge( - config.staking_bridge_contract.address, - signer || provider + if (config) { + const defaultProvider = createDefaultProvider( + ETHEREUM_PROVIDER_URL, + Number(config.chain_id) ); - const vegaAddress = await staking.stakingToken(); - setContracts({ - token: new Token(vegaAddress, signer || provider), - staking: new StakingBridge( + const provider = activeProvider ? activeProvider : defaultProvider; + + if ( + account && + activeProvider && + typeof activeProvider.getSigner === 'function' + ) { + signer = provider.getSigner(); + } + + if (provider && config) { + const staking = new StakingBridge( config.staking_bridge_contract.address, signer || provider - ), - vesting: new TokenVesting( - config.token_vesting_contract.address, - signer || provider - ), - claim: new Claim(ADDRESSES.claimAddress, signer || provider), - }); + ); + const vegaAddress = await staking.stakingToken(); + + setContracts({ + token: new Token(vegaAddress, signer || provider), + staking: new StakingBridge( + config.staking_bridge_contract.address, + signer || provider + ), + vesting: new TokenVesting( + config.token_vesting_contract.address, + signer || provider + ), + claim: new Claim(ENV.addresses.claimAddress, signer || provider), + }); + } } }; run(); - }, [activeProvider, account, config, ADDRESSES, VEGA_ENV, defaultProvider]); + }, [activeProvider, account, config, VEGA_ENV, ETHEREUM_PROVIDER_URL]); if (!contracts) { return ( diff --git a/apps/token/src/hooks/use-add-asset-to-wallet.ts b/apps/token/src/hooks/use-add-asset-to-wallet.ts index 636439505..efffe0de9 100644 --- a/apps/token/src/hooks/use-add-asset-to-wallet.ts +++ b/apps/token/src/hooks/use-add-asset-to-wallet.ts @@ -1,9 +1,9 @@ import React from 'react'; import * as Sentry from '@sentry/react'; -import { Networks } from '@vegaprotocol/react-helpers'; +import { Networks } from '@vegaprotocol/environment'; import { useWeb3React } from '@web3-react/core'; import { MetaMask } from '@web3-react/metamask'; -import { useEnvironment } from '@vegaprotocol/network-switcher'; +import { useEnvironment } from '@vegaprotocol/environment'; export const useAddAssetSupported = () => { const { connector } = useWeb3React(); diff --git a/apps/token/src/hooks/use-tranches.ts b/apps/token/src/hooks/use-tranches.ts index d95268d6b..da9b94d42 100644 --- a/apps/token/src/hooks/use-tranches.ts +++ b/apps/token/src/hooks/use-tranches.ts @@ -1,8 +1,8 @@ -import type { Networks } from '@vegaprotocol/react-helpers'; import { useFetch } from '@vegaprotocol/react-helpers'; import type { Tranche } from '@vegaprotocol/smart-contracts'; import React, { useEffect } from 'react'; -import { useEnvironment } from '@vegaprotocol/network-switcher'; +import type { Networks } from '@vegaprotocol/environment'; +import { useEnvironment } from '@vegaprotocol/environment'; import { BigNumber } from '../lib/bignumber'; @@ -12,6 +12,7 @@ const TRANCHES_URLS: { [N in Networks]: string } = { STAGNET: 'https://static.vega.xyz/assets/stagnet1-tranches.json', STAGNET2: 'https://static.vega.xyz/assets/stagnet2-tranches.json', DEVNET: 'https://static.vega.xyz/assets/devnet-tranches.json', + CUSTOM: 'https://static.vega.xyz/assets/testnet-tranches.json', }; export function useTranches() { diff --git a/apps/token/src/routes/claim/complete.tsx b/apps/token/src/routes/claim/complete.tsx index 8c2eb2c27..41d60e7e9 100644 --- a/apps/token/src/routes/claim/complete.tsx +++ b/apps/token/src/routes/claim/complete.tsx @@ -1,5 +1,5 @@ import { Callout, Intent, Link, Button } from '@vegaprotocol/ui-toolkit'; -import { useEnvironment } from '@vegaprotocol/network-switcher'; +import { useEnvironment } from '@vegaprotocol/environment'; import { Trans, useTranslation } from 'react-i18next'; import { Link as RouteLink } from 'react-router-dom'; diff --git a/apps/token/src/routes/contracts/index.tsx b/apps/token/src/routes/contracts/index.tsx index 7acad9dcb..81cd33dc2 100644 --- a/apps/token/src/routes/contracts/index.tsx +++ b/apps/token/src/routes/contracts/index.tsx @@ -2,13 +2,14 @@ import { t } from '@vegaprotocol/react-helpers'; import { Link, Splash } from '@vegaprotocol/ui-toolkit'; import type { EthereumConfig } from '@vegaprotocol/web3'; import { useEthereumConfig } from '@vegaprotocol/web3'; -import { useEnvironment } from '@vegaprotocol/network-switcher'; +import { useEnvironment } from '@vegaprotocol/environment'; import { Heading } from '../../components/heading'; import { SplashLoader } from '../../components/splash-loader'; +import { ENV } from '../../config/env'; const Contracts = () => { const { config } = useEthereumConfig(); - const { ADDRESSES, ETHERSCAN_URL } = useEnvironment(); + const { ETHERSCAN_URL } = useEnvironment(); if (!config) { return ( @@ -47,7 +48,7 @@ const Contracts = () => {
); })} - {Object.entries(ADDRESSES).map(([key, value]) => ( + {Object.entries(ENV.addresses).map(([key, value]) => (
{children}; + return {children}; } diff --git a/apps/trading/components/web3-container/web3-container.spec.tsx b/apps/trading/components/web3-container/web3-container.spec.tsx index 438899e96..ca3b08a7a 100644 --- a/apps/trading/components/web3-container/web3-container.spec.tsx +++ b/apps/trading/components/web3-container/web3-container.spec.tsx @@ -5,7 +5,7 @@ import { Web3Container } from './web3-container'; import type { useWeb3React } from '@web3-react/core'; import type { NetworkParamsQuery } from '@vegaprotocol/web3'; import { NETWORK_PARAMS_QUERY } from '@vegaprotocol/web3'; -import { EnvironmentProvider } from '@vegaprotocol/network-switcher'; +import { EnvironmentProvider } from '@vegaprotocol/environment'; const defaultHookValue = { isActive: false, diff --git a/apps/trading/components/web3-container/web3-container.tsx b/apps/trading/components/web3-container/web3-container.tsx index 783b93b5c..e0da31b5a 100644 --- a/apps/trading/components/web3-container/web3-container.tsx +++ b/apps/trading/components/web3-container/web3-container.tsx @@ -8,7 +8,7 @@ import { useWeb3React } from '@web3-react/core'; import type { ReactNode } from 'react'; import { useEffect, useState, useMemo } from 'react'; import { t } from '@vegaprotocol/react-helpers'; -import { useEnvironment } from '@vegaprotocol/network-switcher'; +import { useEnvironment } from '@vegaprotocol/environment'; import { createConnectors } from '../../lib/web3-connectors'; interface Web3ContainerProps { @@ -18,14 +18,15 @@ interface Web3ContainerProps { export const Web3Container = ({ children }: Web3ContainerProps) => { const [dialogOpen, setDialogOpen] = useState(false); const { config, loading, error } = useEthereumConfig(); - const { ETHEREUM_PROVIDER_URL, ETHEREUM_CHAIN_ID } = useEnvironment(); - const Connectors = useMemo( - () => createConnectors(ETHEREUM_PROVIDER_URL, ETHEREUM_CHAIN_ID), - [ETHEREUM_CHAIN_ID, ETHEREUM_PROVIDER_URL] - ); + const { ETHEREUM_PROVIDER_URL } = useEnvironment(); + const Connectors = useMemo(() => { + if (config?.chain_id) { + return createConnectors(ETHEREUM_PROVIDER_URL, Number(config?.chain_id)); + } + }, [config?.chain_id, ETHEREUM_PROVIDER_URL]); return ( - {config ? ( + {Connectors && config && ( { desiredChainId={Number(config.chain_id)} /> - ) : null} + )} ); }; diff --git a/apps/trading/pages/_app.page.tsx b/apps/trading/pages/_app.page.tsx index 45d4a20af..38aaf4014 100644 --- a/apps/trading/pages/_app.page.tsx +++ b/apps/trading/pages/_app.page.tsx @@ -1,5 +1,6 @@ import type { AppProps } from 'next/app'; import Head from 'next/head'; +import { useRouter } from 'next/router'; import { Navbar } from '../components/navbar'; import { t, ThemeContext, useThemeSwitcher } from '@vegaprotocol/react-helpers'; import { @@ -7,29 +8,27 @@ import { VegaManageDialog, VegaWalletProvider, } from '@vegaprotocol/wallet'; -import { NetworkSwitcherDialog } from '@vegaprotocol/network-switcher'; +import { + useEnvironment, + EnvironmentProvider, + NetworkSwitcherDialog, +} from '@vegaprotocol/environment'; import { Connectors } from '../lib/vega-connectors'; -import { useMemo } from 'react'; -import { createClient } from '../lib/apollo-client'; import { ThemeSwitcher } from '@vegaprotocol/ui-toolkit'; -import { ApolloProvider } from '@apollo/client'; import { AppLoader } from '../components/app-loader'; import { VegaWalletConnectButton } from '../components/vega-wallet-connect-button'; import './styles.css'; import { useGlobalStore } from '../stores'; -import { ENV } from '../lib/config/env'; -import { EnvironmentProvider } from '@vegaprotocol/network-switcher'; -import { useEnvironment } from '@vegaprotocol/network-switcher'; function AppBody({ Component, pageProps }: AppProps) { + const { push } = useRouter(); const store = useGlobalStore(); const { VEGA_NETWORKS } = useEnvironment(); - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const [theme, toggleTheme] = useThemeSwitcher(); + const [, toggleTheme] = useThemeSwitcher(); return ( - -
+
+
@@ -62,54 +61,46 @@ function AppBody({ Component, pageProps }: AppProps) { setDialogOpen={(open) => store.setVegaNetworkSwitcherDialog(open)} onConnect={({ network }) => { if (VEGA_NETWORKS[network]) { - window.location.href = VEGA_NETWORKS[network]; + push(VEGA_NETWORKS[network] ?? ''); } }} /> -
- + +
); } function VegaTradingApp(props: AppProps) { const [theme] = useThemeSwitcher(); - const client = useMemo(() => createClient(ENV.vegaUrl), []); return ( - - - - - - {t('Welcome to Vega trading!')} - - - {['1', 'true'].includes( - process.env['NX_USE_ENV_OVERRIDES'] || '' - ) ? ( - /* eslint-disable-next-line @next/next/no-sync-scripts */ -