@@ -32,6 +32,7 @@ import {
3232
3333import { PythContract } from "./pyth-contract" ;
3434import { SuperWalletClient } from "./super-wallet" ;
35+ import { PricePusherMetrics } from "../metrics" ;
3536
3637export class EvmPriceListener extends ChainPriceListener {
3738 constructor (
@@ -135,9 +136,13 @@ export class EvmPricePusher implements IPricePusher {
135136 private overrideGasPriceMultiplier : number ,
136137 private overrideGasPriceMultiplierCap : number ,
137138 private updateFeeMultiplier : number ,
139+ private network : string ,
140+ private disablePush : boolean ,
141+ private useRecentGasPriceEstimate : boolean ,
138142 private gasLimit ?: number ,
139143 private customGasStation ?: CustomGasStation ,
140144 private gasPrice ?: number ,
145+ private metrics ?: PricePusherMetrics ,
141146 ) { }
142147
143148 // The pubTimes are passed here to use the values that triggered the push.
@@ -191,8 +196,11 @@ export class EvmPricePusher implements IPricePusher {
191196 this . gasPrice ??
192197 Number (
193198 await ( this . customGasStation ?. getCustomGasPrice ( ) ??
194- this . client . getGasPrice ( ) ) ,
199+ ( this . useRecentGasPriceEstimate
200+ ? this . getMedianRecentGasPrice ( )
201+ : this . client . getGasPrice ( ) ) ) ,
195202 ) ;
203+ this . metrics ?. updateGasPrice ( this . network , gasPrice ) ;
196204
197205 // Try to re-use the same nonce and increase the gas if the last tx is not landed yet.
198206 if ( this . pusherAddress === undefined ) {
@@ -258,11 +266,13 @@ export class EvmPricePusher implements IPricePusher {
258266
259267 this . logger . debug ( { request } , "Simulated request successfully" ) ;
260268
261- const hash = await this . client . writeContract ( request ) ;
262-
263- this . logger . info ( { hash } , "Price update sent" ) ;
264-
265- this . waitForTransactionReceipt ( hash ) ;
269+ if ( ! this . disablePush ) {
270+ const hash = await this . client . writeContract ( request ) ;
271+ this . logger . info ( { hash } , "Price update sent" ) ;
272+ this . waitForTransactionReceipt ( hash ) ;
273+ } else {
274+ this . logger . debug ( "Push disabled, not attempting" ) ;
275+ }
266276 } catch ( err : any ) {
267277 this . logger . debug ( { err } , "Simulating or sending transactions failed." ) ;
268278
@@ -423,4 +433,31 @@ export class EvmPricePusher implements IPricePusher {
423433 } ) ;
424434 return response . binary . data ;
425435 }
436+
437+ async getMedianRecentGasPrice ( blockCount = 5 ) : Promise < bigint > {
438+ this . logger . info ( { blockCount } ) ;
439+ const { baseFeePerGas, reward } = await this . client . getFeeHistory ( {
440+ blockCount,
441+ rewardPercentiles : [ 50 ] ,
442+ blockTag : "latest" ,
443+ } ) ;
444+ this . logger . info ( { baseFeePerGas, reward } , "feeHistory" ) ;
445+ // remove the next block base fee
446+ const trimmedBaseFees = baseFeePerGas . slice ( 0 , - 1 )
447+ const gasPrices = trimmedBaseFees . map ( ( base , i ) => {
448+ const medianTip = reward ?. [ i ] ?. [ 0 ] ?? 0n ;
449+ return base + medianTip ;
450+ } )
451+ this . logger . info ( { gasPrices} , "gasPrices:" ) ;
452+
453+ if ( gasPrices . length === 0 ) {
454+ return 0n ;
455+ } else {
456+ const sorted = [ ...gasPrices ] . sort ( ( a , b ) => ( a < b ? - 1 : a > b ? 1 : 0 ) )
457+ const medianIndex = Math . floor ( sorted . length / 2 )
458+ const medianPrice = sorted [ medianIndex ] ;
459+ this . logger . info ( { medianPrice } , "medianPrice:" ) ;
460+ return medianPrice ;
461+ }
462+ }
426463}
0 commit comments