Skip to content

Commit

Permalink
[FEATURE] Add account listener with tests for connecting and receivin…
Browse files Browse the repository at this point in the history
…g payment callback events
  • Loading branch information
Steven Zeiler committed Jan 3, 2014
1 parent 0e8eb5d commit 492aadc
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 4 deletions.
47 changes: 47 additions & 0 deletions lib/account_listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
WebSocket = require('ws')
EventEmitter = require('events').EventEmitter

function AccountListener(options) {
this.accounts = options.accounts
this.url = options.url || 'wss://s1.ripple.com'
this.webSocket = new WebSocket(this.url)
}

AccountListener.prototype.__proto__ = EventEmitter.prototype

AccountListener.prototype.connect = function() {
try {
this.webSocket.on('open', this.handlers.onOpen)
this.webSocket.on('message', this.handlers.onMessage)
this.webSocket.on('close', this.handlers.onClose)
} catch(e) {
delete this.webSocket
this.webSocket = new WebSocket(this.url)
this.connect()
}
accounts = JSON.stringify(this.accounts)
this.webSocket.send('{"command":"subscribe","id":0,"accounts":'+accounts+'}')
}

AccountListener.prototype.handlers = (function(){
listener = this

function onOpen() {
console.log('onOpen')
}
function onClose(){
console.log('onClose');
}
function onMessage(data){
try {
payment = new Payment(data)
listener.emit('payment', payment.toJSON())
} catch(e) {
console.log(e)
}
}

return { onOpen: onOpen, onClose: onClose, onMessage: onMessage }
})()

module.exports = AccountListener
File renamed without changes.
30 changes: 30 additions & 0 deletions listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@ var WebSocket = require('ws'),
Payment = require('./payment');
var websocketUrl = 'wss://s1.ripple.com';
var rippleAddress = process.env.RIPPLE_ADDRESS;
var request = require('request');
var Coinbase = require('../coinbase-node/lib/coinbase');

var coinbaseClient = new Coinbase.Client({
api_key: process.env.COINBASE_API_KEY
});

function handlePayment(payment) {
if (payment.toIssuer == rippleAddress) {
console.log('issued by this account');
if (payment.toCurrency == 'BTC') {
console.log('is BTC');
if (payment.destinationTag) {
var tag = payment.destinationTag;
console.log("look up the bridge for destination tag", tag);

var url = 'https://www.sendthembitcoins.com/api/ripple_bridges/'+tag;
request.get(url, function(err, resp, body) {
if (!error && response.statusCode == 200) {
var bitcoin_address = body.bitcoin_address;
coinbaseClient.send_money(bitcoin_address, payment.toAmount);
}
})
}
}
} else {
console.log('issued by some other account', payment.toIssuer);
}
}

function onOpen() {
console.log('connection opened');
Expand All @@ -14,6 +43,7 @@ function onMessage(data, flags) {
if (response.type == 'transaction') {
try {
var payment = new Payment(data);
handlePayment(payment);
console.log(payment.toJSON());
} catch(e) {
console.log(e);
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"name": "ripple-account-listener",
"version": "0.0.1",
"dependencies": {
"ws": "~0.4.3"
"ws": "~0.4.3",
"request":""
},
"devDependencies": {
"sinon": ""
}
}
48 changes: 48 additions & 0 deletions test/listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
AccountListener = require('../lib/account_listener.js')
Fixtures = require('./fixtures')
Payment = require('../lib/payment')
sinon = require('sinon')
assert = require('assert')


describe('AccountListener', function(){
it('#connect should listen for account activity', function(){
listener = new AccountListener({
accounts: ['r4EwBWxrx5HxYRyisfGzMto3AT8FZiYdWk']
})

desiredMessage = '{"command":"subscribe","id":0,"accounts":["r4EwBWxrx5HxYRyisfGzMto3AT8FZiYdWk"]}'

listener.webSocket.send = sinon.spy()
listener.connect()
assert(listener.webSocket.send.calledWith(desiredMessage))
})

it('#connect should listen on multiple accounts', function(){
listener = new AccountListener({
accounts: [
'r4EwBWxrx5HxYRyisfGzMto3AT8FZiYdWk',
'rHKueQebtVU9cEamhBbMV8AEViqKjXcBcB'
]
})

desiredMessage = '{"command":"subscribe","id":0,"accounts":["r4EwBWxrx5HxYRyisfGzMto3AT8FZiYdWk","rHKueQebtVU9cEamhBbMV8AEViqKjXcBcB"]}'

listener.webSocket.send = sinon.spy()
listener.connect()
assert(listener.webSocket.send.calledWith(desiredMessage))
})

it('should notify of an incoming payment with an event', function(done){
callback = sinon.spy()
payment = new Payment(Fixtures.IouToSameIou)

listener.on('payment', function(payment){
assert.equal(payment.fromAddress, 'rHKueQebtVU9cEamhBbMV8AEViqKjXcBcB')
assert.equal(payment.toCurrency, 'XAG')
done()
})

listener.webSocket.onmessage(Fixtures.IouToSameIou)
})
})
6 changes: 3 additions & 3 deletions test/inbound_payment_test.js → test/payment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var fixtures = require('./fixtures'),
Payment = require('../payment'),
Payment = require('../lib/payment'),
assert = require('assert')

var transactions = {
Expand Down Expand Up @@ -52,11 +52,11 @@ describe('Payment', function() {
assert.equal(payment.txHash, '10F868E02B370769F6D9FA4C5D56960D72FF98A6D86A7F56785065E100F3E5BA');
});

it('should format an IOU to different IOU payment', function(){
it.skip('should format an IOU to different IOU payment', function(){
throw new Error();
});

it('should format an XRP to IOU payment', function(){
it.skip('should format an XRP to IOU payment', function(){
throw new Error();
});
});
Expand Down

0 comments on commit 492aadc

Please sign in to comment.