floBlockchainAPI v2.5.6
- All search query (get method) now uses URLSearchParams to construct the string - renamed all lastKey property to lastItem to match flosight response
This commit is contained in:
parent
704def4db1
commit
9e10a02dc8
@ -1,4 +1,4 @@
|
|||||||
(function (EXPORTS) { //floBlockchainAPI v2.5.5a
|
(function (EXPORTS) { //floBlockchainAPI v2.5.6
|
||||||
/* 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;
|
||||||
@ -16,6 +16,7 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
const SATOSHI_IN_BTC = 1e8;
|
const SATOSHI_IN_BTC = 1e8;
|
||||||
|
const isUndefined = val => typeof val === 'undefined';
|
||||||
|
|
||||||
const util = floBlockchainAPI.util = {};
|
const util = floBlockchainAPI.util = {};
|
||||||
|
|
||||||
@ -111,9 +112,11 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
//Promised function to get data from API
|
//Promised function to get data from API
|
||||||
const promisedAPI = floBlockchainAPI.promisedAPI = floBlockchainAPI.fetch = function (apicall) {
|
const promisedAPI = floBlockchainAPI.promisedAPI = floBlockchainAPI.fetch = function (apicall, query_params = undefined) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
//console.log(apicall);
|
if (!isUndefined(query_params))
|
||||||
|
apicall += '?' + new URLSearchParams(JSON.parse(JSON.stringify(query_params))).toString();
|
||||||
|
//console.debug(apicall);
|
||||||
fetch_api(apicall)
|
fetch_api(apicall)
|
||||||
.then(result => resolve(result))
|
.then(result => resolve(result))
|
||||||
.catch(error => reject(error));
|
.catch(error => reject(error));
|
||||||
@ -123,13 +126,13 @@
|
|||||||
//Get balance for the given Address
|
//Get balance for the given Address
|
||||||
const getBalance = floBlockchainAPI.getBalance = function (addr, after = null) {
|
const getBalance = floBlockchainAPI.getBalance = function (addr, after = null) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let api = `api/addr/${addr}/balance`;
|
let api = `api/addr/${addr}/balance`, query_params = {};
|
||||||
if (after) {
|
if (after) {
|
||||||
if (typeof after === 'string' && /^[0-9a-z]{64}$/i.test(after))
|
if (typeof after === 'string' && /^[0-9a-z]{64}$/i.test(after))
|
||||||
api += '?after=' + after;
|
query_params.after = after;
|
||||||
else return reject("Invalid 'after' parameter");
|
else return reject("Invalid 'after' parameter");
|
||||||
}
|
}
|
||||||
promisedAPI(api).then(result => {
|
promisedAPI(api, query_params).then(result => {
|
||||||
if (typeof result === 'object' && result.lastItem) {
|
if (typeof result === 'object' && result.lastItem) {
|
||||||
getBalance(addr, result.lastItem)
|
getBalance(addr, result.lastItem)
|
||||||
.then(r => resolve(util.toFixed(r + result.data)))
|
.then(r => resolve(util.toFixed(r + result.data)))
|
||||||
@ -822,32 +825,28 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const isUndefined = val => typeof val === 'undefined';
|
|
||||||
|
|
||||||
//Read Txs of Address between from and to
|
//Read Txs of Address between from and to
|
||||||
const readTxs = floBlockchainAPI.readTxs = function (addr, options = {}) {
|
const readTxs = floBlockchainAPI.readTxs = function (addr, options = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let api = `api/addrs/${addr}/txs`;
|
let api = `api/addrs/${addr}/txs`;
|
||||||
//API options
|
//API options
|
||||||
let api_options = [];
|
let query_params = {};
|
||||||
if (!isUndefined(options.after) || !isUndefined(options.before)) {
|
if (!isUndefined(options.after) || !isUndefined(options.before)) {
|
||||||
if (!isUndefined(options.after))
|
if (!isUndefined(options.after))
|
||||||
api_options.push(`after=${options.after}`);
|
query_params.after = options.after;
|
||||||
if (!isUndefined(options.before))
|
if (!isUndefined(options.before))
|
||||||
api_options.push(`before=${options.before}`);
|
query_params.before = options.before;
|
||||||
} else {
|
} else {
|
||||||
if (!isUndefined(options.from))
|
if (!isUndefined(options.from))
|
||||||
api_options.push(`from=${options.from}`);
|
query_params.from = options.from;
|
||||||
if (!isUndefined(options.to))
|
if (!isUndefined(options.to))
|
||||||
api_options.push(`to=${options.to}`);
|
query_params.to = options.to;
|
||||||
}
|
}
|
||||||
if (!isUndefined(options.latest))
|
if (!isUndefined(options.latest))
|
||||||
api_options.push('latest');
|
query_params.latest = latest;
|
||||||
if (!isUndefined(options.mempool))
|
if (!isUndefined(options.mempool))
|
||||||
api_options.push(`mempool=${options.mempool}`)
|
query_params.mempool = options.mempool;
|
||||||
if (api_options.length)
|
promisedAPI(api, query_params)
|
||||||
api += "?" + api_options.join('&');
|
|
||||||
promisedAPI(api)
|
|
||||||
.then(response => resolve(response))
|
.then(response => resolve(response))
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
});
|
});
|
||||||
@ -869,7 +868,7 @@
|
|||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
} else
|
} else
|
||||||
resolve({
|
resolve({
|
||||||
lastKey: response.lastItem || options.after,
|
lastItem: response.lastItem || options.after,
|
||||||
items: response.items
|
items: response.items
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
@ -894,16 +893,16 @@
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
//fetch options
|
//fetch options
|
||||||
let fetch_options = {};
|
let query_options = {};
|
||||||
fetch_options.mempool = isUndefined(options.mempool) ? false : options.mempool; //DEFAULT: ignore unconfirmed tx
|
query_options.mempool = isUndefined(options.mempool) ? false : options.mempool; //DEFAULT: ignore unconfirmed tx
|
||||||
if (!isUndefined(options.after) || !isUndefined(options.before)) {
|
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/before and ignoreOld in same query");
|
return reject("Invalid options: cannot use after/before and ignoreOld in same query");
|
||||||
//use passed after and/or before options (options remain undefined if not passed)
|
//use passed after and/or before options (options remain undefined if not passed)
|
||||||
fetch_options.after = options.after;
|
query_options.after = options.after;
|
||||||
fetch_options.before = options.before;
|
query_options.before = options.before;
|
||||||
}
|
}
|
||||||
readAllTxs(addr, fetch_options).then(response => {
|
readAllTxs(addr, query_options).then(response => {
|
||||||
|
|
||||||
if (Number.isInteger(options.ignoreOld)) //backward support, cannot be used with options.after or options.before
|
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
|
||||||
@ -950,7 +949,7 @@
|
|||||||
data: tx.floData
|
data: tx.floData
|
||||||
} : tx.floData);
|
} : tx.floData);
|
||||||
|
|
||||||
const result = { lastKey: response.lastKey };
|
const result = { lastItem: response.lastItem };
|
||||||
if (options.tx)
|
if (options.tx)
|
||||||
result.items = filteredData;
|
result.items = filteredData;
|
||||||
else
|
else
|
||||||
@ -976,12 +975,12 @@
|
|||||||
const getLatestData = floBlockchainAPI.getLatestData = function (addr, caseFn, options = {}) {
|
const getLatestData = floBlockchainAPI.getLatestData = function (addr, caseFn, options = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
//fetch options
|
//fetch options
|
||||||
let fetch_options = { latest: true };
|
let query_options = { latest: true };
|
||||||
fetch_options.mempool = isUndefined(options.mempool) ? false : options.mempool; //DEFAULT: ignore unconfirmed tx
|
query_options.mempool = isUndefined(options.mempool) ? false : options.mempool; //DEFAULT: ignore unconfirmed tx
|
||||||
if (!isUndefined(options.after)) fetch_options.after = options.after;
|
if (!isUndefined(options.after)) query_options.after = options.after;
|
||||||
if (!isUndefined(options.before)) fetch_options.before = options.before;
|
if (!isUndefined(options.before)) query_options.before = options.before;
|
||||||
|
|
||||||
readTxs(addr, fetch_options).then(response => {
|
readTxs(addr, query_options).then(response => {
|
||||||
|
|
||||||
if (typeof options.senders === "string") options.senders = [options.senders];
|
if (typeof options.senders === "string") options.senders = [options.senders];
|
||||||
if (typeof options.receivers === "string") options.receivers = [options.receivers];
|
if (typeof options.receivers === "string") options.receivers = [options.receivers];
|
||||||
@ -1005,7 +1004,7 @@
|
|||||||
|
|
||||||
//if item found, then resolve the result
|
//if item found, then resolve the result
|
||||||
if (!isUndefined(item)) {
|
if (!isUndefined(item)) {
|
||||||
const result = { lastKey: response.lastItem };
|
const result = { lastItem: response.lastItem };
|
||||||
if (options.tx) {
|
if (options.tx) {
|
||||||
result.item = {
|
result.item = {
|
||||||
txid: tx.txid,
|
txid: tx.txid,
|
||||||
@ -1024,13 +1023,13 @@
|
|||||||
let next_options = Object.assign({}, options);
|
let next_options = Object.assign({}, options);
|
||||||
options.before = response.initItem; //this fn uses latest option, so using before to chain query
|
options.before = response.initItem; //this fn uses latest option, so using before to chain query
|
||||||
getLatestData(addr, caseFn, next_options).then(r => {
|
getLatestData(addr, caseFn, next_options).then(r => {
|
||||||
r.lastKey = response.lastItem; //update last key as it should be the newest tx
|
r.lastItem = response.lastItem; //update last key as it should be the newest tx
|
||||||
resolve(r);
|
resolve(r);
|
||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
}
|
}
|
||||||
//no data match the caseFn, resolve just the lastKey
|
//no data match the caseFn, resolve just the lastItem
|
||||||
else
|
else
|
||||||
resolve({ lastKey: response.lastItem });
|
resolve({ lastItem: response.lastItem });
|
||||||
|
|
||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user