2017-02-02 23:52:24 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var lbry;
|
|
|
|
var mongo;
|
2017-02-13 21:44:51 +01:00
|
|
|
var slackbot;
|
|
|
|
var channel;
|
2017-02-02 23:52:24 +01:00
|
|
|
var moment = require('moment');
|
2017-06-15 10:12:32 +02:00
|
|
|
var request = require('request');
|
|
|
|
var sleep = require('sleep');
|
2017-02-02 23:52:24 +01:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: init,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-02-13 21:44:51 +01:00
|
|
|
function init(slackbot_, channel_, rpcuser, rpcpassword, mongodburl) {
|
2017-06-12 02:03:31 +02:00
|
|
|
if (lbry) {
|
2017-02-02 23:52:24 +01:00
|
|
|
throw new Error('init was already called once');
|
|
|
|
}
|
|
|
|
|
2017-02-13 21:44:51 +01:00
|
|
|
slackbot = slackbot_;
|
|
|
|
|
|
|
|
channel = channel_;
|
2017-06-12 02:03:31 +02:00
|
|
|
if (!channel_) {
|
2017-02-02 23:52:24 +01:00
|
|
|
console.log('No claims channel, disabling claimbot');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MongoClient = require('mongodb').MongoClient;
|
|
|
|
MongoClient.connect(mongodburl, function (err, db) {
|
2017-06-12 02:03:31 +02:00
|
|
|
if (err) {
|
2017-02-02 23:52:24 +01:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
mongo = db;
|
|
|
|
|
|
|
|
const bitcoin = require('bitcoin');
|
|
|
|
lbry = new bitcoin.Client({
|
|
|
|
host: 'localhost',
|
|
|
|
'port': 9245,
|
|
|
|
'user': rpcuser,
|
|
|
|
'pass': rpcpassword
|
|
|
|
});
|
|
|
|
|
2017-02-13 22:26:01 +01:00
|
|
|
console.log('Activating claimbot');
|
|
|
|
|
2017-02-02 23:52:24 +01:00
|
|
|
setInterval(function () {
|
2017-02-13 21:44:51 +01:00
|
|
|
announceNewClaims();
|
2017-02-02 23:52:24 +01:00
|
|
|
}, 60 * 1000);
|
2017-02-13 21:44:51 +01:00
|
|
|
announceNewClaims();
|
2017-02-02 23:52:24 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-13 21:44:51 +01:00
|
|
|
function announceNewClaims() {
|
2017-02-02 23:52:24 +01:00
|
|
|
|
2017-06-12 02:03:31 +02:00
|
|
|
if (!mongo) {
|
|
|
|
slackPost('Failed to connect to mongo', { icon_emoji: ':exclamation:' });
|
2017-02-02 23:52:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-12 02:03:31 +02:00
|
|
|
if (!lbry) {
|
|
|
|
slackPost('Failed to connect to lbrycrd', { icon_emoji: ':exclamation:' });
|
2017-02-02 23:52:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise.all([getLastBlock(), lbryCall('getinfo')])
|
|
|
|
.then(function ([lastProcessedBlock, currentBlockInfo]) {
|
|
|
|
const currentHeight = currentBlockInfo['blocks'];
|
|
|
|
|
|
|
|
// console.log('Checking for new blocks');
|
|
|
|
|
2017-06-12 02:03:31 +02:00
|
|
|
if (lastProcessedBlock === null) {
|
2017-02-02 23:52:24 +01:00
|
|
|
console.log('First run. Setting last processed block to ' + currentHeight + ' and exiting.');
|
|
|
|
return setLastBlock(currentHeight);
|
|
|
|
}
|
|
|
|
|
2017-02-14 18:50:46 +01:00
|
|
|
const testBlock = false;
|
|
|
|
|
2017-06-12 02:03:31 +02:00
|
|
|
if (testBlock || lastProcessedBlock < currentHeight) {
|
2017-02-14 18:50:46 +01:00
|
|
|
const firstBlockToProcess = testBlock || lastProcessedBlock + 1,
|
|
|
|
lastBlockToProcess = testBlock || currentHeight;
|
2017-02-02 23:52:24 +01:00
|
|
|
|
|
|
|
// console.log('Doing blocks ' + firstBlockToProcess + ' to ' + lastBlockToProcess);
|
2017-02-13 21:44:51 +01:00
|
|
|
return announceClaimsLoop(firstBlockToProcess, lastBlockToProcess, currentHeight);
|
2017-02-02 23:52:24 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2017-06-12 02:03:31 +02:00
|
|
|
slackPost(err.stack, { icon_emoji: ':exclamation:' });
|
2017-02-02 23:52:24 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-13 21:44:51 +01:00
|
|
|
function announceClaimsLoop(block, lastBlock, currentHeight) {
|
2017-02-02 23:52:24 +01:00
|
|
|
// console.log('Doing block ' + block)
|
2017-06-15 10:12:32 +02:00
|
|
|
let claimsFound = 0;
|
2017-02-02 23:52:24 +01:00
|
|
|
return lbryCall('getblockhash', block)
|
|
|
|
.then(function (blockHash) {
|
|
|
|
return lbryCall('getblock', blockHash);
|
|
|
|
})
|
|
|
|
.then(function (blockData) {
|
|
|
|
return Promise.all(blockData['tx'].map(getClaimsForTxid))
|
|
|
|
})
|
|
|
|
.then(function (arrayOfClaimArrays) {
|
|
|
|
const claims = Array.prototype.concat(...arrayOfClaimArrays).filter(function (c) {
|
|
|
|
return !!c;
|
|
|
|
});
|
|
|
|
console.log('Found ' + claims.length + ' claims in ' + block);
|
2017-06-15 10:12:32 +02:00
|
|
|
claimsFound = claims.length;
|
2017-02-02 23:52:24 +01:00
|
|
|
return Promise.all(claims.map(function (claim) {
|
2017-06-15 10:12:32 +02:00
|
|
|
//slack has a rate limit. to avoid hitting it we must have a small delay between each message
|
|
|
|
//if claims were found in this block, then we wait, otherwise we don't
|
|
|
|
if (claimsFound > 0)
|
|
|
|
sleep.msleep(300);
|
2017-02-13 21:44:51 +01:00
|
|
|
return announceClaim(claim, block, currentHeight);
|
2017-02-02 23:52:24 +01:00
|
|
|
}));
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return setLastBlock(block);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
const nextBlock = block + 1;
|
2017-06-12 02:03:31 +02:00
|
|
|
if (nextBlock <= lastBlock) {
|
2017-02-13 21:44:51 +01:00
|
|
|
return announceClaimsLoop(nextBlock, lastBlock, currentHeight);
|
2017-02-02 23:52:24 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-13 21:44:51 +01:00
|
|
|
function announceClaim(claim, claimBlockHeight, currentHeight) {
|
2017-06-12 02:03:31 +02:00
|
|
|
//console.log(claim);
|
2017-02-02 23:52:24 +01:00
|
|
|
console.log('' + claimBlockHeight + ': New claim for ' + claim['name']);
|
2017-06-12 02:03:31 +02:00
|
|
|
var options = {
|
|
|
|
method: 'GET',
|
|
|
|
url: 'https://explorer.lbry.io/api/getclaimbyid/' + claim['claimId']
|
|
|
|
//url: 'http://127.0.0.1:5000/claim_decode/' + claim['name']
|
|
|
|
};
|
|
|
|
|
|
|
|
request(options, function (error, response, body) {
|
|
|
|
if (error) throw new Error(error);
|
|
|
|
try {
|
2017-06-15 10:12:32 +02:00
|
|
|
console.log(body);
|
|
|
|
|
|
|
|
let claimData = null;
|
|
|
|
let channelName = null;
|
|
|
|
try {
|
|
|
|
body = JSON.parse(body);
|
|
|
|
if (body.hasOwnProperty('value') && body.value.hasOwnProperty('stream') && body.value.stream.hasOwnProperty('metadata')) {
|
|
|
|
claimData = body.value.stream.metadata;
|
|
|
|
channelName = (body.hasOwnProperty('channel_name') ? body['channel_name'] : null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
|
2017-06-12 02:03:31 +02:00
|
|
|
return Promise.all([
|
|
|
|
lbryCall('getvalueforname', claim['name']),
|
|
|
|
lbryCall('getclaimsforname', claim['name']),
|
|
|
|
])
|
2017-06-15 01:47:09 +02:00
|
|
|
.then(function ([currentWinningClaim, claimsForName]) {
|
2017-06-12 02:03:31 +02:00
|
|
|
//console.log(JSON.stringify(claimData));
|
2017-06-15 10:12:32 +02:00
|
|
|
let value = null;
|
|
|
|
if (claimData !== null)
|
|
|
|
value = claimData;
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
value = JSON.parse(claim['value']);
|
|
|
|
} catch (e) { }
|
|
|
|
}
|
2017-06-12 02:03:31 +02:00
|
|
|
|
|
|
|
const text = [];
|
|
|
|
|
|
|
|
if (value) {
|
2017-06-15 10:12:32 +02:00
|
|
|
/*if (channelName) {
|
2017-06-12 02:03:31 +02:00
|
|
|
text.push("Channel: lbry://" + channelName);
|
|
|
|
}
|
2017-06-15 10:12:32 +02:00
|
|
|
else*/
|
|
|
|
if (value['author']) {
|
2017-06-12 02:03:31 +02:00
|
|
|
text.push("author: " + value['author']);
|
|
|
|
}
|
|
|
|
if (value['description']) {
|
|
|
|
text.push(value['description']);
|
|
|
|
}
|
|
|
|
// if (value['content_type'])
|
|
|
|
// {
|
|
|
|
// text.push("*Content Type:* " + value['content_type']);
|
|
|
|
// }
|
|
|
|
if (value['nsfw']) {
|
|
|
|
text.push("*Warning: Adult Content*");
|
|
|
|
}
|
2017-06-15 10:12:32 +02:00
|
|
|
|
|
|
|
//"fee":{"currency":"LBC","amount":186,"version":"_0_0_1","address":"bTGoFCakvQXvBrJg1b7FJzombFUu6iRJsk"}
|
2017-06-12 02:03:31 +02:00
|
|
|
if (value['fee']) {
|
|
|
|
const fees = [];
|
2017-06-15 10:12:32 +02:00
|
|
|
text.push("Price: " + value['fee'].amount + " *" + value['fee'].currency + '*');
|
|
|
|
/*for (var key in value['fee']) {
|
2017-06-12 02:03:31 +02:00
|
|
|
fees.push(value['fee'][key]['amount'] + ' ' + key);
|
|
|
|
}
|
2017-06-15 10:12:32 +02:00
|
|
|
text.push(fees.join(', '));*/
|
2017-06-12 02:03:31 +02:00
|
|
|
}
|
|
|
|
}
|
2017-02-02 23:52:24 +01:00
|
|
|
|
2017-06-12 02:03:31 +02:00
|
|
|
if (!claim['is controlling']) {
|
|
|
|
// the following is based on https://lbry.io/faq/claimtrie-implementation
|
|
|
|
const lastTakeoverHeight = claimsForName['nLastTakeoverHeight'],
|
|
|
|
maxDelay = 4032, // 7 days of blocks at 2.5min per block
|
|
|
|
activationDelay = Math.min(maxDelay, Math.floor((claimBlockHeight - lastTakeoverHeight) / 32)),
|
|
|
|
takeoverHeight = claimBlockHeight + activationDelay,
|
|
|
|
secondsPerBlock = 161, // in theory this should be 150, but in practice its closer to 161
|
|
|
|
takeoverTime = Date.now() + ((takeoverHeight - currentHeight) * secondsPerBlock * 1000);
|
2017-02-02 23:52:24 +01:00
|
|
|
|
2017-06-12 02:03:31 +02:00
|
|
|
text.push('Takes effect on approx. *' + moment(takeoverTime, 'x').format('MMMM Do [at] HH:mm [UTC]') + '* (block ' + takeoverHeight + ')');
|
2017-02-14 18:50:46 +01:00
|
|
|
}
|
2017-02-02 23:52:24 +01:00
|
|
|
|
2017-06-12 02:03:31 +02:00
|
|
|
const attachment = {
|
|
|
|
"fallback": "New claim for lbry://" + claim['name'],
|
|
|
|
"color": "#155b4a",
|
|
|
|
// "pretext": "New claim in block " + claimBlockHeight,
|
|
|
|
// "author_name": 'lbry://' + claim['name'],
|
|
|
|
// "author_link": 'lbry://' + claim['name'],
|
|
|
|
// "author_icon": "http://flickr.com/icons/bobby.jpg",
|
|
|
|
"title": "lbry://" + (channelName ? channelName + '/' : '') + claim['name'], //escapeSlackHtml(value['title']),
|
|
|
|
"title_link": "lbry://" + (channelName ? channelName + '/' : '') + claim['name'],
|
|
|
|
"text": escapeSlackHtml(text.join("\n")),
|
|
|
|
// "fields": [],
|
|
|
|
// "image_url": value['nsfw'] ? null : value['thumbnail'],
|
|
|
|
// "thumb_url": (!value || value['nsfw']) ? null : value['thumbnail'],
|
|
|
|
"unfurl_links": false,
|
|
|
|
"unfurl_media": false,
|
|
|
|
"link_names": false,
|
|
|
|
"parse": "none",
|
|
|
|
"footer": "Block " + claimBlockHeight + " • Claim ID " + claim['claimId'],
|
|
|
|
"mrkdwn_in": ['text'],
|
|
|
|
};
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
attachment['fallback'] += (': "' + value['title'] + '" by ' + value['author']);
|
|
|
|
attachment['author_name'] = 'lbry://' + (channelName ? channelName + '/' : '') + claim['name'];
|
|
|
|
attachment['author_link'] = 'lbry://' + (channelName ? channelName + '/' : '') + claim['name'];
|
|
|
|
attachment['title'] = escapeSlackHtml(value['title']);
|
|
|
|
if (!value['nsfw']) {
|
|
|
|
attachment['thumb_url'] = value['thumbnail'];
|
|
|
|
}
|
|
|
|
}
|
2017-02-14 18:50:46 +01:00
|
|
|
|
2017-06-12 02:03:31 +02:00
|
|
|
slackPost('', { icon_emoji: ':bellhop_bell:', attachments: [attachment] });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
});
|
2017-02-02 23:52:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function escapeSlackHtml(txt) {
|
|
|
|
return txt.replace('&', '&').replace('<', '<').replace('>', '>');
|
|
|
|
}
|
|
|
|
|
|
|
|
function getClaimsForTxid(txid) {
|
|
|
|
return lbryCall('getclaimsfortx', txid)
|
|
|
|
.catch(function (err) {
|
|
|
|
// an error here most likely means the transaction is spent,
|
|
|
|
// which also means there are no claims worth looking at
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLastBlock() {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
mongo.collection('claimbot').findOne({}, function (err, obj) {
|
2017-06-12 02:03:31 +02:00
|
|
|
if (err) {
|
2017-02-02 23:52:24 +01:00
|
|
|
reject(err);
|
|
|
|
}
|
2017-06-12 02:03:31 +02:00
|
|
|
else if (!obj) {
|
|
|
|
mongo.collection('claimbot').createIndex({ 'last_block': 1 }, { unique: true });
|
2017-02-02 23:52:24 +01:00
|
|
|
resolve(null);
|
|
|
|
}
|
2017-06-12 02:03:31 +02:00
|
|
|
else {
|
2017-02-02 23:52:24 +01:00
|
|
|
resolve(obj.last_block);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setLastBlock(block) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
mongo.collection('claimbot').findOneAndUpdate(
|
2017-06-12 02:03:31 +02:00
|
|
|
{ 'last_block': { '$exists': true } },
|
|
|
|
{ 'last_block': block },
|
|
|
|
{ 'upsert': true, 'returnOriginal': false },
|
2017-02-02 23:52:24 +01:00
|
|
|
function (err, obj) {
|
2017-06-12 02:03:31 +02:00
|
|
|
if (!err && obj && obj.value.last_block != block) {
|
2017-02-02 23:52:24 +01:00
|
|
|
reject('Last value should be ' + block + ', but it is ' + obj.value.last_block);
|
|
|
|
}
|
2017-06-12 02:03:31 +02:00
|
|
|
else {
|
2017-02-02 23:52:24 +01:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-13 21:44:51 +01:00
|
|
|
function slackPost(text, params) {
|
|
|
|
slackbot.postMessage(channel, text, params).fail(function (value) {
|
2017-02-13 22:26:01 +01:00
|
|
|
console.log('FAILED TO SLACK to ' + channel + '. Text: "' + text + '". Params: ' + JSON.stringify(params) + "\nResponse: " + JSON.stringify(value));
|
2017-02-13 21:44:51 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-02 23:52:24 +01:00
|
|
|
function lbryCall(...args) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
lbry.cmd(...args, function (err, ...response) {
|
2017-06-12 02:03:31 +02:00
|
|
|
if (err) {
|
2017-02-02 23:52:24 +01:00
|
|
|
reject(new Error('JSONRPC call failed. Args: [' + args.join(', ') + ']'));
|
|
|
|
}
|
2017-06-12 02:03:31 +02:00
|
|
|
else {
|
2017-02-02 23:52:24 +01:00
|
|
|
resolve(...response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|