@@ -16,7 +16,7 @@ import {
1616} from "@solana/spl-token" ;
1717import { PublicKey , Keypair , Transaction , sendAndConfirmTransaction } from "@solana/web3.js" ;
1818import { common , DepositDataValues } from "./SvmSpoke.common" ;
19- import { readProgramEvents , intToU8Array32 , u8Array32ToInt } from "./utils" ;
19+ import { readEventsUntilFound , intToU8Array32 } from "./utils" ;
2020const { provider, connection, program, owner, seedBalance, initializeState, depositData } = common ;
2121const { createRoutePda, getVaultAta, assertSE, assert, getCurrentTime, depositQuoteTimeBuffer, fillDeadlineBuffer } =
2222 common ;
@@ -121,7 +121,8 @@ describe("svm_spoke.deposit", () => {
121121 . accounts ( calledDepositAccounts )
122122 . instruction ( ) ;
123123 const depositTx = new Transaction ( ) . add ( approveIx , depositIx ) ;
124- await sendAndConfirmTransaction ( connection , depositTx , [ payer , depositor ] ) ;
124+ const tx = await sendAndConfirmTransaction ( connection , depositTx , [ payer , depositor ] ) ;
125+ return tx ;
125126 } ;
126127
127128 beforeEach ( async ( ) => {
@@ -183,10 +184,9 @@ describe("svm_spoke.deposit", () => {
183184
184185 // Execute the first deposit_v3 call
185186 let depositDataValues = Object . values ( depositData ) as DepositDataValues ;
186- await approvedDepositV3 ( depositDataValues ) ;
187- await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
187+ const tx = await approvedDepositV3 ( depositDataValues ) ;
188188
189- let events = await readProgramEvents ( connection , program ) ;
189+ let events = await readEventsUntilFound ( connection , tx , [ program ] ) ;
190190 let event = events [ 0 ] . data ; // 0th event is the latest event
191191 const expectedValues1 = { ...depositData , depositId : intToU8Array32 ( 1 ) } ; // Verify the event props emitted match the depositData.
192192 for ( let [ key , value ] of Object . entries ( expectedValues1 ) ) {
@@ -195,9 +195,8 @@ describe("svm_spoke.deposit", () => {
195195 }
196196
197197 // Execute the second deposit_v3 call
198- await approvedDepositV3 ( depositDataValues ) ;
199- await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
200- events = await readProgramEvents ( connection , program ) ;
198+ const tx2 = await approvedDepositV3 ( depositDataValues ) ;
199+ events = await readEventsUntilFound ( connection , tx2 , [ program ] ) ;
201200 event = events [ 0 ] . data ; // 0th event is the latest event.
202201
203202 const expectedValues2 = { ...expectedValues1 , depositId : intToU8Array32 ( 2 ) } ; // Verify the event props emitted match the depositData.
@@ -364,8 +363,6 @@ describe("svm_spoke.deposit", () => {
364363
365364 await program . methods . setEnableRoute ( inputToken , fakeRouteChainId , true ) . accounts ( fakeSetEnableRouteAccounts ) . rpc ( ) ;
366365
367- await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
368-
369366 const fakeDepositAccounts = {
370367 state : fakeState . state ,
371368 route : fakeRoutePda ,
@@ -382,11 +379,9 @@ describe("svm_spoke.deposit", () => {
382379 ...depositData ,
383380 destinationChainId : fakeRouteChainId ,
384381 } ) as DepositDataValues ;
385- await approvedDepositV3 ( depositDataValues , fakeDepositAccounts ) ;
386-
387- await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
382+ const tx = await approvedDepositV3 ( depositDataValues , fakeDepositAccounts ) ;
388383
389- let events = await readProgramEvents ( connection , program ) ;
384+ let events = await readEventsUntilFound ( connection , tx , [ program ] ) ;
390385 let event = events [ 0 ] . data ; // 0th event is the latest event.
391386 const expectedValues = {
392387 ...{ ...depositData , destinationChainId : fakeRouteChainId } ,
@@ -457,11 +452,9 @@ describe("svm_spoke.deposit", () => {
457452 . accounts ( depositAccounts )
458453 . instruction ( ) ;
459454 const depositTx = new Transaction ( ) . add ( approveIx , depositIx ) ;
460- await sendAndConfirmTransaction ( connection , depositTx , [ payer , depositor ] ) ;
455+ const tx = await sendAndConfirmTransaction ( connection , depositTx , [ payer , depositor ] ) ;
461456
462- await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
463-
464- const events = await readProgramEvents ( connection , program ) ;
457+ const events = await readEventsUntilFound ( connection , tx , [ program ] ) ;
465458 const event = events [ 0 ] . data ; // 0th event is the latest event.
466459
467460 // Verify the event props emitted match the expected values
@@ -526,10 +519,9 @@ describe("svm_spoke.deposit", () => {
526519 depositData . exclusivityParameter = maxExclusivityOffsetSeconds ;
527520
528521 const depositDataValues = Object . values ( depositData ) as DepositDataValues ;
529- await approvedDepositV3 ( depositDataValues ) ;
522+ const tx = await approvedDepositV3 ( depositDataValues ) ;
530523
531- await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
532- const events = await readProgramEvents ( connection , program ) ;
524+ const events = await readEventsUntilFound ( connection , tx , [ program ] ) ;
533525 const event = events [ 0 ] . data ; // 0th event is the latest event
534526 assertSE (
535527 event . exclusivityDeadline ,
@@ -547,10 +539,9 @@ describe("svm_spoke.deposit", () => {
547539 depositData . exclusivityParameter = exclusivityDeadlineTimestamp ;
548540
549541 const depositDataValues = Object . values ( depositData ) as DepositDataValues ;
550- await approvedDepositV3 ( depositDataValues ) ;
542+ const tx = await approvedDepositV3 ( depositDataValues ) ;
551543
552- await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
553- const events = await readProgramEvents ( connection , program ) ;
544+ const events = await readEventsUntilFound ( connection , tx , [ program ] ) ;
554545 const event = events [ 0 ] . data ; // 0th event is the latest event;
555546
556547 assertSE ( event . exclusivityDeadline , exclusivityDeadlineTimestamp , "exclusivityDeadline should be passed in time" ) ;
@@ -565,10 +556,9 @@ describe("svm_spoke.deposit", () => {
565556 depositData . exclusivityParameter = zeroExclusivity ;
566557
567558 const depositDataValues = Object . values ( depositData ) as DepositDataValues ;
568- await approvedDepositV3 ( depositDataValues ) ;
559+ const tx = await approvedDepositV3 ( depositDataValues ) ;
569560
570- await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
571- const events = await readProgramEvents ( connection , program ) ;
561+ const events = await readEventsUntilFound ( connection , tx , [ program ] ) ;
572562 const event = events [ 0 ] . data ; // 0th event is the latest event;
573563
574564 assertSE ( event . exclusivityDeadline , zeroExclusivity , "Exclusivity deadline should always be 0" ) ;
@@ -629,13 +619,12 @@ describe("svm_spoke.deposit", () => {
629619 . instruction ( ) ;
630620
631621 const unsafeDepositTx = new Transaction ( ) . add ( approveIx , unsafeDepositIx ) ;
632- await sendAndConfirmTransaction ( connection , unsafeDepositTx , [ payer , depositor ] ) ;
622+ const tx = await sendAndConfirmTransaction ( connection , unsafeDepositTx , [ payer , depositor ] ) ;
633623
634624 // Wait for a short period to ensure the event is emitted
635- await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
636625
637626 // Read and verify the event
638- const events = await readProgramEvents ( connection , program ) ;
627+ const events = await readEventsUntilFound ( connection , tx , [ program ] ) ;
639628 const event = events [ 0 ] . data ; // Assuming the latest event is the one we want
640629
641630 const expectedValues = { ...depositData , depositId : expectedDepositIdArray } ;
0 commit comments