Ripple交换websocket等效的纹波数据apiv2

时间:2016-08-09 07:29:04

标签: api ripple

我正试图让涟漪得到交流,我找到了this data API并且它正在发挥作用。但出于某些原因,我想使用ripple websocket工具。这个数据API是否有任何websocket等价物?

2 个答案:

答案 0 :(得分:0)

我认为如果你使用" tx_history"在socket中命令但很抱歉告诉你json结果不等于你的特定数据结果。

ajax正在播放涟漪数据apiv2。在ripple中查看结果json formatter进行交换:

  } else if (resp.rows.length) {
    resp.rows[0] = {
      base_currency: resp.rows[0].base_currency,
      base_issuer: resp.rows[0].base_issuer,
      base_amount: resp.rows[0].base_amount,
      counter_amount: resp.rows[0].counter_amount,
      counter_currency: resp.rows[0].counter_currency,
      counter_issuer: resp.rows[0].counter_issuer,
      rate: resp.rows[0].rate,
      executed_time: resp.rows[0].executed_time,
      ledger_index: resp.rows[0].ledger_index,
      buyer: resp.rows[0].buyer,
      seller: resp.rows[0].seller,
      taker: resp.rows[0].taker,
      provider: resp.rows[0].provider,
      autobridged_currency: resp.rows[0].autobridged_currency,
      autobridged_issuer: resp.rows[0].autobridged_issuer,
      offer_sequence: resp.rows[0].offer_sequence,
      tx_type: resp.rows[0].tx_type,
      tx_index: resp.rows[0].tx_index,
      node_index: resp.rows[0].node_index,
      tx_hash: resp.rows[0].tx_hash
    };
  }

  res.csv(resp.rows, filename);
} else {
  res.json({
    result: 'success',
    count: resp.rows.length,
    marker: resp.marker,
    exchanges: resp.rows
  });
}   }

并且只能通过get url访问:

  

路线:' / v2 / exchange / {:base} / {:counter}'

在server.js中绑定:

  

app.get(' / v2 / deals /:base /:counter',routes.getExchanges);

并且最后提示这是他们使用hbase的数据库查询:

  

