@@ -1318,6 +1318,44 @@ describe('Eth calls using MirrorNode', async function () {
13181318 expect ( resBalance ) . to . equal ( defHexBalance ) ;
13191319 } ) ;
13201320
1321+ it ( 'should return balance for latest block from cache' , async ( ) => {
1322+ restMock . onGet ( `blocks?limit=1&order=desc` ) . reply ( 200 , {
1323+ blocks : [ {
1324+ number : 10000
1325+ } ]
1326+ } ) ;
1327+ restMock . onGet ( `accounts/${ contractAddress1 } ` ) . reply ( 200 , {
1328+ account : contractAddress1 ,
1329+ balance : {
1330+ balance : defBalance
1331+ }
1332+ } ) ;
1333+
1334+ const resBalance = await ethImpl . getBalance ( contractAddress1 , null ) ;
1335+ expect ( resBalance ) . to . equal ( defHexBalance ) ;
1336+
1337+ // next call should use cache
1338+ restMock . onGet ( `accounts/${ contractAddress1 } ` ) . reply ( 404 , { } ) ;
1339+
1340+ const resBalanceCached = await ethImpl . getBalance ( contractAddress1 , null ) ;
1341+ expect ( resBalanceCached ) . to . equal ( resBalance ) ;
1342+
1343+ // Third call should return new number using mirror node
1344+ const newBalance = 55555 ;
1345+ const newBalanceHex = EthImpl . numberTo0x ( BigInt ( newBalance ) * TINYBAR_TO_WEIBAR_COEF_BIGINT ) ;
1346+ restMock . onGet ( `accounts/${ contractAddress1 } ` ) . reply ( 200 , {
1347+ account : contractAddress1 ,
1348+ balance : {
1349+ balance : newBalance
1350+ }
1351+ } ) ;
1352+ // expire cache, instead of waiting for ttl we clear it to simulate expiry faster.
1353+ cache . clear ( ) ;
1354+
1355+ const resBalanceNew = await ethImpl . getBalance ( contractAddress1 , null ) ;
1356+ expect ( newBalanceHex ) . to . equal ( resBalanceNew ) ;
1357+ } ) ;
1358+
13211359 it ( 'should return balance from mirror node with block number passed as param the same as latest' , async ( ) => {
13221360 const blockNumber = "0x2710" ;
13231361 restMock . onGet ( `blocks?limit=1&order=desc` ) . reply ( 200 , {
@@ -1394,7 +1432,7 @@ describe('Eth calls using MirrorNode', async function () {
13941432
13951433 const resCached = await ethImpl . getBalance ( contractAddress1 , null ) ;
13961434 expect ( resNoCache ) . to . equal ( defHexBalance ) ;
1397- expect ( resCached ) . to . equal ( EthImpl . zeroHex ) ;
1435+ expect ( resCached ) . to . equal ( defHexBalance ) ;
13981436 } ) ;
13991437
14001438 describe ( 'with blockNumberOrTag filter' , async function ( ) {
0 commit comments