Skip to content

Instantly share code, notes, and snippets.

@edict3d
Last active December 6, 2018 02:21
Show Gist options
  • Save edict3d/4e8abde562983f15440a5dba46bef623 to your computer and use it in GitHub Desktop.
Save edict3d/4e8abde562983f15440a5dba46bef623 to your computer and use it in GitHub Desktop.
/*
* Dependency:
* <script src="https://unpkg.com/dsteem@^0.8.0/dist/dsteem.js"></script>
* Return block from dsteem API given the date/time of the block.
* Uses a block anchor (known value) to extrapolate data.
* dateTime string example: "2018-10-03T00:32:45"
* Overloaded to accept integer block_num.
*/
var client = new dsteem.Client('https://api.steemit.com')
var BLOCKS_PER_SECOND = 3
var ANCHOR_BLOCK = 26471727
var ANCHOR_DATE = new Date('2018-10-03T02:24:54'+'Z')
var recursive_counter = 0
var blocks = []
var block_nums = []
async function getBlock(dateTime){
var block_num = getBlockNum(dateTime)
console.log(block_num);
client.database.call('get_block',
[block_num]).then(function(block){
if(check(block, dateTime)){
blocks.unshift(block)
block_nums.unshift(block_num)
console.log(block);
recursive_counter = 0
findAuthor(block_num, 'atempt')
} else {
ANCHOR_BLOCK = block_num
ANCHOR_DATE = new Date(block.timestamp+'Z')
recursive_counter++
if(recursive_counter < 6)
getBlock(dateTime)
else {
console.log("Block " + dateTime + " not found.")
}
}
})
}
function getBlockNum(dateTime){
/*
* This fuctionion guesses which block we need to retrieve.
* The more missed blocks there have been in between the anchor and
* active date the more inaccurate the guess will be.
* Testing concludes that only 2 wrong guesses are made for requests
* as far back as the first block.
*/
if( !isNaN(dateTime) && !(dateTime instanceof Date)){
return dateTime // Overloaded to accept integer block_num.
}
if( !(dateTime instanceof Date) ){
dateTime += 'Z' // UTC conversion.
var date = new Date(dateTime)
} else {
var date = dateTime
}
var deltaBlocks = (date - ANCHOR_DATE) / 1000 / BLOCKS_PER_SECOND
var block_num = parseInt(ANCHOR_BLOCK + deltaBlocks)
if(block_num < 1) return 1;
return block_num
}
function check(block, dateTime){
/*
* This function checks to see if we got the right block.
* The date of the block must match the date given.
*/
if( !isNaN(dateTime) )
return true
if(block.timestamp === dateTime.slice(0, 19))
return true
else return false
}
function findAuthor(block_num, author){
//console.log('attempting');
//block_num = 26585709
client.database.call('get_ops_in_block',
[block_num, false]).then(function(operations){
console.log(operations);
for(var i = 0; i < operations.length; i++){
var operation = operations[i]
var block_number = operation.block
var op = operation.op
var action = op[0] // vote, curation_reward, comment, etc
var info = op[1] // voter, curator, author, permlink, etc
if( info.author === author
|| info.curator === author
|| info.voter === author
|| info.from === author
|| info.to === author
|| info.account === author ){
console.log('Author match found at index: ' + i);
}
//console.log(info);
}
})
}
//* Testing
//getBlock("2018-10-06T23:27:21")
//getBlock("2018-10-06T10:29:45")
//getBlock("2018-10-06T22:29:57")
//getBlock("2018-09-30T19:08:57") // got: 26405440 searched: 26585709
//getBlock(26585709)
//getBlock("2018-10-07T01:28:03")
//getBlock("2018-09-30T19:08:57")
//getBlock("2018-10-07T00:51:45")
//getBlock("2018-10-06T23:27:18")
getBlock("2018-10-15T09:51:12")
//getBlock("2018-10-06T23:27:21")
//getBlock("2018-10-07T00:51:42")
//getBlock("2018-10-07T00:51:48")
/*
getBlock(2)
getBlock("2016-09-24T16:05:00")
getBlock("2016-03-24T16:05:36")
getBlock("2016-03-24T16:05:00")
getBlock("2016-03-24T16:10:30")
getBlock("2018-10-03T19:45:54") // 26492535
getBlock("2018-10-03T20:58:09") // 26493980
//*/
<script src="https://unpkg.com/dsteem@^0.8.0/dist/dsteem.js"></script>
<!--<script src="GetBlock.js"></script>-->
Author:
<input type="text" id="author" value="edicted">
<button id="inspect_account"> Inspect Account </button><br>
<button id="inspect_history"> Inspect History </button><br>
Permlink:
<input type="text" id="permlink"
value="steem-is-missing-a-huge-opportunity-on-twitch">
<button id="inspect_discussion"> Inspect Discussion </button><br>
<input type="number" id="block_num" value=''>
<button id="inspect_block"> Inspect Block </button><br>
<button id="inspect_operations"> Inspect Block Operations </button><br>
<p id='history'> History populates here. </p>
<script>
var client = new dsteem.Client('https://api.steemit.com')
var author = document.getElementById('author').value
var permlink = document.getElementById('permlink').value
var block_num = parseInt(document.getElementById('block_num').value)
function inspectDiscussion(){
update()
client.database.call('get_content',
[author, permlink]).then(function(discussion){
console.log(discussion)
})
}
function inspectHistory(){
update()
client.database.call('get_account_history',
[author, -1, 100]).then(function(history_results){
console.log(history_results);
var p = document.getElementById('history')
p.innerHTML = ''
for(var i = history_results.length-1; i >= 0; i--){
var operation = history_results[i][1]
p.innerHTML += ' <button id="' + operation.block
+ '" onClick="importBlock(this.id)"'
+ '>' + operation.op[0] + '</button><br>'
}
})
}
function importBlock(block_num){
document.getElementById('block_num').value = parseInt(block_num)
/*
client.database.call('get_block_header',
[block_num]).then(function(header){
console.log(header);
})
//*/
}
function inspectOperations(){
update()
console.log(block_num);
client.database.call('get_ops_in_block',
[block_num, false]).then(function(operations){
console.log(operations);
for(var i = 0; i < operations.length; i++){
var operation = operations[i]
var block_number = operation.block
var op = operation.op
var action = op[0] // vote, curation_reward, comment, etc
var info = op[1] // voter, curator, author, permlink, etc
if( info.author === author
|| info.curator === author
|| info.voter === author
|| info.from === author
|| info.to === author
|| info.account === author ){
console.log('Author match found at index: ' + i);
}
}
})
}
function inspectBlock(){
update()
console.log(block_num);
client.database.call('get_block',
[block_num]).then(function(block){
console.log(block);
})
}
function inspectAccount(){
update()
client.database.call('get_accounts',
[[author]]).then(function(accounts){
console.log(accounts);
})
}
function update(){
// update global variables
author = document.getElementById('author').value
permlink = document.getElementById('permlink').value
block_num = parseInt(document.getElementById('block_num').value)
}
document.getElementById("inspect_discussion").onclick = inspectDiscussion;
document.getElementById("inspect_history").onclick = inspectHistory;
document.getElementById("inspect_operations").onclick = inspectOperations;
document.getElementById("inspect_block").onclick = inspectBlock;
document.getElementById("inspect_account").onclick = inspectAccount;
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment