-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored hooks/useContract mod into own folder
- Loading branch information
Leandro
committed
Mar 4, 2022
1 parent
e5152a0
commit b923ee0
Showing
2 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { Contract } from '@ethersproject/contracts' | ||
import { Web3Provider } from '@ethersproject/providers' | ||
|
||
import { GP_SETTLEMENT_CONTRACT_ADDRESS, V_COW_CONTRACT_ADDRESS } from 'constants/index' | ||
import { SupportedChainId as ChainId } from 'constants/chains' | ||
|
||
import GPv2_SETTLEMENT_ABI from 'abis/GPv2Settlement.json' | ||
import V_COW_ABI from 'abis/vCow.json' | ||
import ENS_ABI from 'abis/ens-registrar.json' | ||
import ERC20_ABI from 'abis/erc20.json' | ||
import ERC20_BYTES32_ABI from 'abis/erc20_bytes32.json' | ||
import { Erc20, GPv2Settlement, VCow } from 'abis/types' | ||
|
||
import useActiveWeb3React from 'hooks/useActiveWeb3React' | ||
import { getContract } from 'utils' | ||
|
||
import { useContract } from '@src/hooks/useContract' | ||
|
||
export * from '@src/hooks/useContract' | ||
export * from './useContractMod' | ||
|
||
// Custom (non-MOD) hooks | ||
|
||
export function useGP2SettlementContract(): GPv2Settlement | null { | ||
const { chainId } = useActiveWeb3React() | ||
return useContract<GPv2Settlement>( | ||
chainId ? GP_SETTLEMENT_CONTRACT_ADDRESS[chainId] : undefined, | ||
GPv2_SETTLEMENT_ABI, | ||
true | ||
) | ||
} | ||
|
||
export function useVCowContract() { | ||
const { chainId } = useActiveWeb3React() | ||
return useContract<VCow>(chainId ? V_COW_CONTRACT_ADDRESS[chainId] : undefined, V_COW_ABI, true) | ||
} | ||
|
||
export function useENSRegistrarContract(withSignerIfPossible?: boolean): Contract | null { | ||
const { chainId } = useActiveWeb3React() | ||
let address: string | undefined | ||
if (chainId) { | ||
switch (chainId) { | ||
case ChainId.MAINNET: | ||
case ChainId.RINKEBY: | ||
address = '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e' | ||
break | ||
case ChainId.XDAI: | ||
address = '0x25D2252Ec30de7830b6825D6b4A08E70a581cD6a' | ||
break | ||
} | ||
} | ||
return useContract(address, ENS_ABI, withSignerIfPossible) | ||
} | ||
|
||
/** | ||
* Non-hook version of useContract | ||
*/ | ||
function _getContract<T extends Contract = Contract>( | ||
addressOrAddressMap: string | { [chainId: number]: string } | undefined, | ||
ABI: any, | ||
withSignerIfPossible = true, | ||
library?: Web3Provider, | ||
account?: string, | ||
chainId?: ChainId | ||
): T | null { | ||
if (!addressOrAddressMap || !ABI || !library || !chainId) return null | ||
let address: string | undefined | ||
if (typeof addressOrAddressMap === 'string') address = addressOrAddressMap | ||
else address = addressOrAddressMap[chainId] | ||
if (!address) return null | ||
try { | ||
return getContract(address, ABI, library, withSignerIfPossible && account ? account : undefined) as T | ||
} catch (error) { | ||
console.error('Failed to get contract', error) | ||
return null | ||
} | ||
} | ||
|
||
/** | ||
* Non-hook version of useTokenContract | ||
*/ | ||
export function getTokenContract( | ||
tokenAddress?: string, | ||
withSignerIfPossible?: boolean, | ||
library?: Web3Provider, | ||
account?: string, | ||
chainId?: ChainId | ||
): Erc20 | null { | ||
return _getContract<Erc20>(tokenAddress, ERC20_ABI, withSignerIfPossible, library, account, chainId) | ||
} | ||
|
||
/** | ||
* Non-hook version of useBytes32TokenContract | ||
*/ | ||
export function getBytes32TokenContract( | ||
tokenAddress?: string, | ||
withSignerIfPossible?: boolean, | ||
library?: Web3Provider, | ||
account?: string, | ||
chainId?: ChainId | ||
): Contract | null { | ||
return _getContract(tokenAddress, ERC20_BYTES32_ABI, withSignerIfPossible, library, account, chainId) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// import { Contract } from '@ethersproject/contracts' | ||
// import IUniswapV2PairJson from '@uniswap/v2-core/build/IUniswapV2Pair.json' | ||
// import IUniswapV2Router02Json from '@uniswap/v2-periphery/build/IUniswapV2Router02.json' | ||
// import QuoterJson from '@uniswap/v3-periphery/artifacts/contracts/lens/Quoter.sol/Quoter.json' | ||
// import TickLensJson from '@uniswap/v3-periphery/artifacts/contracts/lens/TickLens.sol/TickLens.json' | ||
// import UniswapInterfaceMulticallJson from '@uniswap/v3-periphery/artifacts/contracts/lens/UniswapInterfaceMulticall.sol/UniswapInterfaceMulticall.json' | ||
// import NonfungiblePositionManagerJson from '@uniswap/v3-periphery/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json' | ||
// import V3MigratorJson from '@uniswap/v3-periphery/artifacts/contracts/V3Migrator.sol/V3Migrator.json' | ||
// import ARGENT_WALLET_DETECTOR_ABI from 'abis/argent-wallet-detector.json' | ||
// import EIP_2612 from 'abis/eip_2612.json' | ||
// import ENS_PUBLIC_RESOLVER_ABI from 'abis/ens-public-resolver.json' | ||
import ENS_ABI from 'abis/ens-registrar.json' | ||
// import ERC20_ABI from 'abis/erc20.json' | ||
// import ERC20_BYTES32_ABI from 'abis/erc20_bytes32.json' | ||
// import ERC721_ABI from 'abis/erc721.json' | ||
// import ERC1155_ABI from 'abis/erc1155.json' | ||
// import { ArgentWalletDetector, EnsPublicResolver, EnsRegistrar, Erc20, Erc721, Erc1155, Weth } from 'abis/types' | ||
// import WETH_ABI from 'abis/weth.json' | ||
// import { | ||
// ARGENT_WALLET_DETECTOR_ADDRESS, | ||
// ENS_REGISTRAR_ADDRESSES, | ||
// MULTICALL_ADDRESS, | ||
// NONFUNGIBLE_POSITION_MANAGER_ADDRESSES, | ||
// QUOTER_ADDRESSES, | ||
// TICK_LENS_ADDRESSES, | ||
// V2_ROUTER_ADDRESS, | ||
// V3_MIGRATOR_ADDRESSES, | ||
// } from 'constants/addresses' | ||
// import { WRAPPED_NATIVE_CURRENCY } from 'constants/tokens' | ||
import useActiveWeb3React from 'hooks/useActiveWeb3React' | ||
// import { useMemo } from 'react' | ||
// import { NonfungiblePositionManager, Quoter, TickLens, UniswapInterfaceMulticall } from 'types/v3' | ||
// import { V3Migrator } from 'types/v3/V3Migrator' | ||
|
||
// import { getContract } from 'utils' | ||
|
||
// MOD imports | ||
import { useContract } from '@src/hooks/useContract' | ||
import { SupportedChainId as ChainId } from 'constants/chains' | ||
|
||
/* const { abi: IUniswapV2PairABI } = IUniswapV2PairJson | ||
const { abi: IUniswapV2Router02ABI } = IUniswapV2Router02Json | ||
const { abi: QuoterABI } = QuoterJson | ||
const { abi: TickLensABI } = TickLensJson | ||
const { abi: MulticallABI } = UniswapInterfaceMulticallJson | ||
const { abi: NFTPositionManagerABI } = NonfungiblePositionManagerJson | ||
const { abi: V2MigratorABI } = V3MigratorJson | ||
// returns null on errors | ||
export function useContract<T extends Contract = Contract>( | ||
addressOrAddressMap: string | { [chainId: number]: string } | undefined, | ||
ABI: any, | ||
withSignerIfPossible = true | ||
): T | null { | ||
const { library, account, chainId } = useActiveWeb3React() | ||
return useMemo(() => { | ||
if (!addressOrAddressMap || !ABI || !library || !chainId) return null | ||
let address: string | undefined | ||
if (typeof addressOrAddressMap === 'string') address = addressOrAddressMap | ||
else address = addressOrAddressMap[chainId] | ||
if (!address) return null | ||
try { | ||
return getContract(address, ABI, library, withSignerIfPossible && account ? account : undefined) | ||
} catch (error) { | ||
console.error('Failed to get contract', error) | ||
return null | ||
} | ||
}, [addressOrAddressMap, ABI, library, chainId, withSignerIfPossible, account]) as T | ||
} | ||
export function useV2MigratorContract() { | ||
return useContract<V3Migrator>(V3_MIGRATOR_ADDRESSES, V2MigratorABI, true) | ||
} | ||
export function useTokenContract(tokenAddress?: string, withSignerIfPossible?: boolean) { | ||
return useContract<Erc20>(tokenAddress, ERC20_ABI, withSignerIfPossible) | ||
} | ||
export function useWETHContract(withSignerIfPossible?: boolean) { | ||
const { chainId } = useActiveWeb3React() | ||
return useContract<Weth>( | ||
chainId ? WRAPPED_NATIVE_CURRENCY[chainId]?.address : undefined, | ||
WETH_ABI, | ||
withSignerIfPossible | ||
) | ||
} | ||
export function useERC721Contract(nftAddress?: string) { | ||
return useContract<Erc721>(nftAddress, ERC721_ABI, false) | ||
} | ||
export function useERC1155Contract(nftAddress?: string) { | ||
return useContract<Erc1155>(nftAddress, ERC1155_ABI, false) | ||
} | ||
export function useArgentWalletDetectorContract() { | ||
return useContract<ArgentWalletDetector>(ARGENT_WALLET_DETECTOR_ADDRESS, ARGENT_WALLET_DETECTOR_ABI, false) | ||
} */ | ||
|
||
export function useENSRegistrarContract(withSignerIfPossible?: boolean) { | ||
// return useContract<EnsRegistrar>(ENS_REGISTRAR_ADDRESSES, ENS_ABI, withSignerIfPossible) | ||
const { chainId } = useActiveWeb3React() | ||
let address: string | undefined | ||
if (chainId) { | ||
switch (chainId) { | ||
case ChainId.MAINNET: | ||
case ChainId.RINKEBY: | ||
address = '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e' | ||
break | ||
case ChainId.XDAI: | ||
address = '0x25D2252Ec30de7830b6825D6b4A08E70a581cD6a' | ||
break | ||
} | ||
} | ||
return useContract(address, ENS_ABI, withSignerIfPossible) | ||
} | ||
|
||
/* export function useENSResolverContract(address: string | undefined, withSignerIfPossible?: boolean) { | ||
return useContract<EnsPublicResolver>(address, ENS_PUBLIC_RESOLVER_ABI, withSignerIfPossible) | ||
} | ||
export function useBytes32TokenContract(tokenAddress?: string, withSignerIfPossible?: boolean): Contract | null { | ||
return useContract(tokenAddress, ERC20_BYTES32_ABI, withSignerIfPossible) | ||
} | ||
export function useEIP2612Contract(tokenAddress?: string): Contract | null { | ||
return useContract(tokenAddress, EIP_2612, false) | ||
} | ||
export function usePairContract(pairAddress?: string, withSignerIfPossible?: boolean): Contract | null { | ||
return useContract(pairAddress, IUniswapV2PairABI, withSignerIfPossible) | ||
} | ||
export function useV2RouterContract(): Contract | null { | ||
return useContract(V2_ROUTER_ADDRESS, IUniswapV2Router02ABI, true) | ||
} | ||
export function useInterfaceMulticall() { | ||
return useContract<UniswapInterfaceMulticall>(MULTICALL_ADDRESS, MulticallABI, false) as UniswapInterfaceMulticall | ||
} | ||
export function useV3NFTPositionManagerContract(withSignerIfPossible?: boolean): NonfungiblePositionManager | null { | ||
return useContract<NonfungiblePositionManager>( | ||
NONFUNGIBLE_POSITION_MANAGER_ADDRESSES, | ||
NFTPositionManagerABI, | ||
withSignerIfPossible | ||
) | ||
} | ||
export function useV3Quoter() { | ||
return useContract<Quoter>(QUOTER_ADDRESSES, QuoterABI) | ||
} | ||
export function useTickLens(): TickLens | null { | ||
const { chainId } = useActiveWeb3React() | ||
const address = chainId ? TICK_LENS_ADDRESSES[chainId] : undefined | ||
return useContract(address, TickLensABI) as TickLens | null | ||
} */ |