Skip to content

Commit

Permalink
Merge branch 'main' into cyr/price
Browse files Browse the repository at this point in the history
  • Loading branch information
brittcyr authored Nov 14, 2024
2 parents 8635009 + aedf5f5 commit 73cf219
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 51 deletions.
14 changes: 14 additions & 0 deletions client/ts/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ export class Global {
return toNum(deposit.balanceAtoms) / 10 ** decimals;
}

getGlobalBalanceTokensWithDecimals(
trader: PublicKey,
decimals: number,
): number {
const deposit: GlobalDeposit | undefined = this.data.globalDeposits.find(
(seat) => seat.trader.equals(trader),
);
if (!deposit) {
return 0;
} else {
return toNum(deposit.balanceAtoms) / 10 ** decimals;
}
}

tokenMint(): PublicKey {
return this.data.mint;
}
Expand Down
1 change: 1 addition & 0 deletions client/ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './client';
export * from './market';
export * from './global';
export * from './types';
export * as manifest from './manifest';
export * as utils from './utils';
Expand Down
107 changes: 56 additions & 51 deletions debug-ui/scripts/stats-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,61 +382,66 @@ export class ManifestStatsServer {
* https://docs.google.com/document/d/1v27QFoQq1SKT3Priq3aqPgB70Xd_PnDzbOCiuoCyixw/edit?tab=t.0#heading=h.vgzsfbx8rvps
*/
async getOrderbook(tickerId: string, depth: number) {
const market: Market = await Market.loadFromAddress({
connection: this.connection,
address: new PublicKey(tickerId),
});
if (depth == 0) {
try {
const market: Market = await Market.loadFromAddress({
connection: this.connection,
address: new PublicKey(tickerId),
});
if (depth == 0) {
return {
ticker_id: tickerId,
bids: market
.bids()
.reverse()
.map((restingOrder: RestingOrder) => {
return [
restingOrder.tokenPrice,
Number(restingOrder.numBaseTokens),
];
}),
asks: market
.asks()
.reverse()
.map((restingOrder: RestingOrder) => {
return [
restingOrder.tokenPrice,
Number(restingOrder.numBaseTokens),
];
}),
};
}
const bids: RestingOrder[] = market.bids().reverse();
const asks: RestingOrder[] = market.asks().reverse();
const bidsUpToDepth: RestingOrder[] = [];
const asksUpToDepth: RestingOrder[] = [];
let bidTokens: number = 0;
let askTokens: number = 0;
bids.forEach((bid: RestingOrder) => {
if (bidTokens < depth) {
bidTokens += Number(bid.numBaseTokens);
bidsUpToDepth.push(bid);
}
});
asks.forEach((ask: RestingOrder) => {
if (askTokens < depth) {
askTokens += Number(ask.numBaseTokens);
asksUpToDepth.push(ask);
}
});

return {
ticker_id: tickerId,
bids: market
.bids()
.reverse()
.map((restingOrder: RestingOrder) => {
return [
restingOrder.tokenPrice,
Number(restingOrder.numBaseTokens),
];
}),
asks: market
.asks()
.reverse()
.map((restingOrder: RestingOrder) => {
return [
restingOrder.tokenPrice,
Number(restingOrder.numBaseTokens),
];
}),
bids: bidsUpToDepth.map((restingOrder: RestingOrder) => {
return [restingOrder.tokenPrice, Number(restingOrder.numBaseTokens)];
}),
asks: asksUpToDepth.reverse().map((restingOrder: RestingOrder) => {
return [restingOrder.tokenPrice, Number(restingOrder.numBaseTokens)];
}),
};
} catch (err) {
console.log('Error getOrderbook', tickerId, depth, err);
return {};
}
const bids: RestingOrder[] = market.bids().reverse();
const asks: RestingOrder[] = market.asks().reverse();
const bidsUpToDepth: RestingOrder[] = [];
const asksUpToDepth: RestingOrder[] = [];
let bidTokens: number = 0;
let askTokens: number = 0;
bids.forEach((bid: RestingOrder) => {
if (bidTokens < depth) {
bidTokens += Number(bid.numBaseTokens);
bidsUpToDepth.push(bid);
}
});
asks.forEach((ask: RestingOrder) => {
if (askTokens < depth) {
askTokens += Number(ask.numBaseTokens);
asksUpToDepth.push(ask);
}
});

return {
ticker_id: tickerId,
bids: bidsUpToDepth.map((restingOrder: RestingOrder) => {
return [restingOrder.tokenPrice, Number(restingOrder.numBaseTokens)];
}),
asks: asksUpToDepth.reverse().map((restingOrder: RestingOrder) => {
return [restingOrder.tokenPrice, Number(restingOrder.numBaseTokens)];
}),
};
}

/**
Expand Down

0 comments on commit 73cf219

Please sign in to comment.