forked from zenterp/ripple-account-monitor
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlistener.js
74 lines (66 loc) · 2.02 KB
/
listener.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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');
this.send('{"command":"subscribe","id":0,"accounts":["'+rippleAddress+'"]}');
console.log('listening for activity for account: '+ rippleAddress);
}
function onMessage(data, flags) {
var response = JSON.parse(data);
if (response.type == 'transaction') {
try {
var payment = new Payment(data);
handlePayment(payment);
console.log(payment.toJSON());
} catch(e) {
console.log(e);
}
}
}
function onClose() {
console.log('connection closed');
delete this;
connectWebsocket(websocketUrl);
}
function connectWebsocket(url) {
console.log('connecting to '+url);
try {
var ws = new WebSocket(url);
ws.on('open', onOpen);
ws.on('message', onMessage);
ws.on('close', onClose);
} catch(e) {
console.log('error connecting', e);
console.log('trying again...');
connectWebsocket(url);
}
}
connectWebsocket(websocketUrl);