55} from "@pythnetwork/pyth-evm-js" ;
66import { Contract , EventData } from "web3-eth-contract" ;
77import { PriceConfig } from "./price-config" ;
8- import { ChainPricePusher , PriceInfo , PriceListener } from "./interface" ;
8+ import { ChainPricePusher , PriceInfo , ChainPriceListener } from "./interface" ;
99import { TransactionReceipt } from "ethereum-protocol" ;
1010import { addLeading0x , DurationInSeconds , removeLeading0x } from "./utils" ;
1111import AbstractPythAbi from "@pythnetwork/pyth-sdk-solidity/abis/AbstractPyth.json" ;
@@ -14,30 +14,27 @@ import { Provider } from "web3/providers";
1414import Web3 from "web3" ;
1515import { isWsEndpoint } from "./utils" ;
1616
17- export class EvmPriceListener implements PriceListener {
17+ export class EvmPriceListener extends ChainPriceListener {
1818 private pythContractFactory : PythContractFactory ;
1919 private pythContract : Contract ;
20- private latestPriceInfo : Map < HexString , PriceInfo > ;
21- private priceIds : HexString [ ] ;
2220 private priceIdToAlias : Map < HexString , string > ;
2321
24- private pollingFrequency : DurationInSeconds ;
25-
2622 constructor (
2723 pythContractFactory : PythContractFactory ,
2824 priceConfigs : PriceConfig [ ] ,
2925 config : {
3026 pollingFrequency : DurationInSeconds ;
3127 }
3228 ) {
33- this . latestPriceInfo = new Map ( ) ;
34- this . priceIds = priceConfigs . map ( ( priceConfig ) => priceConfig . id ) ;
29+ super (
30+ "Evm" ,
31+ config . pollingFrequency ,
32+ priceConfigs . map ( ( priceConfig ) => priceConfig . id )
33+ ) ;
3534 this . priceIdToAlias = new Map (
3635 priceConfigs . map ( ( priceConfig ) => [ priceConfig . id , priceConfig . alias ] )
3736 ) ;
3837
39- this . pollingFrequency = config . pollingFrequency ;
40-
4138 this . pythContractFactory = pythContractFactory ;
4239 this . pythContract = this . pythContractFactory . createPythContract ( ) ;
4340 }
@@ -55,10 +52,8 @@ export class EvmPriceListener implements PriceListener {
5552 ) ;
5653 }
5754
58- console . log ( `Polling the prices every ${ this . pollingFrequency } seconds...` ) ;
59- setInterval ( this . pollPrices . bind ( this ) , this . pollingFrequency * 1000 ) ;
60-
61- await this . pollPrices ( ) ;
55+ // base class for polling
56+ await super . start ( ) ;
6257 }
6358
6459 private async startSubscription ( ) {
@@ -97,20 +92,6 @@ export class EvmPriceListener implements PriceListener {
9792 this . updateLatestPriceInfo ( priceId , priceInfo ) ;
9893 }
9994
100- private async pollPrices ( ) {
101- console . log ( "Polling evm prices..." ) ;
102- for ( const priceId of this . priceIds ) {
103- const currentPriceInfo = await this . getOnChainPriceInfo ( priceId ) ;
104- if ( currentPriceInfo !== undefined ) {
105- this . updateLatestPriceInfo ( priceId , currentPriceInfo ) ;
106- }
107- }
108- }
109-
110- getLatestPriceInfo ( priceId : string ) : PriceInfo | undefined {
111- return this . latestPriceInfo . get ( priceId ) ;
112- }
113-
11495 async getOnChainPriceInfo (
11596 priceId : HexString
11697 ) : Promise < PriceInfo | undefined > {
@@ -131,22 +112,6 @@ export class EvmPriceListener implements PriceListener {
131112 publishTime : Number ( priceRaw . publishTime ) ,
132113 } ;
133114 }
134-
135- private updateLatestPriceInfo ( priceId : HexString , observedPrice : PriceInfo ) {
136- const cachedLatestPriceInfo = this . getLatestPriceInfo ( priceId ) ;
137-
138- // Ignore the observed price if the cache already has newer
139- // price. This could happen because we are using polling and
140- // subscription at the same time.
141- if (
142- cachedLatestPriceInfo !== undefined &&
143- cachedLatestPriceInfo . publishTime > observedPrice . publishTime
144- ) {
145- return ;
146- }
147-
148- this . latestPriceInfo . set ( priceId , observedPrice ) ;
149- }
150115}
151116
152117export class EvmPricePusher implements ChainPricePusher {
0 commit comments