11import { MidenWalletAdapter } from "@demox-labs/miden-wallet-adapter-miden" ;
22import { PrivateDataPermission , WalletAdapterNetwork } from "@demox-labs/miden-wallet-adapter-base" ;
3- import { Endpoint , NoteId , RpcClient } from "@demox-labs/miden-sdk" ;
43import { Utils } from './utils.js' ;
54import { UIController } from './ui.js' ;
65import { getConfig , getMetadata , getPowChallenge , getTokens , get_note } from "./api.js" ;
@@ -11,7 +10,6 @@ export class MidenFaucetApp {
1110 this . tokenAmountOptions = [ 100 , 500 , 1000 ] ;
1211 this . metadataInitialized = false ;
1312 this . apiUrl = null ;
14- this . rpcClient = null ;
1513 this . baseAmount = null ;
1614 this . powLoadDifficulty = null ;
1715
@@ -30,7 +28,6 @@ export class MidenFaucetApp {
3028 try {
3129 let config = await getConfig ( ) ;
3230 this . apiUrl = config . api_url ;
33- this . rpcClient = new RpcClient ( new Endpoint ( config . node_url ) ) ;
3431 this . setupEventListeners ( ) ;
3532 this . startMetadataPolling ( ) ;
3633 } catch ( error ) {
@@ -86,15 +83,10 @@ export class MidenFaucetApp {
8683 const nonce = await this . findValidNonce ( powData . challenge , powData . target ) ;
8784
8885 this . ui . updateMintingTitle ( 'MINTING TOKENS' ) ;
89- this . ui . updateProgressBar ( 33 ) ;
86+ this . ui . updateProgressBar ( 50 ) ;
9087
9188 const getTokensResponse = await getTokens ( this . apiUrl , powData . challenge , nonce , recipient , amount , isPrivateNote ) ;
9289
93- this . ui . updateMintingTitle ( 'CONFIRMING TRANSACTION' ) ;
94- this . ui . updateProgressBar ( 66 ) ;
95-
96- await this . pollNote ( getTokensResponse . note_id ) ;
97-
9890 this . ui . showCompletedModal (
9991 recipient ,
10092 amountAsTokens ,
@@ -193,54 +185,6 @@ export class MidenFaucetApp {
193185 }
194186 }
195187
196- pollNote ( noteId ) {
197- return new Promise ( ( resolve , reject ) => {
198- // Poll every 500ms for the first 10 seconds, then every 1s for the next 30 seconds, then every 5s.
199- let currentInterval = 500 ;
200- let pollInterval ;
201- let elapsedTime = 0 ;
202- // Timeout after 5 minutes
203- const timeout = 300000 ;
204- let timeoutId ;
205-
206- const poll = async ( ) => {
207- try {
208- const note = await this . rpcClient . getNotesById ( [ NoteId . fromHex ( noteId ) ] ) ;
209- if ( note && note . length > 0 ) {
210- clearInterval ( pollInterval ) ;
211- clearTimeout ( timeoutId ) ;
212- resolve ( ) ;
213- return ;
214- }
215-
216- elapsedTime += currentInterval ;
217-
218- if ( elapsedTime <= 10000 ) {
219- currentInterval = 500 ;
220- } else if ( elapsedTime <= 40000 ) {
221- currentInterval = 1000 ;
222- } else {
223- currentInterval = 5000 ;
224- }
225-
226- // Update the interval
227- clearInterval ( pollInterval ) ;
228- pollInterval = setInterval ( poll , currentInterval ) ;
229- } catch ( error ) {
230- console . error ( 'Error polling for note:' , error ) ;
231- clearInterval ( pollInterval ) ;
232- clearTimeout ( timeoutId ) ;
233- reject ( 'Error fetching note confirmation.' ) ;
234- }
235- } ;
236- pollInterval = setInterval ( poll , currentInterval ) ;
237- timeoutId = setTimeout ( ( ) => {
238- clearInterval ( pollInterval ) ;
239- reject ( new Error ( 'Timeout while waiting for tx to be committed. Please try again later.' ) ) ;
240- } , timeout ) ;
241- } ) ;
242- }
243-
244188 async findValidNonce ( challenge , target ) {
245189 let nonce = 0 ;
246190 let targetNum = BigInt ( target ) ;
0 commit comments