Viewing File: /home/ubuntu/efiexchange-node-base/src/controllers/ethereum/eth.node.controller.ts

import * as bitcoin from "bitcoinjs-lib";
import fetch from "node-fetch";
import { nodeRpcCall } from "./rpc.connector";
import Web3 from "web3";

const web3 = new Web3();

export const fetchLatestBlockNumber = async (req: any, res: any) => {
  try {
    let inputs = req.body;

    const data = await nodeRpcCall("eth_blockNumber", []);
    return res.sendResponse(data, "Latest Block Number fetched successfully.");

  } catch (error: any) {
    if (error?.message) {
      return res.sendError(error.message);
    }
  }
};

function formatTransaction(tx: any) {
  return {
    ...tx,
    gas: web3.utils.hexToNumberString(tx.gas),
    gasPrice: web3.utils.fromWei(web3.utils.hexToNumberString(tx.gasPrice), "gwei"),
    maxFeePerGas: tx.maxFeePerGas ? web3.utils.fromWei(web3.utils.hexToNumberString(tx.maxFeePerGas), "gwei") : null,
    maxPriorityFeePerGas: tx.maxPriorityFeePerGas
      ? web3.utils.fromWei(web3.utils.hexToNumberString(tx.maxPriorityFeePerGas), "gwei")
      : null,
    nonce: web3.utils.hexToNumberString(tx.nonce),
    transactionIndex: web3.utils.hexToNumberString(tx.transactionIndex),
    value: web3.utils.fromWei(web3.utils.hexToNumberString(tx.value), "ether"),
    type: web3.utils.hexToNumberString(tx.type),
    chainId: tx.chainId ? web3.utils.hexToNumberString(tx.chainId) : null,
    from: tx.from,
    to: tx.to,
    status: tx.status,
  };
}

export const fetchLatestBlockDetails = async (req: any, res: any) => {
  try {
    const blockResponse = await nodeRpcCall("eth_getBlockByNumber", ["latest", true]);
    const block = blockResponse?.result;

    if (!block) return res.sendError("Block not found");

    const formattedTransactions = block.transactions.map((tx: any) => formatTransaction(tx));

    const formattedBlock = {
      ...block,
      number: web3.utils.hexToNumberString(block.number),
      timestamp: web3.utils.hexToNumberString(block.timestamp),
      gasLimit: web3.utils.hexToNumberString(block.gasLimit),
      gasUsed: web3.utils.hexToNumberString(block.gasUsed),
      baseFeePerGas: block.baseFeePerGas
        ? web3.utils.fromWei(web3.utils.hexToNumberString(block.baseFeePerGas), "gwei")
        : null,
      size: web3.utils.hexToNumberString(block.size),
      transactions: formattedTransactions,
    };

    return res.sendResponse(formattedBlock, "Latest Block Details fetched successfully.");
  } catch (error: any) {
    console.error(error);
    return res.sendError(error?.message || "Failed to fetch latest block details");
  }
};

export const fetchSingleTransactionDetails = async (req: any, res: any) => {
  try {
    const { transaction_hash } = req.body;

    if (!transaction_hash) return res.sendError("Transaction Hash is required");

    const txResponse = await nodeRpcCall("eth_getTransactionByHash", [transaction_hash]);
    const tx = txResponse?.result;

    if (!tx) return res.sendError("Transaction not found");

    const receiptResponse = await nodeRpcCall("eth_getTransactionReceipt", [transaction_hash]);
    const receipt = receiptResponse?.result;

    const gasUsed = BigInt(receipt?.gasUsed || "0");
    const gasPrice = BigInt(tx?.gasPrice || "0");
    const transactionFee = web3.utils.fromWei((gasUsed * gasPrice).toString(), "ether");
    const value = web3.utils.fromWei(tx?.value || "0", "ether");

    const responseData = {
      transactionReponse: tx,
      transReceipt: tx.blockHash || null,
      value,
      transaction_fee: transactionFee,
      explorer_url: `https://etherscan.io/tx/${transaction_hash}`,
      token_value: value,
      from_wallet_address: tx.from,
      receiver_wallet_address: tx.to,
      status: receipt?.status === "0x1" ? "success" : "failed",
    };

    return res.sendResponse(responseData, "Transaction Details fetched successfully.");
  } catch (error: any) {
    console.error(error);
    return res.sendError(error?.message || "Something went wrong");
  }
};







Back to Directory File Manager