Skip to content

Commit

Permalink
fix: merge
Browse files Browse the repository at this point in the history
  • Loading branch information
foodaka committed Dec 12, 2024
2 parents 9a9bba3 + e1549b1 commit dcefa4e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 21 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions cypress/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"noEmit": true,
"types": ["cypress", "cypress-wait-until"],
"sourceMap": false,
},
"include": [
"../node_modules/cypress",
Expand Down
29 changes: 20 additions & 9 deletions src/libs/web3-data-provider/Web3Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 });
Expand All @@ -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');
Expand Down
62 changes: 53 additions & 9 deletions src/ui-config/wagmiConfig.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -30,7 +38,7 @@ const testnetChains: CreateConfigParameters['chains'] = [
scrollSepolia,
];

const prodChains: CreateConfigParameters['chains'] = [
let prodChains: CreateConfigParameters['chains'] = [
mainnet,
base,
arbitrum,
Expand All @@ -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;
6 changes: 3 additions & 3 deletions src/utils/marketsAndNetworksConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit dcefa4e

Please sign in to comment.