Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

refactor: Move queries to backend #774

Merged
merged 10 commits into from
Nov 19, 2021
57 changes: 57 additions & 0 deletions backend/src/accounts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const {
queryIndexedAccount,
queryAccountsList,
queryAccountInfo,
queryAccountOutcomeTransactionsCount,
queryAccountIncomeTransactionsCount,
} = require("./db-utils");

async function isAccountIndexed(accountId) {
const account = await queryIndexedAccount(accountId);
return Boolean(account?.account_id);
}

async function getAccountsList(limit, paginationIndexer) {
const accountsList = await queryAccountsList(limit, paginationIndexer);
return accountsList.map((account) => ({
accountId: account.account_id,
createdAtBlockTimestamp: parseInt(account.created_at_block_timestamp),
accountIndex: parseInt(account.account_index),
}));
}

async function getAccountTransactionsCount(accountId) {
const [outcomeTransactions, incomeTransactions] = await Promise.all([
queryAccountOutcomeTransactionsCount(accountId),
queryAccountIncomeTransactionsCount(accountId),
]);
return {
inTransactionsCount: incomeTransactions ? parseInt(incomeTransactions) : 0,
outTransactionsCount: outcomeTransactions
? parseInt(outcomeTransactions)
: 0,
};
}

async function getAccountInfo(accountId) {
const accountInfo = await queryAccountInfo(accountId);
if (!accountInfo) {
return undefined;
}
return {
accountId: accountInfo.account_id,
createdByTransactionHash: accountInfo.created_by_transaction_hash || null,
createdAtBlockTimestamp: accountInfo.created_at_block_timestamp
? parseInt(accountInfo.created_at_block_timestamp)
: null,
deletedByTransactionHash: accountInfo.deleted_by_transaction_hash || null,
deletedAtBlockTimestamp: accountInfo.deleted_at_block_timestamp
? parseInt(accountInfo.deleted_at_block_timestamp)
: null,
};
}

exports.isAccountIndexed = isAccountIndexed;
exports.getAccountsList = getAccountsList;
exports.getAccountTransactionsCount = getAccountTransactionsCount;
exports.getAccountInfo = getAccountInfo;
45 changes: 45 additions & 0 deletions backend/src/blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const {
queryBlocksList,
queryBlockInfo,
queryBlockByHashOrId,
} = require("./db-utils");

async function getBlocksList(limit, paginationIndexer) {
const blocksList = await queryBlocksList(limit, paginationIndexer);
return blocksList.map((block) => ({
hash: block.hash,
height: parseInt(block.height),
timestamp: parseInt(block.timestamp),
prevHash: block.prev_hash,
transactionsCount: parseInt(block.transactions_count),
}));
}

async function getBlockInfo(blockId) {
const blockInfo = await queryBlockInfo(blockId);
if (!blockInfo) {
return undefined;
}
return {
hash: blockInfo.hash,
prevHash: blockInfo.prev_hash,
height: parseInt(blockInfo.height),
timestamp: parseInt(blockInfo.timestamp),
transactionsCount: parseInt(blockInfo.transactions_count),
totalSupply: blockInfo.total_supply,
gasPrice: blockInfo.gas_price,
authorAccountId: blockInfo.author_account_id,
};
}

async function getBlockByHashOrId(blockId) {
const block = await queryBlockByHashOrId(blockId);
if (!block) {
return undefined;
}
return block.block_hash;
}

exports.getBlocksList = getBlocksList;
exports.getBlockInfo = getBlockInfo;
exports.getBlockByHashOrId = getBlockByHashOrId;
12 changes: 12 additions & 0 deletions backend/src/chunks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { queryGasUsedInChunks } = require("./db-utils");
const BN = require("bn.js");

async function getGasUsedInChunks(blockHash) {
const gasUsedInChunks = await queryGasUsedInChunks(blockHash);
if (!gasUsedInChunks) {
return undefined;
}
return gasUsedInChunks;
}

exports.getGasUsedInChunks = getGasUsedInChunks;
14 changes: 14 additions & 0 deletions backend/src/contracts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { queryContractInfo } = require("./db-utils");

async function getContractInfo(accountId) {
const contractInfo = await queryContractInfo(accountId);
if (!contractInfo) {
return undefined;
}
return {
blockTimestamp: parseInt(contractInfo.block_timestamp),
hash: contractInfo.hash,
};
}

exports.getContractInfo = getContractInfo;
Loading