Viewing File: /home/ubuntu/efiexchange-node-base/src/controllers/userApp/eth.controller.ts
import config from "config";
import Web3 from "web3";
import Token from "../../utils/abi.json";
import { ethers } from "ethers";
import {
SUPPORTED_RPC_URLS,
CHAIN_INFO,
SupportedChainId,
SupportedTokens,
USDT_CONTRACT_ADDRESS,
SPECIAL_CONTRACT_ADDRESS,
} from "../../config/chains";
require('dotenv').config();
export const connectWallet = async (privateKey: any, network: any) => {
try {
const provider = new ethers.JsonRpcProvider(network);
const wallet = new ethers.Wallet(privateKey, provider);
return wallet;
} catch (error) {
throw new Error("Failed to connect wallet");
}
};
export const transferTokensHandler = async (req: any, res: any, options: any) => {
try {
let inputs = req.body;
if (!inputs.receiver_wallet_address) {
return res.sendError("Receiver Wallet Address is required");
}
if (!inputs.tokens) {
return res.sendError("Amount is required");
}
if (!inputs.token_type) {
return res.sendError("Token Type is required");
}
if (!inputs.network_type) {
return res.sendError("Network Type is required");
}
if (!inputs.order_id) {
return res.sendError("Order ID field is required");
}
const rpcUrl = SUPPORTED_RPC_URLS[SupportedTokens[inputs.network_type]];
if (!rpcUrl) {
return res.sendError("Invalid Network Type");
}
const privateKey = process.env.ADMIN_PRIVATE_KEY;
let provider, wallet, contract_address, transactionReponse;
let receiver_wallet_address = inputs.receiver_wallet_address;
let amount = inputs.tokens;
wallet = await connectWallet(privateKey, rpcUrl);
const {
gasLimit = 21620, // Minimum gas limit for a simple ETH transfer
gasPrice, // Standard gas price for non-EIP-1559 transactions
maxFeePerGas, // Maximum fee per gas for EIP-1559 transactions
maxPriorityFeePerGas, // Priority fee per gas for EIP-1559 transactions
} = options;
if (["ETH", "POLYGON_AMOY", "BNB", "MATIC"].includes(inputs.token_type)) {
console.log(`Initiating ${inputs.token_type} transfer`);
const txOptions = {
to: receiver_wallet_address,
value: ethers.parseEther(amount.toString()),
gasLimit,
// Include gas settings for EIP-1559 or legacy transactions
...(maxFeePerGas && {
maxFeePerGas: ethers.parseUnits(maxFeePerGas, "gwei"),
}),
...(maxPriorityFeePerGas && {
maxPriorityFeePerGas: ethers.parseUnits(maxPriorityFeePerGas, "gwei"),
}),
...(gasPrice && { gasPrice: ethers.parseUnits(gasPrice, "gwei") }),
};
const tx = await wallet.sendTransaction(txOptions,);
await tx.wait(); // Wait for the transaction to be mined
if (tx) {
console.log(tx);
transactionReponse = tx;
}
}
else {
let abi = [
"function balanceOf(address owner) view returns (uint256)",
"function transfer(address to, uint256 amount) returns (bool)",
"function decimals() view returns (uint8)",
];
let contract_address = SPECIAL_CONTRACT_ADDRESS[inputs.network_type][inputs.token_type];
const tokenContract = new ethers.Contract(contract_address, abi, wallet);
const txOptions = {
gasLimit,
// Include gas settings for EIP-1559 or legacy transactions
...(maxFeePerGas && {
maxFeePerGas: ethers.parseUnits(maxFeePerGas, "gwei"),
}),
...(maxPriorityFeePerGas && {
maxPriorityFeePerGas: ethers.parseUnits(maxPriorityFeePerGas, "gwei"),
}),
...(gasPrice && { gasPrice: ethers.parseUnits(gasPrice, "gwei") }),
};
const tx = await tokenContract.transfer(
receiver_wallet_address,
ethers.parseEther(amount.toString()),
txOptions
);
await tx.wait();
if (tx) {
transactionReponse = tx;
}
}
if (!transactionReponse) {
return res.sendError("Transaction failed, please try again later.");
}
inputs = {};
let responseData = {
transferTokenResponse: transactionReponse,
fromWalletAddress: process.env.ADMIN_WALLET_ADDRESS,
};
return res.sendResponse(responseData, "Token transfer success");
} catch (error) {
return res.sendError(error.message);
}
}
Back to Directory
File Manager