Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

XRP Ledger Orderbook map/reduce

node v8.17.0
version: 5.0.0
endpointsharetweet
const XRPLClient = require('rippled-ws-client') // Added: respect "owner_funds", offer may be > funds // (only the highest-ranked offer includes this field, so: keep per-account balance looping // through the offers) // Same: taker_gets_funded / taker_pays_funded const precision = 4 const currency = { // issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', // Bitstamp USD issuer: 'rhub8VRN55s94qWKDv6jmDy1pUykJzF3wq', // Gatehub USD currency: 'EUR' } new XRPLClient('wss://rippled.xrptipbot.com').then(connection => { console.log(`Connected. Fetching orderbook for ${currency.currency}.${currency.issuer}\n - Precision: ${precision}\n`) Promise.all([ 'Gets', 'Pays' ].map(book => { return new Promise((resolve, reject) => { connection.send({ command: 'book_offers', limit: 1000, [ 'taker_' + book.toLowerCase() ]: { currency: 'XRP' }, [ 'taker_' + (book === 'Gets' ? 'pays' : 'gets') ]: currency }).then(result => { let offers = result.offers.map(r => { let xrp = parseInt(r[ 'taker_' + book.toLowerCase() + '_funded' ] || r[ 'Taker' + book ]) / 1000000 let currField = typeof r[ 'taker_' + (book === 'Gets' ? 'pays' : 'gets') + '_funded' ] !== 'undefined' ? r[ 'taker_' + (book === 'Gets' ? 'pays' : 'gets') + '_funded' ] : r[ 'Taker' + (book === 'Gets' ? 'Pays' : 'Gets') ] let curr = parseFloat(currField.value) if (typeof r.owner_funds !== 'undefined') { let ownerFunds = parseFloat(r.owner_funds) if (book === 'Gets') { // XRP ownerFunds = ownerFunds / 1000000 // console.log(`${book} xrp ${xrp} ${ownerFunds}`) xrp = Math.min(xrp, ownerFunds) } else { // console.log(`${book} curr ${curr} ${ownerFunds}`) curr = Math.min(curr, ownerFunds) } } let rate = curr / xrp return { book: book, rate: rate, roundedRate: Math.round(rate * 10 ** precision) / 10 ** precision, value: { currency: curr, xrp: xrp } } }) resolve({ [book.toLowerCase()]: { offers: offers, offerCount: offers.length, startRate: offers[0].roundedRate, book: offers.reduce((a, b) => { let existingAtRate = a.filter(match => { return match.rate === b.roundedRate }) if (existingAtRate.length > 0) { // Increase existing book value a[a.indexOf(existingAtRate[0])].amountXrp += b.value.xrp } else { // New rate a.push({ rate: b.roundedRate, amountXrp: b.value.xrp }) } return a }, []) } }) }).catch(reject) }) })).then(results => { results = results.reduce((a, b) => { return Object.assign(a, { ...b }) }, {}) console.log(`Taker GETS # offers: ${results.gets.offerCount}, starting at: ${results.gets.startRate} ${currency.currency} / XRP\n`, results.gets.book.slice(0, 15)) console.log(`Taker PAYS # offers: ${results.pays.offerCount}, starting at: ${results.pays.startRate} ${currency.currency} / XRP\n`, results.pays.book.slice(0, 15)) connection.close().then(() => { console.log('Closed connection.') }) }) }).catch(console.error)
Loading…

no comments

    sign in to comment