Skip to content

Instantly share code, notes, and snippets.

Created December 15, 2013 11:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/7972157 to your computer and use it in GitHub Desktop.
Save anonymous/7972157 to your computer and use it in GitHub Desktop.
Hedging client for Ripple
HEDGE = node riphedge
all:
npm install ripple-lib
hedge:
-while date; do \
$(HEDGE); \
sleep 100; \
done
clean:
-rm -fr node_modules
var ripple = require("ripple-lib");
var id = process.env.RIPHEDGE_ID;
var key = process.env.RIPHEDGE_KEY;
var west = {
host: "s_west.ripple.com",
port: 443,
secure: true
};
var east = {
host: "s_east.ripple.com",
port: 443,
secure: true
};
var options = {
fee_cushion: 1.5,
servers: [
east,
west
],
trusted: false
};
var remote = new ripple.Remote(options);
var delta, xrp;
var issuer, currency, fiat;
var index, ledger;
var ratio = 0.02;
function start()
{
console.log("Connected");
remote.set_secret(id, key);
wait();
}
function wait()
{
console.log("Wait for ledger to close");
remote.on("ledger_closed", update);
}
function update()
{
remote.removeListener("ledger_closed", update);
remote.request_account_offers(id, index, ledger, check);
}
function check(error, response)
{
var offers;
if (error) {
console.log("Failed to get offers");
return wait();
}
offers = response.offers;
if (!offers.length) {
console.log("No offers");
remote.request_account_info(id, setxrp);
} else if (1 == offers.length) {
var transaction = remote.transaction();
console.log("Cancel obsolete offer");
transaction.offer_cancel(id, offers[0].seq);
transaction.submit(wait);
} else {
console.log("Offers still exist");
finish();
}
}
function setxrp(error, response)
{
if (error) {
console.log("Failed to get balance");
return wait();
}
xrp = parseFloat(response.account_data.Balance);
xrp /= 1e6;
delta = ratio * xrp;
console.log("Ripples", xrp);
remote.request_account_lines(id, index, ledger, setfiat);
}
function setfiat(error, response)
{
var lines, line;
if (error) {
console.log("Failed to get lines");
return wait();
}
lines = response.lines;
if (1 != lines.length) {
console.log("Only single currency supported");
return wait();
}
line = lines[0];
issuer = line.account;
currency = line.currency;
fiat = parseFloat(line.balance);
console.log("Fiat", fiat, currency, issuer);
bid();
}
function bid()
{
var transaction = remote.transaction();
var price = (xrp - 2 * delta) / fiat;
var buy = delta / price;
var sell = delta;
buy = buy.toFixed(6) + currency;
price = price.toFixed(6);
console.log("Bid", buy, "@", price);
buy = ripple.Amount.from_human(buy);
buy.set_issuer(issuer);
sell = delta.toFixed(6) + "XRP";
sell = ripple.Amount.from_human(sell);
transaction.offer_create(id, buy, sell);
transaction.submit(ask);
}
function ask()
{
var transaction = remote.transaction();
var price = (xrp + 2 * delta) / fiat;
var sell = delta / price;
var buy = delta;
sell = sell.toFixed(6) + currency;
price = price.toFixed(6);
console.log("Ask", sell, "@", price);
sell = ripple.Amount.from_human(sell);
sell.set_issuer(issuer);
buy = delta.toFixed(6) + "XRP";
buy = ripple.Amount.from_human(buy);
transaction.offer_create(id, buy, sell);
transaction.set_flags("Sell");
transaction.submit(finish);
}
function finish()
{
console.log("Disconnect");
process.exit();
}
setTimeout(finish, 1e6);
remote.connect(start);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment