Skip to content

Commit 2c40b9f

Browse files
committed
fix: bugs with external data acct subscriber
1 parent a65d681 commit 2c40b9f

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

sdk/src/accounts/externalOracleDataDriftClientSubscriber.ts renamed to sdk/src/accounts/externalDataDriftClientSubscriber.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import {
88
} from '../oracles/types';
99

1010
import { getOracleId } from '../oracles/oracleId';
11+
import { DevnetPerpMarkets, MainnetPerpMarkets, PublicKey } from '@drift-labs/sdk';
1112

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 {
1315
private oracleLastUpdate = new Map<string, number>();
1416
private pollingOracles = new Map<string, boolean>();
1517
private oraclePollIntervalId: NodeJS.Timeout;
@@ -26,6 +28,9 @@ export class ExternalOracleDataDriftClientSubscriber extends PollingDriftClientA
2628

2729
/** Public method to be called externally with fresh oracle data */
2830
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+
}
2934
const oracleId = getOracleId(oracleInfo.publicKey, oracleInfo.source);
3035
this.oracles.set(oracleId, { data: priceData, slot });
3136
this.oracleLastUpdate.set(oracleId, Date.now());
@@ -43,37 +48,32 @@ export class ExternalOracleDataDriftClientSubscriber extends PollingDriftClientA
4348

4449
public override async subscribe(): Promise<boolean> {
4550
await super.subscribe();
46-
this.removeAllOraclesFromAccountLoader();
4751
this.startOraclePollingWatchdog();
4852
return true;
4953
}
5054

5155
private startOraclePollingWatchdog() {
56+
if(this.oraclePollIntervalId) {
57+
clearInterval(this.oraclePollIntervalId);
58+
}
5259
// how do we handle not polling bet markets every 1s from this change?
5360
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;
5664
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+
}
5870
await this.addOracleToAccountLoader(oracleToPoll);
5971
this.pollingOracles.set(oracleId, true);
6072
}
6173
}
6274
}, 60_000);
6375
}
6476

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-
7777
public override async unsubscribe(): Promise<void> {
7878
clearInterval(this.oraclePollIntervalId);
7979
await super.unsubscribe();

sdk/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export * from './accounts/bulkAccountLoader';
1616
export * from './accounts/bulkUserSubscription';
1717
export * from './accounts/bulkUserStatsSubscription';
1818
export { CustomizedCadenceBulkAccountLoader } from './accounts/customizedCadenceBulkAccountLoader';
19-
export { ExternalOracleDataDriftClientSubscriber } from './accounts/externalOracleDataDriftClientSubscriber';
19+
export { ExternalDataDriftClientSubscriber } from './accounts/externalDataDriftClientSubscriber';
2020
export * from './accounts/pollingDriftClientAccountSubscriber';
2121
export * from './accounts/pollingOracleAccountSubscriber';
2222
export * from './accounts/pollingTokenAccountSubscriber';

0 commit comments

Comments
 (0)