Skip to content

Commit

Permalink
Try catch depth probes (#276)
Browse files Browse the repository at this point in the history
* Try catch depth probes

* Only accept recent fills

* reset
  • Loading branch information
brittcyr authored Nov 12, 2024
1 parent 9f11fcb commit 47f5f2c
Showing 1 changed file with 85 additions and 73 deletions.
158 changes: 85 additions & 73 deletions debug-ui/scripts/stats-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export class ManifestStatsServer {
// Market objects used for mints and decimals.
private markets: Map<string, Market> = new Map();

private lastFillSlot: number = 0;

constructor() {
this.connection = new Connection(RPC_URL!);
this.ws = new WebSocket('wss://mfx-feed-mainnet.fly.dev');
Expand All @@ -93,21 +95,27 @@ export class ManifestStatsServer {
this.ws.on('open', () => {});

this.ws.on('close', () => {
// Rely on the next iteration to force a reset.
console.log('Disconnected. Reconnecting');
this.ws = new WebSocket('wss://mfx-feed-mainnet.fly.dev');
reconnects.inc();
});
this.ws.on('error', () => {
// Rely on the next iteration to force a reset.
console.log('Error. Reconnecting');
this.ws = new WebSocket('wss://mfx-feed-mainnet.fly.dev');
reconnects.inc();
});

this.ws.on('message', async (message) => {
const fill: FillLogResult = JSON.parse(message.toString());
const { market, baseAtoms, quoteAtoms, price } = fill;
const { market, baseAtoms, quoteAtoms, price, slot } = fill;

// Do not accept old spurious messages.
if (this.lastFillSlot > slot) {
return;
}
this.lastFillSlot = slot;

fills.inc({ market });
console.log('Got fill:', fill);

if (this.markets.get(market) == undefined) {
this.baseVolumeAtomsSinceLastCheckpoint.set(market, 0);
Expand Down Expand Up @@ -242,77 +250,81 @@ export class ManifestStatsServer {
},
);

const accountInfos: (AccountInfo<Buffer> | null)[] =
await this.connection.getMultipleAccountsInfo(marketKeys);
accountInfos.forEach(
(accountInfo: AccountInfo<Buffer> | null, index: number) => {
if (!accountInfo) {
return;
}
const marketPk: PublicKey = marketKeys[index];
const market: Market = Market.loadFromBuffer({
buffer: accountInfo.data,
address: marketPk,
});
const bids: RestingOrder[] = market.bids();
const asks: RestingOrder[] = market.asks();
if (bids.length == 0 || asks.length == 0) {
return;
}
try {
const accountInfos: (AccountInfo<Buffer> | null)[] =
await this.connection.getMultipleAccountsInfo(marketKeys);
accountInfos.forEach(
(accountInfo: AccountInfo<Buffer> | null, index: number) => {
if (!accountInfo) {
return;
}
const marketPk: PublicKey = marketKeys[index];
const market: Market = Market.loadFromBuffer({
buffer: accountInfo.data,
address: marketPk,
});
const bids: RestingOrder[] = market.bids();
const asks: RestingOrder[] = market.asks();
if (bids.length == 0 || asks.length == 0) {
return;
}

const mid: number =
(bids[bids.length - 1].tokenPrice +
asks[asks.length - 1].tokenPrice) /
2;

DEPTHS_BPS.forEach((depthBps: number) => {
const bidsAtDepth: RestingOrder[] = bids.filter(
(bid: RestingOrder) => {
return bid.tokenPrice > mid * (1 - depthBps * 0.0001);
},
);
const asksAtDepth: RestingOrder[] = asks.filter(
(ask: RestingOrder) => {
return ask.tokenPrice < mid * (1 + depthBps * 0.0001);
},
);

const bidTraders: Set<string> = new Set(
bidsAtDepth.map((bid: RestingOrder) => bid.trader.toBase58()),
);

bidTraders.forEach((trader: string) => {
const bidTokensAtDepth: number = bidsAtDepth
.filter((bid: RestingOrder) => {
return bid.trader.toBase58() == trader;
})
.map((bid: RestingOrder) => {
return Number(bid.numBaseTokens);
})
.reduce((sum, num) => sum + num, 0);
const askTokensAtDepth: number = asksAtDepth
.filter((ask: RestingOrder) => {
return ask.trader.toBase58() == trader;
})
.map((ask: RestingOrder) => {
return Number(ask.numBaseTokens);
})
.reduce((sum, num) => sum + num, 0);

if (bidTokensAtDepth > 0 && askTokensAtDepth > 0) {
depth.set(
{
depth_bps: depthBps,
market: marketPk.toBase58(),
trader: trader,
},
Math.min(bidTokensAtDepth, askTokensAtDepth),
);
}
const mid: number =
(bids[bids.length - 1].tokenPrice +
asks[asks.length - 1].tokenPrice) /
2;

DEPTHS_BPS.forEach((depthBps: number) => {
const bidsAtDepth: RestingOrder[] = bids.filter(
(bid: RestingOrder) => {
return bid.tokenPrice > mid * (1 - depthBps * 0.0001);
},
);
const asksAtDepth: RestingOrder[] = asks.filter(
(ask: RestingOrder) => {
return ask.tokenPrice < mid * (1 + depthBps * 0.0001);
},
);

const bidTraders: Set<string> = new Set(
bidsAtDepth.map((bid: RestingOrder) => bid.trader.toBase58()),
);

bidTraders.forEach((trader: string) => {
const bidTokensAtDepth: number = bidsAtDepth
.filter((bid: RestingOrder) => {
return bid.trader.toBase58() == trader;
})
.map((bid: RestingOrder) => {
return Number(bid.numBaseTokens);
})
.reduce((sum, num) => sum + num, 0);
const askTokensAtDepth: number = asksAtDepth
.filter((ask: RestingOrder) => {
return ask.trader.toBase58() == trader;
})
.map((ask: RestingOrder) => {
return Number(ask.numBaseTokens);
})
.reduce((sum, num) => sum + num, 0);

if (bidTokensAtDepth > 0 && askTokensAtDepth > 0) {
depth.set(
{
depth_bps: depthBps,
market: marketPk.toBase58(),
trader: trader,
},
Math.min(bidTokensAtDepth, askTokensAtDepth),
);
}
});
});
});
},
);
},
);
} catch (err) {
console.log('Unable to fetch depth probe', err);
}
}

/**
Expand Down

0 comments on commit 47f5f2c

Please sign in to comment.