@@ -56,23 +56,40 @@ async function waitForBalance(baseUrl: string, address: string, expected: bigint
5656 return last ;
5757}
5858
59- async function fetchTxFeeFromSimulator ( simUrl : string , txHash : string ) : Promise < bigint > {
60- // Prefer explicit fee, fallback to gasUsed * gasPrice if needed
61- for ( let i = 0 ; i < 30 ; i ++ ) {
62- const resp = await axios . get ( `${ simUrl } /transaction/${ txHash } ?withResults=true` ) . catch ( ( ) => undefined ) ;
63- const tx = resp ?. data ?. data ?. transaction ;
64- if ( tx ) {
65- if ( tx . fee ) return BigInt ( String ( tx . fee ) ) ;
66- if ( tx . gasUsed && ( tx . gasPrice || tx . initialPaidFee ) ) {
67- // gasPrice might be missing; initialPaidFee may be present. Use what we have.
68- const gasUsed = BigInt ( String ( tx . gasUsed ) ) ;
69- if ( tx . gasPrice ) return gasUsed * BigInt ( String ( tx . gasPrice ) ) ;
70- if ( tx . initialPaidFee ) return BigInt ( String ( tx . initialPaidFee ) ) ;
71- }
72- }
73- await sleep ( 1000 ) ;
74- }
75- throw new Error ( `Could not fetch fee for tx ${ txHash } ` ) ;
59+ // Observe fee via balance deltas (more robust than parsing simulator fields across versions)
60+ function computeFeeFromDeltas ( beforeSender : bigint , afterSender : bigint , amount : bigint ) : bigint {
61+ const debited = beforeSender - afterSender ;
62+ const fee = debited - amount ;
63+ return fee > 0n ? fee : 0n ;
64+ }
65+
66+ async function performTransferAndAssert ( simUrl : string , apiUrl : string , sender : string , receiver : string , amount : bigint ) {
67+ const beforeSender = await fetchApiBalance ( apiUrl , sender ) ;
68+ const beforeReceiver = await fetchApiBalance ( apiUrl , receiver ) ;
69+
70+ const hash = await sendTransaction ( new SendTransactionArgs ( {
71+ chainSimulatorUrl : simUrl ,
72+ sender,
73+ receiver,
74+ value : amount . toString ( ) ,
75+ dataField : '' ,
76+ } ) ) ;
77+
78+ // Wait for receiver to reflect amount increase
79+ const expectedReceiver = beforeReceiver + amount ;
80+ const afterReceiver = await waitForBalance ( apiUrl , receiver , expectedReceiver ) ;
81+ expect ( afterReceiver ) . toBe ( expectedReceiver ) ;
82+
83+ // Read sender post and derive fee
84+ const afterSender = await fetchApiBalance ( apiUrl , sender ) ;
85+ const fee = computeFeeFromDeltas ( beforeSender , afterSender , amount ) ;
86+ expect ( afterSender ) . toBe ( beforeSender - amount - fee ) ;
87+ // Sanity-check fee is > 0 and not absurdly large
88+ expect ( fee ) . toBeGreaterThan ( 0n ) ;
89+ // Fee should be < 0.1 EGLD in simulator settings
90+ expect ( fee ) . toBeLessThan ( 100000000000000000n ) ;
91+
92+ return { fee, afterSender, afterReceiver, hash } ;
7693}
7794
7895describe ( 'State changes: native EGLD transfers reflect in balances' , ( ) => {
@@ -86,27 +103,8 @@ describe('State changes: native EGLD transfers reflect in balances', () => {
86103 await fundAddress ( sim , alice ) ;
87104 await fundAddress ( sim , bob ) ;
88105
89- const beforeAlice = await fetchApiBalance ( api , alice ) ;
90- const beforeBob = await fetchApiBalance ( api , bob ) ;
91-
92106 const amount = BigInt ( '1000000000000000000' ) ; // 1 EGLD
93- const txHash = await sendTransaction ( new SendTransactionArgs ( {
94- chainSimulatorUrl : sim ,
95- sender : alice ,
96- receiver : bob ,
97- value : amount . toString ( ) ,
98- dataField : '' ,
99- } ) ) ;
100-
101- const fee = await fetchTxFeeFromSimulator ( sim , txHash ) ;
102-
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 ) ;
107-
108- expect ( afterAlice ) . toBe ( expectedAlice ) ;
109- expect ( afterBob ) . toBe ( expectedBob ) ;
107+ await performTransferAndAssert ( sim , api , alice , bob , amount ) ;
110108 } ) ;
111109
112110 it ( 'Round-trip transfers: Alice->Bob then Bob->Alice yields expected finals' , async ( ) => {
@@ -117,24 +115,10 @@ describe('State changes: native EGLD transfers reflect in balances', () => {
117115 const startBob = await fetchApiBalance ( api , bob ) ;
118116
119117 const amount1 = BigInt ( '2500000000000000000' ) ; // 2.5 EGLD
120- const hash1 = await sendTransaction ( new SendTransactionArgs ( {
121- chainSimulatorUrl : sim ,
122- sender : alice ,
123- receiver : bob ,
124- value : amount1 . toString ( ) ,
125- dataField : '' ,
126- } ) ) ;
127- const fee1 = await fetchTxFeeFromSimulator ( sim , hash1 ) ;
118+ const { fee : fee1 } = await performTransferAndAssert ( sim , api , alice , bob , amount1 ) ;
128119
129120 const amount2 = BigInt ( '1700000000000000000' ) ; // 1.7 EGLD
130- const hash2 = await sendTransaction ( new SendTransactionArgs ( {
131- chainSimulatorUrl : sim ,
132- sender : bob ,
133- receiver : alice ,
134- value : amount2 . toString ( ) ,
135- dataField : '' ,
136- } ) ) ;
137- const fee2 = await fetchTxFeeFromSimulator ( sim , hash2 ) ;
121+ const { fee : fee2 } = await performTransferAndAssert ( sim , api , bob , alice , amount2 ) ;
138122
139123 const expectedAlice = startAlice - amount1 - fee1 + amount2 ;
140124 const expectedBob = startBob + amount1 - fee2 - amount2 ;
@@ -158,17 +142,10 @@ describe('State changes: native EGLD transfers reflect in balances', () => {
158142 BigInt ( '300000000000000000' ) , // 0.3 EGLD
159143 ] ;
160144
161- let totalSent = BigInt ( 0 ) ;
162- let totalFees = BigInt ( 0 ) ;
145+ let totalSent = 0n ;
146+ let totalFees = 0n ;
163147 for ( const amt of amounts ) {
164- const hash = await sendTransaction ( new SendTransactionArgs ( {
165- chainSimulatorUrl : sim ,
166- sender : alice ,
167- receiver : bob ,
168- value : amt . toString ( ) ,
169- dataField : '' ,
170- } ) ) ;
171- const fee = await fetchTxFeeFromSimulator ( sim , hash ) ;
148+ const { fee } = await performTransferAndAssert ( sim , api , alice , bob , amt ) ;
172149 totalSent += amt ;
173150 totalFees += fee ;
174151 }
@@ -184,7 +161,7 @@ describe('State changes: native EGLD transfers reflect in balances', () => {
184161
185162 it ( 'Sender nonce increases after successful transfers' , async ( ) => {
186163 await fundAddress ( sim , alice ) ;
187- const nonceResp = await axios . get ( `${ api } /proxy/ address/${ alice } /nonce` ) ;
164+ const nonceResp = await axios . get ( `${ api } /address/${ alice } /nonce` ) ;
188165 const startNonce : number = nonceResp ?. data ?. data ?. nonce ?? 0 ;
189166
190167 const amount = BigInt ( '1000000000000000' ) ; // 0.001 EGLD
@@ -201,7 +178,7 @@ describe('State changes: native EGLD transfers reflect in balances', () => {
201178 // Nonce should increase by 1
202179 let newNonce = startNonce ;
203180 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 ) ;
181+ const n = await axios . get ( `${ api } /address/${ alice } /nonce` ) . then ( r => r ?. data ?. data ?. nonce ?? 0 ) . catch ( ( ) => startNonce ) ;
205182 if ( typeof n === 'number' ) newNonce = n ;
206183 if ( newNonce >= startNonce + 1 ) break ;
207184 await sleep ( 1000 ) ;
0 commit comments