UNPKG

web3react-v8-ian

Version:
297 lines (288 loc) 8.7 kB
// src/context/WalletProvider.tsx import { Web3ReactProvider } from "@web3-react/core"; import React, { useMemo, useEffect, useState } from "react"; // src/connectors/metaMask.ts import { initializeConnector } from "@web3-react/core"; import { MetaMask } from "@web3-react/metamask"; var [metaMask, hooks] = initializeConnector((actions) => new MetaMask({ actions })); // src/connectors/network.ts import { initializeConnector as initializeConnector2 } from "@web3-react/core"; import { Network } from "@web3-react/network"; // src/chains.ts var INFURA_KEY = process.env.REACT_APP_INFURA_KEY ? process.env.REACT_APP_INFURA_KEY : process.env.INFURA_KEY; var ALCHEMY_KEY = process.env.REACT_APP_ALCHEMY_KEY ? process.env.REACT_APP_ALCHEMY_KEY : process.env.ALCHEMY_KEY; var ETH = { name: "Ether", symbol: "ETH", decimals: 18 }; var MATIC = { name: "Matic", symbol: "MATIC", decimals: 18 }; var AVAX = { name: "Avax", symbol: "AVAX", decimals: 18 }; var BNB = { name: "Binance Coin", symbol: "BNB", decimals: 18 }; var GLMR = { name: "Glimmer", symbol: "GLMR", decimals: 18 }; var MOVR = { name: "Moonriver", symbol: "MOVR", decimals: 18 }; var METIS = { name: "Metis", symbol: "METIS", decimals: 18 }; function isExtendedChainInformation(chainInformation) { return !!chainInformation.nativeCurrency; } function getAddChainParameters(chainId) { const chainInformation = CHAINS[chainId]; if (isExtendedChainInformation(chainInformation)) { return { chainId, chainName: chainInformation.name, nativeCurrency: chainInformation.nativeCurrency, rpcUrls: chainInformation.urls, blockExplorerUrls: chainInformation.blockExplorerUrls }; } else { return chainId; } } var NetworkChainMap = { ethereum: 1, avalanche: 43114, bsc: 56, moonbeam: 1284, moonriver: 1285, metis: 1088, optimism: 10, polygon: 137 }; var CHAINS = { 1: { urls: [ INFURA_KEY ? `https://mainnet.infura.io/v3/${INFURA_KEY}` : void 0, ALCHEMY_KEY ? `https://eth-mainnet.alchemyapi.io/v2/${ALCHEMY_KEY}` : void 0, "https://cloudflare-eth.com" ].filter((url) => url !== void 0), name: "Mainnet" }, 3: { urls: [INFURA_KEY ? `https://ropsten.infura.io/v3/${INFURA_KEY}` : void 0].filter((url) => url !== void 0), name: "Ropsten" }, 4: { urls: [INFURA_KEY ? `https://rinkeby.infura.io/v3/${INFURA_KEY}` : void 0].filter((url) => url !== void 0), name: "Rinkeby" }, 5: { urls: [INFURA_KEY ? `https://goerli.infura.io/v3/${INFURA_KEY}` : void 0].filter((url) => url !== void 0), name: "G\xF6rli" }, 42: { urls: [INFURA_KEY ? `https://kovan.infura.io/v3/${INFURA_KEY}` : void 0].filter((url) => url !== void 0), name: "Kovan" }, 10: { urls: [ INFURA_KEY ? `https://optimism-mainnet.infura.io/v3/${INFURA_KEY}` : void 0, "https://mainnet.optimism.io" ].filter((url) => url !== void 0), name: "Optimism", nativeCurrency: ETH, blockExplorerUrls: ["https://optimistic.etherscan.io"] }, 69: { urls: [ INFURA_KEY ? `https://optimism-kovan.infura.io/v3/${INFURA_KEY}` : void 0, "https://kovan.optimism.io" ].filter((url) => url !== void 0), name: "Optimism Kovan", nativeCurrency: ETH, blockExplorerUrls: ["https://kovan-optimistic.etherscan.io"] }, 42161: { urls: [ INFURA_KEY ? `https://arbitrum-mainnet.infura.io/v3/${INFURA_KEY}` : void 0, "https://arb1.arbitrum.io/rpc" ].filter((url) => url !== void 0), name: "Arbitrum One", nativeCurrency: ETH, blockExplorerUrls: ["https://arbiscan.io"] }, 421611: { urls: [ INFURA_KEY ? `https://arbitrum-rinkeby.infura.io/v3/${INFURA_KEY}` : void 0, "https://rinkeby.arbitrum.io/rpc" ].filter((url) => url !== void 0), name: "Arbitrum Testnet", nativeCurrency: ETH, blockExplorerUrls: ["https://testnet.arbiscan.io"] }, 137: { urls: [ INFURA_KEY ? `https://polygon-mainnet.infura.io/v3/${INFURA_KEY}` : void 0, "https://polygon-rpc.com" ].filter((url) => url !== void 0), name: "Polygon Mainnet", nativeCurrency: MATIC, blockExplorerUrls: ["https://polygonscan.com"] }, 80001: { urls: [INFURA_KEY ? `https://polygon-mumbai.infura.io/v3/${INFURA_KEY}` : void 0].filter((url) => url !== void 0), name: "Polygon Mumbai", nativeCurrency: MATIC, blockExplorerUrls: ["https://mumbai.polygonscan.com"] }, 43114: { urls: ["https://api.avax.network/ext/bc/C/rpc"].filter((url) => url !== void 0), name: "Avalanche", nativeCurrency: AVAX, blockExplorerUrls: ["https://snowtrace.io"] }, 56: { urls: ["https://bsc-dataseed.binance.org"].filter((url) => url !== void 0), name: "Binance", nativeCurrency: BNB, blockExplorerUrls: ["https://bscscan.com"] }, 1284: { urls: ["https://rpc.api.moonbeam.network"].filter((url) => url !== void 0), name: "Glimmer", nativeCurrency: GLMR, blockExplorerUrls: ["https://moonbeam.moonscan.io"] }, 1285: { urls: ["https://rpc.moonriver.moonbeam.network"].filter((url) => url !== void 0), name: "Moonriver", nativeCurrency: MOVR, blockExplorerUrls: ["https://moonriver.moonscan.io"] }, 1088: { urls: ["https://andromeda.metis.io/?owner=1088"].filter((url) => url !== void 0), name: "Metis", nativeCurrency: METIS, blockExplorerUrls: ["https://andromeda-explorer.metis.io"] } }; var URLS = Object.keys(CHAINS).reduce((accumulator, chainId) => { const validURLs = CHAINS[Number(chainId)].urls; if (validURLs.length) { accumulator[Number(chainId)] = validURLs; } return accumulator; }, {}); var getAddChainParametersfromNetworkName = (network2) => { const chainId = NetworkChainMap[network2]; return getAddChainParameters(chainId); }; // src/connectors/network.ts var [network, hooks2] = initializeConnector2((actions) => new Network({ actions, urlMap: URLS })); // src/connectors/walletConnect.ts import { initializeConnector as initializeConnector3 } from "@web3-react/core"; import { WalletConnect } from "@web3-react/walletconnect"; var [walletConnect, hooks3] = initializeConnector3((actions) => new WalletConnect({ actions, options: { rpc: URLS } })); // src/context/WalletProvider.tsx var initialConnectors = [ [network, hooks2], [metaMask, hooks], [walletConnect, hooks3] ]; var SUPPORTED_WALLETS = { METAMASK: { connector: metaMask, hooks, wallet: "METAMASK" /* METAMASK */, name: "MetaMask" }, WALLET_CONNECT: { connector: walletConnect, hooks: hooks3, wallet: "WALLET_CONNECT" /* WALLET_CONNECT */, name: "WalletConnect", mobile: true } }; var WalletContext = React.createContext({ setSelectedWallet: () => { }, selectedWallet: void 0 }); function ProviderExample({ children }) { const [selectedWallet, setSelectedWallet] = useState(); const connectors = useMemo(() => { if (!selectedWallet) return initialConnectors; const connectorList = []; if (selectedWallet) { const wallet = SUPPORTED_WALLETS[selectedWallet]; connectorList.push([wallet.connector, wallet.hooks]); } Object.keys(SUPPORTED_WALLETS).filter((wallet) => wallet !== selectedWallet).forEach((ele) => { const wallet = SUPPORTED_WALLETS[ele]; connectorList.push([wallet.connector, wallet.hooks]); }); connectorList.push([network, hooks2]); return connectorList; }, [selectedWallet]); useEffect(() => { network.activate(); }, []); return /* @__PURE__ */ React.createElement(WalletContext.Provider, { value: { selectedWallet, setSelectedWallet } }, /* @__PURE__ */ React.createElement(Web3ReactProvider, { connectors }, children)); } // src/index.ts import { useWeb3React } from "@web3-react/core"; // src/hooks/useWallets.ts import { useContext } from "react"; function useWallets() { const context = useContext(WalletContext); if (context === void 0) { throw new Error("useWallet must be used within a useWalletsProvider"); } return context; } // src/types/index.ts var Wallet = /* @__PURE__ */ ((Wallet2) => { Wallet2["INJECTED"] = "INJECTED"; Wallet2["METAMASK"] = "METAMASK"; Wallet2["COINBASE_WALLET"] = "COINBASE_WALLET"; Wallet2["WALLET_CONNECT"] = "WALLET_CONNECT"; Wallet2["FORTMATIC"] = "FORTMATIC"; Wallet2["NETWORK"] = "NETWORK"; Wallet2["GNOSIS_SAFE"] = "GNOSIS_SAFE"; return Wallet2; })(Wallet || {}); export { SUPPORTED_WALLETS, Wallet, ProviderExample as WalletProvider, getAddChainParameters, getAddChainParametersfromNetworkName, useWallets, useWeb3React }; //# sourceMappingURL=index.js.map