forked from zenterp/ripple-account-monitor
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add listener for address at RIPPLE_ADDRESS
- Loading branch information
Steven Zeiler
committed
Dec 31, 2013
0 parents
commit 877b054
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
listener node listener.js |
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
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); |
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
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; |