diff --git a/.env.example b/.env.example index 0a565d8495..d67e6ccb89 100644 --- a/.env.example +++ b/.env.example @@ -16,3 +16,4 @@ NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID= NEXT_PUBLIC_MIXPANEL= NEXT_PUBLIC_FIAT_ON_RAMP=true NEXT_PUBLIC_SUBGRAPH_API_KEY= +NEXT_PUBLIC_IS_CYPRESS_ENABLED=false diff --git a/custom.d.ts b/custom.d.ts index 087bda4a84..745111581b 100644 --- a/custom.d.ts +++ b/custom.d.ts @@ -14,5 +14,6 @@ namespace NodeJS { NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID: string; NEXT_PUBLIC_FIAT_ON_RAMP: string; NEXT_PUBLIC_SUBGRAPH_API_KEY: string; + NEXT_PUBLIC_IS_CYPRESS_ENABLED: string; } } diff --git a/cypress/tsconfig.json b/cypress/tsconfig.json index b96260896a..b5b7784ae7 100644 --- a/cypress/tsconfig.json +++ b/cypress/tsconfig.json @@ -3,6 +3,7 @@ "compilerOptions": { "noEmit": true, "types": ["cypress", "cypress-wait-until"], + "sourceMap": false, }, "include": [ "../node_modules/cypress", diff --git a/src/libs/web3-data-provider/Web3Provider.tsx b/src/libs/web3-data-provider/Web3Provider.tsx index 2a1d802ecc..5bf6520ed3 100644 --- a/src/libs/web3-data-provider/Web3Provider.tsx +++ b/src/libs/web3-data-provider/Web3Provider.tsx @@ -5,12 +5,12 @@ import { BigNumber, PopulatedTransaction } from 'ethers'; import React, { ReactElement, useEffect, useState } from 'react'; import { useRootStore } from 'src/store/root'; import { hexToAscii } from 'src/utils/utils'; +import { UserRejectedRequestError } from 'viem'; +import { useAccount, useConnect, useConnectorClient, useSwitchChain, useWatchAsset } from 'wagmi'; // import { isLedgerDappBrowserProvider } from 'web3-ledgerhq-frame-connector'; import { Web3Context } from '../hooks/useWeb3Context'; -import { useAccount, useClient, useConnectorClient, useSwitchChain, useWatchAsset } from 'wagmi'; -import { clientToSigner, useEthersProvider, useEthersSigner } from './adapters/EthersAdapter'; -import { UserRejectedRequestError } from 'viem'; +import { clientToSigner, useEthersProvider } from './adapters/EthersAdapter'; export type ERC20TokenType = { address: string; @@ -43,17 +43,15 @@ interface ConnectWalletOpts { address?: string | null; } +let didConnect = false; + export const Web3ContextProvider: React.FC<{ children: ReactElement }> = ({ children }) => { // const { chainId: chainId, connector, provider, isActivating, isActive } = useWeb3React(); const { switchChainAsync } = useSwitchChain(); const { watchAssetAsync } = useWatchAsset(); - const { chainId, address, isConnected, connector } = useAccount(); - const client = useClient({ chainId }); + const { chainId, address, isConnected, isConnecting } = useAccount(); const { data: connectorClient } = useConnectorClient({ chainId }); - - console.log(connector?.getChainId()); - console.log(client); - console.log(connectorClient); + const { connect, connectors } = useConnect(); // const { sendTransaction } = useSendTransaction(); const provider = useEthersProvider({ chainId }); @@ -66,6 +64,19 @@ export const Web3ContextProvider: React.FC<{ children: ReactElement }> = ({ chil const setAccountLoading = useRootStore((store) => store.setAccountLoading); const setWalletType = useRootStore((store) => store.setWalletType); + useEffect(() => { + // If running cypress tests, then we try to auto connect on app load + // so it doesn't have to be driven through the UI. + const isCypressEnabled = process.env.NEXT_PUBLIC_IS_CYPRESS_ENABLED === 'true'; + if (!isCypressEnabled || didConnect) { + return; + } + + const injected = connectors[0]; + connect({ connector: injected }); + didConnect = true; + }); + // const disconnectWallet = useCallback(async () => { // localStorage.removeItem('walletProvider'); // localStorage.removeItem('readOnlyModeAddress'); diff --git a/src/ui-config/wagmiConfig.ts b/src/ui-config/wagmiConfig.ts index 38f1b408af..49023d9aa8 100644 --- a/src/ui-config/wagmiConfig.ts +++ b/src/ui-config/wagmiConfig.ts @@ -1,6 +1,14 @@ import { getDefaultConfig } from 'connectkit'; -import { ENABLE_TESTNET } from 'src/utils/marketsAndNetworksConfig'; -import { createConfig, CreateConfigParameters } from 'wagmi'; +import { + ENABLE_TESTNET, + FORK_BASE_CHAIN_ID, + FORK_CHAIN_ID, + FORK_ENABLED, + FORK_RPC_URL, + networkConfigs, +} from 'src/utils/marketsAndNetworksConfig'; +import { type Chain } from 'viem'; +import { createConfig, CreateConfigParameters, injected } from 'wagmi'; import { arbitrum, arbitrumSepolia, @@ -30,7 +38,7 @@ const testnetChains: CreateConfigParameters['chains'] = [ scrollSepolia, ]; -const prodChains: CreateConfigParameters['chains'] = [ +let prodChains: CreateConfigParameters['chains'] = [ mainnet, base, arbitrum, @@ -44,13 +52,49 @@ const prodChains: CreateConfigParameters['chains'] = [ zksync, ]; -export const wagmiConfig = createConfig( +const { name, baseAssetDecimals, baseAssetSymbol } = networkConfigs[FORK_BASE_CHAIN_ID]; + +const forkChain: Chain = { + id: FORK_CHAIN_ID, + name, + nativeCurrency: { + decimals: baseAssetDecimals, + name: baseAssetSymbol, + symbol: baseAssetSymbol, + }, + rpcUrls: { + default: { http: [FORK_RPC_URL] }, + }, + testnet: false, +}; + +if (FORK_ENABLED) { + prodChains = [forkChain, ...prodChains]; +} + +const defaultConfig = { + walletConnectProjectId: process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID as string, + appName: 'Aave', + appDescription: 'Non-custodial liquidity protocol', + appUrl: 'https://app.aave.com', + appIcon: 'https://avatars.githubusercontent.com/u/47617460?s=200&v=4', +}; + +const cypressConfig = createConfig( + getDefaultConfig({ + chains: [forkChain], + connectors: [injected()], + ...defaultConfig, + }) +); + +const prodConfig = createConfig( getDefaultConfig({ chains: ENABLE_TESTNET ? testnetChains : prodChains, - walletConnectProjectId: process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID as string, - appName: 'Aave', - appDescription: 'Non-custodial liquidity protocol', - appUrl: 'https://app.aave.com', - appIcon: 'https://avatars.githubusercontent.com/u/47617460?s=200&v=4', + ...defaultConfig, }) ); + +const isCypressEnabled = process.env.NEXT_PUBLIC_IS_CYPRESS_ENABLED === 'true'; + +export const wagmiConfig = isCypressEnabled ? cypressConfig : prodConfig; diff --git a/src/utils/marketsAndNetworksConfig.ts b/src/utils/marketsAndNetworksConfig.ts index c61d41df6b..1e6efa2434 100644 --- a/src/utils/marketsAndNetworksConfig.ts +++ b/src/utils/marketsAndNetworksConfig.ts @@ -30,14 +30,14 @@ export const FORK_ENABLED = !!process.env.NEXT_PUBLIC_FORK_URL_RPC || global?.window?.localStorage.getItem('forkEnabled') === 'true'; // specifies which network was forked -const FORK_BASE_CHAIN_ID = +export const FORK_BASE_CHAIN_ID = Number(process.env.NEXT_PUBLIC_FORK_BASE_CHAIN_ID) || Number(global?.window?.localStorage.getItem('forkBaseChainId') || 1); // specifies on which chainId the fork is running -const FORK_CHAIN_ID = +export const FORK_CHAIN_ID = Number(process.env.NEXT_PUBLIC_FORK_CHAIN_ID) || Number(global?.window?.localStorage.getItem('forkNetworkId') || 3030); -const FORK_RPC_URL = +export const FORK_RPC_URL = process.env.NEXT_PUBLIC_FORK_URL_RPC || global?.window?.localStorage.getItem('forkRPCUrl') || 'http://127.0.0.1:8545';