Skip to content

Commit

Permalink
[FEATURE] Add listener for address at RIPPLE_ADDRESS
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Zeiler committed Dec 31, 2013
0 parents commit 877b054
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
listener node listener.js
44 changes: 44 additions & 0 deletions listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var WebSocket = require('ws'),
Payment = require('./payment');
var websocketUrl = 'wss://s1.ripple.com';
var rippleAddress = process.env.RIPPLE_ADDRESS;

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);
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);
72 changes: 72 additions & 0 deletions payment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var gatewayHotWalletAddress = process.env.RIPPLE_ADDRESS;

function Ledger(hash, index) {
this.hash = hash;
this.index = index;
this.transactions = [];
}

function Payment(msg) {
var message = JSON.parse(msg);
this.result = message.engine_result;
this.validated = message.validated;

function parseXrpAmount(drops) {
return parseFloat(drops) / 100000;
}

if ((typeof message.transaction.Amount) == 'string') {
this.toAmount = parseXrpAmount(message.transaction.Amount);
this.toCurrency = 'XRP';
} else {
this.toAmount = message.transaction.Amount.value;
this.toCurrency = message.transaction.Amount.currency;
}

if (!!message.transaction.SendMax) {
this.fromAmount = message.transaction.SendMax.value;
this.fromCurrency = message.transaction.SendMax.currency;
} else {
this.fromAmount = parseXrpAmount(message.transaction.Amount);
this.fromCurrency = 'XRP';
}

this.toAddress = message.transaction.Destination;
this.fromAddress = message.transaction.Account;

this.txState = message.meta.TransactionResult;
this.txHash = message.transaction.hash;
this.destinationTag = message.transaction.DestinationTag;

if (this.result != 'tesSUCCESS') {
throw new Error("Result not tesSUCCESS");
}

if (!this.validated) {
throw new Error("Payment not validated");
}
}

Payment.prototype.toJSON = function() {
return {
validated: this.validated,
txState: this.txState,
txHash: this.txHash,
toCurrency: this.toCurrency,
fromCurrency: this.fromCurrency,
toAmount: this.toAmount,
fromAmount: this.fromAmount,
toAddress: this.toAddress,
fromAddress: this.fromAddress,
destinationTag: this.destinationTag
}
}

Payment.parseFromMessage = function(message) {
if (!message.validated) { return false }
if (message.engine_result != 'tesSUCCESS') { return false }
if (message.transaction.PaymentType != 'Payment') { return false }
return new Payment(message);
}

module.exports = Payment;

0 comments on commit 877b054

Please sign in to comment.