@@ -2,38 +2,93 @@ mergeInto(LibraryManager.library, {
22 // Global variable to store the loaded modules and configuration
33 _web3ModalConfig : null ,
44
5- // Method to preload the scripts from CDN
6- PreloadWeb3Modal : function ( projectIdPtr , appNameStrPtr , appLogoUrlStrPtr ) {
7- const projectId = UTF8ToString ( projectIdPtr ) ;
8- const appName = UTF8ToString ( appNameStrPtr ) ;
9- const appLogoUrl = UTF8ToString ( appLogoUrlStrPtr ) ;
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+ console . log ( "Executing call" ) ;
26+ if ( ! _web3ModalConfig ) {
27+ console . error ( "Web3Modal is not initialized. Call Initialize first." ) ;
28+ return ;
29+ }
30+
31+ // Convert the method name and parameter to JS strings
32+ let methodName = UTF8ToString ( methodNameStrPtr ) ;
33+ let parameterStr = UTF8ToString ( parameterStrPtr ) ;
34+
35+ let parameterObj = parameterStr === "" ? undefined : JSON . parse ( parameterStr ) ;
36+
37+ try {
38+ console . log ( `Calling method` , methodName , parameterObj ) ;
39+
40+ // Call the method using the provided function
41+ let result = await callFn ( _web3ModalConfig , methodName , parameterObj ) ;
42+
43+ if ( ! result ) {
44+ { { { makeDynCall ( 'viii' , 'callbackPtr' ) } } } ( id , undefined , undefined ) ;
45+ return ;
46+ }
47+
48+ // Convert the result to JSON
49+ let cache = [ ] ;
50+ let resultJson = SerializeJson ( result ) ;
1051
11- console . log ( "Preloading Web3Modal with Project ID:" , projectId ) ;
52+ // Call the callback with the result
53+ let resultStrPtr = stringToNewUTF8 ( resultJson ) ;
54+ { { { makeDynCall ( 'viii' , 'callbackPtr' ) } } } ( id , resultStrPtr , undefined ) ;
55+ _free ( resultStrPtr ) ;
56+ } catch ( error ) {
57+ console . log ( "Error!" , error ) ;
58+ let errorJson = JSON . stringify ( error , [ 'name' , 'message' ] ) ;
59+ let errorStrPtr = stringToNewUTF8 ( errorJson ) ;
60+ { { { makeDynCall ( 'viii' , 'callbackPtr' ) } } } ( id , undefined , errorStrPtr ) ;
61+ _free ( errorStrPtr ) ;
62+ }
63+ } ,
1264
65+ // Preload the scripts from CDN, initialize the configuration and create the modal
66+ Initialize : function ( parametersJsonPtr , callbackPtr ) {
67+ const parametersJson = UTF8ToString ( parametersJsonPtr ) ;
68+ const parameters = JSON . parse ( parametersJson ) ;
69+
70+ const projectId = parameters . projectId ;
71+ const metadata = parameters . metadata ;
72+ const chains = parameters . chains ;
73+
74+ const enableOnramp = parameters . enableOnramp ;
75+
76+ console . log ( "Parameters" , parameters ) ;
77+
1378 // Load the scripts and initialize the configuration
1479 import ( "https://cdn.jsdelivr.net/npm/cdn-wagmi@3.0.0/dist/cdn-wagmi.js" ) . then ( CDNW3M => {
1580 const { WagmiCore, Chains, Web3modal, Connectors } = CDNW3M ;
16- const { createWeb3Modal, defaultWagmiConfig } = Web3modal ;
17- const { mainnet, polygon, sepolia } = Chains ;
81+ const { createWeb3Modal } = Web3modal ;
1882 const { coinbaseWallet, walletConnect, injected } = Connectors ;
1983 const { createConfig, http, reconnect } = WagmiCore ;
20-
21- console . log ( "Web3Modal loaded successfully" ) ;
2284
23- const metadata = {
24- name : appName ,
25- description : 'Web3Modal Example' ,
26- url : 'https://web3modal.com' , // url must match your domain & subdomain
27- icons : [ appLogoUrl ]
28- } ;
85+ console . log ( "Web3Modal loaded from CDN" ) ;
86+
87+ const chainsMap = chains . map ( chainName => Chains [ chainName ] ) ;
2988
3089 const config = createConfig ( {
31- chains : [ mainnet , polygon , sepolia ] ,
32- // transports: {
33- // [mainnet.id]: http(),
34- // [polygon.id]: http(),
35- // [sepolia.id]: http()
36- // },
90+ chains : chainsMap ,
91+ transport : http ,
3792 connectors : [
3893 walletConnect ( { projectId, metadata, showQrModal : false } ) ,
3994 injected ( { shimDisconnect : true } ) ,
@@ -44,83 +99,74 @@ mergeInto(LibraryManager.library, {
4499 ]
45100 } ) ;
46101
47- console . log ( config . connectors ) ;
48- console . log ( "Web3Modal configuration loaded successfully" ) ;
49-
50102 reconnect ( config ) ;
51103
52104 const modal = createWeb3Modal ( {
53105 wagmiConfig : config ,
54106 projectId,
55- enableAnalytics : true , // Optional - defaults to your Cloud configuration
56- enableOnramp : true // Optional - false as default
107+ enableAnalytics : false , // Optional - defaults to your Cloud configuration
108+ enableOnramp : enableOnramp // Optional - false as default
57109 } ) ;
58-
59- console . log ( "Web3Modal modal created successfully" , modal ) ;
60110
111+ modal . subscribeEvents ( event => console . log ( "New Event!" , event ) ) ;
112+
113+ console . log ( "Web3Modal modal created" , modal ) ;
114+
61115 // Store the configuration and modal globally
62116 _web3ModalConfig = {
63117 config : config ,
64118 modal : modal ,
65119 wagmiCore : WagmiCore
66120 } ;
121+
122+ { { { makeDynCall ( 'v' , 'callbackPtr' ) } } } ( ) ;
67123 } ) ;
68124 } ,
69- OpenWeb3Modal : function ( ) {
70- console . log ( "Opening Web3Modal" , _web3ModalConfig ) ;
71- if ( _web3ModalConfig ) {
72- _web3ModalConfig . modal . open ( ) ;
73- } else {
74- console . error ( "Web3Modal is not initialized. Call PreloadWeb3Modal first." ) ;
75- }
125+
126+ ModalCall__deps : [ '$ExecuteCall' ] ,
127+ ModalCall : async function ( id , methodNameStrPtr , parameterStrPtr , callbackPtr ) {
128+ const callFn = async ( web3modalConfig , methodName , parameterObj ) => {
129+ console . log ( "ModalCall" , methodName , parameterObj , web3modalConfig ) ;
130+ return await web3modalConfig . modal [ methodName ] ( parameterObj ) ;
131+ } ;
132+ await ExecuteCall ( callFn , id , methodNameStrPtr , parameterStrPtr , callbackPtr ) ;
76133 } ,
134+
135+ WagmiCall__deps : [ '$ExecuteCall' ] ,
77136 WagmiCall : async function ( id , methodNameStrPtr , parameterStrPtr , callbackPtr ) {
78- if ( ! _web3ModalConfig ) {
79- console . error ( "Web3Modal is not initialized. Call PreloadWeb3Modal first." ) ;
80- return ;
81- }
82-
83- // Convert the method name and parameter to JS strings
84- let methodName = UTF8ToString ( methodNameStrPtr ) ;
85- let parameterStr = UTF8ToString ( parameterStrPtr ) ;
86-
87- let parameterObj = parameterStr === "" ? undefined : JSON . parse ( parameterStr ) ;
88-
89- try {
90- if ( typeof _web3ModalConfig . wagmiCore [ methodName ] !== 'function' ) {
91- throw new Error ( `Method ${ methodName } does not exist on wagmiCore.` ) ;
137+ const callFn = async ( web3modalConfig , methodName , parameterObj ) => {
138+ return await web3modalConfig . wagmiCore [ methodName ] ( web3modalConfig . config , parameterObj ) ;
139+ } ;
140+ await ExecuteCall ( callFn , id , methodNameStrPtr , parameterStrPtr , callbackPtr ) ;
141+ } ,
142+
143+ WagmiWatchAccount__deps : [ '$SerializeJson' ] ,
144+ WagmiWatchAccount : function ( callbackPtr ) {
145+ _web3ModalConfig . wagmiCore . watchAccount ( _web3ModalConfig . config , {
146+ onChange ( data ) {
147+ const dataStr = stringToNewUTF8 ( SerializeJson ( data ) ) ;
148+ { { { makeDynCall ( 'vi' , 'callbackPtr' ) } } } ( dataStr ) ;
149+ _free ( dataStr ) ;
92150 }
93-
94- console . log ( "Calling WagmiCore method" , methodName , parameterObj ) ;
95-
96- // Call the method and get the result
97- let result = await _web3ModalConfig . wagmiCore [ methodName ] ( _web3ModalConfig . config , parameterObj ) ;
98-
99- // Convert the result to JSON
100- let cache = [ ] ;
101- let resultJson = JSON . stringify ( result , ( key , value ) => {
102- // Handle circular references
103- if ( typeof value === 'object' && value !== null ) {
104- if ( cache . includes ( value ) ) return ;
105- cache . push ( value ) ;
106- }
107- // Check if the value is a BigInt and convert it to a string
108- if ( typeof value === 'bigint' ) {
109- return value . toString ( ) ;
110- }
111- return value ;
112- } ) ;
113- cache = null ;
114-
115- // Call the callback with the result
116- let resultStrPtr = stringToNewUTF8 ( resultJson ) ;
117- { { { makeDynCall ( 'viii' , 'callbackPtr' ) } } } ( id , resultStrPtr , undefined ) ;
118- _free ( resultStrPtr ) ;
119- } catch ( error ) {
120- let errorJson = JSON . stringify ( error , [ 'name' , 'message' ] ) ;
121- let errorStrPtr = stringToNewUTF8 ( errorJson ) ;
122- { { { makeDynCall ( 'viii' , 'callbackPtr' ) } } } ( id , undefined , errorStrPtr ) ;
123- _free ( errorStrPtr ) ;
124- }
125- }
151+ } ) ;
152+ } ,
153+
154+ WagmiWatchChainId__deps : [ '$SerializeJson' ] ,
155+ WagmiWatchChainId : function ( callbackPtr ) {
156+ _web3ModalConfig . wagmiCore . watchChainId ( _web3ModalConfig . config , {
157+ onChange ( data ) {
158+ { { { makeDynCall ( 'vi' , 'callbackPtr' ) } } } ( data ) ;
159+ _free ( dataStr ) ;
160+ }
161+ } ) ;
162+ } ,
163+
164+ ModalSubscribeState__deps : [ '$SerializeJson' ] ,
165+ ModalSubscribeState : function ( callbackPtr ) {
166+ _web3ModalConfig . modal . subscribeState ( newState => {
167+ const newStateStr = stringToNewUTF8 ( SerializeJson ( newState ) ) ;
168+ { { { makeDynCall ( 'vi' , 'callbackPtr' ) } } } ( newStateStr ) ;
169+ _free ( newStateStr ) ;
170+ } ) ;
171+ } ,
126172} ) ;
0 commit comments