-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch_price.function.ts
More file actions
69 lines (59 loc) · 1.94 KB
/
Copy pathfetch_price.function.ts
File metadata and controls
69 lines (59 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { memoize, ttl } from "functools-kit";
import Binance from "node-binance-api";
import { roundTicks } from "../utils/roundTicks";
import { get } from "lodash-es";
const PRICE_UPDATE_MS = 30_000;
const getExchangeInfo = memoize(
([symbol, filterType]) => `${symbol}-${filterType}`,
async (symbol: string, filterType = "LOT_SIZE", binance: Binance) => {
const exchangeInfo = await binance.exchangeInfo();
const lotSizes = Object.values(exchangeInfo.symbols)
.map(({ symbol, filters }) => [
symbol,
filters.find((f: any) => f.filterType === filterType),
])
.reduce<any>((acm, [k, v]) => ({ ...acm, [k]: v }), {});
const { stepSize, tickSize, minQty } = lotSizes[symbol];
return {
stepSize,
tickSize,
minQty,
};
}
);
const getTickerInfo = ttl(
async (binance: Binance) => {
const ticker = await binance.prices();
return ticker;
},
{
timeout: PRICE_UPDATE_MS,
}
);
const formatPrice = async (symbol: string, price: number, binance: Binance) => {
const { tickSize } = await getExchangeInfo(symbol, "PRICE_FILTER", binance);
return roundTicks(price, tickSize);
};
const getBinancePrice = async (symbol: string, binance: Binance) => {
const prices = await binance.prices(symbol);
const price = prices[symbol];
return price;
};
const getTickerPrice = async (symbol: string, binance: Binance) => {
const ticker = await getTickerInfo(binance);
const price = get(ticker, symbol);
return parseFloat(price);
};
const getPrice = async (symbol: string, binance: Binance) => {
const { price } = await binance.avgPrice(symbol);
return parseFloat(price);
};
interface IParams {
symbol: string;
}
export const FETCH_PRICE_FN = async ({ symbol }: IParams, binance: Binance) => {
const averagePrice = await getPrice(symbol, binance);
const tickPrice = await formatPrice(symbol, averagePrice, binance);
return Number(tickPrice);
};
export default FETCH_PRICE_FN;