Skip to content

Instantly share code, notes, and snippets.

@wilsonianb
Created September 22, 2014 17:38
Show Gist options
  • Save wilsonianb/068a40e57643b9303b24 to your computer and use it in GitHub Desktop.
Save wilsonianb/068a40e57643b9303b24 to your computer and use it in GitHub Desktop.
Get account balances in XRP for Ripple Trade competition
var fs = require('fs');
var Remote = require('ripple-lib').Remote;
var remote = new Remote({
trusted: true,
local_signing: true,
local_fee: true,
fee_cushion: 1.5,
servers: [
{
host: 's1.ripple.com'
, port: 443
, secure: true
}
]
});
var currencies = new Array();
function sortTraders(a, b) {
return b.balance - a.balance;
}
function updateLeaderboard() {
traders.sort(sortTraders);
console.log(traders);
}
function ConvertCurrencyToXrp(trader, currency, issuer, balance) {
if (!currencies.hasOwnProperty(currency)) {
currencies[currency] = new Array();
}
if (currencies[currency].hasOwnProperty(issuer)) {
trader.balance += currencies[currency][issuer];
} else {
// console.log('currency: ', currency);
// console.log('issuer:', issuer);
remote.requestBookOffers({
'currency':currency,
'issuer': issuer
}, {
'currency':'XRP'
}, function(err, res) {
if (err) {
console.log(err);
} else {
if (!res.hasOwnProperty('offers') || !res.offers.length || !res.offers[0].hasOwnProperty('quality')) {
console.log(trader);
console.log(res);
console.log(currency + issuer);
return;
}
var price = parseFloat(res.offers[0].quality)/1000000;
currencies[currency][issuer] = price;
trader.balance += balance * price;
updateLeaderboard();
}
});
}
}
function GetAccountBalance(trader) {
trader.balance=0;
remote.requestAccountInfo(trader.address, function(err, res){
if (err) {
console.log(err.remote.error);
if (err.remote.error==='tooBusy') {}
} else {
trader.balance += parseInt(res.account_data.Balance)/1000000;
updateLeaderboard();
}
});
remote.requestAccountLines(trader.address, function(err, res){
if (err) {
console.log(err);
} else {
for (var i=0; i<res.lines.length; i++) {
var balance = parseFloat(res.lines[i].balance);
if (balance>0) {
ConvertCurrencyToXrp(trader, res.lines[i].currency,
res.lines[i].account,
parseFloat(res.lines[i].balance));
}
}
}
});
}
var traders;
remote.connect(function() {
var file = fs.readFileSync('traders.json');
traders = JSON.parse(file);
for (var i=0; i<traders.length; i++) {
GetAccountBalance(traders[i]);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment