Skip to content

Commit

Permalink
replace library of useActiveWeb3React with signer or provider from wa…
Browse files Browse the repository at this point in the history
…gmi hook
  • Loading branch information
nkrulikovsky committed May 26, 2023
1 parent e57a628 commit d24c959
Show file tree
Hide file tree
Showing 40 changed files with 8,059 additions and 4,849 deletions.
12,207 changes: 7,698 additions & 4,509 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"copy-to-clipboard": "^3.3.1",
"date-fns": "^2.29.3",
"ethcall": "^4.8.13",
"ethers": "^5.5.3",
"ethers": "^5.7.2",
"framer-motion": "^4.0.3",
"history": "^5.0.0",
"i18next": "^19.8.3",
Expand Down
6 changes: 3 additions & 3 deletions src/components/DepositPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const DepositPage = (props: Props): ReactElement => {
onConfirmTransaction,
onToggleDepositWrapped,
} = props
const { account, chainId, library } = useActiveWeb3React()
const { account, chainId, signerOrProvider } = useActiveWeb3React()
const { unstakeMinichef, amountStakedMinichef } = useRewardsHelpers(
poolData?.name ?? "",
)
Expand All @@ -107,11 +107,11 @@ const DepositPage = (props: Props): ReactElement => {
const liquidityGaugeContract = getContract(
gaugeAddr,
LIQUIDITY_GAUGE_V5_ABI,
library,
signerOrProvider,
account,
) as LiquidityGaugeV5
setLiquidityGaugeContract(liquidityGaugeContract)
}, [account, chainId, poolData, gaugeAddr, library])
}, [account, chainId, poolData, gaugeAddr, signerOrProvider])

