Created
September 26, 2014 16:55
-
-
Save mpj/58ee72609030adca7020 to your computer and use it in GitHub Desktop.
stellar-stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WebSocket = require 'ws' | |
_ = require 'highland' | |
isNumber = require 'mout/lang/isNumber' | |
listen = (account) -> | |
stream = _() | |
ws = new WebSocket('ws://live.stellar.org:9001'); | |
ws.on 'open', -> | |
payload = JSON.stringify | |
"command" : "subscribe" | |
"streams" : [ "transactions" ] | |
#"accounts" : [ 'gDSSa75HPagWcvQmwH7D51dT5DPmvsKL4q' ] | |
ws.send payload, (error) -> | |
stream.emit('error', error) if error | |
ws.on 'message', (messageString) -> | |
messageObject = JSON.parse messageString | |
#console.log("messageObject", messageObject) | |
if messageObject.status is 'error' | |
stream.emit('error', messageObject) | |
return | |
stream.write messageObject | |
ws.on 'error', (error) -> | |
stream.emit('error', error) | |
stream | |
listen() | |
.filter( (obj) -> obj.transaction and obj.transaction.Amount ) | |
.map( _.get 'transaction' ) | |
.each (tran) -> | |
console.log 'From:', tran.Account | |
console.log 'To:', tran.Destination | |
isStellar = not isNaN(parseInt(tran.Amount)) | |
if isStellar | |
console.log 'Currency: Stellar' | |
console.log 'Amount:', tran.Amount/1000000 | |
else | |
console.log 'Currency:', tran.Amount.currency | |
console.log 'Issuer:', tran.Amount.issuer | |
console.log 'Amount:', tran.Amount.value | |
console.log '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment