send (data) { this.client.send(JSON.stringify(data)) }
write(data) { if (this.closed) { throw closeError } this.client.send(data) }
async close() { console.log('something happened. closing') await this.client.close() this.closed = true }
fetch(addr) { return new Promise((accept) => { var ws = new W3CWebSocket('wss://neotracker.io/graphql', "graphql-ws"); const sendMessage = (() => { ws.send(JSON.stringify({"type":"GQL_START","id":"2","query":{"id":"12","variables":{"hash":addr}},"span":{}})); ws.send(JSON.stringify({"type":"GQL_START","id":"2","query":{"id":"12","variables":{"hash":addr}},"span":{}})); }); ws.onopen = sendMessage; ws.onmessage = (e => { const data = JSON.parse(e.data); if (data.id === "2" && data.type != "GQL_SUBSCRIBE_ERROR" && !data.value.errors) { const mapped = data.value.data.address.coins.edges.map(coin => { return { asset: hashMapper[coin.node.asset.id] || coin.node.asset.symbol, quantity: parseFloat(coin.node.value) }; }) accept(mapped); ws.close(); } }); }); }
async connect(port, ip) { console.log('trying to connect') this.stream = Buffer.alloc(0) this.client = null this.canRead = new Promise((resolve) => { this.resolveRead = resolve }) this.closed = false this.website = this.getWebSocketLink(ip, port) this.client = new WebSocketClient(this.website, 'binary') return new Promise((resolve, reject) => { this.client.onopen = () => { this.receive() resolve(this) } this.client.onerror = reject this.client.onclose = () => { if (this.client.closed) { this.resolveRead(false) this.closed = true } } }) }