|
| 1 | +mergeInto(LibraryManager.library, { |
| 2 | + // Global variable to store the loaded modules and configuration |
| 3 | + _web3ModalConfig: null, |
| 4 | + |
| 5 | + $SerializeJson: function (obj){ |
| 6 | + let cache = []; |
| 7 | + let resultJson = JSON.stringify(obj, (key, value) => { |
| 8 | + // Handle circular references |
| 9 | + if (typeof value === 'object' && value !== null) { |
| 10 | + if (cache.includes(value)) return; |
| 11 | + cache.push(value); |
| 12 | + } |
| 13 | + // Check if the value is a BigInt and convert it to a string |
| 14 | + if (typeof value === 'bigint') { |
| 15 | + return value.toString(); |
| 16 | + } |
| 17 | + return value; |
| 18 | + }); |
| 19 | + cache = null; |
| 20 | + return resultJson; |
| 21 | + }, |
| 22 | + |
| 23 | + $ExecuteCall__deps: ['$SerializeJson'], |
| 24 | + $ExecuteCall: async function (callFn, id, methodNameStrPtr, parameterStrPtr, callbackPtr) { |
| 25 | + if (!_web3ModalConfig) { |
| 26 | + console.error("Web3Modal is not initialized. Call Initialize first."); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + // Convert the method name and parameter to JS strings |
| 31 | + let methodName = UTF8ToString(methodNameStrPtr); |
| 32 | + let parameterStr = UTF8ToString(parameterStrPtr); |
| 33 | + |
| 34 | + let parameterObj = parameterStr === "" ? undefined : JSON.parse(parameterStr); |
| 35 | + |
| 36 | + try { |
| 37 | + // Call the method using the provided function |
| 38 | + let result = await callFn(_web3ModalConfig, methodName, parameterObj); |
| 39 | + |
| 40 | + if (!result) { |
| 41 | + {{{makeDynCall('viii', 'callbackPtr')}}} (id, undefined, undefined); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + let resultJson = SerializeJson(result); |
| 46 | + |
| 47 | + // Call the callback with the result |
| 48 | + let resultStrPtr = stringToNewUTF8(resultJson); |
| 49 | + {{{makeDynCall('viii', 'callbackPtr')}}} (id, resultStrPtr, undefined); |
| 50 | + _free(resultStrPtr); |
| 51 | + } catch (error) { |
| 52 | + console.error("[Web3Modal] Error executing call", error); |
| 53 | + let errorJson = JSON.stringify(error, ['name', 'message']); |
| 54 | + let errorStrPtr = stringToNewUTF8(errorJson); |
| 55 | + {{{makeDynCall('viii', 'callbackPtr')}}} (id, undefined, errorStrPtr); |
| 56 | + _free(errorStrPtr); |
| 57 | + } |
| 58 | + }, |
| 59 | + |
| 60 | + // Preload the scripts from CDN, initialize the configuration and create the modal |
| 61 | + Initialize: function (parametersJsonPtr, callbackPtr) { |
| 62 | + const parametersJson = UTF8ToString(parametersJsonPtr); |
| 63 | + const parameters = JSON.parse(parametersJson); |
| 64 | + |
| 65 | + const projectId = parameters.projectId; |
| 66 | + const metadata = parameters.metadata; |
| 67 | + const chains = parameters.chains; |
| 68 | + |
| 69 | + const enableOnramp = parameters.enableOnramp; |
| 70 | + |
| 71 | + // Load the scripts and initialize the configuration |
| 72 | + import("https://cdn.jsdelivr.net/npm/@web3modal/cdn@5.0.1/dist/wagmi.js").then(CDNW3M => { |
| 73 | + const { WagmiCore, Chains, Web3modal, Connectors } = CDNW3M; |
| 74 | + const { createWeb3Modal } = Web3modal; |
| 75 | + const { coinbaseWallet, walletConnect, injected } = Connectors; |
| 76 | + const { createConfig, http, reconnect } = WagmiCore; |
| 77 | + |
| 78 | + const chainsMap = chains.map(chainName => Chains[chainName]); |
| 79 | + |
| 80 | + const config = createConfig({ |
| 81 | + chains: chainsMap, |
| 82 | + transports: chains.reduce((acc, chainName) => { |
| 83 | + acc[Chains[chainName].id] = http(); |
| 84 | + return acc; |
| 85 | + }, {}), |
| 86 | + connectors: [ |
| 87 | + walletConnect({ projectId, metadata, showQrModal: false }), |
| 88 | + injected({ shimDisconnect: true }), |
| 89 | + coinbaseWallet({ |
| 90 | + appName: metadata.name, |
| 91 | + appLogoUrl: metadata.icons[0] |
| 92 | + }) |
| 93 | + ] |
| 94 | + }); |
| 95 | + |
| 96 | + reconnect(config); |
| 97 | + |
| 98 | + const modal = createWeb3Modal({ |
| 99 | + wagmiConfig: config, |
| 100 | + projectId, |
| 101 | + enableOnramp: enableOnramp, |
| 102 | + disableAppend: true, |
| 103 | + }); |
| 104 | + |
| 105 | + // Store the configuration and modal globally |
| 106 | + _web3ModalConfig = { |
| 107 | + config: config, |
| 108 | + modal: modal, |
| 109 | + wagmiCore: WagmiCore |
| 110 | + }; |
| 111 | + |
| 112 | + |
| 113 | + // Insert the container into the DOM at the canvas's original position |
| 114 | + const canvas = document.getElementsByTagName('canvas')[0]; |
| 115 | + const container = document.createElement('div'); |
| 116 | + container.id = 'canvas-container'; |
| 117 | + canvas.parentNode.insertBefore(container, canvas); |
| 118 | + container.appendChild(canvas); |
| 119 | + |
| 120 | + const web3modal = document.createElement('w3m-modal') |
| 121 | + container.appendChild(web3modal) |
| 122 | + |
| 123 | + |
| 124 | + // Add styles to enable fullscreen compatibility |
| 125 | + const addCanvasActiveStyles = () => { |
| 126 | + const styleElement = document.createElement('style'); |
| 127 | + styleElement.id = 'canvas-active-styles'; |
| 128 | + styleElement.innerHTML = ` |
| 129 | + .canvas-active { |
| 130 | + position: fixed !important; |
| 131 | + top: 0 !important; |
| 132 | + right: 0 !important; |
| 133 | + bottom: 0 !important; |
| 134 | + left: 0 !important; |
| 135 | + width: 100% !important; |
| 136 | + height: 100% !important; |
| 137 | + } |
| 138 | + `; |
| 139 | + document.head.appendChild(styleElement); |
| 140 | + }; |
| 141 | + |
| 142 | + const removeCanvasActiveStyles = () => { |
| 143 | + const styleElement = document.getElementById('canvas-active-styles'); |
| 144 | + if (styleElement) { |
| 145 | + document.head.removeChild(styleElement); |
| 146 | + } |
| 147 | + }; |
| 148 | + |
| 149 | + // Handle fullscreen changes |
| 150 | + container?.addEventListener('fullscreenchange', () => { |
| 151 | + const canvas = document.querySelector('canvas'); |
| 152 | + if (document.fullscreenElement) { |
| 153 | + if (!canvas?.classList.contains('canvas-active')) { |
| 154 | + addCanvasActiveStyles(); |
| 155 | + canvas?.classList.add('canvas-active'); |
| 156 | + } |
| 157 | + } else { |
| 158 | + if (canvas?.classList.contains('canvas-active')) { |
| 159 | + canvas?.classList.remove('canvas-active'); |
| 160 | + removeCanvasActiveStyles(); |
| 161 | + } |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + {{{makeDynCall('v', 'callbackPtr')}}}(); |
| 166 | + }); |
| 167 | + }, |
| 168 | + |
| 169 | + ModalCall__deps: ['$ExecuteCall'], |
| 170 | + ModalCall: async function (id, methodNameStrPtr, parameterStrPtr, callbackPtr) { |
| 171 | + const callFn = async (web3modalConfig, methodName, parameterObj) => { |
| 172 | + return await web3modalConfig.modal[methodName](parameterObj); |
| 173 | + }; |
| 174 | + await ExecuteCall(callFn, id, methodNameStrPtr, parameterStrPtr, callbackPtr); |
| 175 | + }, |
| 176 | + |
| 177 | + WagmiCall__deps: ['$ExecuteCall'], |
| 178 | + WagmiCall: async function(id, methodNameStrPtr, parameterStrPtr, callbackPtr) { |
| 179 | + const callFn = async (web3modalConfig, methodName, parameterObj) => { |
| 180 | + return await web3modalConfig.wagmiCore[methodName](web3modalConfig.config, parameterObj); |
| 181 | + }; |
| 182 | + await ExecuteCall(callFn, id, methodNameStrPtr, parameterStrPtr, callbackPtr); |
| 183 | + }, |
| 184 | + |
| 185 | + WagmiWatchAccount__deps: ['$SerializeJson'], |
| 186 | + WagmiWatchAccount: function(callbackPtr) { |
| 187 | + _web3ModalConfig.wagmiCore.watchAccount(_web3ModalConfig.config, { |
| 188 | + onChange(data) { |
| 189 | + const dataStr = stringToNewUTF8(SerializeJson(data)); |
| 190 | + {{{makeDynCall('vi', 'callbackPtr')}}}(dataStr); |
| 191 | + _free(dataStr); |
| 192 | + } |
| 193 | + }); |
| 194 | + }, |
| 195 | + |
| 196 | + WagmiWatchChainId__deps: ['$SerializeJson'], |
| 197 | + WagmiWatchChainId: function(callbackPtr) { |
| 198 | + _web3ModalConfig.wagmiCore.watchChainId(_web3ModalConfig.config, { |
| 199 | + onChange(data) { |
| 200 | + {{{makeDynCall('vi', 'callbackPtr')}}}(data); |
| 201 | + _free(dataStr); |
| 202 | + } |
| 203 | + }); |
| 204 | + }, |
| 205 | + |
| 206 | + ModalSubscribeState__deps: ['$SerializeJson'], |
| 207 | + ModalSubscribeState: function(callbackPtr) { |
| 208 | + _web3ModalConfig.modal.subscribeState(newState => { |
| 209 | + const newStateStr = stringToNewUTF8(SerializeJson(newState)); |
| 210 | + {{{makeDynCall('vi', 'callbackPtr')}}}(newStateStr); |
| 211 | + _free(newStateStr); |
| 212 | + }); |
| 213 | + }, |
| 214 | +}); |
0 commit comments