Skip to content

Commit

Permalink
explorer module added and search api created
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulAhadArain committed Jul 19, 2023
1 parent f5dadd1 commit 8ca6ea7
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/controllers/blocks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export const getBlockByHash = async (
): Promise<void> => {
try {
const block = await blockService.getBlockByBlockHash(
req.query.networkId as any,
req.query.blockHash as any,
req.query.networkId as any,
);
res.send(block);
} catch (error) {
Expand Down
36 changes: 36 additions & 0 deletions src/controllers/explorer.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Web3 from 'web3';
import { transactionsService, accountService, blockService } from '../services';
import { Request, Response, NextFunction } from 'express';
import { isTransactionOrBlockHash } from '../utils/constants';
import ApiError from '../utils/ApiError';
import httpStatus from 'http-status';

export const searchData = async (
req: Request,
res: Response,
next: NextFunction,
): Promise<void> => {
try {
if (
Web3.utils.isHex(req.query.data as string) &&
isTransactionOrBlockHash(req.query.data as string)
) {
let hash;
hash = await transactionsService.getTransaction(req.query.data as string);
if (!hash) {
hash = await blockService.getBlockByBlockHash(req.query.data as string);
}
if (!hash) {
next(new ApiError(httpStatus.BAD_REQUEST, 'Not Found'));
}
res.send(hash);
} else if (Web3.utils.isAddress(req.query.data as string)) {
const account = await accountService.getAccount(req.query.data as string);
res.send(account);
} else {
next(new ApiError(httpStatus.BAD_REQUEST, 'Not Found'));
}
} catch (error) {
next(error);
}
};
1 change: 1 addition & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * as accountController from './accounts.controller';
export * as networkController from './network.controller';
export * as contractController from './contract.controller';
export * as methodController from './method.controller';
export * as explorerController from './explorer.controller';
13 changes: 13 additions & 0 deletions src/routes/explorer.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import express from 'express';
const router = express.Router();
import validate from '../middlewares/validate.middleware';
import { explorerController } from '../controllers';
import { explorerValidation } from '../validations';

router.get(
'/search',
validate(explorerValidation.searchData),
explorerController.searchData,
);

export default router;
5 changes: 5 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import accounts from './accounts.route';
import networkRoute from './network.route';
import contractRoute from './contract.route';
import methodRoute from './method.route';
import explorerRoute from './explorer.route';

const router = express.Router();

Expand Down Expand Up @@ -33,6 +34,10 @@ const defaultRoute = [
path: '/methods',
route: methodRoute,
},
{
path: '/explorer',
route: explorerRoute,
},
];

defaultRoute.forEach(route => {
Expand Down
4 changes: 2 additions & 2 deletions src/services/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
} from '../interfaces';

export const getBlockByBlockHash = async (
networkId: string,
blockHash?: string,
blockHash: string,
networkId?: string,
): Promise<QuantumPortalMinedBlockDocument> => {
const filter: { networkId?: string; blockHash: string } = {
blockHash: blockHash,
Expand Down
4 changes: 4 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export const NETWORK_SEEDED_DATA = {
name: 'BSC',
rpcUrl: 'https://bsc-dataseed.binance.org/',
};

export const isTransactionOrBlockHash = (addr: string) => {
return /^0x([A-Fa-f0-9]{64})$/.test(addr);
};
7 changes: 7 additions & 0 deletions src/validations/explorer.validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Joi from 'joi';

export const searchData = {
query: Joi.object().keys({
data: Joi.string().required(),
}),
};
1 change: 1 addition & 0 deletions src/validations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * as transactionValidation from './transaction.validation';
export * as accountValidation from './account.validation';
export * as contractValidation from './contract.validation';
export * as networkValidation from './network.validation';
export * as explorerValidation from './explorer.validation';

0 comments on commit 8ca6ea7

Please sign in to comment.