2019-07-09 09:22:36 +00:00
import express from 'express' ;
import { Request , Response , NextFunction } from 'express' ;
import { getRepository , getConnection } from 'typeorm' ;
import { validationMiddleware } from '../middlewares' ;
import {
InternalServerErrorException ,
NoDataFoundException ,
IntegerValidationException ,
} from '../exceptions' ;
import {
BlockchainBlock ,
BlockchainInput ,
BlockchainOutput ,
BlockchainKernel ,
} from '../entities' ;
import {
BlockchainBlockCreateDto ,
BlockchainBlockSingleViewDto ,
BlockchainBlockUpdateDto ,
BlockchainBlockPaginationDto ,
TotalDifficultyNBlockDto ,
} from '../dtos' ;
import { Paginate } from '../utils' ;
var moment = require ( 'moment' ) ;
export class BlockchainBlockController {
public path = '/blockchain_block' ;
public router = express . Router ( ) ;
constructor ( ) {
this . IntializeRoutes ( ) ;
}
nanoEpic ( fee ) {
if ( fee == 0 ) {
return this . epic ( 0 ) ;
} else if ( fee < 1000 ) {
return fee / 1000000000 ;
} else {
return this . microEpic ( parseFloat ( fee ) / 1000 ) ;
}
}
microEpic ( fee ) {
if ( fee == 0 ) {
return this . epic ( 0 ) ;
} else if ( fee < 1000 ) {
return ( fee * 1000000 ) / 1000000000 ;
} else {
return this . milliEpic ( parseFloat ( fee ) / 1000 ) ;
}
}
milliEpic ( fee ) {
if ( fee == 0 ) {
return this . epic ( 0 ) ;
} else if ( fee < 1000 ) {
return ( fee * 1000 ) / 1000000000 ;
} else {
return this . epic ( parseFloat ( fee ) / 1000 ) ;
}
}
epic ( fee ) {
return fee ;
}
dateDiff ( date2 ) {
var current_date = new Date ( ) ;
// var current_date = new Date("Sat Apr 2 2018 15:04:00 GMT+0530 (IST)");
var enddaydif =
Math . abs ( date2 . getTime ( ) - current_date . getTime ( ) ) /
( 1000 * 60 * 60 * 24 ) ;
var enddayrnd = Math . round ( enddaydif ) ;
// if(enddayrnd < 1) {
var time = this . convertMinsToHrmin (
Math . abs ( date2 . getTime ( ) - current_date . getTime ( ) ) ,
) ;
return time ;
// } else if(enddayrnd == 1) {
// return 'Ends in ' + enddayrnd + ' day';
// }else {
// return 'Ends in ' + enddayrnd + ' days';
// }
}
// convertMinsToHrmin(millseconds) {
// var seconds = Math.floor(millseconds / 1000);
// var days = Math.floor(seconds / 86400);
// var hours = Math.floor((seconds % 86400) / 3600);
// var minutes = Math.floor(((seconds % 86400) % 3600) / 60);
// var dateTimeDurationString = '';
// if ((days > 0) && (hours === 0 && minutes === 0)) dateTimeDurationString += (days > 1) ? (days + ' days ') : (days + ' day ');
// if ((days > 0) && (hours > 0 || minutes > 0)) dateTimeDurationString += (days > 1) ? (days + ' days, ') : (days + ' day, ');
// if ((hours > 0) && (minutes > 0)) dateTimeDurationString += (hours > 1) ? (hours + ' hours, ') : (hours + ' hour, ');
// if ((hours > 0) && (minutes === 0)) dateTimeDurationString += (hours > 1) ? (hours + ' hours ') : (hours + ' hour ');
// if (minutes > 0) dateTimeDurationString += (minutes > 1) ? (minutes + ' minutes ') : (minutes + ' minute ');
// if (seconds > 0) dateTimeDurationString += (seconds > 1) ? (minutes + ' seconds ') : (minutes + ' second ');
// return dateTimeDurationString;
// }
convertMinsToHrmin ( millseconds ) {
var seconds = Math . floor ( millseconds / 1000 ) ;
var days = Math . floor ( seconds / 86400 ) ;
var hours = Math . floor ( ( seconds % 86400 ) / 3600 ) ;
var minutes = Math . floor ( ( ( seconds % 86400 ) % 3600 ) / 60 ) ;
seconds = seconds % 60 ;
var dateTimeDurationString = '' ;
if ( days > 0 && ( hours === 0 && minutes === 0 ) )
dateTimeDurationString += days > 1 ? days + 'd ' : days + 'd ' ;
if ( days > 0 && ( hours > 0 || minutes > 0 ) )
dateTimeDurationString += days > 1 ? days + 'd ' : days + 'd ' ;
if ( hours > 0 && minutes > 0 )
dateTimeDurationString += hours > 1 ? hours + 'h ' : hours + 'h ' ;
if ( hours > 0 && minutes === 0 )
dateTimeDurationString += hours > 1 ? hours + 'h ' : hours + 'h ' ;
if ( minutes > 0 )
dateTimeDurationString += minutes > 1 ? minutes + 'm ' : minutes + 'm ' ;
if ( seconds > 0 )
dateTimeDurationString += seconds > 1 ? seconds + 's ' : seconds + 's ' ;
return dateTimeDurationString ;
}
public IntializeRoutes() {
/ * *
* @swagger
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k :
* post :
* tags :
* - name : BLOCKCHAIN_BLOCK | BLOCKCHAIN_BLOCK CONTROLLER
* summary : create a blockchain_block
* description : create a blockchain_block
* consumes :
* - application / json
* produces :
* - application / json
* parameters :
* - name : BlockchainBlock
* description : create a blockchain_block
* in : body
* required : true
* schema :
* $ref : '#/definitions/BlockchainBlockDto'
* responses :
* 200 :
* description : blockchain_block created successfully
* definitions :
* BlockchainBlockDto :
* description : Dto
* properties :
* Hash :
* type : string
* Version :
* type : integer
* Height :
* type : integer
* Timestamp :
* type : string
* OutputRoot :
* type : string
* RangeProofRoot :
* type : string
* KernelRoot :
* type : string
* Nonce :
* type : string
* TotalDifficulty :
* type : string
* Previous :
* type : string
* TotalKernelOffset :
* type : string
* EdgeBits :
* type : integer
* CuckooSolution :
* type : string
* PrevRoot :
* type : string
* SecondaryScaling :
* type : integer
* /
this . router . post (
` ${ this . path } ` ,
validationMiddleware ( BlockchainBlockCreateDto ) ,
this . BlockchainBlockCreate ,
) ;
/ * *
* @swagger
2019-08-06 13:10:10 +00:00
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k / t o t a l d i f f :
2019-07-09 09:22:36 +00:00
* get :
* tags :
* - name : BLOCKCHAIN_BLOCK | BLOCKCHAIN_BLOCK CONTROLLER
* description : To get Total Difficulty and Number of Blocks
* summary : To get Total Difficulty and Number of Blocks
* consumes :
* - application / json
* produces :
* - application / json
* parameters :
* - name : FromDate
* description : Enter the From date
* in : query
* type : string
* - name : ToDate
* description : Enter the To date
* in : query
* type : string
* - name : Interval
* description : Try to give Intevals such as 1 week / 15 days / 30 days / 60 days / 3 months
* in : query
* type : string
2019-08-06 14:28:31 +00:00
* - name : Type
* description : Enter the Algorithm Type
* in : query
* type : string
2019-08-07 08:09:41 +00:00
* - name : Difftype
* description : Enter the Difficulty Type
* in : query
* type : string
2019-07-09 09:22:36 +00:00
* responses :
* 200 :
2019-08-06 13:10:10 +00:00
* description : Total Difficulty fetched successfully
2019-07-09 09:22:36 +00:00
* /
this . router . get (
2019-08-06 13:10:10 +00:00
` ${ this . path } /totaldiff ` ,
2019-07-09 09:22:36 +00:00
validationMiddleware ( TotalDifficultyNBlockDto , true ) ,
this . TotalDifficultyNBlock ,
) ;
2019-08-06 14:28:31 +00:00
2019-08-06 13:10:10 +00:00
/ * *
* @swagger
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k / b l o c k c o u n t :
* get :
* tags :
* - name : BLOCKCHAIN_BLOCK | BLOCKCHAIN_BLOCK CONTROLLER
* description : To get Total Difficulty and Number of Blocks
* summary : To get Total Difficulty and Number of Blocks
* consumes :
* - application / json
* produces :
* - application / json
* parameters :
* - name : FromDate
* description : Enter the From date
* in : query
* type : string
* - name : ToDate
* description : Enter the To date
* in : query
* type : string
* - name : Interval
* description : Try to give Intevals such as 1 week / 15 days / 30 days / 60 days / 3 months
* in : query
* type : string
* responses :
* 200 :
* description : Total Difficulty fetched successfully
* /
this . router . get (
` ${ this . path } /blockcount ` ,
validationMiddleware ( TotalDifficultyNBlockDto , true ) ,
this . BlockCount ,
) ;
2019-07-09 09:22:36 +00:00
2019-07-29 10:59:06 +00:00
/ * *
* @swagger
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k / s t a c k b l o c k :
* get :
* tags :
* - name : STACK_BLOCK | STACK_BLOCK CONTROLLER
* description : To get Total Difficulty and Number of Blocks
* summary : To get Total Difficulty and Number of Blocks
* consumes :
* - application / json
* produces :
* - application / json
* parameters :
* - name : FromDate
* description : Enter the From date
* in : query
* type : string
* - name : ToDate
* description : Enter the To date
* in : query
* type : string
* - name : Interval
* description : Try to give Intevals such as 1 week / 15 days / 30 days / 60 days / 3 months
* in : query
* type : string
* responses :
* 200 :
* description : Total Difficulty and No . of blocks fetched successfully
* /
this . router . get (
` ${ this . path } /stackblock ` ,
validationMiddleware ( TotalDifficultyNBlockDto , true ) ,
this . StackBlock ,
) ;
2019-07-30 07:43:44 +00:00
/ * *
* @swagger
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k / b l o c k p i e c h a r t :
* get :
* tags :
* - name : STACK_BLOCK | STACK_BLOCK CONTROLLER
* description : To get Total Difficulty and Number of Blocks
* summary : To get Total Difficulty and Number of Blocks
* consumes :
* - application / json
* produces :
* - application / json
* parameters :
* - name : FromDate
* description : Enter the From date
* in : query
* type : string
* - name : ToDate
* description : Enter the To date
* in : query
* type : string
* - name : Interval
* description : Try to give Intevals such as 1 week / 15 days / 30 days / 60 days / 3 months
* in : query
* type : string
* responses :
* 200 :
* description : Total Difficulty and No . of blocks fetched successfully
* /
this . router . get (
` ${ this . path } /blockpiechart ` ,
validationMiddleware ( TotalDifficultyNBlockDto , true ) ,
this . BlockPieChart ,
) ;
2019-08-06 14:28:31 +00:00
2019-07-09 09:22:36 +00:00
/ * *
* @swagger
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k / h a s h r a t e :
* get :
* tags :
* - name : BLOCKCHAIN_BLOCK | BLOCKCHAIN_BLOCK CONTROLLER
* description : To get Hash Rate of AR29 abd AT31
* summary : To get Hash Rate of AR29 abd AT31
* consumes :
* - application / json
* produces :
* - application / json
* parameters :
* - name : FromDate
* description : Enter the From date
* in : query
* type : string
* - name : ToDate
* description : Enter the To date
* in : query
* type : string
* - name : Interval
* description : Try to give Intevals such as 1 week / 15 days / 30 days / 60 days / 3 months
* in : query
* type : string
* responses :
* 200 :
* description : Hash Rate of AR29 abd AT31 fetched successfully
* /
this . router . get (
` ${ this . path } /hashrate ` ,
validationMiddleware ( TotalDifficultyNBlockDto , true ) ,
this . HashRate ,
) ;
/ * *
* @swagger
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k / l a t e s b l o c k d e t a i l s :
* get :
* tags :
* - name : BLOCKCHAIN_BLOCK | BLOCKCHAIN_BLOCK CONTROLLER
* description : To get Latest Block Height and Coin in Existence
* summary : To get Latest Block Height and Coin in Existence
* consumes :
* - application / json
* produces :
* - application / json
* responses :
* 200 :
* description : Total Difficulty and No . of blocks fetched successfully
* /
this . router . get (
` ${ this . path } /latesblockdetails ` ,
validationMiddleware ( TotalDifficultyNBlockDto , true ) ,
this . LatestDifficultyNBlock ,
) ;
/ * *
* @swagger
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k / b l o c k s p e r s e c :
* get :
* tags :
* - name : BLOCKCHAIN_BLOCK | BLOCKCHAIN_BLOCK CONTROLLER
* description : period of blocks generation per unit ( second )
* summary : period of blocks generation per unit ( second )
* consumes :
* - application / json
* produces :
* - application / json
* parameters :
* - name : FromDate
* description : Enter the From date
* in : query
* type : string
* - name : ToDate
* description : Enter the To date
* in : query
* type : string
* - name : Interval
* description : Try to give Intevals such as 1 week / 15 days / 30 days / 60 days / 3 months
* in : query
* type : string
* responses :
* 200 :
* description : period of blocks generation per second fetched Successfully
* /
this . router . get (
` ${ this . path } /blockspersec ` ,
validationMiddleware ( TotalDifficultyNBlockDto , true ) ,
this . BlockchainBlockPerSecond ,
) ;
/ * *
* @swagger
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k / s u p p l y g r o w t h :
* get :
* tags :
* - name : BLOCKCHAIN_BLOCK | BLOCKCHAIN_BLOCK CONTROLLER
* description : Supply growth
* summary : Supply growth
* consumes :
* - application / json
* produces :
* - application / json
* parameters :
* - name : FromDate
* description : Enter the From date
* in : query
* type : string
* - name : ToDate
* description : Enter the To date
* in : query
* type : string
* - name : Interval
* description : Try to give Intevals such as 1 week / 15 days / 30 days / 60 days / 3 months
* in : query
* type : string
* responses :
* 200 :
* description : period of blocks generation per second fetched Successfully
* /
this . router . get (
` ${ this . path } /supplygrowth ` ,
validationMiddleware ( TotalDifficultyNBlockDto , true ) ,
this . SupplyGrowth ,
) ;
/ * *
* @swagger
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k / b l o c k m i n e d c h a r t :
* get :
* tags :
* - name : BLOCKCHAIN_BLOCK | BLOCKCHAIN_BLOCK CONTROLLER
* description : To get Total Difficulty and Number of Blocks
* summary : To get Block Mined
* consumes :
* - application / json
* produces :
* - application / json
* parameters :
* - name : FromDate
* description : Enter the From date
* in : query
* type : string
* - name : ToDate
* description : Enter the To date
* in : query
* type : string
* - name : Interval
* description : Try to give Intevals such as 1 week / 15 days / 30 days / 60 days / 3 months
* in : query
* type : string
* responses :
* 200 :
* description : Block Mined chart data fetched successfully
* /
this . router . get (
` ${ this . path } /blockminedchart ` ,
validationMiddleware ( TotalDifficultyNBlockDto , true ) ,
this . BlockMineChart ,
) ;
/ * *
* @swagger
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k / l i s t :
* get :
* tags :
* - name : BLOCKCHAIN_BLOCK | BLOCKCHAIN_BLOCK CONTROLLER
* description : pagination blockchain_block
* summary : pagination blockchain_block
* consumes :
* - application / json
* produces :
* - application / json
* parameters :
* - name : CurrentPage
* description : current page specification
* in : query
* type : integer
* required : true
* - name : PageSize
* description : page size specification
* in : query
* type : integer
* required : true
* responses :
* 200 :
* description : blockchain_block list fetched successfully
* /
this . router . get (
` ${ this . path } /list ` ,
validationMiddleware ( BlockchainBlockPaginationDto ) ,
this . BlockchainBlockPagination ,
) ;
/ * *
* @swagger
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k / { h a s h } :
* get :
* tags :
* - name : BLOCKCHAIN_BLOCK | BLOCKCHAIN_BLOCK CONTROLLER
* summary : get single blockchain_block
* description : get single blockchain_block
* consumes :
* - application / json
* produces :
* - application / json
* parameters :
* - name : hash
* in : path
* description : blockchain_block hash
* required : true
* type : string
* responses :
* 200 :
* description : blockchain_block successfully fetched for given hash . .
* /
this . router . get (
` ${ this . path } /:hash ` ,
validationMiddleware ( BlockchainBlockSingleViewDto , true ) ,
this . BlockchainBlockFetch ,
) ;
/ * *
* @swagger
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k :
* patch :
* tags :
* - name : BLOCKCHAIN_BLOCK | BLOCKCHAIN_BLOCK CONTROLLER
* summary : update a blockchain_block
* description : update a blockchain_block
* consumes :
* - application / json
* produces :
* - application / json
* parameters :
* - name : blockchain_block
* description :
* in : body
* required : true
* schema :
* $ref : '#/definitions/BlockchainBlockDto'
* responses :
* 200 :
* description : blockchain_block updated successfully
* /
this . router . patch (
` ${ this . path } ` ,
validationMiddleware ( BlockchainBlockUpdateDto ) ,
this . BlockchainBlockUpdate ,
) ;
/ * *
* @swagger
* / e p i c _ e x p l o r e r / v 1 / b l o c k c h a i n _ b l o c k / h a s h :
* delete :
* tags :
* - name : BLOCKCHAIN_BLOCK | BLOCKCHAIN_BLOCK CONTROLLER
* summary : delete a blockchain_block
* description : delete a blockchain_block
* consumes :
* - application / json
* produces :
* - application / json
* parameters :
* - name : Hash
* in : path
* description : blockchain_block hash
* required : true
* type : string
* responses :
* 200 :
* description : blockchain_block successfully deleted for given hash . .
* /
this . router . delete (
` ${ this . path } /:hash ` ,
validationMiddleware ( BlockchainBlockSingleViewDto ) ,
this . BlockchainBlockDelete ,
) ;
}
private BlockchainBlockCreate = async (
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
const BlockchainBlockRequestData : BlockchainBlockCreateDto = request . body ;
const BlockchainBlockCreateQuery = await getRepository (
BlockchainBlock ,
) . save ( BlockchainBlockRequestData ) ;
response . status ( 200 ) . json ( {
status : 200 ,
timestamp : Date.now ( ) ,
message : 'blockchain_block created successfully' ,
response : BlockchainBlockCreateQuery ,
} ) ;
} catch ( error ) {
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
private BlockchainBlockFetch = async (
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
var BlockchainBlockFetchQuery = await getRepository (
BlockchainBlock ,
) . findOne ( {
select : [
'Hash' ,
'Height' ,
'Timestamp' ,
2019-07-29 14:01:27 +00:00
'TotalDifficultyCuckaroo' ,
'TotalDifficultyCuckatoo' ,
'TotalDifficultyProgpow' ,
'TotalDifficultyRandomx' ,
2019-07-09 09:22:36 +00:00
'PreviousId' ,
'EdgeBits' ,
'SecondaryScaling' ,
2019-07-29 14:01:27 +00:00
'Proof' ,
2019-07-09 09:22:36 +00:00
] ,
where : { Hash : request.params.hash } ,
} ) ;
let paramVal = request . params . hash ;
2019-07-16 06:17:19 +00:00
if (
! BlockchainBlockFetchQuery &&
! isNaN ( paramVal ) &&
paramVal . length <= 10 &&
paramVal <= 2147483647
) {
2019-07-09 09:22:36 +00:00
var BlockchainBlockFetchQuery = await getRepository (
BlockchainBlock ,
) . findOne ( {
select : [
'Hash' ,
'Height' ,
'Timestamp' ,
2019-07-29 14:01:27 +00:00
'TotalDifficultyCuckaroo' ,
'TotalDifficultyCuckatoo' ,
'TotalDifficultyProgpow' ,
'TotalDifficultyRandomx' ,
2019-07-09 09:22:36 +00:00
'PreviousId' ,
'EdgeBits' ,
'SecondaryScaling' ,
2019-07-29 14:01:27 +00:00
'Proof' ,
2019-07-09 09:22:36 +00:00
] ,
where : { Height : paramVal } ,
} ) ;
}
if ( ! BlockchainBlockFetchQuery ) {
next ( new NoDataFoundException ( ) ) ;
}
const BlockchainBlockInputFetchQuery = await getRepository (
BlockchainInput ,
) . find ( {
select : [ 'Data' ] ,
where : { BlockId : BlockchainBlockFetchQuery.Hash } ,
} ) ;
const BlockchainBlockOutputFetchQuery = await getRepository (
BlockchainOutput ,
) . find ( {
select : [ 'OutputType' , 'Commit' , 'Spent' ] ,
where : { BlockId : BlockchainBlockFetchQuery.Hash } ,
} ) ;
const BlockchainBlockKernalFetchQuery = await getRepository (
BlockchainKernel ,
) . find ( {
select : [ 'Features' , 'Fee' , 'LockHeight' ] ,
where : { BlockId : BlockchainBlockFetchQuery.Hash } ,
} ) ;
2019-07-29 14:01:27 +00:00
// if (BlockchainBlockFetchQuery.EdgeBits == 29) {
// BlockchainBlockFetchQuery['PoWAlgorithm'] = 'CuckARoo29';
// } else {
// BlockchainBlockFetchQuery['PoWAlgorithm'] = 'CuckAToo31';
// }
2019-08-06 14:28:31 +00:00
BlockchainBlockFetchQuery [ 'TotalCuckoo' ] = parseInt ( BlockchainBlockFetchQuery . TotalDifficultyCuckaroo ) +
2019-08-04 00:40:13 +00:00
parseInt ( BlockchainBlockFetchQuery . TotalDifficultyCuckatoo ) ;
2019-07-09 09:22:36 +00:00
if ( BlockchainBlockFetchQuery . Height <= 1440 ) {
BlockchainBlockFetchQuery [ 'BlockReward' ] = 200 ;
} else if ( BlockchainBlockFetchQuery . Height <= 2880 ) {
BlockchainBlockFetchQuery [ 'BlockReward' ] = 180 ;
} else if ( BlockchainBlockFetchQuery . Height <= 4320 ) {
BlockchainBlockFetchQuery [ 'BlockReward' ] = 160 ;
} else if ( BlockchainBlockFetchQuery . Height <= 5760 ) {
BlockchainBlockFetchQuery [ 'BlockReward' ] = 140 ;
} else if ( BlockchainBlockFetchQuery . Height <= 7200 ) {
BlockchainBlockFetchQuery [ 'BlockReward' ] = 120 ;
} else if ( BlockchainBlockFetchQuery . Height <= 8640 ) {
BlockchainBlockFetchQuery [ 'BlockReward' ] = 100 ;
} else if ( BlockchainBlockFetchQuery . Height <= 10080 ) {
BlockchainBlockFetchQuery [ 'BlockReward' ] = 80 ;
} else if ( BlockchainBlockFetchQuery . Height <= 11520 ) {
BlockchainBlockFetchQuery [ 'BlockReward' ] = 60 ;
} else if ( BlockchainBlockFetchQuery . Height <= 12960 ) {
BlockchainBlockFetchQuery [ 'BlockReward' ] = 50 ;
} else {
BlockchainBlockFetchQuery [ 'BlockReward' ] = 25 ;
}
if ( BlockchainBlockFetchQuery . PreviousId ) {
const BlockchainPreviousBlockFetchQuery = await getRepository (
BlockchainBlock ,
) . findOne ( {
2019-07-30 05:12:23 +00:00
select : [ 'TotalDifficultyCuckaroo' , 'TotalDifficultyCuckatoo' , 'TotalDifficultyProgpow' , 'TotalDifficultyRandomx' ] ,
2019-07-09 09:22:36 +00:00
where : { Hash : BlockchainBlockFetchQuery.PreviousId } ,
} ) ;
2019-07-29 14:01:27 +00:00
BlockchainBlockFetchQuery [ 'TargetDifficultyCuckaroo' ] =
parseInt ( BlockchainBlockFetchQuery . TotalDifficultyCuckaroo ) -
parseInt ( BlockchainPreviousBlockFetchQuery . TotalDifficultyCuckaroo ) ;
2019-07-30 05:12:23 +00:00
BlockchainBlockFetchQuery [ 'TargetDifficultyCuckatoo' ] =
2019-07-29 14:01:27 +00:00
parseInt ( BlockchainBlockFetchQuery . TotalDifficultyCuckatoo ) -
parseInt ( BlockchainPreviousBlockFetchQuery . TotalDifficultyCuckatoo ) ;
2019-07-30 05:12:23 +00:00
BlockchainBlockFetchQuery [ 'TargetDifficultyProgpow' ] =
2019-07-29 14:01:27 +00:00
parseInt ( BlockchainBlockFetchQuery . TotalDifficultyProgpow ) -
parseInt ( BlockchainPreviousBlockFetchQuery . TotalDifficultyProgpow ) ;
2019-07-30 05:12:23 +00:00
BlockchainBlockFetchQuery [ 'TargetDifficultyRandomx' ] =
2019-07-29 14:01:27 +00:00
parseInt ( BlockchainBlockFetchQuery . TotalDifficultyRandomx ) -
parseInt ( BlockchainPreviousBlockFetchQuery . TotalDifficultyRandomx ) ;
2019-07-09 09:22:36 +00:00
} else {
2019-07-29 14:01:27 +00:00
BlockchainBlockFetchQuery [ 'TargetDifficultyCuckaroo' ] = 'NULL' ;
BlockchainBlockFetchQuery [ 'TargetDifficultyCuckatoo' ] = 'NULL' ;
BlockchainBlockFetchQuery [ 'TargetDifficultyProgpow' ] = 'NULL' ;
BlockchainBlockFetchQuery [ 'TargetDifficultyRandomx' ] = 'NULL' ;
2019-07-09 09:22:36 +00:00
}
var Epicfee = 0 ;
BlockchainBlockKernalFetchQuery . forEach ( e = > {
e . Fee = this . nanoEpic ( e . Fee ) ;
Epicfee = Epicfee + e . Fee ;
} ) ;
BlockchainBlockFetchQuery [ 'Fee' ] = Epicfee ;
BlockchainBlockFetchQuery [ 'Timestamp' ] = moment
. utc ( BlockchainBlockFetchQuery [ 'Timestamp' ] )
. utc ( )
2019-08-02 10:24:24 +00:00
. format ( 'YYYY-MM-DD, HH:MM:SS UTC' ) ;
2019-07-09 09:22:36 +00:00
BlockchainBlockFetchQuery [ 'hashstart' ] = BlockchainBlockFetchQuery [
'Hash'
] . slice ( 0 , 2 ) ;
BlockchainBlockFetchQuery [ 'hashend' ] = BlockchainBlockFetchQuery [
'Hash'
] . slice ( 62 , 64 ) ;
let balance = BlockchainBlockFetchQuery [ 'Hash' ] . substring ( 2 , 62 ) ;
let arr = balance . match ( /.{1,6}/g ) ;
BlockchainBlockFetchQuery [ 'hasharray' ] = arr . map ( i = > '#' + i ) ;
BlockchainBlockFetchQuery
? response . status ( 200 ) . json ( {
2019-07-30 05:12:23 +00:00
status : 200 ,
timestamp : Date.now ( ) ,
message : 'blockchain_blocksuccessfully fetched for given id.' ,
response : {
BlockchainBlockFetchQuery : BlockchainBlockFetchQuery ,
BlockchainBlockInputFetchQuery : BlockchainBlockInputFetchQuery ,
BlockchainBlockOutputFetchQuery : BlockchainBlockOutputFetchQuery ,
BlockchainBlockKernalFetchQuery : BlockchainBlockKernalFetchQuery ,
} ,
} )
2019-07-09 09:22:36 +00:00
: next ( new NoDataFoundException ( ) ) ;
} catch ( error ) {
2019-07-30 05:12:23 +00:00
console . log ( 'error' , error ) ;
2019-07-09 09:22:36 +00:00
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
private BlockchainBlockUpdate = async (
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
const BlockchainBlockRequestData : BlockchainBlockUpdateDto = request . body ;
const BlockchainBlockUpdateQuery = await getRepository (
BlockchainBlock ,
) . update ( BlockchainBlockRequestData . Hash , BlockchainBlockRequestData ) ;
response . status ( 200 ) . json ( {
status : 200 ,
timestamp : Date.now ( ) ,
message : 'blockchain_block updated succesfully' ,
response : { . . . BlockchainBlockUpdateQuery } ,
} ) ;
} catch ( error ) {
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
private BlockchainBlockDelete = async (
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
const BlockchainBlockDeleteQuery = await getRepository (
BlockchainBlock ,
) . delete ( request . params . Hash ) ;
BlockchainBlockDeleteQuery
? response . status ( 200 ) . json ( {
2019-07-30 05:12:23 +00:00
status : 200 ,
timestamp : Date.now ( ) ,
message : 'blockchain_block successfully deleted for given hash.' ,
response : { . . . BlockchainBlockDeleteQuery } ,
} )
2019-07-09 09:22:36 +00:00
: next ( new NoDataFoundException ( ) ) ;
} catch ( error ) {
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
private BlockchainBlockPagination = async (
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
const {
CurrentPage ,
// MaxPages,
PageSize ,
} : BlockchainBlockPaginationDto = request . query ;
if ( parseInt ( CurrentPage ) == NaN ) {
next ( new IntegerValidationException ( 'CurrentPage' ) ) ;
} else if ( parseInt ( PageSize ) == NaN ) {
next ( new IntegerValidationException ( 'PageSize' ) ) ;
}
// else if (parseInt(MaxPages) == NaN) {
// next(new IntegerValidationException('MaxPages'));
// }
else {
const BlockchainBlockCountQuery = await getRepository ( BlockchainBlock )
. createQueryBuilder ( )
. getCount ( ) ;
if ( BlockchainBlockCountQuery ) {
const PaginationReponseData = Paginate (
BlockchainBlockCountQuery ,
parseInt ( CurrentPage ) ,
parseInt ( PageSize ) ,
// parseInt(MaxPages),
) ;
// const BlockchainBlockPaginationQuery = await getRepository(
// BlockchainBlock,
// ).find({
// skip: PaginationReponseData.startIndex,
// take: PaginationReponseData.pageSize,
// order: {
// Hash: 'DESC',
// },
// });
const BlockchainBlockPaginationQuery = await getRepository (
BlockchainBlock ,
)
. createQueryBuilder ( 'blockchain_block' )
. select ( [
'blockchain_block.Hash' ,
'blockchain_block.Timestamp' ,
2019-08-04 00:40:13 +00:00
'(blockchain_block.TotalDifficultyCuckaroo + blockchain_block.TotalDifficultyCuckatoo) as TotalCuckoo' ,
2019-07-29 14:01:27 +00:00
'blockchain_block.TotalDifficultyCuckaroo' ,
'blockchain_block.TotalDifficultyCuckatoo' ,
'blockchain_block.TotalDifficultyProgpow' ,
'blockchain_block.TotalDifficultyRandomx' ,
2019-07-09 09:22:36 +00:00
'blockchain_block.previous_id' ,
2019-07-29 14:01:27 +00:00
'blockchain_block.total_difficulty_cuckaroo - LAG(blockchain_block.total_difficulty_cuckaroo) OVER (ORDER BY blockchain_block.total_difficulty_cuckaroo) AS target_difficulty_cuckaroo' ,
'blockchain_block.total_difficulty_cuckatoo - LAG(blockchain_block.total_difficulty_cuckatoo) OVER (ORDER BY blockchain_block.total_difficulty_cuckatoo) AS target_difficulty_cuckatoo' ,
'blockchain_block.total_difficulty_progpow - LAG(blockchain_block.total_difficulty_progpow) OVER (ORDER BY blockchain_block.total_difficulty_progpow) AS target_difficulty_progpow' ,
'blockchain_block.total_difficulty_randomx - LAG(blockchain_block.total_difficulty_randomx) OVER (ORDER BY blockchain_block.total_difficulty_randomx) AS target_difficulty_randomx' ,
2019-07-09 09:22:36 +00:00
'blockchain_block.Height' ,
'blockchain_block.EdgeBits' ,
'COUNT(DISTINCT(blockchain_input.block_id)) AS input_count' ,
'COUNT(DISTINCT(blockchain_kernel.block_id)) AS kernal_count' ,
'COUNT(DISTINCT(blockchain_output.block_id)) AS output_count' ,
2019-07-29 14:01:27 +00:00
'blockchain_block.Proof As PoWAlgo' ,
2019-07-09 09:22:36 +00:00
] )
2019-07-29 14:01:27 +00:00
// .addSelect(
// `CASE
// WHEN blockchain_block.EdgeBits = 29 THEN 'CuckARoo29'
// WHEN blockchain_block.EdgeBits = 31 THEN 'CuckAToo31'
// END`,
// 'PoWAlgo',
// )
2019-07-09 09:22:36 +00:00
. leftJoin ( 'blockchain_block.BlockchainInputs' , 'blockchain_input' )
. leftJoin ( 'blockchain_block.BlockchainKernels' , 'blockchain_kernel' )
. leftJoin ( 'blockchain_block.BlockchainOutputs' , 'blockchain_output' )
. skip ( PaginationReponseData . startIndex )
. take ( PaginationReponseData . pageSize )
. orderBy ( 'blockchain_block.Timestamp' , 'DESC' )
. groupBy ( 'blockchain_block.Hash' )
. getRawAndEntities ( ) ;
//console.log(BlockchainBlockPaginationQuery.raw);
let BlockchainBlockResult = BlockchainBlockPaginationQuery . raw ;
let lastElemt =
BlockchainBlockResult [ BlockchainBlockResult . length - 1 ] ;
const BlockchainPreviousBlockFetchQuery = await getRepository (
BlockchainBlock ,
) . findOne ( {
2019-07-30 05:12:23 +00:00
select : [ 'TotalDifficultyCuckaroo' , 'TotalDifficultyCuckatoo' , 'TotalDifficultyProgpow' , 'TotalDifficultyRandomx' ] ,
2019-07-09 09:22:36 +00:00
where : { Hash : lastElemt.previous_id } ,
} ) ;
BlockchainBlockResult [ BlockchainBlockResult . length - 1 ] [
2019-07-29 14:01:27 +00:00
'target_difficulty_cuckaroo'
2019-07-30 05:12:23 +00:00
] = BlockchainPreviousBlockFetchQuery &&
BlockchainPreviousBlockFetchQuery != undefined ?
( parseInt ( lastElemt . blockchain_block_total_difficulty_cuckaroo )
? parseInt ( lastElemt . blockchain_block_total_difficulty_cuckaroo )
: 0 ) -
( parseInt ( BlockchainPreviousBlockFetchQuery . TotalDifficultyCuckaroo ) ? parseInt ( BlockchainPreviousBlockFetchQuery . TotalDifficultyCuckaroo ) : 0 )
: 0 ;
BlockchainBlockResult [ BlockchainBlockResult . length - 1 ] [
'target_difficulty_cuckatoo'
] = BlockchainPreviousBlockFetchQuery &&
BlockchainPreviousBlockFetchQuery != undefined ?
2019-07-29 14:01:27 +00:00
( parseInt ( lastElemt . blockchain_block_total_difficulty_cuckatoo )
? parseInt ( lastElemt . blockchain_block_total_difficulty_cuckatoo )
: 0 ) -
2019-07-30 05:12:23 +00:00
( parseInt ( BlockchainPreviousBlockFetchQuery . TotalDifficultyCuckatoo ) ? parseInt ( BlockchainPreviousBlockFetchQuery . TotalDifficultyCuckatoo ) : 0 )
: 0 ;
BlockchainBlockResult [ BlockchainBlockResult . length - 1 ] [
'target_difficulty_progpow'
] = BlockchainPreviousBlockFetchQuery &&
BlockchainPreviousBlockFetchQuery != undefined ?
( parseInt ( lastElemt . blockchain_block_total_difficulty_progpow )
? parseInt ( lastElemt . blockchain_block_total_difficulty_progpow )
: 0 ) -
( parseInt ( BlockchainPreviousBlockFetchQuery . TotalDifficultyProgpow ) ? parseInt ( BlockchainPreviousBlockFetchQuery . TotalDifficultyProgpow ) : 0 )
: 0 ;
BlockchainBlockResult [ BlockchainBlockResult . length - 1 ] [
'target_difficulty_randomx'
] = BlockchainPreviousBlockFetchQuery &&
BlockchainPreviousBlockFetchQuery != undefined ?
( parseInt ( lastElemt . blockchain_block_total_difficulty_randomx )
? parseInt ( lastElemt . blockchain_block_total_difficulty_randomx )
: 0 ) -
( parseInt ( BlockchainPreviousBlockFetchQuery . TotalDifficultyRandomx ) ? parseInt ( BlockchainPreviousBlockFetchQuery . TotalDifficultyRandomx ) : 0 )
: 0 ;
2019-07-09 09:22:36 +00:00
BlockchainBlockResult . forEach ( e = > {
var latest_block = this . dateDiff ( e . blockchain_block_timestamp ) ;
e [ 'hashstart' ] = e . blockchain_block_hash . slice ( 0 , 2 ) ;
e [ 'hashend' ] = e . blockchain_block_hash . slice ( 62 , 64 ) ;
let balance = e . blockchain_block_hash . substring ( 2 , 62 ) ;
let arr = balance . match ( /.{1,6}/g ) ;
e [ 'hasharray' ] = arr . map ( i = > '#' + i ) ;
e [ 'age' ] = latest_block ;
} ) ;
//console.log(BlockchainBlockPaginationQueryRawResult);
response . status ( 200 ) . json ( {
status : 200 ,
timestamp : Date.now ( ) ,
message : 'blockchain_block list fetched successfully' ,
response : {
. . . PaginationReponseData ,
BlockchainBlockResult ,
} ,
} ) ;
} else {
next ( new NoDataFoundException ( ) ) ;
}
}
} catch ( error ) {
2019-07-30 05:12:23 +00:00
console . log ( 'error' , error ) ;
2019-07-09 09:22:36 +00:00
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
2019-08-06 14:28:31 +00:00
2019-08-06 13:10:10 +00:00
private BlockCount = async (
2019-07-09 09:22:36 +00:00
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
const TotalDifficultyNBlockRequestData : TotalDifficultyNBlockDto =
request . query ;
if ( TotalDifficultyNBlockRequestData . Interval ) {
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' > current_date - interval '" +
TotalDifficultyNBlockRequestData . Interval +
"'" ;
} else if (
TotalDifficultyNBlockRequestData . FromDate ||
TotalDifficultyNBlockRequestData . ToDate
) {
let fromdate = moment ( TotalDifficultyNBlockRequestData . FromDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
let todate = moment ( TotalDifficultyNBlockRequestData . ToDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' BETWEEN SYMMETRIC '" +
fromdate +
"' AND '" +
todate +
"'" ;
} else {
2019-08-06 13:10:10 +00:00
var timeIntervalQry =
2019-07-09 09:22:36 +00:00
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' > current_date - interval '30 days'" ;
}
2019-07-31 14:13:06 +00:00
const BlockQuery = await getConnection ( )
2019-07-09 09:22:36 +00:00
. query (
2019-07-30 07:13:24 +00:00
" select 1 as hash , max ( total_difficulty_cuckaroo ) as total_difficulty_cuckaroo , \
max ( total_difficulty_cuckatoo ) as total_difficulty_cuckatoo , \
max ( total_difficulty_progpow ) as total_difficulty_progpow , \
max ( total_difficulty_randomx ) as total_difficulty_randomx , date ( DATE_TRUNC ( 'day' , timestamp at time zone ' " +
2019-07-30 05:12:23 +00:00
process . env . TIME_ZONE +
" ' ) ) as date , count ( hash ) as blocks \
2019-07-09 09:22:36 +00:00
from blockchain_block where " +
2019-08-06 13:10:10 +00:00
timeIntervalQry +
2019-07-30 05:12:23 +00:00
"group by DATE_TRUNC('day', timestamp at time zone '" +
process . env . TIME_ZONE +
"') order by date" ,
2019-07-09 09:22:36 +00:00
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
2019-08-06 13:10:10 +00:00
let date = [ ] ,
blockDate = [ ] ,
blocks = [ ] ;
BlockQuery . forEach ( e = > {
blockDate . push ( moment ( e . date ) . format ( 'YYYY-MM-DD' ) ) ;
blocks . push ( parseInt ( e . blocks ) ) ;
} ) ;
response . status ( 200 ) . json ( {
status : 200 ,
timestamp : Date.now ( ) ,
message : 'Total Difficulty and Blocks Data fetched Successfully' ,
response : {
Date : date ,
Blocks : blocks ,
blockDate :blockDate
} ,
} ) ;
} catch ( error ) {
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
private TotalDifficultyNBlock = async (
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
const TotalDifficultyNBlockRequestData : TotalDifficultyNBlockDto =
request . query ;
if ( TotalDifficultyNBlockRequestData . Interval ) {
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' > current_date - interval '" +
TotalDifficultyNBlockRequestData . Interval +
"'" ;
} else if (
TotalDifficultyNBlockRequestData . FromDate ||
TotalDifficultyNBlockRequestData . ToDate
) {
let fromdate = moment ( TotalDifficultyNBlockRequestData . FromDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
let todate = moment ( TotalDifficultyNBlockRequestData . ToDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' BETWEEN SYMMETRIC '" +
fromdate +
"' AND '" +
todate +
"'" ;
} else {
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' > current_date - interval '1 day'" ;
2019-08-06 14:28:31 +00:00
2019-08-06 13:10:10 +00:00
}
2019-08-06 14:28:31 +00:00
var alog_type = TotalDifficultyNBlockRequestData . Type ;
2019-08-07 08:09:41 +00:00
var Difftype = TotalDifficultyNBlockRequestData . Difftype ;
if ( TotalDifficultyNBlockRequestData . Interval && TotalDifficultyNBlockRequestData . Interval != '1 day' ) {
var dateFormat = 'YYYY-MM-DD' ;
} else {
2019-08-07 09:58:54 +00:00
var dateFormat = 'HH:mm' ;
2019-08-07 08:09:41 +00:00
}
if ( Difftype == "target" ) {
var TotalDifficultyNBlockQuery = await getConnection ( )
2019-07-31 14:13:06 +00:00
. query (
2019-08-06 14:28:31 +00:00
"SELECT a.hash, a.tarket_difficulty, a.date FROM(select 1 as hash, (total_difficulty_" + alog_type + " - LAG(total_difficulty_" + alog_type + ") OVER (ORDER BY total_difficulty_" + alog_type + " ) ) AS tarket_difficulty , \
DATE_TRUNC ( 'minute' , timestamp at time zone ' " +
process . env . TIME_ZONE +
" ' ) as date \
from blockchain_block where " +
timeIntervalQry +
" order by height) as a WHERE a.tarket_difficulty IS NOT NULL" ,
2019-07-31 14:13:06 +00:00
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
2019-08-07 08:09:41 +00:00
} else if ( Difftype == "solution" ) {
var TotalDifficultyNBlockQuery = await getConnection ( )
. query (
"SELECT a.hash, a.tarket_difficulty, a.date FROM(select 1 as hash, (total_difficulty_" + alog_type + " - LAG(total_difficulty_" + alog_type + ") OVER (ORDER BY total_difficulty_" + alog_type + " ) ) AS tarket_difficulty , \
DATE_TRUNC ( 'minute' , timestamp at time zone ' " +
process . env . TIME_ZONE +
" ' ) as date \
from blockchain_block where " +
timeIntervalQry +
" order by height) as a WHERE a.tarket_difficulty IS NOT NULL" ,
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
} else if ( Difftype == "total" ) {
var TotalDifficultyNBlockQuery = await getConnection ( )
. query (
"select 1 as hash, total_difficulty_" + alog_type + " as tarket_difficulty , \
DATE_TRUNC ( 'minute' , timestamp at time zone ' " +
process . env . TIME_ZONE +
" ' ) as date \
from blockchain_block where " +
timeIntervalQry +
" order by height" ,
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
} else {
var TotalDifficultyNBlockQuery = await getConnection ( )
. query (
"SELECT a.hash, a.tarket_difficulty, a.date FROM(select 1 as hash, (total_difficulty_" + alog_type + " - LAG(total_difficulty_" + alog_type + ") OVER (ORDER BY total_difficulty_" + alog_type + " ) ) AS tarket_difficulty , \
DATE_TRUNC ( 'minute' , timestamp at time zone ' " +
process . env . TIME_ZONE +
" ' ) as date \
from blockchain_block where " +
timeIntervalQry +
" order by height) as a WHERE a.tarket_difficulty IS NOT NULL" ,
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
}
2019-07-09 09:22:36 +00:00
let date = [ ] ,
2019-08-06 14:28:31 +00:00
//DifficultyCuckaroo = [],
//DifficultyCuckatoo = [],
//DifficultyProgpow = [],
//DifficultyRandomx = [];
TargetDifficulty = [ ] ;
2019-08-06 13:10:10 +00:00
2019-07-31 14:13:06 +00:00
TotalDifficultyNBlockQuery . forEach ( e = > {
2019-08-06 14:28:31 +00:00
//date.indexOf(moment(e.date).format('YYYY-MM-DD')) < 0 ?
2019-08-07 08:09:41 +00:00
date . push ( moment ( e . date ) . format ( dateFormat ) ) ;
2019-08-06 14:28:31 +00:00
// DifficultyCuckaroo.push(parseInt(e.total_difficulty_cuckaroo));
// DifficultyCuckatoo.push(parseInt(e.total_difficulty_cuckatoo));
// DifficultyProgpow.push(parseInt(e.total_difficulty_progpow));
// DifficultyRandomx.push(parseInt(e.total_difficulty_randomx));
TargetDifficulty . push ( parseInt ( e . tarket_difficulty ) ) ;
2019-07-31 14:13:06 +00:00
} ) ;
2019-08-06 14:28:31 +00:00
2019-08-07 12:28:41 +00:00
var Maxrange = Math . max . apply ( Math , TotalDifficultyNBlockQuery . map ( function ( o ) { return o . tarket_difficulty ; } ) ) ;
var Minrange = Math . min . apply ( Math , TotalDifficultyNBlockQuery . map ( function ( o ) { return o . tarket_difficulty ; } ) ) ;
// Minrange = parseInt(Minrange);
// var Minrange2 = parseInt(Minrange * 0.3);
2019-07-09 09:22:36 +00:00
response . status ( 200 ) . json ( {
status : 200 ,
timestamp : Date.now ( ) ,
2019-08-07 08:14:06 +00:00
message : 'Difficulty and Blocks Data fetched Successfully' ,
2019-07-09 09:22:36 +00:00
response : {
Date : date ,
2019-08-06 14:28:31 +00:00
// DifficultyCuckaroo: DifficultyCuckaroo,
// DifficultyCuckatoo: DifficultyCuckatoo,
// DifficultyProgpow: DifficultyProgpow,
2019-08-07 12:28:41 +00:00
Maxrange : Maxrange ,
Minrange : ( Minrange * 0.3 ) ,
2019-08-06 14:28:31 +00:00
TargetDifficulty : TargetDifficulty
2019-07-09 09:22:36 +00:00
} ,
} ) ;
} catch ( error ) {
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
2019-07-29 10:59:06 +00:00
private StackBlock = async (
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
const TotalDifficultyNBlockRequestData : TotalDifficultyNBlockDto =
request . query ;
if ( TotalDifficultyNBlockRequestData . Interval ) {
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' > current_date - interval '" +
TotalDifficultyNBlockRequestData . Interval +
"'" ;
} else if (
TotalDifficultyNBlockRequestData . FromDate ||
TotalDifficultyNBlockRequestData . ToDate
) {
let fromdate = moment ( TotalDifficultyNBlockRequestData . FromDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
let todate = moment ( TotalDifficultyNBlockRequestData . ToDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' BETWEEN SYMMETRIC '" +
fromdate +
"' AND '" +
todate +
"'" ;
} else {
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' > current_date - interval '30 days'" ;
}
const stackNBlockQuery = await getConnection ( )
. query (
2019-07-30 07:13:24 +00:00
"select 1 as hash, date(DATE_TRUNC('day', timestamp at time zone '" +
2019-07-30 05:12:23 +00:00
process . env . TIME_ZONE +
" ')) as date, Count( CASE WHEN proof = ' RandomX ' THEN 1 ELSE NULL END ) AS RandomX , \
2019-08-01 07:38:45 +00:00
Count ( CASE WHEN proof = 'Cuckoo' THEN 1 ELSE NULL END ) AS Cuckoo , \
2019-07-29 10:59:06 +00:00
Count ( CASE WHEN proof = 'ProgPow' THEN 1 ELSE NULL END ) AS ProgPow \
from blockchain_block where " +
2019-07-30 05:12:23 +00:00
timeIntervalQry +
"group by DATE_TRUNC('day', timestamp at time zone '" +
process . env . TIME_ZONE +
"') order by date" ,
2019-07-29 10:59:06 +00:00
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
let date = [ ] ,
2019-07-30 07:13:24 +00:00
Blocks = [ ] ,
2019-08-01 07:38:45 +00:00
Cuckoo = [ ] ,
2019-07-30 07:13:24 +00:00
ProgPow = [ ] ,
RandomX = [ ] ;
2019-07-30 05:12:23 +00:00
stackNBlockQuery . forEach ( e = > {
2019-07-29 10:59:06 +00:00
date . push ( moment ( e . date ) . format ( 'YYYY-MM-DD' ) ) ;
2019-07-30 10:48:20 +00:00
// Blocks.push({Cuckaroo: parseInt(e.cuckaroo), Cuckatoo : parseInt(e.cuckatoo), ProgPow : parseInt(e.progpow), RandomX : parseInt(e.randomx)})
2019-08-01 07:38:45 +00:00
Cuckoo . push ( parseInt ( e . cuckoo ) ) ;
2019-07-30 07:13:24 +00:00
ProgPow . push ( parseInt ( e . progpow ) ) ;
RandomX . push ( parseInt ( e . randomx ) ) ;
2019-07-09 09:22:36 +00:00
} ) ;
response . status ( 200 ) . json ( {
status : 200 ,
timestamp : Date.now ( ) ,
2019-07-30 10:48:20 +00:00
message : 'Stack Data fetched Successfully' ,
2019-07-09 09:22:36 +00:00
response : {
Date : date ,
2019-08-01 07:38:45 +00:00
Cuckoo :Cuckoo ,
2019-07-30 10:48:20 +00:00
ProgPow :ProgPow ,
RandomX :RandomX
2019-07-09 09:22:36 +00:00
} ,
} ) ;
} catch ( error ) {
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
2019-07-30 07:43:44 +00:00
private BlockPieChart = async (
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
const TotalDifficultyNBlockRequestData : TotalDifficultyNBlockDto =
request . query ;
if ( TotalDifficultyNBlockRequestData . Interval ) {
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' > current_date - interval '" +
TotalDifficultyNBlockRequestData . Interval +
"'" ;
} else if (
TotalDifficultyNBlockRequestData . FromDate ||
TotalDifficultyNBlockRequestData . ToDate
) {
let fromdate = moment ( TotalDifficultyNBlockRequestData . FromDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
let todate = moment ( TotalDifficultyNBlockRequestData . ToDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' BETWEEN SYMMETRIC '" +
fromdate +
"' AND '" +
todate +
"'" ;
} else {
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' > current_date - interval '30 days'" ;
}
const stackNBlockQuery = await getConnection ( )
. query (
2019-08-01 07:38:45 +00:00
" SELECT hash , total_edge_bits , RandomX , Cuckoo , ProgPow , Round ( RandomX * 100.0 / total_edge_bits , 2 ) AS RandomXper , Round ( Cuckoo * 100.0 / total_edge_bits , 2 ) AS Cuckooper , Round ( ProgPow * 100.0 / total_edge_bits , 2 ) AS ProgPowper from ( select 1 as hash , COUNT ( edge_bits ) AS total_edge_bits , \
2019-07-30 07:43:44 +00:00
Count ( CASE WHEN proof = 'RandomX' THEN 1 ELSE NULL END ) AS RandomX , \
2019-08-01 07:38:45 +00:00
Count ( CASE WHEN proof = 'Cuckoo' THEN 1 ELSE NULL END ) AS Cuckoo , \
2019-07-30 07:43:44 +00:00
Count ( CASE WHEN proof = 'ProgPow' THEN 1 ELSE NULL END ) AS ProgPow \
from blockchain_block where " +
timeIntervalQry +
")t"
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
2019-07-30 10:48:20 +00:00
let label = [ ] ,
value = [ ] ;
stackNBlockQuery . forEach ( e = > {
2019-08-01 07:38:45 +00:00
label . push ( "Cuckoo" , "ProgPow" , "RandomX" ) ;
value . push ( parseInt ( e . cuckoo ) , parseInt ( e . progpow ) , parseInt ( e . randomx ) ) ;
2019-07-30 10:48:20 +00:00
} ) ;
2019-07-30 07:43:44 +00:00
response . status ( 200 ) . json ( {
status : 200 ,
timestamp : Date.now ( ) ,
message : 'Piechart for block is fetched Successfully' ,
response : {
2019-07-30 10:48:20 +00:00
label ,
value
2019-07-30 07:43:44 +00:00
} ,
} ) ;
} catch ( error ) {
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
2019-07-09 09:22:36 +00:00
private HashRate = async (
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
const TotalDifficultyNBlockRequestData : TotalDifficultyNBlockDto =
request . query ;
if ( TotalDifficultyNBlockRequestData . Interval ) {
var timeIntervalQry =
"timestamp > current_date - interval '" +
TotalDifficultyNBlockRequestData . Interval +
"'" ;
var seriesquery =
"now() - interval '" +
TotalDifficultyNBlockRequestData . Interval +
"', now()" ;
} else if (
TotalDifficultyNBlockRequestData . FromDate ||
TotalDifficultyNBlockRequestData . ToDate
) {
let fromdate = moment ( TotalDifficultyNBlockRequestData . FromDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
let todate = moment ( TotalDifficultyNBlockRequestData . ToDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
var timeIntervalQry =
'timestamp BETWEEN SYMMETRIC ' + fromdate + ' AND ' + todate ;
var seriesquery = "'" + fromdate + "'::timestamp, '" + todate + "'" ;
} else {
var timeIntervalQry = "timestamp > current_date - interval '30 days'" ;
var seriesquery = "now() - interval '30 days', now()" ;
}
const HashRateQueryAR29 = await getConnection ( )
. query (
'with hours as ( SELECT hour::date from generate_series(' +
2019-07-30 05:12:23 +00:00
seriesquery +
", '1 day') as hour),tblDifference AS(select ROW_NUMBER() OVER(ORDER BY DATE_TRUNC('day', timestamp)) AS RowNumber, max(total_difficulty) as total_difficulty, date(DATE_TRUNC('day', timestamp)) as date, count(hash) as blocks from blockchain_block where " +
timeIntervalQry +
" and edge_bits=29 group by DATE_TRUNC('day', timestamp) order by date) select coalesce(max(t1.total_difficulty), 0) as total_difficulty, coalesce(max(t1.difficulty), 0) as difficulty, hours.hour, coalesce(max(t1.blocks), 0) as blocks, coalesce(max(t1.hashrate), 0) as hashrate from hours left join(SELECT cur.total_difficulty, cur.total_difficulty - previous.total_difficulty as difficulty, cur.date, cur.blocks, ((cast(cur.blocks as decimal) / 1440) * (cur.total_difficulty - previous.total_difficulty) * (2^32 / 60)) as hashrate FROM tblDifference cur LEFT OUTER JOIN tblDifference previous ON cur.RowNumber = previous.RowNumber + 1) as t1 on t1.date = hours.hour group by hours.hour order by hours.hour" ,
2019-07-09 09:22:36 +00:00
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
const HashRateQueryAT31 = await getConnection ( )
. query (
'with hours as ( SELECT hour::date from generate_series(' +
2019-07-30 05:12:23 +00:00
seriesquery +
", '1 day') as hour),tblDifference AS(select ROW_NUMBER() OVER(ORDER BY DATE_TRUNC('day', timestamp)) AS RowNumber, max(total_difficulty) as total_difficulty, date(DATE_TRUNC('day', timestamp)) as date, count(hash) as blocks from blockchain_block where " +
timeIntervalQry +
" and edge_bits=31 group by DATE_TRUNC('day', timestamp) order by date) select coalesce(max(t1.total_difficulty), 0) as total_difficulty , coalesce(max(t1.difficulty), 0) as difficulty, hours.hour, coalesce(max(t1.blocks), 0) as blocks, coalesce(max(t1.hashrate), 0) as hashrate from hours left join(SELECT cur.total_difficulty, cur.total_difficulty - previous.total_difficulty as difficulty, cur.date, cur.blocks, ((cast(cur.blocks as decimal) / 1440) * (cur.total_difficulty - previous.total_difficulty) * (2^32 / 60)) as hashrate FROM tblDifference cur LEFT OUTER JOIN tblDifference previous ON cur.RowNumber = previous.RowNumber + 1) as t1 on t1.date = hours.hour group by hours.hour order by hours.hour" ,
2019-07-09 09:22:36 +00:00
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
let date = [ ] ,
hashrate29 = [ ] ,
hashrate31 = [ ] ;
2019-07-30 05:12:23 +00:00
HashRateQueryAR29 . forEach ( function ( e , index ) {
2019-07-09 09:22:36 +00:00
if ( index > 0 ) {
date . push ( moment ( e . hour ) . format ( 'YYYY-MM-DD' ) ) ;
hashrate29 . push (
Math . round ( ( parseInt ( e . hashrate ) / 1000000000 ) * 100 ) / 100 ,
) ;
}
} ) ;
2019-07-30 05:12:23 +00:00
HashRateQueryAT31 . forEach ( function ( e , index ) {
2019-07-09 09:22:36 +00:00
if ( index > 0 ) {
hashrate31 . push (
Math . round ( ( parseInt ( e . hashrate ) / 1000000000 ) * 100 ) / 100 ,
) ;
}
} ) ;
response . status ( 200 ) . json ( {
status : 200 ,
timestamp : Date.now ( ) ,
message : 'Total Difficulty and Blocks Data fetched Successfully' ,
response : {
date ,
hashrate29 ,
hashrate31 ,
} ,
} ) ;
} catch ( error ) {
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
private LatestDifficultyNBlock = async (
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
let block_height = '' ,
letest_block = '' ,
letest_block_num = '' ,
letest_block_duration = '' ;
const BlockchainLatestBlockQuery = await getConnection ( )
. query (
2019-07-30 11:46:26 +00:00
'SELECT timestamp,height,edge_bits,hash,secondary_scaling, previous_id, total_difficulty_cuckaroo, total_difficulty_cuckatoo, total_difficulty_progpow, total_difficulty_randomx FROM blockchain_block ORDER BY timestamp DESC LIMIT 1' ,
2019-07-09 09:22:36 +00:00
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
const BlockchainPreviousBlockQuery = await getConnection ( )
. query (
2019-07-30 11:46:26 +00:00
'SELECT total_difficulty_cuckaroo, total_difficulty_cuckatoo, total_difficulty_progpow, total_difficulty_randomx FROM blockchain_block WHERE hash=' +
2019-07-30 05:12:23 +00:00
"'" +
BlockchainLatestBlockQuery [ 0 ] . previous_id +
"'" ,
2019-07-09 09:22:36 +00:00
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
let height = BlockchainLatestBlockQuery [ 0 ] . height ;
if ( height > 12960 ) {
var remain_block = height - 12960 ;
var coin_existence =
1440 * 200 +
1440 * 180 +
1440 * 160 +
1440 * 140 +
1440 * 120 +
1440 * 100 +
1440 * 80 +
1440 * 60 +
1440 * 50 +
25 * remain_block ;
} else if ( height > 11520 ) {
var remain_block = height - 11520 ;
var coin_existence =
1440 * 200 +
1440 * 180 +
1440 * 160 +
1440 * 140 +
1440 * 120 +
1440 * 100 +
1440 * 80 +
1440 * 60 +
remain_block * 50 ;
} else if ( height > 10080 ) {
var remain_block = height - 10080 ;
var coin_existence =
1440 * 200 +
1440 * 180 +
1440 * 160 +
1440 * 140 +
1440 * 120 +
1440 * 100 +
1440 * 80 +
remain_block * 60 ;
} else if ( height > 8640 ) {
var remain_block = height - 8640 ;
var coin_existence =
1440 * 200 +
1440 * 180 +
1440 * 160 +
1440 * 140 +
1440 * 120 +
1440 * 100 +
remain_block * 80 ;
} else if ( height > 7200 ) {
var remain_block = height - 7200 ;
var coin_existence =
1440 * 200 +
1440 * 180 +
1440 * 160 +
1440 * 140 +
1440 * 120 +
remain_block * 100 ;
} else if ( height > 5760 ) {
var remain_block = height - 5760 ;
var coin_existence =
1440 * 200 +
1440 * 180 +
1440 * 160 +
1440 * 140 +
remain_block * 120 ;
} else if ( height > 4320 ) {
var remain_block = height - 4320 ;
var coin_existence =
1440 * 200 + 1440 * 180 + 1440 * 160 + remain_block * 140 ;
} else if ( height > 2880 ) {
var remain_block = height - 2880 ;
var coin_existence = 1440 * 200 + 1440 * 180 + remain_block * 160 ;
} else if ( height > 1440 ) {
var remain_block = height - 1440 ;
var coin_existence = 1440 * 200 + remain_block * 180 ;
} else {
var coin_existence = height * 200 ;
}
letest_block = moment ( BlockchainLatestBlockQuery [ 0 ] . timestamp ) . fromNow ( ) ;
letest_block_num = letest_block . substr ( 0 , letest_block . indexOf ( ' ' ) ) ; // "72"
letest_block_duration = letest_block . substr (
letest_block . indexOf ( ' ' ) + 1 ,
) ; // "tocirah sneab"
const SECOND_POW_EDGE_BITS = 29 ;
const BASE_EDGE_BITS = 24 ;
if ( BlockchainLatestBlockQuery [ 0 ] . edge_bits == SECOND_POW_EDGE_BITS ) {
var hashvalue = BlockchainLatestBlockQuery [ 0 ] . hash ;
var diff =
( BlockchainLatestBlockQuery [ 0 ] . secondary_scaling * 2 * * 64 ) /
parseInt ( hashvalue . substring ( 0 , 16 ) , 16 ) ;
var result = Math . min ( diff , 0xffffffffffffffff ) ;
var difficulty = Math . round ( result ) ;
} else {
var graph_weight =
2 *
2 * * ( BlockchainLatestBlockQuery [ 0 ] . edge_bits - BASE_EDGE_BITS ) *
BlockchainLatestBlockQuery [ 0 ] . edge_bits ;
var hashvalue = BlockchainLatestBlockQuery [ 0 ] . hash ;
var diff =
( graph_weight * 2 * * 64 ) / parseInt ( hashvalue . substring ( 0 , 16 ) , 16 ) ;
var result = Math . min ( diff , 0xffffffffffffffff ) ;
var difficulty = Math . round ( result ) ;
}
if ( BlockchainLatestBlockQuery [ 0 ] . previous_id ) {
2019-07-30 11:46:26 +00:00
var targetdifficultycuckaroo =
BlockchainLatestBlockQuery [ 0 ] . total_difficulty_cuckaroo -
BlockchainPreviousBlockQuery [ 0 ] . total_difficulty_cuckaroo ;
var targetdifficultycuckatoo =
BlockchainLatestBlockQuery [ 0 ] . total_difficulty_cuckatoo -
BlockchainPreviousBlockQuery [ 0 ] . total_difficulty_cuckatoo ;
var targetdifficultyprogpow =
BlockchainLatestBlockQuery [ 0 ] . total_difficulty_progpow -
BlockchainPreviousBlockQuery [ 0 ] . total_difficulty_progpow ;
var targetdifficultyrandomx =
BlockchainLatestBlockQuery [ 0 ] . total_difficulty_randomx -
BlockchainPreviousBlockQuery [ 0 ] . total_difficulty_randomx ;
2019-07-09 09:22:36 +00:00
}
block_height = BlockchainLatestBlockQuery [ 0 ] . height ;
2019-08-06 14:28:31 +00:00
var TotalCuckoo = parseInt ( BlockchainLatestBlockQuery [ 0 ] . total_difficulty_cuckatoo ) +
2019-08-03 23:48:38 +00:00
parseInt ( BlockchainLatestBlockQuery [ 0 ] . total_difficulty_cuckaroo ) ;
2019-07-09 09:22:36 +00:00
response . status ( 200 ) . json ( {
status : 200 ,
timestamp : Date.now ( ) ,
message : 'Latest Block Details fetched Successfully' ,
response : {
block_height ,
letest_block ,
letest_block_num ,
letest_block_duration ,
coin_existence ,
difficulty ,
2019-07-30 11:46:26 +00:00
targetdifficultycuckaroo ,
targetdifficultycuckatoo ,
targetdifficultyprogpow ,
2019-08-03 23:48:38 +00:00
targetdifficultyrandomx ,
TotalCuckoo ,
TotalDifficultyCuckaroo :BlockchainLatestBlockQuery [ 0 ] . total_difficulty_cuckaroo ,
TotalDifficultyCuckatoo :BlockchainLatestBlockQuery [ 0 ] . total_difficulty_cuckatoo ,
TotalDifficultyProgpow :BlockchainLatestBlockQuery [ 0 ] . total_difficulty_progpow ,
TotalDifficultyRandomx :BlockchainLatestBlockQuery [ 0 ] . total_difficulty_randomx
2019-07-09 09:22:36 +00:00
} ,
} ) ;
} catch ( error ) {
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
private BlockchainBlockPerSecond = async (
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
const BlockchainBlockPerSecondData : TotalDifficultyNBlockDto =
request . query ;
if ( BlockchainBlockPerSecondData . Interval ) {
var timeIntervalQry =
"timestamp > current_date - interval '" +
BlockchainBlockPerSecondData . Interval +
"'" ;
} else if (
BlockchainBlockPerSecondData . FromDate ||
BlockchainBlockPerSecondData . ToDate
) {
let fromdate = moment ( BlockchainBlockPerSecondData . FromDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
let todate = moment ( BlockchainBlockPerSecondData . ToDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
var timeIntervalQry =
'timestamp BETWEEN SYMMETRIC ' + fromdate + ' AND ' + todate ;
} else {
var timeIntervalQry = "timestamp > current_date - interval '30 days'" ;
}
const BlockchainBlockPerSecondQuery = await getConnection ( )
. query (
" select date ( DATE_TRUNC ( 'day' , timestamp ) ) as date , count ( hash ) as blocks , 86400 / count ( hash ) as period \
from blockchain_block where " +
2019-07-30 05:12:23 +00:00
timeIntervalQry +
"group by DATE_TRUNC('day', timestamp) order by date" ,
2019-07-09 09:22:36 +00:00
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
let date = [ ] ,
period = [ ] ;
BlockchainBlockPerSecondQuery . forEach ( e = > {
date . push ( moment ( e . date ) . format ( 'YYYY-MM-DD' ) ) ;
period . push ( parseInt ( e . period ) ) ;
} ) ;
response . status ( 200 ) . json ( {
status : 200 ,
timestamp : Date.now ( ) ,
message : 'period of blocks generation per second fetched Successfully' ,
response : {
date ,
period ,
} ,
} ) ;
} catch ( error ) {
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
private SupplyGrowth = async (
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
const BlockchainBlockPerSecondData : TotalDifficultyNBlockDto =
request . query ;
if ( BlockchainBlockPerSecondData . Interval ) {
var timeIntervalQry =
"timestamp > current_date - interval '" +
BlockchainBlockPerSecondData . Interval +
"'" ;
} else if (
BlockchainBlockPerSecondData . FromDate ||
BlockchainBlockPerSecondData . ToDate
) {
let fromdate = moment ( BlockchainBlockPerSecondData . FromDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
let todate = moment ( BlockchainBlockPerSecondData . ToDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
var timeIntervalQry =
'timestamp BETWEEN SYMMETRIC ' + fromdate + ' AND ' + todate ;
} else {
var timeIntervalQry = "timestamp > current_date - interval '30 days'" ;
}
const BlockchainBlockPerSecondQuery = await getConnection ( )
. query (
' select x . timestamp , SUM ( x . reward ) as total_reward_per_day \
from ( SELECT hash , height , CAST ( timestamp AS DATE ) , \
CASE \
WHEN height <= 1440 THEN 200 \
WHEN height <= 2880 THEN 180 \
WHEN height <= 4320 THEN 160 \
WHEN height <= 5760 THEN 140 \
WHEN height <= 7200 THEN 120 \
WHEN height <= 8640 THEN 100 \
WHEN height <= 10080 THEN 80 \
WHEN height <= 11520 THEN 60 \
WHEN height <= 12960 THEN 50 \
ELSE 25 \
END AS reward \
FROM blockchain_block where ' +
2019-07-30 05:12:23 +00:00
timeIntervalQry +
') as x group by x.timestamp Order by x.timestamp ASC' ,
2019-07-09 09:22:36 +00:00
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
let date = [ ] ,
total_reward_per_day = [ ] ,
addedreward = [ ] ,
prev_value = 0 ;
BlockchainBlockPerSecondQuery . forEach ( e = > {
date . push ( moment ( e . timestamp ) . format ( 'YYYY-MM-DD' ) ) ;
total_reward_per_day . push ( parseInt ( e . total_reward_per_day ) ) ;
addedreward . push ( prev_value + parseInt ( e . total_reward_per_day ) ) ;
prev_value = prev_value + parseInt ( e . total_reward_per_day ) ;
} ) ;
response . status ( 200 ) . json ( {
status : 200 ,
timestamp : Date.now ( ) ,
message : 'period of blocks generation per second fetched Successfully' ,
response : {
date ,
total_reward_per_day ,
addedreward ,
} ,
} ) ;
} catch ( error ) {
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
private BlockMineChart = async (
request : Request ,
response : Response ,
next : NextFunction ,
) = > {
try {
const BlockchainBlockPerSecondData : TotalDifficultyNBlockDto =
request . query ;
if ( BlockchainBlockPerSecondData . Interval ) {
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' > current_date - interval '" +
BlockchainBlockPerSecondData . Interval +
"'" ;
} else if (
BlockchainBlockPerSecondData . FromDate ||
BlockchainBlockPerSecondData . ToDate
) {
let fromdate = moment ( BlockchainBlockPerSecondData . FromDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
let todate = moment ( BlockchainBlockPerSecondData . ToDate )
. utc ( )
. format ( 'YYYY-MM-DD' ) ;
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' BETWEEN SYMMETRIC '" +
fromdate +
"' AND '" +
todate +
"'" ;
} else {
var timeIntervalQry =
"timestamp at time zone '" +
process . env . TIME_ZONE +
"' > current_date - interval '30 days'" ;
}
const BlockMineChartQuery = await getConnection ( )
2019-07-30 05:12:23 +00:00
. query (
2019-08-01 07:38:45 +00:00
" SELECT hash , date , total_edge_bits , RandomX , Cuckoo , ProgPow , Round ( RandomX * 100.0 / total_edge_bits , 2 ) AS RandomXper , Round ( Cuckoo * 100.0 / total_edge_bits , 2 ) AS Cuckooper , Round ( ProgPow * 100.0 / total_edge_bits , 2 ) AS ProgPowper \
2019-07-29 06:01:50 +00:00
FROM ( SELECT 1 as hash , \
date ( DATE_TRUNC ( 'day' , timestamp at time zone ' " +
process . env . TIME_ZONE +
" ' ) ) as date , \
COUNT ( edge_bits ) AS total_edge_bits , \
Count ( CASE WHEN proof = 'RandomX' THEN 1 ELSE NULL END ) AS RandomX , \
2019-08-01 07:38:45 +00:00
Count ( CASE WHEN proof = 'Cuckoo' THEN 1 ELSE NULL END ) AS Cuckoo , \
2019-07-29 06:01:50 +00:00
Count ( CASE WHEN proof = 'ProgPow' THEN 1 ELSE NULL END ) AS ProgPow \
FROM blockchain_block \
where " +
timeIntervalQry +
"GROUP BY DATE_TRUNC('day', timestamp at time zone '" +
process . env . TIME_ZONE +
"')) t order by date" ,
2019-07-30 05:12:23 +00:00
)
. catch ( err_msg = > {
next ( err_msg ) ;
} ) ;
let date = [ ] ,
RandomXper = [ ] ,
2019-08-01 07:38:45 +00:00
Cuckooper = [ ] ,
2019-07-30 05:12:23 +00:00
ProgPowper = [ ] ,
RandomX = [ ] ,
2019-08-01 07:38:45 +00:00
Cuckoo = [ ] ,
2019-07-30 05:12:23 +00:00
ProgPow = [ ] ;
BlockMineChartQuery . forEach ( e = > {
date . push ( moment ( e . date ) . format ( 'YYYY-MM-DD' ) ) ;
RandomXper . push ( parseFloat ( e . randomxper ) ) ;
2019-08-01 09:51:43 +00:00
Cuckooper . push ( parseFloat ( e . cuckooper ) ) ;
2019-07-30 05:12:23 +00:00
ProgPowper . push ( parseFloat ( e . progpowper ) ) ;
RandomX . push ( parseInt ( e . randomx ) ) ;
2019-08-01 07:38:45 +00:00
Cuckoo . push ( parseInt ( e . cuckatoo ) ) ;
2019-07-30 05:12:23 +00:00
ProgPow . push ( parseInt ( e . progpow ) ) ;
2019-07-09 09:22:36 +00:00
} ) ;
2019-07-30 05:12:23 +00:00
response . status ( 200 ) . json ( {
status : 200 ,
timestamp : Date.now ( ) ,
message : 'period of blocks generation per second fetched Successfully' ,
response : {
date ,
RandomXper ,
2019-08-01 07:38:45 +00:00
Cuckooper ,
2019-07-30 05:12:23 +00:00
ProgPowper ,
RandomX ,
2019-08-01 07:38:45 +00:00
Cuckoo ,
ProgPow
2019-07-30 05:12:23 +00:00
} ,
} ) ;
} catch ( error ) {
next ( new InternalServerErrorException ( error ) ) ;
}
} ;
2019-07-09 09:22:36 +00:00
}