HbaseClient.getExchanges = function(options,callback){
  var base = options.base.currency +' |' +(options.base.issuer ||'');   var counter = options.counter.currency +' |' +(options.counter.issuer   ||'&#39); var table;       var keyBase;       var startRow;       var endRow;

     

var descending;

     

var columns;

     

if(counter.toLowerCase()> base.toLowerCase()){       keyBase = base +' |' +柜台;

     

} else {       keyBase = counter +' |' +基地;       options.invert = true; }

     

if(!options.interval){       table =' exchange&#39 ;;       descending = options.descending?真假;       options.unreduced = true;

//only need certain columns
if (options.reduce) {
  columns = [
    'd:base_amount',
    'd:counter_amount',
    'd:rate',
    'f:executed_time',
    'f:buyer',
    'f:seller',
    'f:taker'
  ];
}
     

} else if(exchangeIntervals.indexOf(options.interval)!== -1){       keyBase = options.interval +' |' + keyBase;       descending = options.descending?真假;       table =' agg_exchanges';

     

} else {       回调('无效区间:' + options.interval);       返回; }

     

startRow = keyBase +' |' + options.start.hbaseFormatStartRow();
  endRow = keyBase +' |' + options.end.hbaseFormatStopRow();

     

if(options.autobridged){       options.filterstring =" DependentColumnFilter(' f',' autobridged_currency')&#34 ;;       if(columns){         columns.push(' F:autobridged_currency&#39);       }}

     

this.getScanWithMarker(this,{       表:表,       startRow:startRow,       stopRow:endRow,       marker:options.marker,       limit:options.limit,       下降:下降,       列:列,       filterString:options.filterstring},function(err,resp){

if (!resp) {
  resp = {rows: []};
}

if (!resp.rows) {
  resp.rows = [];
}

if (options.reduce && options.unreduced) {
  if (descending) {
    resp.rows.reverse();
  }

  resp.reduced = reduce(resp.rows);
} else if (table === 'exchanges') {
  resp.rows = formatExchanges(resp.rows);
} else {
  resp.rows = formatAggregates(resp.rows);
}

callback(err, resp);   });

/ ** * formatExchanges * /

  

function formatExchanges(rows){       rows.forEach(function(row){         var key = row.rowkey.split(' |');

  delete row.base_issuer;
  delete row.base_currency;
  delete row.counter_issuer;
  delete row.counter_currency;

  row.base_amount    = parseFloat(row.base_amount);
  row.counter_amount = parseFloat(row.counter_amount);
  row.rate           = parseFloat(row.rate);
  row.offer_sequence = Number(row.offer_sequence || 0);
  row.ledger_index   = Number(row.ledger_index);
  row.tx_index       = Number(key[6]);
  row.node_index     = Number(key[7]);
  row.time           = utils.unformatTime(key[4]).unix();
});

if (options.invert) {
  rows = invertPair(rows);
}

return rows;   }

/ ** * formatAggregates * /

  

function formatAggregates(rows){       rows.forEach(function(row){         var key = row.rowkey.split(' |');         row.base_volume = parseFloat(row.base_volume),         row.counter_volume = parseFloat(row.counter_volume),         row.buy_volume = parseFloat(row.buy_volume),         row.count = Number(row.count);         row.open = parseFloat(row.open);         row.high = parseFloat(row.high);         row.low = parseFloat(row.low);         row.close = parseFloat(row.close);         row.vwap = parseFloat(row.vwap);         row.close_time = Number(row.close_time);         row.open_time = Number(row.open_time);       });

if (options.invert) {
  rows = invertPair(rows);
}

return rows;   }

/ ** *如果基本/计数器键被反转,我们需要交换*结果中的一些值* /

  

function invertPair(rows){       var swap;       var i;

if (options.unreduced) {

  for (i=0; i<rows.length; i++) {
    rows[i].rate = 1/rows[i].rate;

    //swap base and counter vol
    swap = rows[i].base_amount;
    rows[i].base_amount    = rows[i].counter_amount;
    rows[i].counter_amount = swap;

    //swap buyer and seller
    swap = rows[i].buyer;
    rows[i].buyer  = rows[i].seller;
    rows[i].seller = swap;
  }

} else {

  for (i=0; i<rows.length; i++) {

    //swap base and counter vol
    swap = rows[i].base_volume;
    rows[i].base_volume    = rows[i].counter_volume;
    rows[i].counter_volume = swap;

    //swap high and low
    swap = 1/rows[i].high;
    rows[i].high = 1/rows[i].low;
    rows[i].low  = swap;

    //invert open, close, vwap
    rows[i].open  = 1/rows[i].open;
    rows[i].close = 1/rows[i].close;
    rows[i].vwap  = 1/rows[i].vwap;

    //invert buy_volume
    rows[i].buy_volume /= rows[i].vwap;
  }
}

return rows;   }

/ ** *减少*减少所有行* /

  

function reduce(rows){

var buyVolume = 0;
var reduced = {
  open: 0,
  high: 0,
  low: Infinity,
  close: 0,
  base_volume: 0,
  counter_volume: 0,
  buy_volume: 0,
  count: 0,
  open_time: 0,
  close_time: 0
};

rows = formatExchanges(rows);
// filter out small XRP amounts
  

rows = rows.filter(function(row){         if(options.base.currency ===&#39; XRP&amp;&amp;&amp; row.base_amount&lt; 0.0005){           返回false;         } else if(options.counter.currency ===&#39; XRP&amp;&amp;&amp; row.counter_amount&lt; 0.0005){           返回false;         } else {           返回true;         }       });

if (rows.length) {
  reduced.open_time  = moment.unix(rows[0].time).utc().format();
  reduced.close_time = moment.unix(rows[rows.length-1].time).utc().format();

  reduced.open  = rows[0].rate;
  reduced.close = rows[rows.length -1].rate;
  reduced.count = rows.length;

} else {
  reduced.low = 0;
  return reduced;
}

rows.forEach(function(row) {
  reduced.base_volume    += row.base_amount;
  reduced.counter_volume += row.counter_amount;

  if (row.rate < reduced.low)  reduced.low  = row.rate;
  if (row.rate > reduced.high) reduced.high = row.rate;

  if (row.buyer === row.taker) {
    reduced.buy_volume += row.base_amount;
  }
});


reduced.vwap = reduced.counter_volume / reduced.base_volume;
return reduced;   } };

答案 1 :(得分:0)

也许您应该创建一个自定义websocket,使您的RPC调用升级到1.1 http协议(ws)。

nodejs中你可以简单地

// for http
var http = require('http');

// for websocket
var ws = require("nodejs-websocket")

var options = {
  host: 'URL-RPC-HERE',
  port: '80',
  method: 'POST',
  headers: {
   'Content-Type': 'application/x-www-form-urlencoded',
   'Content-Length': post_data.length
  }
};

var req = http.request(options, function(res) {
  // after getting the response wich is the <res>
  // we can upgrade it to ws
  upToWebsocket(res);
});

//Upgrade to websocket
var upToWebsocket = function(json) {
  var server = ws.createServer(function (conn) {
    conn.on("json", function (str) {
      conn.sendText(str.toUpperCase()+"!!!")
  })

    conn.on("close", function (code, reason) {
      console.log("Connection closed")
    })
  }).listen(8001)
}

如果你在服务器上运行Rippled,这也无济于事,因为没有支持交换API的RPC或WS。