@@ -8,8 +8,10 @@ import {
8
8
} from '../oracles/types' ;
9
9
10
10
import { getOracleId } from '../oracles/oracleId' ;
11
+ import { DevnetPerpMarkets , MainnetPerpMarkets , PublicKey } from '@drift-labs/sdk' ;
11
12
12
- export class ExternalOracleDataDriftClientSubscriber extends PollingDriftClientAccountSubscriber {
13
+ // allowing app UI state to mixin and replace with RPC polled data
14
+ export class ExternalDataDriftClientSubscriber extends PollingDriftClientAccountSubscriber {
13
15
private oracleLastUpdate = new Map < string , number > ( ) ;
14
16
private pollingOracles = new Map < string , boolean > ( ) ;
15
17
private oraclePollIntervalId : NodeJS . Timeout ;
@@ -26,6 +28,9 @@ export class ExternalOracleDataDriftClientSubscriber extends PollingDriftClientA
26
28
27
29
/** Public method to be called externally with fresh oracle data */
28
30
public feedOracle ( oracleInfo : OracleInfo , priceData : OraclePriceData , slot : number ) {
31
+ if ( oracleInfo . publicKey . equals ( new PublicKey ( "BtUUSUc9rZSzBmmKhQq4no65zHQTzMFeVYss7xcMRD53" ) ) ) {
32
+ console . log ( "feedOracle Republican popular vote" , oracleInfo . publicKey . toBase58 ( ) , oracleInfo . source ) ;
33
+ }
29
34
const oracleId = getOracleId ( oracleInfo . publicKey , oracleInfo . source ) ;
30
35
this . oracles . set ( oracleId , { data : priceData , slot } ) ;
31
36
this . oracleLastUpdate . set ( oracleId , Date . now ( ) ) ;
@@ -43,37 +48,32 @@ export class ExternalOracleDataDriftClientSubscriber extends PollingDriftClientA
43
48
44
49
public override async subscribe ( ) : Promise < boolean > {
45
50
await super . subscribe ( ) ;
46
- this . removeAllOraclesFromAccountLoader ( ) ;
47
51
this . startOraclePollingWatchdog ( ) ;
48
52
return true ;
49
53
}
50
54
51
55
private startOraclePollingWatchdog ( ) {
56
+ if ( this . oraclePollIntervalId ) {
57
+ clearInterval ( this . oraclePollIntervalId ) ;
58
+ }
52
59
// how do we handle not polling bet markets every 1s from this change?
53
60
this . oraclePollIntervalId = setInterval ( async ( ) => {
54
- for ( const [ oracleId , oracleToPoll ] of this . oraclesToPoll . entries ( ) ) {
55
- const lastUpdate = this . oracleLastUpdate . get ( oracleId ) || 0 ;
61
+ for ( const [ oracleId , lastUpdate ] of this . oracleLastUpdate . entries ( ) ) {
62
+ const oracleToPoll = this . oraclesToPoll . get ( oracleId ) ;
63
+ if ( ! oracleToPoll ) continue ;
56
64
const now = Date . now ( ) ;
57
- if ( now - lastUpdate > 70_000 && ! this . pollingOracles . has ( oracleId ) ) {
65
+ if ( now - lastUpdate > 130_000 && ! this . pollingOracles . has ( oracleId ) ) {
66
+ const market = [ ...DevnetPerpMarkets , ...MainnetPerpMarkets ] . find ( m => m . oracle . equals ( oracleToPoll . publicKey ) ) ;
67
+ if ( oracleToPoll . publicKey . equals ( new PublicKey ( "BtUUSUc9rZSzBmmKhQq4no65zHQTzMFeVYss7xcMRD53" ) ) ) {
68
+ console . log ( "startOraclePollingWatchdog timeout fallback Republican popular vote" , oracleToPoll . publicKey . toBase58 ( ) , market ?. symbol ) ;
69
+ }
58
70
await this . addOracleToAccountLoader ( oracleToPoll ) ;
59
71
this . pollingOracles . set ( oracleId , true ) ;
60
72
}
61
73
}
62
74
} , 60_000 ) ;
63
75
}
64
76
65
- public removeAllOraclesFromAccountLoader ( ) {
66
- for ( const oracleInfo of this . oracleInfos ) {
67
- const existingAccountToLoad = this . accountLoader . accountsToLoad . get ( oracleInfo . publicKey . toString ( ) ) ;
68
- if ( existingAccountToLoad ) {
69
- // console.log('ORACLEDATA remove from account loader', oracleInfo.publicKey.toBase58());
70
- for ( const [ callbackId ] of existingAccountToLoad . callbacks ) {
71
- this . accountLoader . removeAccount ( oracleInfo . publicKey , callbackId ) ;
72
- }
73
- }
74
- }
75
- }
76
-
77
77
public override async unsubscribe ( ) : Promise < void > {
78
78
clearInterval ( this . oraclePollIntervalId ) ;
79
79
await super . unsubscribe ( ) ;
0 commit comments