Viewing File: /home/ubuntu/efiexchange-node-base/src/helpers/crypto.helper.ts

import config from "config";
import Web3 from "web3";
import Token from "../utils/abi.json";
let XpayToken: any = Token;
const web3 = new Web3(config.get("rpcUrl"));
const ethPrice = require("eth-price");

/**
 *
 * @param wallet_address
 * @param contract_address
 * @returns
 */
export async function createEthWallet(password: any = "") {
  let walletInfo = await web3.eth.accounts.create(web3.utils.randomHex(32));
  // console.log("walletInfo HELPER", walletInfo);

  if (walletInfo) {
    let encryptedWallet = await web3.eth.accounts.encrypt(
      walletInfo.privateKey,
      password
    );
    // console.log("encryptedWallet HELPER", encryptedWallet);
    return encryptedWallet;
  }
  return walletInfo;
}

/**
 * @param wallet_address
 * @returns
 */
export async function decryptWallet(inputs: any) {
  let walletInfo = await web3.eth.accounts.decrypt(
    inputs.jsonKey,
    inputs.password
  );
  // console.log("walletInfo HELPER", walletInfo);

  return walletInfo;
}

/**
 *
 * @param wallet_address
 * @param contract_address
 * @returns
 */
export async function importEthWallet(inputKey: any, password: any) {
  let walletInfo = await web3.eth.accounts.privateKeyToAccount(inputKey);
  if (walletInfo) {
    let encryptedWallet = await web3.eth.accounts.encrypt(
      walletInfo.privateKey,
      password
    );

    // console.log("encryptedWallet", encryptedWallet);
    return encryptedWallet;
  }
  return walletInfo;
}

/**
 *
 * @param wallet_address
 * @param contract_address
 * @returns
 */
export async function SendAmountToWallet(data: any) {
  // var txFee: any = (await web3.eth.getGasPrice()) * 21000;
  var gasPrice = await web3.eth.getGasPrice();
  var txFee = Number(gasPrice) * 21000;
  const createTransaction = await web3.eth.accounts.signTransaction(
    {
      from: data.from_wallet_address,
      to: data.to_wallet_address,
      value: web3.utils.toWei(data.amount, "ether"),
      gas: txFee,
    },
    data.xwallet_key
  );
  // console.log("createTransaction", createTransaction);
  await web3.eth.sendSignedTransaction(
    createTransaction.rawTransaction,
    function (error, hash) {
      if (!error) {
        console.log(hash);
        return { success: true, hash: hash, data: data };
      } else {
        return { success: false, error: error, data: data };
      }
    }
  );
}

/**
 *
 * @param wallet_address
 * @param contract_address
 * @returns
 */
export async function getCryptoBalance(
  wallet_address: any,
  contract_address: any
) {
  let contractData = await new web3.eth.Contract(
    XpayToken.abi,
    contract_address
  );
  console.log("wallet_address", wallet_address);
  // console.log("contractData", contractData);

  console.log("contract_address", contract_address);
  let balance = await contractData.methods.balanceOf(wallet_address).call();
  console.log("Balan", balance);
  let formatted: any = web3.utils.fromWei(balance, "ether");

  // return 10;

  return parseFloat(Number(formatted).toFixed(2));
}

/**
 *
 * @param busdx_balance
 * @returns
 */
export async function getBnbBalance(wallet_address: any) {
  let balance = await web3.eth.getBalance(wallet_address);
  let formatted: any = web3.utils.fromWei(balance, "ether");
  return parseFloat(Number(formatted).toFixed(2));
}
/**
 * Used to get the BNB amount for the given amount
 * @param amount
 * @returns
 */
export async function getBnbPrice(amount: any) {
  let price = await ethPrice("usd").then((data: any) => {
    const usdPrice = data.toString();
    return usdPrice.slice(5, usdPrice.length);
  });
  let bnbUsd = (amount * price).toString();
  return parseFloat(bnbUsd).toFixed(5);
}
Back to Directory File Manager