explorer/server/utils/methods.ts

194 lines
5.7 KiB
TypeScript
Raw Normal View History

2024-09-09 18:04:58 +07:00
import { literal, Op } from "sequelize";
2024-08-11 16:54:21 +07:00
import Block from "../schemes/Block";
import axios from "axios";
import { config, log } from "./utils";
import { get_info, get_mining_history, getbalance } from "./zanod";
import BigNumber from "bignumber.js";
import Transaction from "../schemes/Transaction";
import Pool from "../schemes/Pool";
2024-08-20 22:39:59 +07:00
import { io } from "../server";
2024-09-07 20:32:28 +03:00
import { blockInfo, lastBlock, state } from "./states";
2024-08-27 11:57:09 +07:00
import { Socket } from "socket.io";
2024-08-11 16:54:21 +07:00
interface getBlocksDetailsParams {
start: number;
count: number;
}
export async function getBlocksDetails(params: getBlocksDetailsParams) {
const { start, count } = params;
const result = await Block.findAll({
attributes: [
'height',
2024-08-27 14:34:43 +07:00
[literal(`CASE WHEN type = '0' THEN actual_timestamp ELSE timestamp END`), 'timestamp'],
2024-08-11 16:54:21 +07:00
'base_reward',
'blob',
'block_cumulative_size',
'block_tself_size',
'cumulative_diff_adjusted',
'cumulative_diff_precise',
'difficulty',
'effective_fee_median',
2024-08-27 14:34:43 +07:00
'tx_id',
2024-08-11 16:54:21 +07:00
'is_orphan',
'penalty',
'prev_id',
'summary_reward',
'this_block_fee_median',
'actual_timestamp',
'total_fee',
'total_txs_size',
'tr_count',
'type',
'miner_text_info',
'already_generated_coins',
'object_in_json',
'pow_seed'
],
where: {
height: {
[Op.gte]: start
}
},
order: [['height', 'ASC']],
limit: count
});
return result.length > 0 ? result.map(e => e.toJSON()) : [];
}
export async function getVisibilityInfo() {
const result = {
amount: 0,
percentage: 0,
balance: 0,
unlocked_balance: 0,
2024-09-07 20:32:28 +03:00
apy: 0,
zano_burned: undefined as (number | undefined),
2024-09-08 20:22:50 +03:00
pos_value: 0,
2024-08-11 16:54:21 +07:00
}
try {
if (config.enableVisibilityInfo) {
2024-09-08 20:22:50 +03:00
const [res1, res2, res3] = await axios.all([
2024-08-11 16:54:21 +07:00
getbalance(),
get_mining_history(),
get_info()
])
2024-09-08 22:29:32 +03:00
console.log('RES1', res1.data);
2024-08-11 16:54:21 +07:00
const pos_diff_to_total_ratio = new BigNumber(res3.data.result.pos_difficulty)
.dividedBy(new BigNumber(res3.data.result.total_coins));
2024-09-09 17:07:30 +07:00
const divider = new BigNumber(176.3630);
2024-08-11 16:54:21 +07:00
const stakedPercentage = (
new BigNumber(0.55).multipliedBy(pos_diff_to_total_ratio)
2024-09-09 17:07:30 +07:00
).dividedBy(divider).multipliedBy(100).toNumber();
2024-08-11 16:54:21 +07:00
result.percentage = parseFloat(stakedPercentage.toFixed(2));
const stakedCoins = new BigNumber(res3.data.result.total_coins)
.dividedBy(100)
.multipliedBy(new BigNumber(result.percentage));
result.amount = stakedCoins.toNumber();
result.balance = res1.data.result.balance
2024-09-07 20:32:28 +03:00
result.unlocked_balance = res1.data.result.unlocked_balance;
result.zano_burned = state.zanoBurned;
2024-09-09 17:54:30 +07:00
2024-08-11 16:54:21 +07:00
const stakedNumber = new BigNumber(result.amount).dividedBy(new BigNumber(10 ** 12)).toNumber();
2024-09-08 20:22:50 +03:00
result.apy = 720 * 365 / stakedNumber * 100;
let stakedCoinsLast7Days = new BigNumber(0);
2024-09-09 18:04:58 +07:00
const mined_entries = res2?.data?.result?.mined_entries || [];
for (const item of mined_entries) {
stakedCoinsLast7Days = stakedCoinsLast7Days.plus(item.a);
2024-09-08 20:22:50 +03:00
}
2024-09-09 18:04:58 +07:00
const coinsPerDay = stakedCoinsLast7Days.div(7);
2024-09-09 18:12:32 +07:00
2024-09-09 18:11:11 +07:00
const neededToStakeCoinPerDay = new BigNumber(res1.data.result.balance).div(coinsPerDay);
2024-09-09 18:06:29 +07:00
2024-09-09 18:12:32 +07:00
result.pos_value = neededToStakeCoinPerDay.toNumber();
2024-08-11 16:54:21 +07:00
}
} catch (error) {
log(`getVisibilityInfo() ERROR ${error}`)
}
return JSON.stringify(result)
}
export async function getMainBlockDetails(id: string) {
const block = await Block.findOne({
2024-08-27 14:34:43 +07:00
where: { tx_id: id }
2024-08-11 16:54:21 +07:00
});
if (block) {
2024-08-27 14:34:43 +07:00
const nextBlock = await Block.findOne({
where: {
height: {
[Op.gt]: block.height
}
},
order: [['height', 'ASC']]
});
if (nextBlock) {
block.setDataValue('nextBlock', nextBlock.tx_id);
}
2024-08-11 16:54:21 +07:00
const transactions = await Transaction.findAll({
where: { keeper_block: block.height }
});
2024-09-09 17:54:30 +07:00
2024-08-27 14:34:43 +07:00
block.setDataValue('transactions_details', transactions.map(e => e.toJSON()));
2024-08-11 16:54:21 +07:00
return block.toJSON();
}
}
export async function getTxPoolDetails(count: number) {
// When count is 0, retrieve all records ordered by timestamp DESC
if (count === 0) {
const result = await Pool.findAll({
2024-09-09 21:12:56 +03:00
attributes: ['blob_size', 'fee', 'id', 'timestamp', 'tx_id'],
2024-08-11 16:54:21 +07:00
order: [['timestamp', 'DESC']]
});
return result.length > 0 ? result : [];
}
// Retrieve records with a limit, ordered by timestamp DESC
const result = await Pool.findAll({
attributes: [
'blob_size',
'fee',
'id',
'timestamp',
2024-09-09 21:12:56 +03:00
'tx_id',
2024-08-11 16:54:21 +07:00
[literal('false'), 'isNew'] // Adding a literal false as "isNew"
],
order: [['timestamp', 'DESC']],
limit: count || 500
});
return result.length > 0 ? result : [];
2024-08-20 22:39:59 +07:00
}
2024-08-27 11:57:09 +07:00
export const emitSocketInfo = async (socket?: Socket) => {
2024-08-20 22:39:59 +07:00
if (config.websocket.enabled_during_sync && lastBlock) {
blockInfo.lastBlock = lastBlock.height
const emitter = socket || io;
emitter.emit('get_info', JSON.stringify(blockInfo));
emitter.emit('get_visibility_info', getVisibilityInfo());
}
2024-08-11 16:54:21 +07:00
}