Skip to content

Commit d2b3580

Browse files
committed
feat: temp
1 parent 32e7915 commit d2b3580

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import {
2+
PollingDriftClientAccountSubscriber
3+
} from './pollingDriftClientAccountSubscriber';
4+
5+
import {
6+
OraclePriceData,
7+
OracleInfo
8+
} from '../oracles/types';
9+
10+
import { getOracleId } from '../oracles/oracleId';
11+
12+
export class ExternalOracleDataDriftClientSubscriber extends PollingDriftClientAccountSubscriber {
13+
private oracleLastUpdate = new Map<string, number>();
14+
private pollingOracles = new Map<string, boolean>();
15+
private oraclePollIntervalId: NodeJS.Timeout;
16+
17+
constructor(...args: ConstructorParameters<typeof PollingDriftClientAccountSubscriber>) {
18+
super(...args);
19+
20+
}
21+
22+
/** Override to prevent oracles from being automatically polled later */
23+
public override updateOraclesToPoll(): boolean {
24+
return true;
25+
}
26+
27+
/** Public method to be called externally with fresh oracle data */
28+
public feedOracle(oracleInfo: OracleInfo, priceData: OraclePriceData, slot: number) {
29+
console.log('ORACLEDATA feedOracle');
30+
const oracleId = getOracleId(oracleInfo.publicKey, oracleInfo.source);
31+
this.oracles.set(oracleId, { data: priceData, slot });
32+
this.oracleLastUpdate.set(oracleId, Date.now());
33+
34+
if (this.pollingOracles.has(oracleId)) {
35+
const oracleToPoll = this.oraclesToPoll.get(oracleId);
36+
if (oracleToPoll) {
37+
this.accountLoader.removeAccount(
38+
oracleToPoll.publicKey,
39+
oracleToPoll.callbackId
40+
);
41+
this.pollingOracles.delete(oracleId);
42+
}
43+
}
44+
}
45+
46+
public override async subscribe(): Promise<boolean> {
47+
await super.subscribe();
48+
this.removeAllOraclesFromAccountLoader();
49+
this.startOraclePollingWatchdog();
50+
return true;
51+
}
52+
53+
private startOraclePollingWatchdog() {
54+
this.oraclePollIntervalId = setInterval(async () => {
55+
for (const [oracleId, oracleToPoll] of this.oraclesToPoll.entries()) {
56+
const lastUpdate = this.oracleLastUpdate.get(oracleId) || 0;
57+
const now = Date.now();
58+
if (now - lastUpdate > 70_000 && !this.pollingOracles.has(oracleId)) {
59+
console.log('ORACLEDATA polling oracle via RPC', oracleToPoll.publicKey.toBase58());
60+
await this.addOracleToAccountLoader(oracleToPoll);
61+
this.pollingOracles.set(oracleId, true);
62+
}
63+
}
64+
}, 70_000);
65+
}
66+
67+
public removeAllOraclesFromAccountLoader() {
68+
for (const oracleInfo of this.oracleInfos) {
69+
const existingAccountToLoad = this.accountLoader.accountsToLoad.get(oracleInfo.publicKey.toString());
70+
if (existingAccountToLoad) {
71+
console.log('ORACLEDATA remove from account loader', oracleInfo.publicKey.toBase58());
72+
for (const [callbackId] of existingAccountToLoad.callbacks) {
73+
this.accountLoader.removeAccount(oracleInfo.publicKey, callbackId);
74+
}
75+
}
76+
}
77+
}
78+
79+
public override async unsubscribe(): Promise<void> {
80+
clearInterval(this.oraclePollIntervalId);
81+
await super.unsubscribe();
82+
this.oracleLastUpdate.clear();
83+
this.pollingOracles.clear();
84+
}
85+
}

sdk/src/accounts/pollingDriftClientAccountSubscriber.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export class PollingDriftClientAccountSubscriber
9191
}
9292

9393
public async subscribe(): Promise<boolean> {
94+
console.log('ORACLEDATA subscribe on PollingDriftClientAccountSubscriber');
9495
if (this.isSubscribed) {
9596
return true;
9697
}
@@ -212,8 +213,10 @@ export class PollingDriftClientAccountSubscriber
212213
}
213214

214215
updateOraclesToPoll(): boolean {
216+
console.log('ORACLEDATA updateOraclesToPoll');
215217
for (const oracleInfo of this.oracleInfos) {
216218
if (!oracleInfo.publicKey.equals(PublicKey.default)) {
219+
console.log('ORACLEDATA updateOraclesToPoll', oracleInfo.publicKey.toBase58());
217220
this.addOracleToPoll(oracleInfo);
218221
}
219222
}
@@ -222,6 +225,7 @@ export class PollingDriftClientAccountSubscriber
222225
}
223226

224227
addOracleToPoll(oracleInfo: OracleInfo): boolean {
228+
console.log('ORACLEDATA addOracleToPoll', oracleInfo.publicKey.toBase58());
225229
this.oraclesToPoll.set(
226230
getOracleId(oracleInfo.publicKey, oracleInfo.source),
227231
{
@@ -240,6 +244,7 @@ export class PollingDriftClientAccountSubscriber
240244

241245
const oraclePromises = [];
242246
for (const [_, oracleToPoll] of this.oraclesToPoll) {
247+
console.log('ORACLEDATA oraclePromises addOracleToPoll', oracleToPoll.publicKey.toBase58());
243248
oraclePromises.push(this.addOracleToAccountLoader(oracleToPoll));
244249
}
245250

@@ -382,6 +387,7 @@ export class PollingDriftClientAccountSubscriber
382387
}
383388

384389
public async unsubscribe(): Promise<void> {
390+
console.log('ORACLEDATA unsubscribe');
385391
for (const [_, accountToPoll] of this.accountsToPoll) {
386392
this.accountLoader.removeAccount(
387393
accountToPoll.publicKey,
@@ -546,8 +552,13 @@ export class PollingDriftClientAccountSubscriber
546552

547553
for (const oracle of oracles) {
548554
const oracleId = getOracleId(oracle.publicKey, oracle.source);
549-
const callbackId = this.oraclesToPoll.get(oracleId).callbackId;
550-
this.accountLoader.removeAccount(oracle.publicKey, callbackId);
555+
const oracleToPoll = this.oraclesToPoll.get(oracleId);
556+
if (oracleToPoll) {
557+
this.accountLoader.removeAccount(
558+
oracleToPoll.publicKey,
559+
oracleToPoll.callbackId
560+
);
561+
}
551562
if (this.delistedMarketSetting === DelistedMarketSetting.Discard) {
552563
this.oracles.delete(oracleId);
553564
}

sdk/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +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';
1920
export * from './accounts/pollingDriftClientAccountSubscriber';
2021
export * from './accounts/pollingOracleAccountSubscriber';
2122
export * from './accounts/pollingTokenAccountSubscriber';

0 commit comments

Comments
 (0)