summaryrefslogtreecommitdiff
path: root/static/src/assets/trading-in-the-rain/CW.js
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2021-07-31 11:35:39 -0600
committerBrian Picciano <mediocregopher@gmail.com>2021-07-31 11:35:39 -0600
commitf1998c321a4eec6d75b58d84aa8610971bf21979 (patch)
treea90783eb296cc50e1c48433f241624f26b99be27 /static/src/assets/trading-in-the-rain/CW.js
parent03a35dcc38b055f15df160bd300969e3b703d4b1 (diff)
move static files into static sub-dir, refactor nix a bit
Diffstat (limited to 'static/src/assets/trading-in-the-rain/CW.js')
-rw-r--r--static/src/assets/trading-in-the-rain/CW.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/static/src/assets/trading-in-the-rain/CW.js b/static/src/assets/trading-in-the-rain/CW.js
new file mode 100644
index 0000000..043c1a8
--- /dev/null
+++ b/static/src/assets/trading-in-the-rain/CW.js
@@ -0,0 +1,43 @@
+function CW(resource) {
+ this.conn = new WebSocket('wss://stream.cryptowat.ch/connect?apikey=GPDLXH702E1NAD96OSBO');
+ this.conn.binaryType = "arraybuffer";
+
+ this.conn.onopen = () => {
+ console.log("CW websocket connected");
+ if (this.onconnect) this.onconnect();
+ }
+
+ let decoder = new TextDecoder();
+ this.conn.onmessage = (msg) => {
+ let d = JSON.parse(decoder.decode(msg.data));
+
+ // The server will always send an AUTHENTICATED signal when you establish a valid connection
+ // At this point you can subscribe to resources
+ if (d.authenticationResult && d.authenticationResult.status === 'AUTHENTICATED') {
+ if (this.onauth) this.onauth();
+ this.conn.send(JSON.stringify({
+ subscribe: {
+ subscriptions: [
+ {streamSubscription: {resource: resource}},
+ ],
+ }
+ }));
+ return;
+ }
+
+ // Market data comes in a marketUpdate
+ // In this case, we're expecting trades so we look for marketUpdate.tradesUpdate
+ if (!d.marketUpdate || !d.marketUpdate.tradesUpdate) {
+ return;
+ }
+
+ let trades = d.marketUpdate.tradesUpdate.trades;
+ for (let i in trades) {
+ trades[i].price = parseFloat(trades[i].priceStr);
+ trades[i].volume = parseFloat(trades[i].amountStr);
+ }
+ if (this.ontrades) this.ontrades(trades);
+ }
+
+ this.close = () => this.conn.close();
+}