Skip to content

External Data account subscriber #1687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sdk/src/accounts/customizedCadenceBulkAccountLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export class CustomizedCadenceBulkAccountLoader extends BulkAccountLoader {

for (const [key, frequency] of this.accountFrequencies.entries()) {
const lastPollTime = this.lastPollingTimes.get(key) || 0;
if (currentTime - lastPollTime >= frequency) {
// Add 200ms buffer to account for timing wiggle room with js execution
const timeDiff = currentTime - lastPollTime;
if (timeDiff >= frequency - 200) {
const account = this.accountsToLoad.get(key);
if (account) {
accountsToLoad.push(account);
Expand Down
76 changes: 76 additions & 0 deletions sdk/src/accounts/externalDataDriftClientSubscriber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {
PollingDriftClientAccountSubscriber
} from './pollingDriftClientAccountSubscriber';

import {
OraclePriceData,
OracleInfo
} from '../oracles/types';

import { getOracleId } from '../oracles/oracleId';


// allowing app UI state to incrementally replace RPC fetched acconut data with data from our infra that is pre-indexed and decoded
export class ExternalDataDriftClientSubscriber extends PollingDriftClientAccountSubscriber {
private oracleLastUpdate = new Map<string, number>();
private pollingOracles = new Map<string, boolean>();
private oraclePollIntervalId: NodeJS.Timeout;

constructor(...args: ConstructorParameters<typeof PollingDriftClientAccountSubscriber>) {
super(...args);

}

/** Override to prevent oracles from being automatically polled later */
public override updateOraclesToPoll(): boolean {
return true;
}

/** Public method to be called externally with fresh oracle data */
public feedOracle(oracleInfo: OracleInfo, priceData: OraclePriceData, slot: number) {
const oracleId = getOracleId(oracleInfo.publicKey, oracleInfo.source);
this.oracles.set(oracleId, { data: priceData, slot });
this.oracleLastUpdate.set(oracleId, Date.now());
if (this.pollingOracles.has(oracleId) || this.accountLoader.accountsToLoad.has(oracleInfo.publicKey.toBase58())) {
const oracleToPoll = this.oraclesToPoll.get(oracleId);
if (oracleToPoll) {
this.accountLoader.removeAccount(
oracleToPoll.publicKey,
oracleToPoll.callbackId
);
this.pollingOracles.delete(oracleId);
}
}
}

public override async subscribe(): Promise<boolean> {
await super.subscribe();
this.startOraclePollingWatchdog();
return true;
}

private startOraclePollingWatchdog() {
if(this.oraclePollIntervalId) {
clearInterval(this.oraclePollIntervalId);
}
// how do we handle not polling bet markets every 1s from this change?
this.oraclePollIntervalId = setInterval(async () => {
for (const [oracleId, lastUpdate] of this.oracleLastUpdate.entries()) {
const oracleToPoll = this.oraclesToPoll.get(oracleId);
if(!oracleToPoll) continue;
const now = Date.now();
if (now - lastUpdate > 130_000 && !this.pollingOracles.has(oracleId)) {
await this.addOracleToAccountLoader(oracleToPoll);
this.pollingOracles.set(oracleId, true);
}
}
}, 60_000);
}

public override async unsubscribe(): Promise<void> {
clearInterval(this.oraclePollIntervalId);
await super.unsubscribe();
this.oracleLastUpdate.clear();
this.pollingOracles.clear();
}
}
9 changes: 7 additions & 2 deletions sdk/src/accounts/pollingDriftClientAccountSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,13 @@ export class PollingDriftClientAccountSubscriber

for (const oracle of oracles) {
const oracleId = getOracleId(oracle.publicKey, oracle.source);
const callbackId = this.oraclesToPoll.get(oracleId).callbackId;
this.accountLoader.removeAccount(oracle.publicKey, callbackId);
const oracleToPoll = this.oraclesToPoll.get(oracleId);
if (oracleToPoll) {
this.accountLoader.removeAccount(
oracleToPoll.publicKey,
oracleToPoll.callbackId
);
}
if (this.delistedMarketSetting === DelistedMarketSetting.Discard) {
this.oracles.delete(oracleId);
}
Expand Down
1 change: 1 addition & 0 deletions sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from './accounts/bulkAccountLoader';
export * from './accounts/bulkUserSubscription';
export * from './accounts/bulkUserStatsSubscription';
export { CustomizedCadenceBulkAccountLoader } from './accounts/customizedCadenceBulkAccountLoader';
export { ExternalDataDriftClientSubscriber } from './accounts/externalDataDriftClientSubscriber';
export * from './accounts/pollingDriftClientAccountSubscriber';
export * from './accounts/pollingOracleAccountSubscriber';
export * from './accounts/pollingTokenAccountSubscriber';
Expand Down
Loading