refactor: replace serum-history with new endpoint (#6)
This commit is contained in:
parent
05ed47fde4
commit
5dcee0f58c
|
@ -143,23 +143,17 @@ class MarketsController implements Controller {
|
|||
const market = allMarkets[marketConfig.publicKey.toBase58()];
|
||||
|
||||
const [
|
||||
volume,
|
||||
change1h,
|
||||
change24h,
|
||||
changeBod,
|
||||
marketData, // contains volume, 1H, 24H, bod changes
|
||||
ordersInfo, // used for latest bid+ask
|
||||
tradesResponse, // used for latest trade+price
|
||||
] = await Promise.all([
|
||||
getVolumeForMarket(marketConfig),
|
||||
getChange1H(marketConfig),
|
||||
getChange24H(marketConfig),
|
||||
getChangeBod(marketConfig),
|
||||
getMarketData(marketConfig),
|
||||
(await this.mangoSimpleClient.fetchAllBidsAndAsks(
|
||||
false,
|
||||
marketConfig.name
|
||||
)) as OrderInfo[][],
|
||||
fetch(
|
||||
`https://serum-history.herokuapp.com/trades/address/${marketConfig.publicKey.toBase58()}`
|
||||
`https://event-history-api-candles.herokuapp.com/trades/address/${marketConfig.publicKey.toBase58()}`
|
||||
),
|
||||
]);
|
||||
|
||||
|
@ -215,10 +209,10 @@ class MarketsController implements Controller {
|
|||
baseCurrency: marketConfig.baseSymbol,
|
||||
quoteCurrency: "USDC",
|
||||
// note: event-history-api doesn't index volume for spot
|
||||
quoteVolume24h: volume !== 0 ? volume : undefined,
|
||||
change1h,
|
||||
change24h,
|
||||
changeBod,
|
||||
quoteVolume24h: marketData.quoteVolume24h !== 0 ? marketData.quoteVolume24h : undefined,
|
||||
change1h: marketData.change1h,
|
||||
change24h: marketData.change24h,
|
||||
changeBod: marketData.changeBod,
|
||||
highLeverageFeeExempt: undefined,
|
||||
minProvideSize: undefined,
|
||||
type: marketConfig.name.includes("PERP") ? "futures" : "spot",
|
||||
|
@ -233,7 +227,7 @@ class MarketsController implements Controller {
|
|||
sizeIncrement: minOrderSize,
|
||||
restricted: undefined,
|
||||
// note: event-history-api doesn't index volume for spot
|
||||
volumeUsd24h: volume !== 0 ? volume : undefined,
|
||||
volumeUsd24h: marketData.volumeUsd24h !== 0 ? marketData.volumeUsd24h : undefined,
|
||||
} as MarketDto;
|
||||
}
|
||||
|
||||
|
@ -331,7 +325,7 @@ class MarketsController implements Controller {
|
|||
|
||||
private async getTradesInternal(marketPk: PublicKey) {
|
||||
const tradesResponse = await fetch(
|
||||
`https://serum-history.herokuapp.com/trades/address/${marketPk.toBase58()}`
|
||||
`https://event-history-api-candles.herokuapp.com/trades/address/${marketPk.toBase58()}`
|
||||
);
|
||||
const parsedTradesResponse = (await tradesResponse.json()) as any;
|
||||
if ("s" in parsedTradesResponse && parsedTradesResponse["s"] === "error") {
|
||||
|
@ -397,46 +391,12 @@ export default MarketsController;
|
|||
|
||||
/// helper functions
|
||||
|
||||
async function getChange24H(marketConfig: MarketConfig): Promise<number> {
|
||||
const fromS =
|
||||
new Date(new Date().getTime() - 24 * 60 * 60 * 1000).getTime() / 1000;
|
||||
const toS = new Date(new Date().getTime()).getTime() / 1000;
|
||||
const { t, o, h, l, c, v } = await getOhlcv(
|
||||
marketConfig.name,
|
||||
"1D",
|
||||
fromS,
|
||||
toS
|
||||
async function getMarketData(marketConfig: MarketConfig): Promise<Partial<MarketDto>> {
|
||||
const marketDataResponse = await fetch(
|
||||
`https://event-history-api-candles.herokuapp.com/markets/` +
|
||||
`${patchInternalMarketName(marketConfig.name)}`
|
||||
);
|
||||
return c ? (c[0] - o[0]) / o[0] : undefined;
|
||||
}
|
||||
|
||||
async function getChange1H(marketConfig: MarketConfig): Promise<number> {
|
||||
const fromS =
|
||||
new Date(new Date().getTime() - 60 * 60 * 1000).getTime() / 1000;
|
||||
const toS = new Date(new Date().getTime()).getTime() / 1000;
|
||||
const { t, o, h, l, c, v } = await getOhlcv(
|
||||
marketConfig.name,
|
||||
"60",
|
||||
fromS,
|
||||
toS
|
||||
);
|
||||
return c ? (c[0] - o[0]) / o[0] : undefined;
|
||||
}
|
||||
|
||||
async function getChangeBod(marketConfig: MarketConfig): Promise<number> {
|
||||
const from = new Date();
|
||||
from.setUTCHours(0, 0, 0, 0);
|
||||
const fromS = from.getTime() / 1000;
|
||||
const to = new Date();
|
||||
const toS = to.getTime() / 1000;
|
||||
const { t, o, h, l, c, v } = await getOhlcv(
|
||||
marketConfig.name,
|
||||
"1",
|
||||
fromS,
|
||||
toS
|
||||
);
|
||||
// todo double check this
|
||||
return c ? (c[0] - o[o.length - 1]) / o[o.length - 1] : undefined;
|
||||
return marketDataResponse.json();
|
||||
}
|
||||
|
||||
async function getOhlcv(
|
||||
|
@ -456,22 +416,12 @@ async function getOhlcv(
|
|||
const fromSFixed = fromS.toFixed();
|
||||
const toSFixed = toS.toFixed();
|
||||
const historyResponse = await fetch(
|
||||
`https://serum-history.herokuapp.com/tv/history` +
|
||||
`https://event-history-api-candles.herokuapp.com/tv/history` +
|
||||
`?symbol=${market}&resolution=${resolution}&from=${fromSFixed}&to=${toSFixed}`
|
||||
);
|
||||
return historyResponse.json();
|
||||
}
|
||||
|
||||
export async function getVolumeForMarket(
|
||||
marketConfig: MarketConfig
|
||||
): Promise<number> {
|
||||
const perpVolume = await fetch(
|
||||
`https://event-history-api.herokuapp.com/stats/perps/${marketConfig.publicKey.toBase58()}`
|
||||
);
|
||||
const parsedPerpVolume = await perpVolume.json();
|
||||
return Number(parsedPerpVolume?.data?.volume);
|
||||
}
|
||||
|
||||
/// Dtos
|
||||
|
||||
// e.g.
|
||||
|
|
Loading…
Reference in New Issue