Skip to content

Instantly share code, notes, and snippets.

@alandotcom
Last active December 2, 2015 00:45
Show Gist options
  • Save alandotcom/2d7df8547be351c2c1d0 to your computer and use it in GitHub Desktop.
Save alandotcom/2d7df8547be351c2c1d0 to your computer and use it in GitHub Desktop.
rippleAPI JSON RPC server over HTTP port 3000

Using a simple javascript client

const jayson = require('jayson');
const Promise = require('bluebird');

// create a client
const client = jayson.client.http({
  port: 3000,
  hostname: 'localhost'
});

Promise.promisifyAll(client);
const id = '9EC02E71D47A82964F777408D565B4CF607DBDE9703C509B67F8E690D5B07715';

client.requestAsync('getTransaction', [id])
.then(res => console.log(res));
// => 
{ jsonrpc: '2.0',
  id: 'e466f6c6-9875-46be-8832-1f2a5feedc4c',
  error:
   { code: 99,
     message: 'Server is missing ledger history in the specified range',
     data: { name: 'MissingLedgerHistoryError' } } }
client.requestAsync('getTransactions', 
  ['rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', {binary: true, limit: 5}])
.then(res => console.log(res.result));
// => 
[
 {
   "type": "order",
   "address": "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw",
   "sequence": 10451208,
   "id": "1AEC150110D2C65FB6A3BAC323F234421F95FA496B88C7C8517E50C2E17321B9",
   "specification": {
     "direction": "buy",
     "quantity": {
       "currency": "XRP",
       "value": "465440.337341"
     },
     "totalPrice": {
       "currency": "USD",
       "value": "1950",
       "counterparty": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"
     },
     "expirationTime": "2015-11-25T05:24:49.000Z"
   },
   "outcome": {
     "result": "tesSUCCESS",
     "fee": "0.011",
     "balanceChanges": {
       "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw": [
         {
           "currency": "XRP",
           "value": "-0.011"
         }
       ]
...

Using curl to getFee

$ curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "id":"2", "method":"getFee" }' localhost:3000
> {"jsonrpc":"2.0","id":"2","result":"0.012"}

Using curl to getTransaction with jq

$ curl -X POST -H "Content-Type: application/json" -d \
'{"jsonrpc": "2.0", "id":"2", "method":"getTransactions","params": ["rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",{"binary": true, "limit": 1}] }' \
localhost:3000 \
| jq '.result'

> [
  {
    "type": "order",
    "address": "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw",
    "sequence": 10459164,
    "id": "1E4C965ACF0C738FE425A81ACF8E2AA100C65AEF0F2595BC791CE07DC96E2A7E",
    "specification": {
      "direction": "buy",
      "quantity": {
        "currency": "USD",
        "value": "194",
        "counterparty": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"
      },
      "totalPrice": {
        "currency": "XRP",
        "value": "45735.804427"
      },
      "expirationTime": "2015-11-25T16:07:07.000Z"
    },
    "outcome": {
      "result": "tesSUCCESS",
      "fee": "0.011",
      "balanceChanges": {
        "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw": [
          {
            "currency": "XRP",
            "value": "-0.011"
          }
        ]
      },
      "orderbookChanges": {
        "rfCFLzNJYvvnoGHWQYACmJpTgkLUaugLEw": [
          {
            "direction": "buy",
            "quantity": {
              "currency": "USD",
              "counterparty": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
              "value": "194"
            },
            "totalPrice": {
              "currency": "XRP",
              "value": "45735.804427"
            },
            "sequence": 10459164,
            "status": "created",
            "makerExchangeRate": "0.004241753314072523",
            "expirationTime": "2015-11-25T16:07:07.000Z"
          },
          {
            "direction": "buy",
            "quantity": {
              "currency": "USD",
              "counterparty": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
              "value": "194"
            },
            "totalPrice": {
              "currency": "XRP",
              "value": "45658.7739"
            },
            "sequence": 10459114,
            "status": "cancelled",
            "makerExchangeRate": "0.004248909539815742",
            "expirationTime": "2015-11-25T16:06:03.000Z"
          }
        ]
      },
      "ledgerVersion": 17298616,
      "indexInLedger": 18
    }
  }
]
const _ = require('lodash');
const RippleAPI = require('ripple-lib').RippleAPI;
const jayson = require('jayson');
const Promise = require('bluebird');
const rippleAPI = new RippleAPI({
servers: ['wss://s1.ripple.com']
});
const functions = _.filter(_.keys(RippleAPI.prototype), k => {
return typeof RippleAPI.prototype[k] === 'function'
&& k !== 'connect'
&& k !== 'disconnect'
&& k !== 'constructor'
&& k !== 'RippleAPI'
});
function applyPromiseWithCallback(fnName, callback, args) {
return Promise.try(() => rippleAPI[fnName].apply(rippleAPI, args))
.then(res => callback(null, res))
.catch(err => {
callback({code: 99, message: err.message, data: {name: err.name}});
});
}
const methods = {};
_.forEach(functions, fn => {
methods[fn] = jayson.Method((args, cb) => {
applyPromiseWithCallback(fn, cb, args);
}, {collect: true})
});
const server = jayson.server(methods);
rippleAPI.connect().then(() => {
server.http().listen(3000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment