@@ -26,7 +26,9 @@ import { Block, CachedBlock, Transaction, Log } from './model';
2626import { MirrorNode } from './mirrorNode' ;
2727import { MirrorNodeClient , SDKClient } from './clients' ;
2828import { JsonRpcError , predefined } from './errors' ;
29+ import constants from './constants' ;
2930
31+ const cache = require ( 'js-cache' ) ;
3032const createHash = require ( 'keccak' ) ;
3133
3234/**
@@ -114,13 +116,21 @@ export class EthImpl implements Eth {
114116 * Gets the fee history.
115117 */
116118 async feeHistory ( blockCount : number , newestBlock : string , rewardPercentiles : Array < number > | null ) {
117- this . logger . trace ( ' feeHistory()' ) ;
119+ this . logger . trace ( ` feeHistory(blockCount= ${ blockCount } , newestBlock= ${ newestBlock } , rewardPercentiles= ${ rewardPercentiles } )` ) ;
118120 try {
119- const feeWeibars = await this . getFeeWeibars ( ) ;
121+ let feeHistory : object | undefined = cache . get ( constants . CACHE_KEY . FEE_HISTORY ) ;
122+ if ( ! feeHistory ) {
123+ const feeWeibars = await this . getFeeWeibars ( ) ;
120124
121- return await this . mirrorNode . getFeeHistory ( feeWeibars , blockCount , newestBlock , rewardPercentiles ) ;
125+ feeHistory = await this . mirrorNode . getFeeHistory ( feeWeibars , blockCount , newestBlock , rewardPercentiles ) ;
126+
127+ this . logger . trace ( `caching ${ constants . CACHE_KEY . FEE_HISTORY } for ${ constants . CACHE_TTL . ONE_HOUR } ms` ) ;
128+ cache . set ( constants . CACHE_KEY . FEE_HISTORY , feeHistory , constants . CACHE_TTL . ONE_HOUR ) ;
129+ }
130+
131+ return feeHistory ;
122132 } catch ( e ) {
123- this . logger . trace ( e ) ;
133+ this . logger . error ( e , 'Error constructing default feeHistory' ) ;
124134 }
125135 }
126136
@@ -187,12 +197,21 @@ export class EthImpl implements Eth {
187197 // FIXME: This should come from the mainnet and get cached. The gas price does change dynamically based on
188198 // the price of the HBAR relative to the USD. It only needs to be updated hourly.
189199 this . logger . trace ( 'gasPrice()' ) ;
190- return this . getFeeWeibars ( )
191- . then ( ( weiBars ) => EthImpl . numberTo0x ( ( weiBars ) ) )
192- . catch ( ( e : any ) => {
193- this . logger . trace ( e ) ;
194- throw e ;
195- } ) ;
200+ try {
201+ let gasPrice : number | undefined = cache . get ( constants . CACHE_KEY . GAS_PRICE ) ;
202+
203+ if ( ! gasPrice ) {
204+ gasPrice = await this . getFeeWeibars ( ) ;
205+
206+ this . logger . trace ( `caching ${ constants . CACHE_KEY . GAS_PRICE } for ${ constants . CACHE_TTL . ONE_HOUR } ms` ) ;
207+ cache . set ( constants . CACHE_KEY . GAS_PRICE , gasPrice , constants . CACHE_TTL . ONE_HOUR ) ;
208+ }
209+
210+ return EthImpl . numberTo0x ( gasPrice ) ;
211+ } catch ( error ) {
212+ this . logger . trace ( error ) ;
213+ throw error ;
214+ }
196215 }
197216
198217 /**
0 commit comments