const onMigrateToGaugeClick = async () => {
if (
Expand Down
40 changes: 15 additions & 25 deletions src/connectors/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { Chain, connectorsForWallets } from "@rainbow-me/rainbowkit"
import { STALL_TIMEOUT, SUPPORTED_NETWORKS } from "../constants/networks"
import {
arbitrum,
aurora,
avalanche,
evmos,
evmosTestnet,
fantom,
goerli,
hardhat,
mainnet,
optimism,
Expand All @@ -20,9 +17,9 @@ import {
walletConnectWallet,
} from "@rainbow-me/rainbowkit/wallets"
import { configureChains, createClient } from "wagmi"
import { STALL_TIMEOUT } from "../constants/networks"
import Tally from "../components/Rainbowkit"
import { alchemyProvider } from "wagmi/providers/alchemy"
import { jsonRpcProvider } from "wagmi/providers/jsonRpc"
import { publicProvider } from "wagmi/providers/public"

const kava: Readonly<Chain> = {
Expand Down Expand Up @@ -51,40 +48,32 @@ const kava: Readonly<Chain> = {
}

export const chain: Record<string, Chain> = {
mainnet,
arbitrum,
aurora,
avalanche,
evmos,
evmosTestnet,
fantom,
goerli,
hardhat,
mainnet,
kava,
optimism,
optimismGoerli,
kava,
}

const alchemyKey = process.env.REACT_APP_ALCHEMY_API_KEY

const { chains, provider } = configureChains(Object.values(chain), [
alchemyProvider({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
apiKey: alchemyKey!,
stallTimeout: STALL_TIMEOUT,
priority: alchemyKey ? 0 : 2,
}),
jsonRpcProvider({
rpc: (networkChain) => ({
http: SUPPORTED_NETWORKS[networkChain.id]
? networkChain.rpcUrls.default.http[0]
: "",
const { chains, provider, webSocketProvider } = configureChains(
Object.values(chain),
[
alchemyProvider({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
apiKey: alchemyKey!,
stallTimeout: STALL_TIMEOUT,
priority: alchemyKey ? 0 : 2,
}),
stallTimeout: STALL_TIMEOUT,
priority: 1,
}),
publicProvider({ stallTimeout: STALL_TIMEOUT, priority: 5 }),
])
publicProvider({ stallTimeout: STALL_TIMEOUT, priority: 1 }),
],
)
const connectors = connectorsForWallets([
{
groupName: "Popular",
Expand All @@ -102,6 +91,7 @@ const connectors = connectorsForWallets([
export const wagmiConfig = createClient({
autoConnect: true,
provider,
webSocketProvider,
connectors,
})

Expand Down
17 changes: 5 additions & 12 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
useAccount,
useChainId,
useConnect,
useProvider,
useSigner,
} from "wagmi"
import { useAccount, useChainId, useProvider, useSigner } from "wagmi"

import { ChainId } from "../constants/networks"
import { useMemo } from "react"
Expand All @@ -14,13 +8,12 @@ export function useActiveWeb3React() {
const { address: account } = useAccount()
const provider = useProvider()
const { data: signer } = useSigner()
const { isLoading } = useConnect()
console.log("isloading ==>", isLoading)
const library = useMemo(() => signer || provider, [signer, provider])

const signerOrProvider = useMemo(() => signer || provider, [signer, provider])

const activeWeb3React = useMemo(
() => ({ chainId, account, library }),
[chainId, account, library],
() => ({ chainId, account, signerOrProvider }),
[chainId, account, signerOrProvider],
)

return activeWeb3React
Expand Down
35 changes: 21 additions & 14 deletions src/hooks/useAddTokenToMetamask.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
import { SUPPORTED_WALLETS, Token } from "../constants"
import { useCallback, useState } from "react"
import { useChainId, useConnect } from "wagmi"

import { ChainId } from "../constants/networks"
import { find } from "lodash"
import { Token } from "../constants"
import { getTokenIconPath } from "../utils"
import { useAccount } from "wagmi"
import { useActiveWeb3React } from "."

// @dev https://eips.ethereum.org/EIPS/eip-747#wallet_watchasset
export default function useAddTokenToMetamask(token: Token | undefined): {
addToken: () => void
success: boolean | undefined
canAdd: boolean
} {
const chainId = useChainId()
const connector = useConnect()
const [success] = useState<boolean | undefined>()
const { chainId } = useActiveWeb3React()
const { connector: activeConnector } = useAccount()
const [success, setSuccess] = useState<boolean | undefined>()

const isMetaMask: boolean =
find(SUPPORTED_WALLETS, ["connector", connector])?.name == "MetaMask"
const canAdd = Boolean(
isMetaMask && chainId && token?.addresses[chainId as ChainId],
)
const isMetaMask = activeConnector?.name === "MetaMask"
const canAdd = Boolean(isMetaMask && chainId && token?.addresses[chainId])

const addToken = useCallback(() => {
console.log("add")
}, [])
if (activeConnector && isMetaMask && token) {
activeConnector
.watchAsset?.({
address: token.addresses[chainId],
symbol: token.symbol,
decimals: token.decimals,
image: getTokenIconPath(token.symbol),
})
.then((success: boolean) => setSuccess(success))
.catch(() => setSuccess(false))
}
}, [activeConnector, chainId, isMetaMask, token])

return { addToken, success, canAdd }
}
12 changes: 6 additions & 6 deletions src/hooks/useApproveAndDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function useApproveAndDeposit(
const dispatch = useDispatch()
const swapContract = useSwapContract(poolName)
const lpTokenContract = useLPTokenContract(poolName)
const { account, chainId, library } = useActiveWeb3React()
const { account, chainId, signerOrProvider } = useActiveWeb3React()
const { gasStandard, gasFast, gasInstant } = useSelector(
(state: AppState) => state.application,
)
Expand All @@ -65,12 +65,12 @@ export function useApproveAndDeposit(
return getContract(
pool.poolAddress,
META_SWAP_ABI,
library,
signerOrProvider,
account ?? undefined,
) as MetaSwap
}
return null
}, [pool?.poolAddress, chainId, library, account])
}, [pool?.poolAddress, chainId, signerOrProvider, account])

return async function approveAndDeposit(
state: ApproveAndDepositStateArgument,
Expand Down Expand Up @@ -118,17 +118,17 @@ export function useApproveAndDeposit(
if (!token) {
enqueueToast(
"error",
"There was a problem loading the token or library",
"There was a problem loading the token or signerOrProvider",
)
console.error("Token or library is not loaded")
console.error("Token or signerOrProvider is not loaded")
return
}
const spendingValue = BigNumber.from(state[token.address].valueSafe)
if (spendingValue.isZero()) return
const tokenContract = getContract(
token.address,
ERC20_ABI,
library,
signerOrProvider,
account ?? undefined,
) as Erc20
if (tokenContract == null) return
Expand Down
10 changes: 5 additions & 5 deletions src/hooks/useApproveAndMigrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function useApproveAndMigrate(): (
const dispatch = useDispatch()
const migratorContract = useGeneralizedSwapMigratorContract()
const basicPools = useContext(BasicPoolsContext)
const { account, chainId, library } = useActiveWeb3React()
const { account, chainId, signerOrProvider } = useActiveWeb3React()
const { gasStandard, gasFast, gasInstant } = useSelector(
(state: AppState) => state.application,
)
Expand All @@ -41,7 +41,7 @@ export function useApproveAndMigrate(): (
if (
!migratorContract ||
!account ||
!library ||
!signerOrProvider ||
!oldPool ||
!lpTokenBalance ||
lpTokenBalance.isZero()
Expand All @@ -50,19 +50,19 @@ export function useApproveAndMigrate(): (
const lpTokenContract = getContract(
oldPool.lpToken,
ERC20_ABI,
library,
signerOrProvider,
account,
) as Erc20
const oldPoolAddress = oldPool.poolAddress

const newPoolContract = getSwapContract(
library,
signerOrProvider,
oldPool?.newPoolAddress,
{},
account ?? undefined,
) as SwapFlashLoanNoWithdrawFee
const oldPoolContract = getSwapContract(
library,
signerOrProvider,
oldPoolAddress,
{},
account ?? undefined,
Expand Down
7 changes: 4 additions & 3 deletions src/hooks/useApproveAndSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function useApproveAndSwap(): (
const dispatch = useDispatch()
const basicPools = useContext(BasicPoolsContext)
const { tokenAddrToTokenMap } = useTokenMaps()
const { library } = useActiveWeb3React()
const { signerOrProvider } = useActiveWeb3React()
const { address: account } = useAccount()
const chainId = useChainId()
const baseSynthetixContract = useSynthetixContract()
Expand All @@ -76,7 +76,8 @@ export function useApproveAndSwap(): (
state: ApproveAndSwapStateArgument,
): Promise<void> {
try {
if (!account || !library) throw new Error("Wallet must be connected")
if (!account || !signerOrProvider)
throw new Error("Wallet must be connected")
if (state.swapType === SWAP_TYPES.DIRECT && !state.swapContract)
throw new Error("Swap contract is not loaded")
if (state.swapType !== SWAP_TYPES.DIRECT && !state.bridgeContract)
Expand All @@ -88,7 +89,7 @@ export function useApproveAndSwap(): (
const tokenContract = getContract(
token.address.toLowerCase(),
ERC20_ABI,
library,
signerOrProvider,
account,
) as Erc20
let gasPrice
Expand Down
18 changes: 9 additions & 9 deletions src/hooks/useApproveAndWithdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ export function useApproveAndWithdraw(
const basicPools = useContext(BasicPoolsContext)
const swapContract = useSwapContract(poolName)
const lpTokenContract = useLPTokenContract(poolName)
const { account, chainId, library } = useActiveWeb3React()
const { account, chainId, signerOrProvider } = useActiveWeb3React()
const { gasStandard, gasFast, gasInstant } = useSelector(
(state: AppState) => state.application,
)
const tokens = useContext(TokensContext)
const pool = basicPools?.[poolName]

const metaSwapContract = useMemo(() => {
if (pool?.poolAddress && chainId && library) {
if (pool?.poolAddress && chainId && signerOrProvider) {
return getContract(
pool.poolAddress,
META_SWAP_ABI,
library,
signerOrProvider,
account ?? undefined,
) as MetaSwap
}
}, [chainId, library, account, pool?.poolAddress])
}, [chainId, signerOrProvider, account, pool?.poolAddress])

const {
slippageCustom,
Expand All @@ -74,7 +74,7 @@ export function useApproveAndWithdraw(
try {
const basicPool = basicPools?.[poolName]
if (!state || !tokens) return
if (!account || !chainId || !library)
if (!account || !chainId || !signerOrProvider)
throw new Error("Wallet must be connected")
if (!swapContract || !basicPool || !lpTokenContract)
throw new Error("Swap contract is not loaded")
Expand Down Expand Up @@ -117,12 +117,12 @@ export function useApproveAndWithdraw(
const approveSingleToken = async (
token: BasicToken | undefined,
): Promise<void> => {
if (!token || !library) {
if (!token || !signerOrProvider) {
enqueueToast(
"error",
"There was a problem loading the token or library",
"There was a problem loading the token or signerOrProvider",
)
console.error("Token or library is not loaded")
console.error("Token or signerOrProvider is not loaded")
return
}
const spendingValue = BigNumber.from(
Expand All @@ -132,7 +132,7 @@ export function useApproveAndWithdraw(
const tokenContract = getContract(
token.address,
ERC20_ABI,
library,
signerOrProvider,
account ?? undefined,
) as Erc20
if (tokenContract == null) return
Expand Down
Loading

0 comments on commit d24c959

Please sign in to comment.