Skip to content
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

sdk: new DelistMarketSetting #1229

Merged
merged 4 commits into from
Sep 30, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- program: reusue unused maker order id as success condition for place and take perp order ([#1218](https://github.com/drift-labs/protocol-v2/pull/1218))
- program/sdk: swift for devnet ([#1195](https://github.com/drift-labs/protocol-v2/pull/1195))
- sdk: EventSubscriber: support events server ([#1222](https://github.com/drift-labs/protocol-v2/pull/1222))
- sdk: add new DelistMarketSetting to handle delisted markets ([#1229](https://github.com/drift-labs/protocol-v2/pull/1229))

### Fixes

Expand Down
46 changes: 41 additions & 5 deletions sdk/src/accounts/pollingDriftClientAccountSubscriber.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
DataAndSlot,
AccountToPoll,
DataAndSlot,
DelistedMarketSetting,
DriftClientAccountEvents,
DriftClientAccountSubscriber,
NotSubscribedError,
Expand All @@ -10,18 +11,18 @@ import { Program } from '@coral-xyz/anchor';
import StrictEventEmitter from 'strict-event-emitter-types';
import { EventEmitter } from 'events';
import {
SpotMarketAccount,
PerpMarketAccount,
SpotMarketAccount,
StateAccount,
UserAccount,
} from '../types';
import {
getDriftStateAccountPublicKey,
getSpotMarketPublicKey,
getPerpMarketPublicKey,
getSpotMarketPublicKey,
} from '../addresses/pda';
import { BulkAccountLoader } from './bulkAccountLoader';
import { capitalize } from './utils';
import { capitalize, findDelistedPerpMarketsAndOracles } from './utils';
import { PublicKey } from '@solana/web3.js';
import { OracleInfo, OraclePriceData } from '../oracles/types';
import { OracleClientCache } from '../oracles/oracleClientCache';
Expand Down Expand Up @@ -58,6 +59,7 @@ export class PollingDriftClientAccountSubscriber
spotOracleStringMap = new Map<number, string>();
oracles = new Map<string, DataAndSlot<OraclePriceData>>();
user?: DataAndSlot<UserAccount>;
delistedMarketSetting: DelistedMarketSetting;

private isSubscribing = false;
private subscriptionPromise: Promise<boolean>;
Expand All @@ -69,7 +71,8 @@ export class PollingDriftClientAccountSubscriber
perpMarketIndexes: number[],
spotMarketIndexes: number[],
oracleInfos: OracleInfo[],
shouldFindAllMarketsAndOracles: boolean
shouldFindAllMarketsAndOracles: boolean,
delistedMarketSetting: DelistedMarketSetting
) {
this.isSubscribed = false;
this.program = program;
Expand All @@ -79,6 +82,7 @@ export class PollingDriftClientAccountSubscriber
this.spotMarketIndexes = spotMarketIndexes;
this.oracleInfos = oracleInfos;
this.shouldFindAllMarketsAndOracles = shouldFindAllMarketsAndOracles;
this.delistedMarketSetting = delistedMarketSetting;
}

public async subscribe(): Promise<boolean> {
Expand Down Expand Up @@ -120,6 +124,8 @@ export class PollingDriftClientAccountSubscriber
this.eventEmitter.emit('update');
}

this.handleDelistedMarkets();

await Promise.all([this.setPerpOracleMap(), this.setSpotOracleMap()]);

this.isSubscribing = false;
Expand Down Expand Up @@ -500,6 +506,36 @@ export class PollingDriftClientAccountSubscriber
await Promise.all(oraclePromises);
}

handleDelistedMarkets(): void {
if (this.delistedMarketSetting === DelistedMarketSetting.Subscribe) {
return;
}

const { perpMarketIndexes, oracles } = findDelistedPerpMarketsAndOracles(
this.getMarketAccountsAndSlots(),
this.getSpotMarketAccountsAndSlots()
);

for (const perpMarketIndex of perpMarketIndexes) {
const perpMarketPubkey = this.perpMarket.get(perpMarketIndex).data.pubkey;
const callbackId = this.accountsToPoll.get(
perpMarketPubkey.toBase58()
).callbackId;
this.accountLoader.removeAccount(perpMarketPubkey, callbackId);
if (this.delistedMarketSetting === DelistedMarketSetting.Discard) {
this.perpMarket.delete(perpMarketIndex);
}
}

for (const oracle of oracles) {
const callbackId = this.oraclesToPoll.get(oracle.toBase58()).callbackId;
this.accountLoader.removeAccount(oracle, callbackId);
if (this.delistedMarketSetting === DelistedMarketSetting.Discard) {
this.oracles.delete(oracle.toBase58());
}
}
}

assertIsSubscribed(): void {
if (!this.isSubscribed) {
throw new NotSubscribedError(
Expand Down
6 changes: 6 additions & 0 deletions sdk/src/accounts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ export interface DriftClientAccountSubscriber {
updateAccountLoaderPollingFrequency?: (pollingFrequency: number) => void;
}

export enum DelistedMarketSetting {
Unsubscribe,
Subscribe,
Discard,
}

export interface UserAccountEvents {
userAccountUpdate: (payload: UserAccount) => void;
update: void;
Expand Down
42 changes: 42 additions & 0 deletions sdk/src/accounts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
import { PublicKey } from '@solana/web3.js';
import { DataAndSlot } from './types';
import { isVariant, PerpMarketAccount, SpotMarketAccount } from '../types';

export function capitalize(value: string): string {
return value[0].toUpperCase() + value.slice(1);
}

export function findDelistedPerpMarketsAndOracles(
perpMarkets: DataAndSlot<PerpMarketAccount>[],
spotMarkets: DataAndSlot<SpotMarketAccount>[]
): { perpMarketIndexes: number[]; oracles: PublicKey[] } {
const delistedPerpMarketIndexes = [];
const delistedOracles = [];
for (const perpMarket of perpMarkets) {
if (!perpMarket.data) {
continue;
}

if (isVariant(perpMarket.data.status, 'delisted')) {
delistedPerpMarketIndexes.push(perpMarket.data.marketIndex);
delistedOracles.push(perpMarket.data.amm.oracle);
}
}

// make sure oracle isn't used by spot market
const filteredDelistedOracles = [];
for (const delistedOracle of delistedOracles) {
for (const spotMarket of spotMarkets) {
if (!spotMarket.data) {
continue;
}

if (spotMarket.data.oracle.equals(delistedOracle)) {
break;
}
}
filteredDelistedOracles.push(delistedOracle);
}

return {
perpMarketIndexes: delistedPerpMarketIndexes,
oracles: filteredDelistedOracles,
};
}
45 changes: 40 additions & 5 deletions sdk/src/accounts/webSocketDriftClientAccountSubscriber.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import {
DriftClientAccountSubscriber,
DriftClientAccountEvents,
AccountSubscriber,
DataAndSlot,
DelistedMarketSetting,
DriftClientAccountEvents,
DriftClientAccountSubscriber,
NotSubscribedError,
ResubOpts,
} from './types';
import { AccountSubscriber, NotSubscribedError } from './types';
import { SpotMarketAccount, PerpMarketAccount, StateAccount } from '../types';
import { PerpMarketAccount, SpotMarketAccount, StateAccount } from '../types';
import { Program } from '@coral-xyz/anchor';
import StrictEventEmitter from 'strict-event-emitter-types';
import { EventEmitter } from 'events';
import {
getDriftStateAccountPublicKey,
getSpotMarketPublicKey,
getPerpMarketPublicKey,
getPerpMarketPublicKeySync,
getSpotMarketPublicKey,
getSpotMarketPublicKeySync,
} from '../addresses/pda';
import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
Expand All @@ -23,6 +25,7 @@ import { OracleClientCache } from '../oracles/oracleClientCache';
import * as Buffer from 'buffer';
import { QUOTE_ORACLE_PRICE_DATA } from '../oracles/quoteAssetOracleClient';
import { findAllMarketAndOracles } from '../config';
import { findDelistedPerpMarketsAndOracles } from './utils';

const ORACLE_DEFAULT_KEY = PublicKey.default.toBase58();

Expand Down Expand Up @@ -55,6 +58,7 @@ export class WebSocketDriftClientAccountSubscriber
spotOracleMap = new Map<number, PublicKey>();
spotOracleStringMap = new Map<number, string>();
oracleSubscribers = new Map<string, AccountSubscriber<OraclePriceData>>();
delistedMarketSetting: DelistedMarketSetting;

initialPerpMarketAccountData: Map<number, PerpMarketAccount>;
initialSpotMarketAccountData: Map<number, SpotMarketAccount>;
Expand All @@ -70,6 +74,7 @@ export class WebSocketDriftClientAccountSubscriber
spotMarketIndexes: number[],
oracleInfos: OracleInfo[],
shouldFindAllMarketsAndOracles: boolean,
delistedMarketSetting: DelistedMarketSetting,
resubOpts?: ResubOpts,
commitment?: Commitment
) {
Expand All @@ -80,6 +85,7 @@ export class WebSocketDriftClientAccountSubscriber
this.spotMarketIndexes = spotMarketIndexes;
this.oracleInfos = oracleInfos;
this.shouldFindAllMarketsAndOracles = shouldFindAllMarketsAndOracles;
this.delistedMarketSetting = delistedMarketSetting;
this.resubOpts = resubOpts;
this.commitment = commitment;
}
Expand Down Expand Up @@ -151,6 +157,8 @@ export class WebSocketDriftClientAccountSubscriber

this.eventEmitter.emit('update');

await this.handleDelistedMarkets();

await Promise.all([this.setPerpOracleMap(), this.setSpotOracleMap()]);

this.isSubscribing = false;
Expand Down Expand Up @@ -480,6 +488,33 @@ export class WebSocketDriftClientAccountSubscriber
await Promise.all(addOraclePromises);
}

async handleDelistedMarkets(): Promise<void> {
if (this.delistedMarketSetting === DelistedMarketSetting.Subscribe) {
return;
}

const { perpMarketIndexes, oracles } = findDelistedPerpMarketsAndOracles(
this.getMarketAccountsAndSlots(),
this.getSpotMarketAccountsAndSlots()
);

for (const perpMarketIndex of perpMarketIndexes) {
await this.perpMarketAccountSubscribers
.get(perpMarketIndex)
.unsubscribe();
if (this.delistedMarketSetting === DelistedMarketSetting.Discard) {
this.perpMarketAccountSubscribers.delete(perpMarketIndex);
}
}

for (const oracle of oracles) {
await this.oracleSubscribers.get(oracle.toBase58()).unsubscribe();
if (this.delistedMarketSetting === DelistedMarketSetting.Discard) {
this.oracleSubscribers.delete(oracle.toBase58());
}
}
}

assertIsSubscribed(): void {
if (!this.isSubscribed) {
throw new NotSubscribedError(
Expand Down
Loading
Loading