Skip to content

Instantly share code, notes, and snippets.

@bradennapier
Last active May 21, 2018 15:37
Show Gist options
  • Save bradennapier/54bc6d215153792c3071f0ff6e858bfe to your computer and use it in GitHub Desktop.
Save bradennapier/54bc6d215153792c3071f0ff6e858bfe to your computer and use it in GitHub Desktop.
async function Event(req, res, data, callback) {
if (data.query.block) {
const prefix = data.query.block.split('_')[0];
// If the parameter passed in starts with xrb_ or nano_, send them to an account search
if (prefix === 'xrb' || prefix === 'nano') {
res.writeHead(302, { Location: `/${data.params.lang}/explore/account/${data.query.block}` });
} else {
res.writeHead(302, { Location: `/${data.params.lang}/explore/block/${data.query.block}` });
}
return res.end();
callback();
}
const hash = data.params.block; // 'xrb_1ipx847tk8o46pwxt5qjdbncjqcbwcc1rrmqnkztrfjy5k7z4imsrata9est'
if (data.params.tool === 'block') {
data.block = {};
const result = await raiblocks.blocksInfo([hash]);
if (result.error) {
return callback(null);
}
// TODO:
// $timestamp = qq("SELECT date FROM bext WHERE hash = '$hash' LIMIT 1");
// ($timestamp["n"] >= 1) ? ($timestamp = $timestamp["r"][0]["date"]) : ($timestamp = 0);
data.block.content = JSON.parse(result.blocks[hash].contents);
data.block.type = data.block.content.type;
data.block.owner = result.blocks[hash].block_account;
data.block.receptionblock = 'none';
data.block.amount = 0;
data.block.target = '';
data.block.timestamp = ''; // $timestamp;
const hashes = await raiblocks.history(hash, 1);
if (Array.isArray(hashes)) {
if (hashes.history[0].amount) {
data.block.amount = hashes.history[0].amount;
}
if (hashes.history[0].account) {
data.block.target = hashes.history[0].account;
}
}
if (data.block.type === 'send') {
data.block.receptionblock = 'yes';
const pending = await raiblocks.pending(data.block.content.destination, 10); // TODO: PHP_INT_MAX
if (pending && Array.isArray(pending.blocks)) {
pending.blocks.forEach(checkBlock => {
if (checkBlock === hash) {
data.block.receptionblock = 'Not Pocketed (Waiting for recipient wallet to accept)';
return callback(null);
}
});
}
}
callback(null);
} else if (data.params.tool === 'account') {
const account = {
account: hash,
txcount: 0,
delegators: [],
pending_history: [],
};
const result = await raiblocks.validateAccountNumber(hash);
if (result && result.valid === '1') {
// get account information
const accountInfo = await raiblocks.accountInfo(hash);
if (accountInfo.block_count) {
account.txcount = accountInfo.block_count;
}
account.accountInfo = accountInfo;
const blockCount = await raiblocks.accountBlockCount(account.account);
if (blockCount.block_count) {
account.txcount = blockCount.block_count;
}
// get history
if (account.accountInfo.frontier) {
const result = await raiblocks.history(account.accountInfo.frontier, maxtx);
if (result && result.history && result.history.length > 0) {
account.history = result.history;
account.history.forEach(tx => {
tx.amount /= RaiBlocks.constants.RAIN;
});
}
if (account.history.length > 0) {
const representative = await raiblocks.accountRepresentative(hash);
if (representative) {
account.lastrep = representative.representative;
}
}
const balances = await raiblocks.accountBalance(hash);
account.balance = Math.floor(balances.balance / RaiBlocks.constants.RAIN);
account.pending = Math.floor(balances.pending / RaiBlocks.constants.RAIN);
// Count pending tx
if (account.pending !== 0) {
return getPendingHistory(hash);
}
}
}
// .then(pendingHistory => {
// account.pending_history = pendingHistory;
// // get weight
// return raiblocks.accountWeight(hash);
// })
// .then(weight => {
// account.weight = Math.floor(weight.weight / RaiBlocks.constants.RAIN);
// account.delegators_count = 0; // $delegators_count;
// account.delegators = []; // $delegators['r'];
// data.account = account;
// return callback(null);
// })
// .catch(err => {
// account.delegators_count = 0; // $delegators_count;
// account.delegators = []; // $delegators['r'];
// account.history = [];
// data.account = account;
// return callback(null);
// });
} else {
callback();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment