@open-oracle-origami/origami-js-mill-coinbase
Version:
Open Oracle Origami Node JS Coinbase Mill
36 lines (35 loc) • 1.41 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ws_1 = __importDefault(require("ws"));
exports.default = ({ press, productIds, socketServerUrl = 'wss://ws-feed.exchange.coinbase.com', }) => {
const ws = new ws_1.default(socketServerUrl);
ws.on('open', () => {
const openMessage = {
type: 'subscribe',
product_ids: productIds,
channels: ['ticker'],
};
ws.send(JSON.stringify(openMessage));
});
// TODO: Fix types
ws.on('message', (data) => {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
const d = JSON.parse(data.toString());
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (d.type !== 'ticker')
return;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
else if (d.type === 'error')
return; // TODO: Handle this error, leaving placeholder
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
press(d.product_id, d);
});
// TODO: Handle errors somehow...
ws.on('error', () => { });
return () => {
ws.terminate();
};
};