Last active
December 29, 2015 21:59
-
-
Save codedot/7733540 to your computer and use it in GitHub Desktop.
Hedging client for Ripple
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
HEDGE = node riphedge | |
all: | |
npm install ripple-lib | |
hedge: | |
$(HEDGE) | tee hedge.txt | |
grep -q Ask hedge.txt | |
mailx -s "`grep Ripples hedge.txt`" me <hedge.txt | |
loop: | |
-while date; do \ | |
$(MAKE) hedge & pid=$$!; \ | |
sleep 60; \ | |
kill $$pid; \ | |
done | |
clean: | |
-rm -fr *.txt node_modules |
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
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.01; | |
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 iscorrect(offers) | |
{ | |
var first, second; | |
if (2 != offers.length) | |
return false; | |
first = offers[0]; | |
second = offers[1]; | |
if (first.flags == second.flags) | |
return false; | |
return true; | |
} | |
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 (iscorrect(offers)) { | |
console.log("Offers still exist"); | |
finish(); | |
} else { | |
var transaction = remote.transaction(); | |
console.log("Cancel obsolete offer"); | |
transaction.offer_cancel(id, offers[0].seq); | |
transaction.submit(wait); | |
} | |
} | |
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 line; | |
if (error) { | |
console.log("Failed to get lines"); | |
return wait(); | |
} | |
line = response.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(); | |
} | |
remote.connect(start); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment