2017-08-09 02:32:03 +02:00
|
|
|
const isFreeClaim = require('./isFreeClaim.js');
|
2017-08-03 02:13:02 +02:00
|
|
|
const lbryApi = require('../lbryApi.js');
|
2017-06-20 01:37:56 +02:00
|
|
|
const logger = require('winston');
|
2017-06-17 02:48:51 +02:00
|
|
|
|
2017-06-17 22:51:30 +02:00
|
|
|
function filterForFreePublicClaims (claimsListArray) {
|
2017-06-20 04:34:34 +02:00
|
|
|
logger.debug('filtering claims list for free, public claims');
|
2017-06-17 22:51:30 +02:00
|
|
|
if (!claimsListArray) {
|
2017-06-19 18:37:35 +02:00
|
|
|
return null;
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
const freePublicClaims = claimsListArray.filter(claim => {
|
|
|
|
if (!claim.value) {
|
2017-06-19 18:37:35 +02:00
|
|
|
return false;
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
2017-08-09 02:32:03 +02:00
|
|
|
return isFreeClaim(claim);
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
return freePublicClaims;
|
2017-06-17 02:48:51 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 08:33:03 +02:00
|
|
|
function orderClaims (claimsListArray) {
|
|
|
|
logger.debug('ordering the free public claims');
|
2017-06-20 01:37:56 +02:00
|
|
|
claimsListArray.sort((a, b) => {
|
|
|
|
if (a.amount === b.amount) {
|
|
|
|
return a.height < b.height;
|
2017-06-17 22:51:30 +02:00
|
|
|
} else {
|
2017-06-20 01:37:56 +02:00
|
|
|
return a.amount < b.amount;
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
return claimsListArray;
|
2017-06-17 02:48:51 +02:00
|
|
|
}
|
|
|
|
|
2017-07-04 03:27:35 +02:00
|
|
|
module.exports = (claimName) => {
|
2017-08-03 19:44:19 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-06-17 22:51:30 +02:00
|
|
|
// make a call to the daemon to get the claims list
|
|
|
|
lbryApi
|
|
|
|
.getClaimsList(claimName)
|
|
|
|
.then(({ claims }) => {
|
2017-07-01 00:31:23 +02:00
|
|
|
logger.debug(`Number of claims: ${claims.length}`);
|
2017-06-17 22:51:30 +02:00
|
|
|
// return early if no claims were found
|
2017-07-01 00:31:23 +02:00
|
|
|
if (claims.length === 0) {
|
2017-06-20 04:34:34 +02:00
|
|
|
logger.debug('exiting due to lack of claims');
|
2017-07-01 00:31:23 +02:00
|
|
|
resolve(null);
|
2017-06-19 18:37:35 +02:00
|
|
|
return;
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
// filter the claims to return only free, public claims
|
2017-07-01 00:31:23 +02:00
|
|
|
const freePublicClaims = filterForFreePublicClaims(claims);
|
2017-06-17 22:51:30 +02:00
|
|
|
// return early if no free, public claims were found
|
|
|
|
if (!freePublicClaims || freePublicClaims.length === 0) {
|
2017-06-20 04:34:34 +02:00
|
|
|
logger.debug('exiting due to lack of free or public claims');
|
2017-07-01 00:31:23 +02:00
|
|
|
resolve(null);
|
2017-06-19 18:37:35 +02:00
|
|
|
return;
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
// order the claims
|
2017-06-22 08:33:03 +02:00
|
|
|
const orderedPublicClaims = orderClaims(freePublicClaims);
|
2017-06-17 22:51:30 +02:00
|
|
|
// resolve the promise
|
2017-06-19 18:37:35 +02:00
|
|
|
resolve(orderedPublicClaims);
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|