UNPKG

web3react-v8-ian

Version:
332 lines (321 loc) 10.5 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { SUPPORTED_WALLETS: () => SUPPORTED_WALLETS, Wallet: () => Wallet, WalletProvider: () => ProviderExample, getAddChainParameters: () => getAddChainParameters, getAddChainParametersfromNetworkName: () => getAddChainParametersfromNetworkName, useWallets: () => useWallets, useWeb3React: () => import_core5.useWeb3React }); module.exports = __toCommonJS(src_exports); // src/context/WalletProvider.tsx var import_core4 = require("@web3-react/core"); var import_react = __toESM(require("react"), 1); // src/connectors/metaMask.ts var import_core = require("@web3-react/core"); var import_metamask = require("@web3-react/metamask"); var [metaMask, hooks] = (0, import_core.initializeConnector)((actions) => new import_metamask.MetaMask({ actions })); // src/connectors/network.ts var import_core2 = require("@web3-react/core"); var import_network = require("@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] = (0, import_core2.initializeConnector)((actions) => new import_network.Network({ actions, urlMap: URLS })); // src/connectors/walletConnect.ts var import_core3 = require("@web3-react/core"); var import_walletconnect = require("@web3-react/walletconnect"); var [walletConnect, hooks3] = (0, import_core3.initializeConnector)((actions) => new import_walletconnect.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 = import_react.default.createContext({ setSelectedWallet: () => { }, selectedWallet: void 0 }); function ProviderExample({ children }) { const [selectedWallet, setSelectedWallet] = (0, import_react.useState)(); const connectors = (0, import_react.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]); (0, import_react.useEffect)(() => { network.activate(); }, []); return /* @__PURE__ */ import_react.default.createElement(WalletContext.Provider, { value: { selectedWallet, setSelectedWallet } }, /* @__PURE__ */ import_react.default.createElement(import_core4.Web3ReactProvider, { connectors }, children)); } // src/index.ts var import_core5 = require("@web3-react/core"); // src/hooks/useWallets.ts var import_react2 = require("react"); function useWallets() { const context = (0, import_react2.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 || {}); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { SUPPORTED_WALLETS, Wallet, WalletProvider, getAddChainParameters, getAddChainParametersfromNetworkName, useWallets, useWeb3React }); //# sourceMappingURL=index.cjs.map