UNPKG

@reactivemarkets/switchboard-sdk

Version:
106 lines (105 loc) 3.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toStrategyParameters = exports.toMetrics = exports.toExecVenues = exports.toSide = exports.toDate = exports.toOfferMap = exports.toBidMap = exports.now = void 0; const switchboard_api_1 = require("@reactivemarkets/switchboard-api"); const now = () => { const nanos = Date.now() * 1e6; return BigInt(nanos); }; exports.now = now; const toBidMap = (snapshot) => { const map = new Map(); for (let i = 0; i < snapshot.bidLength(); i++) { const entry = snapshot.bid(i); const price = entry === null || entry === void 0 ? void 0 : entry.price(); const size = entry === null || entry === void 0 ? void 0 : entry.size(); if (price !== undefined && size !== undefined) { map.set(i, [price, size]); } } return map; }; exports.toBidMap = toBidMap; const toOfferMap = (snapshot) => { const map = new Map(); for (let i = 0; i < snapshot.offerLength(); i++) { const entry = snapshot.offer(i); const price = entry === null || entry === void 0 ? void 0 : entry.price(); const size = entry === null || entry === void 0 ? void 0 : entry.size(); if (price !== undefined && size !== undefined) { map.set(i, [price, size]); } } return map; }; exports.toOfferMap = toOfferMap; const million = BigInt(1e6); const toDate = (nanos) => { return new Date((nanos / million).toString()); }; exports.toDate = toDate; const toSide = (side) => { switch (side) { case switchboard_api_1.Side.Buy: return "buy"; case switchboard_api_1.Side.Sell: return "sell"; case switchboard_api_1.Side.None: default: return undefined; } }; exports.toSide = toSide; const toExecVenues = (executionReport) => { const execVenuesLength = executionReport.execVenuesLength(); if (execVenuesLength === 0) { return undefined; } const execVenues = []; for (let i = 0; i < execVenuesLength; i++) { execVenues.push(executionReport.execVenues(i)); } return execVenues; }; exports.toExecVenues = toExecVenues; const toMetrics = (message) => { var _a; const metricsLength = message.metricsLength(); if (metricsLength === 0) { return undefined; } const metrics = []; for (let i = 0; i < metricsLength; i++) { const metric = message.metrics(i); if (metric === null) { continue; } metrics.push({ name: (_a = metric.name()) !== null && _a !== void 0 ? _a : "", value: metric.value(), }); } return metrics; }; exports.toMetrics = toMetrics; const toStrategyParameters = (executionReport) => { var _a, _b; const parametersLength = executionReport.strategyParametersLength(); if (parametersLength === 0) { return undefined; } const strategyParameters = []; for (let i = 0; i < parametersLength; i++) { const parameter = executionReport.strategyParameters(i); if (parameter === null) { continue; } strategyParameters.push({ name: (_a = parameter.name()) !== null && _a !== void 0 ? _a : "", parameterType: parameter.parameterType(), value: (_b = parameter.value()) !== null && _b !== void 0 ? _b : "", }); } return strategyParameters; }; exports.toStrategyParameters = toStrategyParameters;