UNPKG

@iandjx/wallet

Version:

Implmentation of web3react-v8 for RT widgets

512 lines (502 loc) 16.3 kB
// src/index.ts export * from "@web3-react/core"; // src/hooks/useWallets.ts import { useContext } from "react"; // src/context/WalletProvider.tsx import { useWeb3React, Web3ReactProvider } from "@web3-react/core"; import React, { useEffect, useState, useMemo as useMemo2 } from "react"; import { ethers } from "ethers"; import { RomeEventType, widgetBridge } from "@romeblockchain/bridge"; // src/bannedAccounts.json var bannedAccounts_default = [ "0x8589427373d6d84e98730d7795d8f6f8731fda16", "0x722122df12d4e14e13ac3b6895a86e84145b6967", "0xdd4c48c0b24039969fc16d1cdf626eab821d3384", "0xd90e2f925da726b50c4ed8d0fb90ad053324f31b", "0xd96f2b1c14db8458374d9aca76e26c3d18364307", "0x4736dcf1b7a3d580672cce6e7c65cd5cc9cfba9d", "0xd4b88df4d29f5cedd6857912842cff3b20c8cfa3", "0x910cbd523d972eb0a6f4cae4618ad62622b39dbf", "0xa160cdab225685da1d56aa342ad8841c3b53f291", "0xfd8610d20aa15b7b2e3be39b396a1bc3516c7144", "0xf60dd140cff0706bae9cd734ac3ae76ad9ebc32a", "0x22aaa7720ddd5388a3c0a3333430953c68f1849b", "0xba214c1c1928a32bffe790263e38b4af9bfcd659", "0xb1c8094b234dce6e03f10a5b673c1d8c69739a00", "0x527653ea119f3e6a1f5bd18fbf4714081d7b31ce", "0x58e8dcc13be9780fc42e8723d8ead4cf46943df2", "0xd691f27f38b395864ea86cfc7253969b409c362d", "0xaeaac358560e11f52454d997aaff2c5731b6f8a6", "0x1356c899d8c9467c7f71c195612f8a395abf2f0a", "0xa60c772958a3ed56c1f15dd055ba37ac8e523a0d", "0x169ad27a470d064dede56a2d3ff727986b15d52b", "0x0836222f2b2b24a3f36f98668ed8f0b38d1a872f", "0xf67721a2d8f736e75a49fdd7fad2e31d8676542a", "0x9ad122c22b14202b4490edaf288fdb3c7cb3ff5e", "0x905b63fff465b9ffbf41dea908ceb12478ec7601", "0x07687e702b410fa43f4cb4af7fa097918ffd2730", "0x94a1b5cdb22c43faab4abeb5c74999895464ddaf", "0xb541fc07bc7619fd4062a54d96268525cbc6ffef", "0x12d66f87a04a9e220743712ce6d9bb1b5616b8fc", "0x47ce0c6ed5b0ce3d3a51fdb1c52dc66a7c3c2936", "0x23773e65ed146a459791799d01336db287f25334", "0xd21be7248e0197ee08e0c20d4a96debdac3d20af", "0x610b717796ad172b316836ac95a2ffad065ceab4", "0x178169b423a011fff22b9e3f3abea13414ddd0f1", "0xbb93e510bbcd0b7beb5a853875f9ec60275cf498", "0x2717c5e28cf931547b621a5dddb772ab6a35b701", "0x03893a7c7463ae47d46bc7f091665f1893656003", "0xca0840578f57fe71599d29375e16783424023357", "0x58e8dcc13be9780fc42e8723d8ead4cf46943df2", "0x8589427373d6d84e98730d7795d8f6f8731fda16", "0x722122df12d4e14e13ac3b6895a86e84145b6967", "0xdd4c48c0b24039969fc16d1cdf626eab821d3384", "0xd90e2f925da726b50c4ed8d0fb90ad053324f31b", "0xd96f2b1c14db8458374d9aca76e26c3d18364307", "0x4736dcf1b7a3d580672cce6e7c65cd5cc9cfba9d" ]; // src/connection/connectors.ts import { CoinbaseWallet } from "@web3-react/coinbase-wallet"; import { initializeConnector } from "@web3-react/core"; import { MetaMask } from "@web3-react/metamask"; import { Network } from "@web3-react/network"; import { WalletConnect } from "@web3-react/walletconnect"; // 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 }; var JEWEL = { name: "Jewel", symbol: "JEWEL", decimals: 18 }; var KLAY = { name: "Klay", symbol: "KLAY", decimals: 18 }; var NetworkName = /* @__PURE__ */ ((NetworkName2) => { NetworkName2["ETHEREUM"] = "ethereum"; NetworkName2["AVALANCHE"] = "avalanche"; NetworkName2["BINANCE"] = "bsc"; NetworkName2["MOONBEAM"] = "moonbeam"; NetworkName2["MOONRIVER"] = "moonriver"; NetworkName2["METIS"] = "metis"; NetworkName2["OPTIMISM"] = "optimism"; NetworkName2["POLYGON"] = "polygon"; NetworkName2["DFK"] = "dfk"; NetworkName2["KLAYTN"] = "klaytn"; return NetworkName2; })(NetworkName || {}); function getAddChainParameters(chainId) { const chainInformation = CHAINS[chainId]; return { chainId, chainName: chainInformation.name, nativeCurrency: chainInformation.nativeCurrency, rpcUrls: chainInformation.urls, blockExplorerUrls: chainInformation.blockExplorerUrls }; } var NetworkChainMap = { ethereum: 1, avalanche: 43114, bsc: 56, moonbeam: 1284, moonriver: 1285, metis: 1088, optimism: 10, polygon: 137, dfk: 53935, klaytn: 8217 }; var CHAINS = { 1: { urls: [ INFURA_KEY ? `https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161` : 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", nativeCurrency: ETH, blockExplorerUrls: ["https://etherscan.io/"] }, 5: { urls: [INFURA_KEY ? `https://goerli.infura.io/v3/${INFURA_KEY}` : void 0].filter((url) => url !== void 0), name: "G\xF6rli", nativeCurrency: ETH, blockExplorerUrls: ["https://goerli.etherscan.io/"] }, 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"] }, 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"] }, 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"] }, 53935: { urls: ["https://subnets.avax.network/defi-kingdoms/dfk-chain/rpc"].filter((url) => url !== void 0), name: "DFK", nativeCurrency: JEWEL, blockExplorerUrls: ["https://subnets.avax.network/defi-kingdoms"] }, 8217: { urls: ["https://public-node-api.klaytnapi.com/v1/cypress"].filter((url) => url !== void 0), name: "Klaytn", nativeCurrency: KLAY, blockExplorerUrls: ["https://scope.klaytn.com"] } }; 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 = (network) => { const chainId = NetworkChainMap[network]; return getAddChainParameters(chainId); }; // src/connection/connectors.ts var ConnectionType = /* @__PURE__ */ ((ConnectionType2) => { ConnectionType2["INJECTED"] = "INJECTED"; ConnectionType2["COINBASE_WALLET"] = "COINBASE_WALLET"; ConnectionType2["WALLET_CONNECT"] = "WALLET_CONNECT"; ConnectionType2["NETWORK"] = "NETWORK"; ConnectionType2["GNOSIS_SAFE"] = "GNOSIS_SAFE"; return ConnectionType2; })(ConnectionType || {}); function onError(error) { console.debug(`web3-react error: ${error}`); } var [web3Network, web3NetworkHooks] = initializeConnector( (actions) => new Network({ actions, urlMap: URLS, defaultChainId: 1 }) ); var networkConnection = { connector: web3Network, hooks: web3NetworkHooks, type: "NETWORK" /* NETWORK */ }; var [web3Injected, web3InjectedHooks] = initializeConnector((actions) => new MetaMask({ actions, onError })); var injectedConnection = { connector: web3Injected, hooks: web3InjectedHooks, type: "INJECTED" /* INJECTED */ }; var [web3WalletConnect, web3WalletConnectHooks] = initializeConnector((actions) => { return new WalletConnect({ actions, options: { rpc: URLS, qrcode: true }, onError }); }); var walletConnectConnection = { connector: web3WalletConnect, hooks: web3WalletConnectHooks, type: "WALLET_CONNECT" /* WALLET_CONNECT */ }; var [web3CoinbaseWallet, web3CoinbaseWalletHooks] = initializeConnector( (actions) => new CoinbaseWallet({ actions, options: { url: URLS[1][0], appName: "RBL", appLogoUrl: "https://app.benqi.fi/favicon.svg", reloadOnDisconnect: false }, onError }) ); var coinbaseWalletConnection = { connector: web3CoinbaseWallet, hooks: web3CoinbaseWalletHooks, type: "COINBASE_WALLET" /* COINBASE_WALLET */ }; var ConnectionList = [injectedConnection, coinbaseWalletConnection, walletConnectConnection]; // src/connection/utils.ts import { Connector } from "@web3-react/types"; var CONNECTIONS = [injectedConnection, coinbaseWalletConnection, walletConnectConnection, networkConnection]; function getConnection(connector) { if (connector instanceof Connector) { const connection = CONNECTIONS.find((connection2) => connection2.connector === connector); if (!connection) { throw Error("unsupported connector"); } return connection; } else { switch (connector) { case "INJECTED" /* INJECTED */: return injectedConnection; case "COINBASE_WALLET" /* COINBASE_WALLET */: return coinbaseWalletConnection; case "WALLET_CONNECT" /* WALLET_CONNECT */: return walletConnectConnection; case "NETWORK" /* NETWORK */: return networkConnection; default: throw Error("invalid connector"); } } } function getConnectionName(connectionType, isMetaMask, connectionName) { switch (connectionType) { case "INJECTED" /* INJECTED */: return isMetaMask ? "MetaMask" : connectionName ? connectionName : "Browser Wallet"; case "COINBASE_WALLET" /* COINBASE_WALLET */: return "Coinbase Wallet"; case "WALLET_CONNECT" /* WALLET_CONNECT */: return "WalletConnect"; case "NETWORK" /* NETWORK */: return "Network"; case "GNOSIS_SAFE" /* GNOSIS_SAFE */: return "Gnosis Safe"; } } // src/hooks/useOrderedConnections.ts import { useMemo } from "react"; var SELECTABLE_WALLETS = ["INJECTED" /* INJECTED */, "COINBASE_WALLET" /* COINBASE_WALLET */, "WALLET_CONNECT" /* WALLET_CONNECT */]; function useOrderedConnections(selectedWallet) { return useMemo(() => { const orderedConnectionTypes = []; if (selectedWallet) { orderedConnectionTypes.push(selectedWallet); } orderedConnectionTypes.push(...SELECTABLE_WALLETS.filter((wallet) => wallet !== selectedWallet)); orderedConnectionTypes.push("NETWORK" /* NETWORK */); return orderedConnectionTypes.map(getConnection); }, [selectedWallet]); } // src/context/WalletProvider.tsx var WalletContext = React.createContext({ setSelectedWallet: () => { }, selectedWallet: void 0, handleConnect: async () => { }, loading: false }); var OfacBan = ({ children }) => { const { account } = useWeb3React(); const ofacBanned = account && bannedAccounts_default.includes(account.toLowerCase()); if (ofacBanned) { return /* @__PURE__ */ React.createElement( "div", { style: { display: "flex", justifyContent: "center", alignItems: "center", width: "100%", height: "100%" } }, /* @__PURE__ */ React.createElement( "div", { style: { padding: "1.25rem", backgroundColor: "#991b1b", borderRadius: "0.375rem" } }, /* @__PURE__ */ React.createElement( "p", { style: { marginBottom: "0.75rem", fontSize: "large" } }, "Blocked Address" ), /* @__PURE__ */ React.createElement("p", null, "This address is blocked because it is associated with banned activities.") ) ); } return children; }; var WalletProvider = ({ children, connectToNetwork }) => { const [selectedWallet, setSelectedWallet] = useState(); const [loading, setLoading] = useState(false); const connections = useOrderedConnections(selectedWallet); const handleConnect = async (connection, chainParams) => { const chainIdInput = chainParams && chainParams.chainId; const chainIdInputHex = chainIdInput && ethers.utils.hexValue(chainIdInput); const { connector } = connection; setLoading(true); try { await connector.activate(chainIdInput); console.log(connector.provider, "provider"); await connector.provider?.request({ method: "wallet_addEthereumChain", params: [ { ...chainParams, chainId: chainIdInputHex } ] }); await connector.provider?.request({ method: "wallet_switchEthereumChain", params: [ { chainId: chainIdInputHex } ] }); const accounts = connector.provider && await connector.provider.request({ method: "eth_requestAccounts" }); widgetBridge && accounts && widgetBridge.sendWalletConnectEvent(RomeEventType.WIDGET_WALLET_CONNECT_EVENT, { address: accounts[0], wallet: ConnectionType }); } catch (error) { throw new Error("Unable to connect to wallet. error:", error); } setLoading(false); }; useEffect(() => { if (!selectedWallet && widgetBridge) { widgetBridge.sendWalletDisconnectEvent(RomeEventType.WIDGET_WALLET_DISCONNECT_EVENT); } }, [selectedWallet]); useEffect(() => { if (connectToNetwork) { networkConnection.connector.activate(); } }, [connectToNetwork]); const key = useMemo2(() => connections.map(({ type }) => getConnectionName(type)).join("-"), [connections]); const connectors = connections.map(({ hooks, connector }) => [connector, hooks]); return /* @__PURE__ */ React.createElement(Web3ReactProvider, { connectors, key }, /* @__PURE__ */ React.createElement(WalletContext.Provider, { value: { selectedWallet, setSelectedWallet, handleConnect, loading } }, /* @__PURE__ */ React.createElement(OfacBan, null, children))); }; // src/hooks/useWallets.ts function useWallets() { const context = useContext(WalletContext); if (context === void 0) { throw new Error("useWallet must be used within a useWalletsProvider"); } return context; } export { CHAINS, ConnectionList, ConnectionType, NetworkName, URLS, WalletContext, WalletProvider, coinbaseWalletConnection, getAddChainParameters, getAddChainParametersfromNetworkName, getConnection, getConnectionName, injectedConnection, networkConnection, useWallets, walletConnectConnection }; //# sourceMappingURL=index.js.map