66using System ;
77using System . Collections . Generic ;
88using Immutable . Passport ;
9- using Cysharp . Threading . Tasks ;
10- using System . Numerics ;
11- using System . Net . Http ;
129
1310namespace HyperCasual . Runner
1411{
@@ -106,7 +103,7 @@ public int CoinCount
106103 }
107104 }
108105
109- public async void OnEnable ( )
106+ public void OnEnable ( )
110107 {
111108 // Set listener to 'Next' button
112109 m_NextButton . RemoveListener ( OnNextButtonClicked ) ;
@@ -120,79 +117,10 @@ public async void OnEnable()
120117 m_TryAgainButton . RemoveListener ( OnTryAgainButtonClicked ) ;
121118 m_TryAgainButton . AddListener ( OnTryAgainButtonClicked ) ;
122119
123- ShowError ( false ) ;
124- ShowLoading ( false ) ;
125-
126- // If player is logged into Passport mint coins to player
127- if ( SaveManager . Instance . IsLoggedIn )
128- {
129- // Mint collected coins to player
130- await MintCoins ( ) ;
131- }
132- else
133- {
134- // Show 'Next' button if player is already logged into Passport
135- ShowNextButton ( SaveManager . Instance . IsLoggedIn ) ;
136- // Show "Continue with Passport" button if the player is not logged into Passport
137- ShowContinueWithPassportButton ( ! SaveManager . Instance . IsLoggedIn ) ;
138- }
139- }
140-
141- /// <summary>
142- /// Mints collected coins (i.e. Immutable Runner Token) to the player's wallet
143- /// </summary>
144- private async UniTask MintCoins ( )
145- {
146- // This function is similar to MintCoins() in MintScreen.cs. Consider refactoring duplicate code in production.
147- Debug . Log ( "Minting coins..." ) ;
148- bool success = false ;
149-
150- // Show loading
151- ShowLoading ( true ) ;
152- ShowNextButton ( false ) ;
153- ShowError ( false ) ;
154-
155- try
156- {
157- // Don't mint any coins if player did not collect any
158- if ( m_CoinCount == 0 )
159- {
160- success = true ;
161- }
162- else
163- {
164- // Get the player's wallet address to mint the coins to
165- List < string > accounts = await Passport . Instance . ZkEvmRequestAccounts ( ) ;
166- string address = accounts [ 0 ] ;
167- if ( address != null )
168- {
169- // Calculate the quantity to mint
170- // Need to take into account Immutable Runner Token decimal value i.e. 18
171- BigInteger quantity = BigInteger . Multiply ( new BigInteger ( m_CoinCount ) , BigInteger . Pow ( 10 , 18 ) ) ;
172- Debug . Log ( $ "Quantity: { quantity } ") ;
173- var nvc = new List < KeyValuePair < string , string > >
174- {
175- // Set 'to' to the player's wallet address
176- new KeyValuePair < string , string > ( "to" , address ) ,
177- // Set 'quanity' to the number of coins collected
178- new KeyValuePair < string , string > ( "quantity" , quantity . ToString ( ) )
179- } ;
180- using var client = new HttpClient ( ) ;
181- string url = $ "http://localhost:3000/mint/token"; // Endpoint to mint token
182- using var req = new HttpRequestMessage ( HttpMethod . Post , url ) { Content = new FormUrlEncodedContent ( nvc ) } ;
183- using var res = await client . SendAsync ( req ) ;
184- success = res . IsSuccessStatusCode ;
185- }
186- }
187- }
188- catch ( Exception ex )
189- {
190- Debug . Log ( $ "Failed to mint coins: { ex . Message } ") ;
191- }
192-
193- ShowLoading ( false ) ;
194- ShowNextButton ( success ) ;
195- ShowError ( ! success ) ;
120+ // Show 'Next' button if player is already logged into Passport
121+ ShowNextButton ( SaveManager . Instance . IsLoggedIn ) ;
122+ // Show "Continue with Passport" button if the player is not logged into Passport
123+ ShowContinueWithPassportButton ( ! SaveManager . Instance . IsLoggedIn ) ;
196124 }
197125
198126 private async void OnContinueWithPassportButtonClicked ( )
@@ -204,11 +132,7 @@ private async void OnContinueWithPassportButtonClicked()
204132 ShowLoading ( true ) ;
205133
206134 // Log into Passport
207- #if ( UNITY_ANDROID && ! UNITY_EDITOR_WIN ) || ( UNITY_IPHONE && ! UNITY_EDITOR_WIN ) || UNITY_STANDALONE_OSX
208- await Passport . Instance . LoginPKCE ( ) ;
209- #else
210135 await Passport . Instance . Login ( ) ;
211- #endif
212136
213137 // Successfully logged in
214138 // Save a persistent flag in the game that the player is logged in
@@ -228,9 +152,8 @@ private async void OnContinueWithPassportButtonClicked()
228152 }
229153 }
230154
231- private async void OnTryAgainButtonClicked ( )
155+ private void OnTryAgainButtonClicked ( )
232156 {
233- await MintCoins ( ) ;
234157 }
235158
236159 private void OnNextButtonClicked ( )
@@ -287,4 +210,4 @@ void DisplayCoins(int count)
287210 }
288211 }
289212 }
290- }
213+ }
0 commit comments