From b8c9f9772ee1d1340ac744eb0fc846b838af2da5 Mon Sep 17 00:00:00 2001 From: Matthew Russell Date: Mon, 26 Jun 2023 23:40:42 -0700 Subject: [PATCH] chore(trading): allow window._env_ override (#4159) --- apps/trading-e2e/.env | 1 + apps/trading/.env.devnet | 2 +- apps/trading/.env.mainnet | 2 +- apps/trading/.env.stagnet1 | 2 +- apps/trading/.env.testnet | 2 +- apps/trading/.env.validators-testnet | 2 +- .../telemetry-approval.spec.tsx | 18 ++- .../welcome-dialog/welcome-dialog.tsx | 5 +- apps/trading/lib/config/env.ts | 20 --- apps/trading/lib/config/index.ts | 1 - .../lib/hooks/use-telemetry-approval.spec.ts | 18 +-- .../lib/hooks/use-telemetry-approval.ts | 9 +- apps/trading/pages/_app.page.tsx | 4 +- apps/trading/pages/_document.page.tsx | 4 - .../src/hooks/use-environment.spec.ts | 19 +++ libs/environment/src/hooks/use-environment.ts | 119 ++++++++++++++---- libs/environment/src/hooks/use-node-health.ts | 3 +- .../src/utils/validate-environment.ts | 1 + .../use-next-protocol-upgrade-proposals.ts | 4 +- .../use-time-to-upgrade.ts | 3 +- libs/utils/src/index.ts | 1 + libs/utils/src/lib/is-test-env.ts | 3 + libs/wallet/src/use-json-rpc-connect.ts | 3 +- libs/web3/src/lib/use-eager-connect.ts | 3 +- nx.json | 1 - 25 files changed, 169 insertions(+), 81 deletions(-) delete mode 100644 apps/trading/lib/config/env.ts delete mode 100644 apps/trading/lib/config/index.ts create mode 100644 libs/utils/src/lib/is-test-env.ts diff --git a/apps/trading-e2e/.env b/apps/trading-e2e/.env index ce19df7a0..d7d339f53 100644 --- a/apps/trading-e2e/.env +++ b/apps/trading-e2e/.env @@ -14,6 +14,7 @@ NX_VEGA_WALLET_URL=http://localhost:1789 NX_ETH_LOCAL_PROVIDER_URL=http://localhost:8545/ NX_ETH_WALLET_MNEMONIC="ozone access unlock valid olympic save include omit supply green clown session" NX_WALLETCONNECT_PROJECT_ID=fe8091dc35738863e509fc4947525c72 +NX_SENTRY_DSN=https://dummy@o999999.ingest.sentry.io/9999999 # Expose some env vars to cypress environment for market setup CYPRESS_ETH_WALLET_MNEMONIC=ozone access unlock valid olympic save include omit supply green clown session diff --git a/apps/trading/.env.devnet b/apps/trading/.env.devnet index e53d01828..78681b985 100644 --- a/apps/trading/.env.devnet +++ b/apps/trading/.env.devnet @@ -2,7 +2,7 @@ NX_ETHEREUM_PROVIDER_URL=https://sepolia.infura.io/v3/4f846e79e13f44d1b51bbd7ed9 NX_ETHERSCAN_URL=https://sepolia.etherscan.io NX_GITHUB_FEEDBACK_URL=https://github.com/vegaprotocol/feedback/discussions NX_HOSTED_WALLET_URL=https://wallet.testnet.vega.xyz -NX_TRADING_SENTRY_DSN=https://2ffce43721964aafa78277c50654ece4@o286262.ingest.sentry.io/6300613 +NX_SENTRY_DSN=https://2ffce43721964aafa78277c50654ece4@o286262.ingest.sentry.io/6300613 NX_VEGA_CONFIG_URL=https://raw.githubusercontent.com/vegaprotocol/networks-internal/main/devnet1/vegawallet-devnet1.toml NX_VEGA_ENV=DEVNET NX_VEGA_EXPLORER_URL=https://dev.explorer.vega.xyz diff --git a/apps/trading/.env.mainnet b/apps/trading/.env.mainnet index e227063a2..f95396b07 100644 --- a/apps/trading/.env.mainnet +++ b/apps/trading/.env.mainnet @@ -1,7 +1,7 @@ NX_ETHEREUM_PROVIDER_URL=https://eth-mainnet.gateway.pokt.network/v1/lb/af6a2d529a11f8158bc8ca2a NX_ETHERSCAN_URL=https://etherscan.io NX_GITHUB_FEEDBACK_URL=https://github.com/vegaprotocol/feedback/discussions -NX_TRADING_SENTRY_DSN=https://2ffce43721964aafa78277c50654ece4@o286262.ingest.sentry.io/6300613 +NX_SENTRY_DSN=https://2ffce43721964aafa78277c50654ece4@o286262.ingest.sentry.io/6300613 NX_VEGA_CONFIG_URL=https://raw.githubusercontent.com/vegaprotocol/networks/master/mainnet1/mainnet1.toml NX_VEGA_ENV=MAINNET NX_VEGA_EXPLORER_URL=https://explorer.vega.xyz diff --git a/apps/trading/.env.stagnet1 b/apps/trading/.env.stagnet1 index 016ede29c..c4183b794 100644 --- a/apps/trading/.env.stagnet1 +++ b/apps/trading/.env.stagnet1 @@ -2,7 +2,7 @@ NX_ETHEREUM_PROVIDER_URL=https://sepolia.infura.io/v3/4f846e79e13f44d1b51bbd7ed9 NX_ETHERSCAN_URL=https://sepolia.etherscan.io NX_GITHUB_FEEDBACK_URL=https://github.com/vegaprotocol/feedback/discussions NX_HOSTED_WALLET_URL=https://wallet.testnet.vega.xyz -NX_TRADING_SENTRY_DSN=https://2ffce43721964aafa78277c50654ece4@o286262.ingest.sentry.io/6300613 +NX_SENTRY_DSN=https://2ffce43721964aafa78277c50654ece4@o286262.ingest.sentry.io/6300613 NX_VEGA_CONFIG_URL=https://raw.githubusercontent.com/vegaprotocol/networks-internal/main/stagnet1/vegawallet-stagnet1.toml NX_VEGA_ENV=STAGNET1 NX_VEGA_EXPLORER_URL=https://explorer.stagnet1.vega.rocks diff --git a/apps/trading/.env.testnet b/apps/trading/.env.testnet index 2426f57af..03f612e1f 100644 --- a/apps/trading/.env.testnet +++ b/apps/trading/.env.testnet @@ -2,7 +2,7 @@ NX_ETHEREUM_PROVIDER_URL=https://sepolia.infura.io/v3/4f846e79e13f44d1b51bbd7ed9 NX_ETHERSCAN_URL=https://sepolia.etherscan.io NX_GITHUB_FEEDBACK_URL=https://github.com/vegaprotocol/feedback/discussions NX_HOSTED_WALLET_URL=https://wallet.testnet.vega.xyz -NX_TRADING_SENTRY_DSN=https://2ffce43721964aafa78277c50654ece4@o286262.ingest.sentry.io/6300613 +NX_SENTRY_DSN=https://2ffce43721964aafa78277c50654ece4@o286262.ingest.sentry.io/6300613 NX_VEGA_CONFIG_URL=https://raw.githubusercontent.com/vegaprotocol/networks-internal/main/fairground/vegawallet-fairground.toml NX_VEGA_ENV=TESTNET NX_VEGA_EXPLORER_URL=https://explorer.fairground.wtf diff --git a/apps/trading/.env.validators-testnet b/apps/trading/.env.validators-testnet index 5cf7eeaae..341ad2fc8 100644 --- a/apps/trading/.env.validators-testnet +++ b/apps/trading/.env.validators-testnet @@ -2,7 +2,7 @@ NX_ETHEREUM_PROVIDER_URL=https://sepolia.infura.io/v3/4f846e79e13f44d1b51bbd7ed9 NX_ETHERSCAN_URL=https://sepolia.etherscan.io NX_GITHUB_FEEDBACK_URL=https://github.com/vegaprotocol/feedback/discussions NX_HOSTED_WALLET_URL=https://wallet.testnet.vega.xyz -NX_TRADING_SENTRY_DSN=https://2ffce43721964aafa78277c50654ece4@o286262.ingest.sentry.io/6300613 +NX_SENTRY_DSN=https://2ffce43721964aafa78277c50654ece4@o286262.ingest.sentry.io/6300613 NX_VEGA_CONFIG_URL=https://raw.githubusercontent.com/vegaprotocol/networks/master/testnet2/testnet2.toml NX_VEGA_ENV=VALIDATOR_TESTNET NX_VEGA_EXPLORER_URL=https://explorer.validators-testnet.vega.rocks diff --git a/apps/trading/components/welcome-dialog/telemetry-approval.spec.tsx b/apps/trading/components/welcome-dialog/telemetry-approval.spec.tsx index 3061845aa..ac66dcaff 100644 --- a/apps/trading/components/welcome-dialog/telemetry-approval.spec.tsx +++ b/apps/trading/components/welcome-dialog/telemetry-approval.spec.tsx @@ -1,17 +1,25 @@ -import { render, screen, act } from '@testing-library/react'; +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; import { TelemetryApproval } from './telemetry-approval'; +jest.mock('@vegaprotocol/logger', () => ({ + SentryInit: () => undefined, + SentryClose: () => undefined, +})); + +jest.mock('@vegaprotocol/environment', () => ({ + useEnvironment: () => ({ VEGA_ENV: 'test', SENTRY_DSN: 'sentry-dsn' }), +})); + describe('TelemetryApproval', () => { - it('click on checkbox should be properly handled', () => { + it('click on checkbox should be properly handled', async () => { const helpText = 'My help text'; render(); expect(screen.getByRole('checkbox')).toHaveAttribute( 'data-state', 'unchecked' ); - act(() => { - screen.getByRole('checkbox').click(); - }); + await userEvent.click(screen.getByRole('checkbox')); expect(screen.getByRole('checkbox')).toHaveAttribute( 'data-state', 'checked' diff --git a/apps/trading/components/welcome-dialog/welcome-dialog.tsx b/apps/trading/components/welcome-dialog/welcome-dialog.tsx index f57202282..8ca2ade72 100644 --- a/apps/trading/components/welcome-dialog/welcome-dialog.tsx +++ b/apps/trading/components/welcome-dialog/welcome-dialog.tsx @@ -11,6 +11,7 @@ import { WelcomeNoticeDialog } from './welcome-notice-dialog'; import { useGlobalStore } from '../../stores'; import { useEnvironment } from '@vegaprotocol/environment'; import { Networks } from '@vegaprotocol/environment'; +import { isTestEnv } from '@vegaprotocol/utils'; export const WelcomeDialog = () => { const { VEGA_ENV } = useEnvironment(); @@ -31,9 +32,7 @@ export const WelcomeDialog = () => { ); const isRiskDialogNeeded = - riskAccepted !== 'true' && - VEGA_ENV !== Networks.MAINNET && - !('Cypress' in window); + riskAccepted !== 'true' && VEGA_ENV !== Networks.MAINNET && !isTestEnv(); const isWelcomeDialogNeeded = pathname === '/' || shouldDisplayWelcomeDialog; diff --git a/apps/trading/lib/config/env.ts b/apps/trading/lib/config/env.ts deleted file mode 100644 index b153f0721..000000000 --- a/apps/trading/lib/config/env.ts +++ /dev/null @@ -1,20 +0,0 @@ -const windowOrDefault = (key: string, defaultValue?: string) => { - if (typeof window !== 'undefined') { - if (window._env_ && window._env_[key]) { - return window._env_[key]; - } - } - return defaultValue || ''; -}; - -/** - * Need to have default value as next in-lines environment variables. Cannot figure out dynamic keys. - * So must provide the default with the key so that next can figure it out. - */ -export const ENV = { - envName: windowOrDefault('NX_VEGA_ENV', process.env['NX_VEGA_ENV']), - dsn: windowOrDefault( - 'NX_TRADING_SENTRY_DSN', - process.env['NX_TRADING_SENTRY_DSN'] - ), -}; diff --git a/apps/trading/lib/config/index.ts b/apps/trading/lib/config/index.ts deleted file mode 100644 index c1532d6d2..000000000 --- a/apps/trading/lib/config/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './env'; diff --git a/apps/trading/lib/hooks/use-telemetry-approval.spec.ts b/apps/trading/lib/hooks/use-telemetry-approval.spec.ts index 97ec110d6..bd0be96b9 100644 --- a/apps/trading/lib/hooks/use-telemetry-approval.spec.ts +++ b/apps/trading/lib/hooks/use-telemetry-approval.spec.ts @@ -12,22 +12,26 @@ jest.mock('@vegaprotocol/react-helpers', () => ({ .fn() .mockImplementation(() => [false, mockSetValue, mockRemoveValue]), })); +jest.mock('@vegaprotocol/environment', () => ({ + useEnvironment: () => ({ VEGA_ENV: 'test', SENTRY_DSN: 'sentry-dsn' }), +})); describe('useTelemetryApproval', () => { beforeEach(() => { jest.clearAllMocks(); }); + it('hook should return proper array', () => { - const res = renderHook(() => useTelemetryApproval()); - expect(res.result.current[0]).toEqual(false); - expect(res.result.current[1]).toEqual(expect.any(Function)); + const { result } = renderHook(() => useTelemetryApproval()); + expect(result.current[0]).toEqual(false); + expect(result.current[1]).toEqual(expect.any(Function)); expect(useLocalStorage).toHaveBeenCalledWith(STORAGE_KEY); }); it('hook should init stuff properly', async () => { - const res = renderHook(() => useTelemetryApproval()); + const { result } = renderHook(() => useTelemetryApproval()); await act(() => { - res.result.current[1](true); + result.current[1](true); }); await waitFor(() => { expect(SentryInit).toHaveBeenCalled(); @@ -36,9 +40,9 @@ describe('useTelemetryApproval', () => { }); it('hook should close stuff properly', async () => { - const res = renderHook(() => useTelemetryApproval()); + const { result } = renderHook(() => useTelemetryApproval()); await act(() => { - res.result.current[1](false); + result.current[1](false); }); await waitFor(() => { expect(SentryClose).toHaveBeenCalled(); diff --git a/apps/trading/lib/hooks/use-telemetry-approval.ts b/apps/trading/lib/hooks/use-telemetry-approval.ts index d469e58ac..25e0203b5 100644 --- a/apps/trading/lib/hooks/use-telemetry-approval.ts +++ b/apps/trading/lib/hooks/use-telemetry-approval.ts @@ -1,24 +1,25 @@ import { useLocalStorage } from '@vegaprotocol/react-helpers'; import { useCallback } from 'react'; import { SentryInit, SentryClose } from '@vegaprotocol/logger'; -import { ENV } from '../config'; +import { useEnvironment } from '@vegaprotocol/environment'; export const STORAGE_KEY = 'vega_telemetry_approval'; export const useTelemetryApproval = (): [ value: boolean, setValue: (value: boolean) => void ] => { + const { VEGA_ENV, SENTRY_DSN } = useEnvironment(); const [value, setValue, removeValue] = useLocalStorage(STORAGE_KEY); const setApprove = useCallback( (value: boolean) => { - if (value) { - SentryInit(ENV.dsn, ENV.envName); + if (value && SENTRY_DSN) { + SentryInit(SENTRY_DSN, VEGA_ENV); return setValue('1'); } SentryClose(); removeValue(); }, - [setValue, removeValue] + [setValue, removeValue, SENTRY_DSN, VEGA_ENV] ); return [Boolean(value), setApprove]; }; diff --git a/apps/trading/pages/_app.page.tsx b/apps/trading/pages/_app.page.tsx index 0c5d4686c..c787eebff 100644 --- a/apps/trading/pages/_app.page.tsx +++ b/apps/trading/pages/_app.page.tsx @@ -34,7 +34,6 @@ import { ViewingBanner } from '../components/viewing-banner'; import { AnnouncementBanner, UpgradeBanner } from '../components/banner'; import { AppLoader, DynamicLoader } from '../components/app-loader'; import { Navbar } from '../components/navbar'; -import { ENV } from '../lib/config'; import { useDataProvider } from '@vegaprotocol/data-provider'; import { activeOrdersProvider } from '@vegaprotocol/orders'; import { useTelemetryApproval } from '../lib/hooks/use-telemetry-approval'; @@ -155,10 +154,11 @@ const PartyData = () => { }; const MaybeConnectEagerly = () => { + const { VEGA_ENV, SENTRY_DSN } = useEnvironment(); useVegaEagerConnect(Connectors); const [isTelemetryApproved] = useTelemetryApproval(); useEthereumEagerConnect( - isTelemetryApproved ? { dsn: ENV.dsn, env: ENV.envName } : {} + isTelemetryApproved ? { dsn: SENTRY_DSN, env: VEGA_ENV } : {} ); const { pubKey, connect } = useVegaWallet(); diff --git a/apps/trading/pages/_document.page.tsx b/apps/trading/pages/_document.page.tsx index 8d42dcc68..02495416a 100644 --- a/apps/trading/pages/_document.page.tsx +++ b/apps/trading/pages/_document.page.tsx @@ -20,10 +20,6 @@ export default function Document() { href="https://static.vega.xyz/favicon.ico" />