@@ -45,6 +45,17 @@ async function fetchApiBalance(baseUrl: string, address: string): Promise<bigint
4545 return BigInt ( bal ) ;
4646}
4747
48+ async function waitForBalance ( baseUrl : string , address : string , expected : bigint , timeoutMs = 60000 ) : Promise < bigint > {
49+ const start = Date . now ( ) ;
50+ let last : bigint = BigInt ( 0 ) ;
51+ while ( Date . now ( ) - start < timeoutMs ) {
52+ last = await fetchApiBalance ( baseUrl , address ) ;
53+ if ( last === expected ) return last ;
54+ await sleep ( 1000 ) ;
55+ }
56+ return last ;
57+ }
58+
4859async function fetchTxFeeFromSimulator ( simUrl : string , txHash : string ) : Promise < bigint > {
4960 // Prefer explicit fee, fallback to gasUsed * gasPrice if needed
5061 for ( let i = 0 ; i < 30 ; i ++ ) {
@@ -89,11 +100,13 @@ describe('State changes: native EGLD transfers reflect in balances', () => {
89100
90101 const fee = await fetchTxFeeFromSimulator ( sim , txHash ) ;
91102
92- const afterAlice = await fetchApiBalance ( api , alice ) ;
93- const afterBob = await fetchApiBalance ( api , bob ) ;
103+ const expectedAlice = beforeAlice - amount - fee ;
104+ const expectedBob = beforeBob + amount ;
105+ const afterAlice = await waitForBalance ( api , alice , expectedAlice ) ;
106+ const afterBob = await waitForBalance ( api , bob , expectedBob ) ;
94107
95- expect ( afterAlice ) . toBe ( beforeAlice - amount - fee ) ;
96- expect ( afterBob ) . toBe ( beforeBob + amount ) ;
108+ expect ( afterAlice ) . toBe ( expectedAlice ) ;
109+ expect ( afterBob ) . toBe ( expectedBob ) ;
97110 } ) ;
98111
99112 it ( 'Round-trip transfers: Alice->Bob then Bob->Alice yields expected finals' , async ( ) => {
@@ -123,13 +136,13 @@ describe('State changes: native EGLD transfers reflect in balances', () => {
123136 } ) ) ;
124137 const fee2 = await fetchTxFeeFromSimulator ( sim , hash2 ) ;
125138
126- const endAlice = await fetchApiBalance ( api , alice ) ;
127- const endBob = await fetchApiBalance ( api , bob ) ;
139+ const expectedAlice = startAlice - amount1 - fee1 + amount2 ;
140+ const expectedBob = startBob + amount1 - fee2 - amount2 ;
141+ const endAlice = await waitForBalance ( api , alice , expectedAlice ) ;
142+ const endBob = await waitForBalance ( api , bob , expectedBob ) ;
128143
129- // Alice: -amount1 - fee1 + amount2
130- expect ( endAlice ) . toBe ( startAlice - amount1 - fee1 + amount2 ) ;
131- // Bob: +amount1 - fee2 - amount2
132- expect ( endBob ) . toBe ( startBob + amount1 - fee2 - amount2 ) ;
144+ expect ( endAlice ) . toBe ( expectedAlice ) ;
145+ expect ( endBob ) . toBe ( expectedBob ) ;
133146 } ) ;
134147
135148 it ( 'Multiple sequential transfers accumulate correctly (Alice->Bob x3)' , async ( ) => {
@@ -160,11 +173,39 @@ describe('State changes: native EGLD transfers reflect in balances', () => {
160173 totalFees += fee ;
161174 }
162175
163- const endAlice = await fetchApiBalance ( api , alice ) ;
164- const endBob = await fetchApiBalance ( api , bob ) ;
176+ const expectedAlice = startAlice - totalSent - totalFees ;
177+ const expectedBob = startBob + totalSent ;
178+ const endAlice = await waitForBalance ( api , alice , expectedAlice ) ;
179+ const endBob = await waitForBalance ( api , bob , expectedBob ) ;
165180
166- expect ( endAlice ) . toBe ( startAlice - totalSent - totalFees ) ;
167- expect ( endBob ) . toBe ( startBob + totalSent ) ;
181+ expect ( endAlice ) . toBe ( expectedAlice ) ;
182+ expect ( endBob ) . toBe ( expectedBob ) ;
168183 } ) ;
169- } ) ;
170184
185+ it ( 'Sender nonce increases after successful transfers' , async ( ) => {
186+ await fundAddress ( sim , alice ) ;
187+ const nonceResp = await axios . get ( `${ api } /proxy/address/${ alice } /nonce` ) ;
188+ const startNonce : number = nonceResp ?. data ?. data ?. nonce ?? 0 ;
189+
190+ const amount = BigInt ( '1000000000000000' ) ; // 0.001 EGLD
191+ const hash = await sendTransaction ( new SendTransactionArgs ( {
192+ chainSimulatorUrl : sim ,
193+ sender : alice ,
194+ receiver : bob ,
195+ value : amount . toString ( ) ,
196+ dataField : '' ,
197+ } ) ) ;
198+ // Ensure simulator included the tx
199+ await fetchTxFeeFromSimulator ( sim , hash ) ;
200+
201+ // Nonce should increase by 1
202+ let newNonce = startNonce ;
203+ for ( let i = 0 ; i < 30 ; i ++ ) {
204+ const n = await axios . get ( `${ api } /proxy/address/${ alice } /nonce` ) . then ( r => r ?. data ?. data ?. nonce ?? 0 ) . catch ( ( ) => startNonce ) ;
205+ if ( typeof n === 'number' ) newNonce = n ;
206+ if ( newNonce >= startNonce + 1 ) break ;
207+ await sleep ( 1000 ) ;
208+ }
209+ expect ( newNonce ) . toBeGreaterThanOrEqual ( startNonce + 1 ) ;
210+ } ) ;
211+ } ) ;
0 commit comments