spee.ch/helpers/functions/getAllFreePublicClaims.js

65 lines
2 KiB
JavaScript
Raw Normal View History

const isFreePublicClaim = require('./isFreePublicClaim.js');
const lbryApi = require('../libraries/lbryApi.js');
2017-06-20 01:37:56 +02:00
const logger = require('winston');
function filterForFreePublicClaims (claimsListArray) {
2017-06-20 04:34:34 +02:00
logger.debug('filtering claims list for free, public claims');
if (!claimsListArray) {
return null;
}
const freePublicClaims = claimsListArray.filter(claim => {
if (!claim.value) {
return false;
}
return isFreePublicClaim(claim);
});
return freePublicClaims;
}
function orderTopClaims (claimsListArray) {
2017-06-20 04:34:34 +02:00
logger.debug('ordering the top claims');
2017-06-20 01:37:56 +02:00
claimsListArray.sort((a, b) => {
if (a.amount === b.amount) {
return a.height < b.height;
} else {
2017-06-20 01:37:56 +02:00
return a.amount < b.amount;
}
});
return claimsListArray;
}
module.exports = claimName => {
const deferred = new Promise((resolve, reject) => {
// make a call to the daemon to get the claims list
lbryApi
.getClaimsList(claimName)
.then(({ claims }) => {
const claimsList = claims;
2017-06-20 04:34:34 +02:00
logger.debug(`Number of claims: ${claimsList.length}`);
// return early if no claims were found
if (claimsList.length === 0) {
reject('NO_CLAIMS');
2017-06-20 04:34:34 +02:00
logger.debug('exiting due to lack of claims');
return;
}
// filter the claims to return only free, public claims
const freePublicClaims = filterForFreePublicClaims(claimsList);
// return early if no free, public claims were found
if (!freePublicClaims || freePublicClaims.length === 0) {
reject('NO_FREE_PUBLIC_CLAIMS');
2017-06-20 04:34:34 +02:00
logger.debug('exiting due to lack of free or public claims');
return;
}
// order the claims
const orderedPublicClaims = orderTopClaims(freePublicClaims);
// resolve the promise
resolve(orderedPublicClaims);
})
.catch(error => {
2017-06-20 04:34:34 +02:00
logger.error('error received from lbryApi.getClaimsList', error);
reject(error);
});
});
return deferred;
};