floBlockchainAPI v2.5.4
- Adding support for 'before' and 'latest' option in readTxs and its derivatives
This commit is contained in:
parent
7bd528d55e
commit
fe33250b47
@ -1,4 +1,4 @@
|
|||||||
(function (EXPORTS) { //floBlockchainAPI v2.5.3
|
(function (EXPORTS) { //floBlockchainAPI v2.5.4
|
||||||
/* FLO Blockchain Operator to send/receive data from blockchain using API calls*/
|
/* FLO Blockchain Operator to send/receive data from blockchain using API calls*/
|
||||||
'use strict';
|
'use strict';
|
||||||
const floBlockchainAPI = EXPORTS;
|
const floBlockchainAPI = EXPORTS;
|
||||||
@ -830,14 +830,19 @@
|
|||||||
let api = `api/addrs/${addr}/txs`;
|
let api = `api/addrs/${addr}/txs`;
|
||||||
//API options
|
//API options
|
||||||
let api_options = [];
|
let api_options = [];
|
||||||
|
if (!isUndefined(options.after) || !isUndefined(options.before)) {
|
||||||
if (!isUndefined(options.after))
|
if (!isUndefined(options.after))
|
||||||
api_options.push(`after=${options.after}`);
|
api_options.push(`after=${options.after}`);
|
||||||
else {
|
if (!isUndefined(options.before))
|
||||||
|
api_options.push(`before=${options.before}`);
|
||||||
|
} else {
|
||||||
if (!isUndefined(options.from))
|
if (!isUndefined(options.from))
|
||||||
api_options.push(`from=${options.from}`);
|
api_options.push(`from=${options.from}`);
|
||||||
if (!isUndefined(options.to))
|
if (!isUndefined(options.to))
|
||||||
api_options.push(`to=${options.to}`);
|
api_options.push(`to=${options.to}`);
|
||||||
}
|
}
|
||||||
|
if (!isUndefined(options.latest))
|
||||||
|
api_options.push('latest');
|
||||||
if (!isUndefined(options.mempool))
|
if (!isUndefined(options.mempool))
|
||||||
api_options.push(`mempool=${options.mempool}`)
|
api_options.push(`mempool=${options.mempool}`)
|
||||||
if (api_options.length)
|
if (api_options.length)
|
||||||
@ -854,7 +859,10 @@
|
|||||||
readTxs(addr, options).then(response => {
|
readTxs(addr, options).then(response => {
|
||||||
if (response.incomplete) {
|
if (response.incomplete) {
|
||||||
let next_options = Object.assign({}, options);
|
let next_options = Object.assign({}, options);
|
||||||
next_options.after = response.lastItem;
|
if (options.latest)
|
||||||
|
next_options.before = response.initItem; //update before for chain query (latest 1st)
|
||||||
|
else
|
||||||
|
next_options.after = response.lastItem; //update after for chain query (oldest 1st)
|
||||||
readAllTxs(addr, next_options).then(r => {
|
readAllTxs(addr, next_options).then(r => {
|
||||||
r.items = r.items.concat(response.items); //latest tx are 1st in array
|
r.items = r.items.concat(response.items); //latest tx are 1st in array
|
||||||
resolve(r);
|
resolve(r);
|
||||||
@ -871,6 +879,7 @@
|
|||||||
/*Read flo Data from txs of given Address
|
/*Read flo Data from txs of given Address
|
||||||
options can be used to filter data
|
options can be used to filter data
|
||||||
after : query after the given txid
|
after : query after the given txid
|
||||||
|
before : query before the given txid
|
||||||
mempool : query mempool tx or not (options same as readAllTx, DEFAULT=false: ignore unconfirmed tx)
|
mempool : query mempool tx or not (options same as readAllTx, DEFAULT=false: ignore unconfirmed tx)
|
||||||
ignoreOld : ignore old txs (deprecated: support for backward compatibility only, cannot be used with 'after')
|
ignoreOld : ignore old txs (deprecated: support for backward compatibility only, cannot be used with 'after')
|
||||||
sentOnly : filters only sent data
|
sentOnly : filters only sent data
|
||||||
@ -887,15 +896,16 @@
|
|||||||
//fetch options
|
//fetch options
|
||||||
let fetch_options = {};
|
let fetch_options = {};
|
||||||
fetch_options.mempool = isUndefined(options.mempool) ? 'false' : options.mempool; //DEFAULT: ignore unconfirmed tx
|
fetch_options.mempool = isUndefined(options.mempool) ? 'false' : options.mempool; //DEFAULT: ignore unconfirmed tx
|
||||||
if (!isUndefined(options.after)) {
|
if (!isUndefined(options.after) || !isUndefined(options.before)) {
|
||||||
if (!isUndefined(options.ignoreOld)) //Backward support
|
if (!isUndefined(options.ignoreOld)) //Backward support
|
||||||
return reject("Invalid options: cannot use after and ignoreOld in same query");
|
return reject("Invalid options: cannot use after/before and ignoreOld in same query");
|
||||||
else
|
//use passed after and/or before options (options remain undefined if not passed)
|
||||||
fetch_options.after = options.after;
|
fetch_options.after = options.after;
|
||||||
|
fetch_options.before = options.before;
|
||||||
}
|
}
|
||||||
readAllTxs(addr, fetch_options).then(response => {
|
readAllTxs(addr, fetch_options).then(response => {
|
||||||
|
|
||||||
if (Number.isInteger(options.ignoreOld)) //backward support, cannot be used with options.after
|
if (Number.isInteger(options.ignoreOld)) //backward support, cannot be used with options.after or options.before
|
||||||
response.items.splice(-options.ignoreOld); //negative to count from end of the array
|
response.items.splice(-options.ignoreOld); //negative to count from end of the array
|
||||||
|
|
||||||
if (typeof options.senders === "string") options.senders = [options.senders];
|
if (typeof options.senders === "string") options.senders = [options.senders];
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user